blob: 40820fa9adfd24193d6e97e086c0e1ae39f73015 [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;
Michael Jurkaaf442092010-06-10 17:01:57 -070048import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070049import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070050import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020051import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080053import android.os.Handler;
Winson Chunga2413752012-04-03 14:22:34 -070054import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040055import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070056import android.os.Trace;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070057import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.text.Selection;
59import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070060import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070062import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070063import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070064import android.view.HapticFeedbackConstants;
65import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070066import android.view.KeyboardShortcutGroup;
67import android.view.KeyboardShortcutInfo;
Adam Cohen96d30a12013-07-16 18:13:21 -070068import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070069import android.view.MotionEvent;
70import android.view.Surface;
71import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080072import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.ViewGroup;
74import android.view.ViewTreeObserver;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080075import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070076import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070077import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078import android.view.inputmethod.InputMethodManager;
Chet Haasea8f996d2015-02-17 12:56:04 -080079import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070081
Sunny Goyal651077b2014-06-30 14:15:31 -070082import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070083import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyalae502842016-06-17 08:43:56 -070084import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070085import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070086import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080087import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -070088import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010089import com.android.launcher3.compat.LauncherActivityInfoCompat;
90import com.android.launcher3.compat.LauncherAppsCompat;
91import com.android.launcher3.compat.UserHandleCompat;
92import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -080093import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -070094import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -070095import com.android.launcher3.dragndrop.DragController;
96import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -070097import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -070098import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -070099import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000100import com.android.launcher3.folder.Folder;
101import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700102import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700103import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700104import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700105import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyald164b7f2016-10-12 20:49:31 -0700106import com.android.launcher3.model.PackageItemInfo;
107import com.android.launcher3.model.WidgetItem;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700108import com.android.launcher3.pageindicators.PageIndicator;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700109import com.android.launcher3.shortcuts.DeepShortcutManager;
Tony Wickham49c8d292016-07-01 11:44:34 -0700110import com.android.launcher3.shortcuts.DeepShortcutsContainer;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700111import com.android.launcher3.shortcuts.ShortcutKey;
Hyunyoung Song5aa27142016-07-21 11:48:37 -0700112import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal2100c782016-08-22 16:00:03 -0700113import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700114import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700115import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700116import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700117import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal2100c782016-08-22 16:00:03 -0700118import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal322d5562015-06-25 19:35:49 -0700119import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700120import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700121import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700122import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700123import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700124import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700125
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700126import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700127import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700128import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700129import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800130import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700131import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700132import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700133import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700134
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135/**
136 * Default launcher application.
137 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100138public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700139 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
140 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700141 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700142 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700143 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700145 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700146 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400147 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700148
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700150 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700151 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700152 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153
Michael Jurka8b805b12012-04-18 14:23:14 -0700154 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700155 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700156 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700157
Sunny Goyal28c6b962015-10-12 11:42:05 -0700158 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
159
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700160 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
161
Mathew Inwood876a8462013-06-14 14:12:41 +0100162 /**
163 * IntentStarter uses request codes starting with this. This must be greater than all activity
164 * request codes used internally.
165 */
166 protected static final int REQUEST_LAST = 100;
167
Michael Jurka0a457bf2012-11-19 14:05:05 -0800168 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700169 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800170 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
Winson Chung2672ff92012-05-04 16:22:30 -0700172 // The Intent extra that defines whether to ignore the launch animation
173 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400174 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700175
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 // Type: int
177 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700178 // Type: int
179 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700180 // Type: PendingRequestArgs
181 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
182 // Type: ActivityResultInfo
183 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700185 static final String APPS_VIEW_SHOWN = "launcher.apps_view_shown";
Adam Cohenb54a5982014-01-08 15:21:04 -0800186
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700187 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700188 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
189 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700190
Adam Cohen091440a2015-03-18 14:16:05 -0700191 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700192 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700193
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700194 private boolean mIsSafeModeEnabled;
195
Joe Onorato9c1289c2009-08-17 11:03:03 -0400196 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800197 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700198 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700199 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200
Winson Chunga2413752012-04-03 14:22:34 -0700201 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700202 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
203 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700204 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700205
Adam Cohen091440a2015-03-18 14:16:05 -0700206 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700207 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700208 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800209 private DragController mDragController;
Sunny Goyal6178f132016-07-11 17:30:03 -0700210 private View mQsbContainer;
Sunny Goyal322d5562015-06-25 19:35:49 -0700211
212 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700213
Sunny Goyalffe83f12014-08-14 17:39:34 -0700214 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700215 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700216
Michael Jurka0280c3b2010-09-17 15:00:07 -0700217 private int[] mTmpAddItemCellCoordinates = new int[2];
218
Sunny Goyal316490e2015-06-02 09:38:28 -0700219 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800220 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700221
Michael Jurka838a4ca2011-02-07 13:33:06 -0800222 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700223 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700224
Sunny Goyal47328fd2016-05-25 18:56:41 -0700225 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700226
227 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700228 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700229 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700230
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700231 // Main container view and the model for the widget tray screen.
232 @Thunk WidgetsContainerView mWidgetsView;
Sunny Goyald164b7f2016-10-12 20:49:31 -0700233 @Thunk MultiHashMap<PackageItemInfo, WidgetItem> mAllWidgets;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700234
Winson Chung4a2afa32012-07-19 14:53:05 -0700235 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
236 // scroll issues (because the workspace may not have been measured yet) and extra work.
237 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
238 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239
240 private SpannableStringBuilder mDefaultKeySsb = null;
241
Adam Cohen091440a2015-03-18 14:16:05 -0700242 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400243
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800244 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700245 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246
Michael Jurka1e2f4652013-07-08 18:03:46 -0700247 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700248 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700249 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700250
Joe Onorato9c1289c2009-08-17 11:03:03 -0400251 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800252 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700253 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700254 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700255 private Handler mHandler = new Handler();
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700256 private boolean mIsResumeFromActionScreenOff;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700257 private boolean mHasFocus = false;
258 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400259
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700260 /** Maps launcher activity components to their list of shortcut ids. */
261 private MultiHashMap<ComponentKey, String> mDeepShortcutMap = new MultiHashMap<>();
262
Adam Cohen61f560d2013-09-30 15:58:20 -0700263 private View.OnTouchListener mHapticFeedbackTouchListener;
264
Winson Chung400438b2011-01-16 17:53:48 -0800265 // Determines how long to wait after a rotation before restoring the screen orientation to
266 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700267 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800268
Adam Cohen1462de32012-07-24 22:34:36 -0700269 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
270
Winson Chung46353de2012-02-16 14:05:10 -0800271 // We only want to get the SharedPreferences once since it does an FS stat each time we get
272 // it from the context.
273 private SharedPreferences mSharedPrefs;
274
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700275 private DeviceProfile mDeviceProfile;
276
Adam Cohen4b66bf32015-09-18 12:15:19 -0700277 private boolean mMoveToDefaultScreenFromNewIntent;
278
Winson Chung13eb5272015-05-11 16:30:13 -0700279 // This is set to the view that launched the activity that navigated the user away from
280 // launcher. Since there is no callback for when the activity has finished launching, enable
281 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800282 private BubbleTextView mWaitingForResume;
283
Adam Cohen59400422014-03-05 18:07:04 -0800284 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
285 new HashMap<String, CustomAppWidget>();
286
Adam Cohen59400422014-03-05 18:07:04 -0800287 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700288 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
289 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800290 }
291 }
292
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700293 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
294 // state transition. If another state transition happened during this delay,
295 // simply unregister this runnable.
296 private Runnable mExitSpringLoadedModeRunnable;
297
Adam Cohen091440a2015-03-18 14:16:05 -0700298 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700299 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700300 if (mWorkspace != null) {
301 mWorkspace.buildPageHardwareLayers();
302 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700303 }
304 };
305
Sunny Goyal2100c782016-08-22 16:00:03 -0700306 // Activity result which needs to be processed after workspace has loaded.
307 private ActivityResultInfo mPendingActivityResult;
308 /**
309 * Holds extra information required to handle a result from an external call, like
310 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
311 */
312 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800313
Hyunyoung Songaa953652016-04-19 18:30:24 -0700314 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800315
Jon Miranda379198e2016-09-23 08:54:40 -0700316 private float mLastDispatchTouchEventX = 0.0f;
317 private float mEdgeOfScreenThresholdPx = 0.0f;
318
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700319 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700320 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400321
322 @Thunk void setOrientation() {
323 if (mRotationEnabled) {
324 unlockScreenOrientation(true);
325 } else {
326 setRequestedOrientation(
327 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700328 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400329 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700330
Sunny Goyal745bad92016-05-02 10:54:12 -0700331 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700332
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800333 @Override
334 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700335 if (DEBUG_STRICT_MODE) {
336 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
337 .detectDiskReads()
338 .detectDiskWrites()
339 .detectNetwork() // or .detectAll() for all detectable problems
340 .penaltyLog()
341 .build());
342 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
343 .detectLeakedSqlLiteObjects()
344 .detectLeakedClosableObjects()
345 .penaltyLog()
346 .penaltyDeath()
347 .build());
348 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700349 if (LauncherAppState.PROFILE_STARTUP) {
350 Trace.beginSection("Launcher-onCreate");
351 }
Winson Chunga2413752012-04-03 14:22:34 -0700352
Adam Cohen9211d422014-10-07 18:14:53 -0700353 if (mLauncherCallbacks != null) {
354 mLauncherCallbacks.preOnCreate();
355 }
356
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400358
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400359 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700360
Adam Cohen2e6da152015-05-06 11:42:25 -0700361 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700362 mDeviceProfile = getResources().getConfiguration().orientation
363 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700364 app.getInvariantDeviceProfile().landscapeProfile
365 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700366
Sunny Goyalf7258242015-10-19 16:59:07 -0700367 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700368 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800369 mModel = app.setLauncher(this);
370 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700371 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700372
Joe Onorato41a12d22009-10-31 18:30:00 -0400373 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700374 mAllAppsController = new AllAppsTransitionController(this);
375 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700376
Sunny Goyalffe83f12014-08-14 17:39:34 -0700377 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700378
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700379 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
380 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700381
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700382 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
383 // this also ensures that any synchronous binding below doesn't re-trigger another
384 // LauncherModel load.
385 mPaused = false;
386
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700388
Jon Miranda379198e2016-09-23 08:54:40 -0700389 mEdgeOfScreenThresholdPx = getResources()
390 .getDimensionPixelSize(R.dimen.edge_of_screen_threshold);
391
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 setupViews();
Winson1f064272016-07-18 17:18:02 -0700393 mDeviceProfile.layout(this, false /* notifyListeners */);
Tony Wickham827cef22016-03-17 15:39:39 -0700394 mExtractedColors = new ExtractedColors();
395 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396
Tony Wickhame2217252016-03-22 16:34:23 -0700397 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
398 .addAccessibilityStateChangeListener(this);
399
Joe Onorato7c312c12009-08-13 21:36:53 -0700400 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700401
Sunny Goyalfe770c92016-09-27 14:38:58 -0700402 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403
Sunny Goyale26d1002016-06-20 14:52:14 -0700404 if (LauncherAppState.PROFILE_STARTUP) {
405 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 }
407
Sunny Goyal2100c782016-08-22 16:00:03 -0700408 // We only load the page synchronously if the user rotates (or triggers a
409 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700410 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
411 if (savedInstanceState != null) {
412 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
413 }
414 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700415 // If we are not binding synchronously, show a fade in animation when
416 // the first page bind completes.
417 mDragLayer.setAlpha(0);
418 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700419 // Pages bound synchronously.
420 mWorkspace.setCurrentPage(currentScreen);
421
Sunny Goyal2100c782016-08-22 16:00:03 -0700422 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 }
424
425 // For handling default keys
426 mDefaultKeySsb = new SpannableStringBuilder();
427 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800428
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800429 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700430 // In case we are on a device with locked rotation, we should look at preferences to check
431 // if the user has specifically allowed rotation.
432 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700433 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700434 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
435 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700436 }
437
438 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
439 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400440 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700441
Adam Cohen9211d422014-10-07 18:14:53 -0700442 if (mLauncherCallbacks != null) {
443 mLauncherCallbacks.onCreate(savedInstanceState);
444 }
445 }
446
Sunny Goyal7779d622015-06-11 16:18:39 -0700447 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700448 public void onExtractedColorsChanged() {
449 loadExtractedColorsAndColorItems();
450 }
451
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700452 @Override
453 public void onAppWidgetHostReset() {
454 if (mAppWidgetHost != null) {
455 mAppWidgetHost.startListening();
456 }
457 }
458
Tony Wickham827cef22016-03-17 15:39:39 -0700459 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700460 // TODO: do this in pre-N as well, once the extraction part is complete.
Tony Wickham345bff32016-09-28 15:34:51 -0700461 if (Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700462 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700463 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700464 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham345bff32016-09-28 15:34:51 -0700465 // It's possible that All Apps is visible when this is run,
466 // so always use light status bar in that case.
467 activateLightStatusBar(isAllAppsVisible());
Tony Wickham827cef22016-03-17 15:39:39 -0700468 }
469 }
470
Tony Wickham345bff32016-09-28 15:34:51 -0700471 /**
472 * Sets the status bar to be light or not. Light status bar means dark icons.
473 * @param activate if true, make sure the status bar is light, otherwise base on wallpaper.
474 */
475 public void activateLightStatusBar(boolean activate) {
Tony Wickham0fed55b2016-10-12 14:55:46 -0700476 boolean lightStatusBar = activate || (FeatureFlags.LIGHT_STATUS_BAR
477 && mExtractedColors.getColor(ExtractedColors.STATUS_BAR_INDEX,
478 ExtractedColors.DEFAULT_DARK) == ExtractedColors.DEFAULT_LIGHT);
Tony Wickham345bff32016-09-28 15:34:51 -0700479 int oldSystemUiFlags = getWindow().getDecorView().getSystemUiVisibility();
480 int newSystemUiFlags = oldSystemUiFlags;
Tony Wickham93113872016-09-14 16:46:32 -0700481 if (lightStatusBar) {
Tony Wickham345bff32016-09-28 15:34:51 -0700482 newSystemUiFlags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
Tony Wickham93113872016-09-14 16:46:32 -0700483 } else {
Tony Wickham345bff32016-09-28 15:34:51 -0700484 newSystemUiFlags &= ~(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Tony Wickham93113872016-09-14 16:46:32 -0700485 }
Tony Wickham345bff32016-09-28 15:34:51 -0700486 if (newSystemUiFlags != oldSystemUiFlags) {
487 getWindow().getDecorView().setSystemUiVisibility(newSystemUiFlags);
488 }
Tony Wickham93113872016-09-14 16:46:32 -0700489 }
490
Adam Cohen9211d422014-10-07 18:14:53 -0700491 private LauncherCallbacks mLauncherCallbacks;
492
493 public void onPostCreate(Bundle savedInstanceState) {
494 super.onPostCreate(savedInstanceState);
495 if (mLauncherCallbacks != null) {
496 mLauncherCallbacks.onPostCreate(savedInstanceState);
497 }
498 }
499
Winson1f064272016-07-18 17:18:02 -0700500 public void onInsetsChanged(Rect insets) {
501 mDeviceProfile.updateInsets(insets);
502 mDeviceProfile.layout(this, true /* notifyListeners */);
503 }
504
Sunny Goyal32554d12015-12-03 15:31:25 -0800505 /**
506 * Call this after onCreate to set or clear overlay.
507 */
508 public void setLauncherOverlay(LauncherOverlay overlay) {
509 if (overlay != null) {
510 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
511 }
512 mWorkspace.setLauncherOverlay(overlay);
513 }
514
Adam Cohen9211d422014-10-07 18:14:53 -0700515 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
516 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700517 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700518 private boolean mWorkspaceImportanceStored = false;
519 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700520 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800521 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700522 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
523
524 @Override
525 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700526 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700527 return;
528 }
529 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700530 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700531 if (mWorkspace != null) {
532 mWorkspaceImportanceForAccessibility =
533 mWorkspace.getImportantForAccessibility();
534 mWorkspace.setImportantForAccessibility(
535 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
536 mWorkspaceImportanceStored = true;
537 }
538 if (mHotseat != null) {
539 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
540 mHotseat.setImportantForAccessibility(
541 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
542 mHotseatImportanceStored = true;
543 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700544 }
545
546 @Override
547 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700548 if (mWorkspaceImportanceStored && mWorkspace != null) {
549 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
550 }
551 if (mHotseatImportanceStored && mHotseat != null) {
552 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
553 }
554 mWorkspaceImportanceStored = false;
555 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700556 }
557 });
Adam Cohen9211d422014-10-07 18:14:53 -0700558 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700559 }
560
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700561 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700562 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700563 if (mLauncherCallbacks != null) {
564 mLauncherCallbacks.onLauncherProviderChange();
565 }
566 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700567
Adam Cohen9211d422014-10-07 18:14:53 -0700568 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700569 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700570 if (mLauncherCallbacks != null) {
571 return mLauncherCallbacks.hasCustomContentToLeft();
572 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700573 return false;
574 }
575
Dave Hawkeya8881582013-09-17 15:55:33 -0600576 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500577 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600578 * {@link #addToCustomContentPage}. This will only be invoked if
579 * {@link #hasCustomContentToLeft()} is {@code true}.
580 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500581 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700582 if (mLauncherCallbacks != null) {
583 mLauncherCallbacks.populateCustomContentContainer();
584 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600585 }
586
587 /**
Sunny Goyalfe770c92016-09-27 14:38:58 -0700588 * Invoked by subclasses to signal a change to the {@link #addToCustomContentPage} value to
Dave Hawkeya8881582013-09-17 15:55:33 -0600589 * ensure the custom content page is added or removed if necessary.
590 */
591 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600592 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600593 // Not bound yet, wait for bindScreens to be called.
594 return;
595 }
596
597 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
598 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500599 mWorkspace.createCustomContentContainer();
600 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600601 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
602 mWorkspace.removeCustomContentPage();
603 }
604 }
605
Hyunyoung Songaa953652016-04-19 18:30:24 -0700606 public UserEventDispatcher getUserEventDispatcher() {
607 if (mLauncherCallbacks != null) {
608 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
609 if (dispatcher != null) {
610 return dispatcher;
611 }
612 }
613
Sunny Goyal64976d52016-06-20 14:56:28 -0700614 // Logger object is a singleton and does not have to be coupled with the foreground
615 // activity. Since most user event logging is done on the UI, the object is retrieved
616 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700617 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700618 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700619 }
620 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800621 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100622
Hyunyoung Song3f471442015-04-08 19:01:34 -0700623 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700624 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
625 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700626 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700627 }
628
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000629 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700630 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
631 // This cast is safe as long as the id < 0x00FFFFFF
632 // Since we jail all the dynamically generated views, there should be no clashes
633 // with any other views.
634 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000635 }
636
637 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700638 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
639 * a configuration step, this allows the proper animations to run after other transitions.
640 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700641 private long completeAdd(
642 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
643 long screenId = info.screenId;
644 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700645 // When the screen id represents an actual screen (as opposed to a rank) we make sure
646 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700647 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700648 }
Adam Cohendb364c32014-05-20 14:23:40 -0700649
Sunny Goyal2100c782016-08-22 16:00:03 -0700650 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800651 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700652 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700653 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800654 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700655 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700656 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700657 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700658 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700659 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700660 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700661 int widgetId = appWidgetId;
662 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700663 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700664 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700665 // Since the view was just bound, also launch the configure activity if needed
666 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
667 .getLauncherAppWidgetInfo(widgetId);
668 if (provider != null && provider.configure != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700669 startRestoredWidgetReconfigActivity(provider, widgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -0700670 }
671 }
672 break;
673 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800674 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700675
Adam Cohendb364c32014-05-20 14:23:40 -0700676 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800677 }
678
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800679 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700680 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700681 if (isWorkspaceLoading()) {
682 // process the result once the workspace has loaded.
683 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
684 return;
685 }
686 mPendingActivityResult = null;
687
Winson Chunge341d302013-08-16 14:31:00 -0700688 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700689 final PendingRequestArgs requestArgs = mPendingRequestArgs;
690 setWaitingForResult(null);
691 if (requestArgs == null) {
692 return;
693 }
694
695 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700696
Adam Cohenad4e15c2013-10-17 16:21:35 -0700697 Runnable exitSpringLoaded = new Runnable() {
698 @Override
699 public void run() {
700 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
701 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
702 }
703 };
704
Michael Jurka8b805b12012-04-18 14:23:14 -0700705 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700706 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700707 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700708 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
709 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700710 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700711 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700712 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700713 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700714 addAppWidgetImpl(
715 appWidgetId, requestArgs, null,
716 requestArgs.getWidgetProvider(),
717 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700718 }
719 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200720 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
721 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700722 // User could have free-scrolled between pages before picking a wallpaper; make sure
723 // we move to the closest one now.
724 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700725 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200726 }
727 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700728 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200729
Adam Cohened66b2b2012-01-23 17:28:51 -0800730 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700731 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700732
Adam Cohened66b2b2012-01-23 17:28:51 -0800733 // We have special handling for widgets
734 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800735 final int appWidgetId;
736 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
737 : -1;
738 if (widgetId < 0) {
739 appWidgetId = pendingAddWidgetId;
740 } else {
741 appWidgetId = widgetId;
742 }
743
Adam Cohenad4e15c2013-10-17 16:21:35 -0700744 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800745 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700746 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
747 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700748 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700749 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700750 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700751 @Override
752 public void run() {
753 exitSpringLoadedDragModeDelayed(false, 0, null);
754 }
755 };
Adam Cohendb364c32014-05-20 14:23:40 -0700756
Sunny Goyal2100c782016-08-22 16:00:03 -0700757 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
758 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
759 } else {
760 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
761 // When the screen id represents an actual screen (as opposed to a rank)
762 // we make sure that the drop page actually exists.
763 requestArgs.screenId =
764 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700765 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700766 final CellLayout dropLayout =
767 mWorkspace.getScreenWithId(requestArgs.screenId);
768
769 dropLayout.setDropPending(true);
770 final Runnable onComplete = new Runnable() {
771 @Override
772 public void run() {
773 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
774 dropLayout.setDropPending(false);
775 }
776 };
777 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
778 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700779 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800780 return;
781 }
782
Sunny Goyald478c832016-04-01 12:04:16 -0700783 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
784 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700785 if (resultCode == RESULT_OK) {
786 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700787 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700788 }
789 // Leave the widget in the pending state if the user canceled the configure.
790 return;
791 }
792
Sunny Goyalaad90582015-07-09 14:22:50 -0700793 if (requestCode == REQUEST_CREATE_SHORTCUT) {
794 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700795 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
796 completeAdd(requestCode, data, -1, requestArgs);
797 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
798 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
799
Sunny Goyalaad90582015-07-09 14:22:50 -0700800 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700801 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
802 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800804 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800805 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800806 }
807
808 @Override
809 protected void onActivityResult(
810 final int requestCode, final int resultCode, final Intent data) {
811 handleActivityResult(requestCode, resultCode, data);
812 if (mLauncherCallbacks != null) {
813 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
814 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800815 }
816
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600817 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700818 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600819 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700820 PendingRequestArgs pendingArgs = mPendingRequestArgs;
821 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
822 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
823 setWaitingForResult(null);
824
Sunny Goyal28c6b962015-10-12 11:42:05 -0700825 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700826 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700827 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700828 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700829 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700830 Intent intent = pendingArgs.getPendingIntent();
831
Sunny Goyal28c6b962015-10-12 11:42:05 -0700832 if (grantResults.length > 0
833 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700834 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700835 } else {
836 // TODO: Show a snack bar with link to settings
837 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700838 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700839 }
840 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600841 if (mLauncherCallbacks != null) {
842 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
843 grantResults);
844 }
845 }
846
Adam Cohendb364c32014-05-20 14:23:40 -0700847 /**
848 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
849 *
850 * @param screenId the screen id to check
851 * @return the new screen, or screenId if it exists
852 */
853 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800854 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700855 if (dropLayout == null) {
856 // it's possible that the add screen was removed because it was
857 // empty and a re-bind occurred
858 mWorkspace.addExtraEmptyScreen();
859 return mWorkspace.commitExtraEmptyScreen();
860 } else {
861 return screenId;
862 }
863 }
864
Sunny Goyal2100c782016-08-22 16:00:03 -0700865 @Thunk void completeTwoStageWidgetDrop(
866 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
867 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800868 Runnable onCompleteRunnable = null;
869 int animationType = 0;
870
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700871 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800872 if (resultCode == RESULT_OK) {
873 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
874 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal2100c782016-08-22 16:00:03 -0700875 requestArgs.getWidgetProvider());
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700876 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800877 onCompleteRunnable = new Runnable() {
878 @Override
879 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700880 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700881 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
882 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800883 }
884 };
885 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800886 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800887 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800888 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700889 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700890 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700891 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
892 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700893 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700894 // The animated view may be null in the case of a rotation during widget configuration
895 onCompleteRunnable.run();
896 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897 }
898
899 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700900 protected void onStop() {
901 super.onStop();
902 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700903
904 if (mLauncherCallbacks != null) {
905 mLauncherCallbacks.onStop();
906 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700907
908 if (Utilities.isNycMR1OrAbove()) {
909 mAppWidgetHost.stopListening();
910 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700911 }
912
913 @Override
914 protected void onStart() {
915 super.onStart();
916 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700917
918 if (mLauncherCallbacks != null) {
919 mLauncherCallbacks.onStart();
920 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700921
922 if (Utilities.isNycMR1OrAbove()) {
923 mAppWidgetHost.startListening();
924 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700925 }
926
927 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200929 long startTime = 0;
930 if (DEBUG_RESUME_TIME) {
931 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400932 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200933 }
Adam Cohen9211d422014-10-07 18:14:53 -0700934
935 if (mLauncherCallbacks != null) {
936 mLauncherCallbacks.preOnResume();
937 }
938
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700940 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700941
Winson Chung4a2afa32012-07-19 14:53:05 -0700942 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700943 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700944 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800945 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700946 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700947 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700948 // view after launching an app, as they may be depending on the UI to be static to
949 // switch to another app, otherwise, if it was
Hyunyoung Songdd60ce42016-07-27 17:08:38 -0700950 showAppsView(false /* animated */, !launchedFromApp /* updatePredictedApps */,
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700951 mAppsView.shouldRestoreImeState() /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800952 } else if (mOnResumeState == State.WIDGETS) {
953 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700954 }
955 mOnResumeState = State.NONE;
956
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800957 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700958 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700959 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700960 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700961 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800962 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700963 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200964 // We might have postponed some bind calls until onResume (see waitUntilResume) --
965 // execute them here
966 long startTimeCallbacks = 0;
967 if (DEBUG_RESUME_TIME) {
968 startTimeCallbacks = System.currentTimeMillis();
969 }
970
Michael Jurka1e2f4652013-07-08 18:03:46 -0700971 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
972 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200973 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700974 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200975 if (DEBUG_RESUME_TIME) {
976 Log.d(TAG, "Time spent processing callbacks in onResume: " +
977 (System.currentTimeMillis() - startTimeCallbacks));
978 }
Michael Jurka447bf842013-05-15 14:52:15 +0200979 }
Michael Jurka54554252013-08-01 12:52:23 +0200980 if (mOnResumeCallbacks.size() > 0) {
981 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
982 mOnResumeCallbacks.get(i).run();
983 }
984 mOnResumeCallbacks.clear();
985 }
Winson Chunge4e50662012-01-23 14:45:13 -0800986
987 // Reset the pressed state of icons that were locked in the press state while activities
988 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800989 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800990 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800991 mWaitingForResume.setStayPressed(false);
992 }
Winson Chung82963d52013-10-09 11:20:57 -0700993
Adam Cohen06dff352012-06-01 17:17:08 -0700994 // It is possible that widgets can receive updates while launcher is not in the foreground.
995 // Consequently, the widgets will be inflated in the orientation of the foreground activity
996 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
997 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -0700998 if (!isWorkspaceLoading()) {
999 getWorkspace().reinflateWidgetsIfNecessary();
1000 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001001
Michael Jurka447bf842013-05-15 14:52:15 +02001002 if (DEBUG_RESUME_TIME) {
1003 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1004 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001005
Adam Cohen4b66bf32015-09-18 12:15:19 -07001006 // We want to suppress callbacks about CustomContent being shown if we have just received
1007 // onNewIntent while the user was present within launcher. In that case, we post a call
1008 // to move the user to the main screen (which will occur after onResume). We don't want to
1009 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1010 // suppress here.
1011 if (mWorkspace.getCustomContentCallbacks() != null
1012 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001013 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001014 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001015 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1016 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001017 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001018 }
1019 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001020 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001021 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001022 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001023
Sunny Goyal756adbc2015-04-16 15:20:51 -07001024 if (!isWorkspaceLoading()) {
1025 // Process any items that were added while Launcher was away.
1026 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001027
1028 // Refresh shortcuts if the permission changed.
1029 mModel.refreshShortcutsIfRequired();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001030 }
Adam Cohen9211d422014-10-07 18:14:53 -07001031
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001032 if (shouldShowDiscoveryBounce()) {
1033 mAllAppsController.showDiscoveryBounce();
1034 }
1035 mIsResumeFromActionScreenOff = false;
Adam Cohen9211d422014-10-07 18:14:53 -07001036 if (mLauncherCallbacks != null) {
1037 mLauncherCallbacks.onResume();
1038 }
Adam Cohen06dff352012-06-01 17:17:08 -07001039 }
1040
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001042 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001043 // Ensure that items added to Launcher are queued until Launcher returns
1044 InstallShortcutReceiver.enableInstallQueue();
1045
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001046 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001047 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001048 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001049 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001050
1051 // We call onHide() aggressively. The custom content callbacks should be able to
1052 // debounce excess onHide calls.
1053 if (mWorkspace.getCustomContentCallbacks() != null) {
1054 mWorkspace.getCustomContentCallbacks().onHide();
1055 }
Romain Guycbb89e42009-06-08 15:52:54 -07001056
Adam Cohen9211d422014-10-07 18:14:53 -07001057 if (mLauncherCallbacks != null) {
1058 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001059 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001060 }
1061
1062 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001063 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1064 // by a onResume or by scrolling otherwise.
1065 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001066
1067 // Custom content is completely hidden
1068 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001069
1070 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1071 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001072
1073 // Indicates whether the user is allowed to scroll away from the custom content.
1074 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001075 }
1076
Adam Cohenc2d6e892014-10-16 09:49:24 -07001077 public interface LauncherOverlay {
1078
1079 /**
1080 * Touch interaction leading to overscroll has begun
1081 */
1082 public void onScrollInteractionBegin();
1083
1084 /**
1085 * Touch interaction related to overscroll has ended
1086 */
1087 public void onScrollInteractionEnd();
1088
1089 /**
1090 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1091 * screen (or in the case of RTL, the rightmost screen).
1092 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001093 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001094
1095 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001096 * Called when the launcher is ready to use the overlay
1097 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001098 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001099 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001100 }
1101
Jun Mukai8af0cd82015-05-12 12:32:12 -07001102 public interface LauncherSearchCallbacks {
1103 /**
1104 * Called when the search overlay is shown.
1105 */
1106 public void onSearchOverlayOpened();
1107
1108 /**
1109 * Called when the search overlay is dismissed.
1110 */
1111 public void onSearchOverlayClosed();
1112 }
1113
Adam Cohenc2d6e892014-10-16 09:49:24 -07001114 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001115 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001116 }
1117
1118 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1119
Sunny Goyalc86df472016-02-25 09:19:38 -08001120 public void onScrollChanged(float progress) {
1121 if (mWorkspace != null) {
1122 mWorkspace.onOverlayScrollChanged(progress);
1123 }
1124 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001125 }
1126
Jorim Jaggid017f882014-01-14 17:08:48 -08001127 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001128 if (mLauncherCallbacks != null) {
1129 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001130 } else {
1131 // On devices with a locked orientation, we will at least have the allow rotation
1132 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001133 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001134 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001135 }
1136
Adam Cohen9211d422014-10-07 18:14:53 -07001137 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001138 CustomContentCallbacks callbacks, String description) {
1139 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001140 }
1141
Adam Cohenedb40762013-07-18 16:45:45 -07001142 // The custom content needs to offset its content to account for the QSB
1143 public int getTopOffsetForCustomContent() {
1144 return mWorkspace.getPaddingTop();
1145 }
1146
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001147 @Override
1148 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001149 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001150 if (mModel.isCurrentCallbacks(this)) {
1151 mModel.stopLoader();
1152 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001153 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1154
Romain Guy13c2e7b2010-03-10 19:45:00 -08001155 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001156 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001157
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001158 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001159 @Override
1160 public void onWindowFocusChanged(boolean hasFocus) {
1161 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001162 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001163
1164 if (mLauncherCallbacks != null) {
1165 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1166 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001167 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001168
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169 private boolean acceptFilter() {
1170 final InputMethodManager inputManager = (InputMethodManager)
1171 getSystemService(Context.INPUT_METHOD_SERVICE);
1172 return !inputManager.isFullscreenMode();
1173 }
1174
1175 @Override
1176 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001177 final int uniChar = event.getUnicodeChar();
1178 final boolean handled = super.onKeyDown(keyCode, event);
1179 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1180 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1182 keyCode, event);
1183 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001184 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001185 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001186 // showSearchDialog()
1187 // If there are multiple keystrokes before the search dialog takes focus,
1188 // onSearchRequested() will be called for every keystroke,
1189 // but it is idempotent, so it's fine.
1190 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191 }
1192 }
1193
Joe Onorato8a9625e2010-01-28 15:55:35 -08001194 // Eat the long press event so the keyboard doesn't come up.
1195 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1196 return true;
1197 }
1198
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 return handled;
1200 }
1201
Winson46163472015-09-22 17:31:56 -07001202 @Override
1203 public boolean onKeyUp(int keyCode, KeyEvent event) {
1204 if (keyCode == KeyEvent.KEYCODE_MENU) {
1205 // Ignore the menu key if we are currently dragging or are on the custom content screen
1206 if (!isOnCustomContent() && !mDragController.isDragging()) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001207 // Close any open floating view
1208 AbstractFloatingView.closeAllOpenViews(this);
Tony Wickham49c8d292016-07-01 11:44:34 -07001209
Winson46163472015-09-22 17:31:56 -07001210 // Stop resizing any widgets
1211 mWorkspace.exitWidgetResizeMode();
1212
1213 // Show the overview mode if we are on the workspace
1214 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1215 !mWorkspace.isSwitchingState()) {
1216 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001217 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001218 }
1219 }
1220 return true;
1221 }
1222 return super.onKeyUp(keyCode, event);
1223 }
1224
Karl Rosaen138a0412009-04-23 19:00:21 -07001225 private String getTypedText() {
1226 return mDefaultKeySsb.toString();
1227 }
1228
Sunny Goyal6f28e712016-09-13 14:19:29 -07001229 @Override
1230 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001231 mDefaultKeySsb.clear();
1232 mDefaultKeySsb.clearSpans();
1233 Selection.setSelection(mDefaultKeySsb, 0);
1234 }
1235
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 /**
1237 * Restores the previous state, if it exists.
1238 *
1239 * @param savedState The previous state.
1240 */
1241 private void restoreState(Bundle savedState) {
1242 if (savedState == null) {
1243 return;
1244 }
1245
Sunny Goyalfe770c92016-09-27 14:38:58 -07001246 int stateOrdinal = savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal());
1247 State[] stateValues = State.values();
1248 State state = (stateOrdinal >= 0 && stateOrdinal < stateValues.length)
1249 ? stateValues[stateOrdinal] : State.WORKSPACE;
Winson Chungb745afb2015-03-02 11:51:23 -08001250 if (state == State.APPS || state == State.WIDGETS) {
1251 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001252 }
1253
Sunny Goyal2100c782016-08-22 16:00:03 -07001254 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1255 if (requestArgs != null) {
1256 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001258
1259 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 }
1261
1262 /**
1263 * Finds all the views we need and configure them properly.
1264 */
1265 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001266 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001267 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001268 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001269 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyal6178f132016-07-11 17:30:03 -07001270 mQsbContainer = mDragLayer.findViewById(mDeviceProfile.isVerticalBarLayout()
1271 ? R.id.workspace_blocked_row : R.id.qsb_container);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001272 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001273
Sunny Goyal784f9c32016-03-23 16:56:54 -07001274 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1275 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1276 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277
Winson Chung4c98d922011-05-31 16:50:48 -07001278 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001279 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001280
Winson Chung3d503fb2011-07-13 17:25:49 -07001281 // Setup the hotseat
1282 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1283 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001284 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001285 }
1286
Winsone9f27272015-10-13 10:47:51 -07001287 // Setup the overview panel
1288 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001289
Winson Chung4c98d922011-05-31 16:50:48 -07001290 // Setup the workspace
1291 mWorkspace.setHapticFeedbackEnabled(false);
1292 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001293 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001294 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1295 // default state, otherwise we will update to the wrong offsets in RTL
1296 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001297 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001298 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001299
Tony Wickham34d2c912015-09-11 09:27:58 -07001300 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001301 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001302
Winson Chungef7f8742015-06-04 17:18:17 -07001303 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001304 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001305 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001306 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1307 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1308 } else {
1309 mAppsView.setSearchBarController(new DefaultAppSearchController());
1310 }
Craig Mautner360310b2012-10-26 15:13:08 -07001311
Winson Chung3d503fb2011-07-13 17:25:49 -07001312 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song0de01172016-10-05 16:27:48 -07001313 mDragController.setDragScroller(mWorkspace);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001314 mDragController.setScrollView(mDragLayer);
1315 mDragController.setMoveTarget(mWorkspace);
1316 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001317 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001318
Peter Schiller310a9882016-06-30 13:52:36 -07001319 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1320 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1321 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001322
Sunny Goyal322d5562015-06-25 19:35:49 -07001323 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1324 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001325 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326 }
1327
Winsone9f27272015-10-13 10:47:51 -07001328 private void setupOverviewPanel() {
1329 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1330
Winsone9f27272015-10-13 10:47:51 -07001331 // Bind wallpaper button actions
1332 View wallpaperButton = findViewById(R.id.wallpaper_button);
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001333 new OverviewButtonClickListener(LauncherLogProto.WALLPAPER_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001334 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001335 public void handleViewClick(View view) {
1336 onClickWallpaperPicker(view);
Winsone9f27272015-10-13 10:47:51 -07001337 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001338 }.attachTo(wallpaperButton);
Winsone9f27272015-10-13 10:47:51 -07001339 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1340
1341 // Bind widget button actions
1342 mWidgetsButton = findViewById(R.id.widget_button);
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001343 new OverviewButtonClickListener(LauncherLogProto.WIDGETS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001344 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001345 public void handleViewClick(View view) {
1346 onClickAddWidgetButton(view);
Winsone9f27272015-10-13 10:47:51 -07001347 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001348 }.attachTo(mWidgetsButton);
Winsone9f27272015-10-13 10:47:51 -07001349 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1350
1351 // Bind settings actions
1352 View settingsButton = findViewById(R.id.settings_button);
1353 boolean hasSettings = hasSettings();
1354 if (hasSettings) {
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001355 new OverviewButtonClickListener(LauncherLogProto.SETTINGS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001356 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001357 public void handleViewClick(View view) {
1358 onClickSettingsButton(view);
Winsone9f27272015-10-13 10:47:51 -07001359 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001360 }.attachTo(settingsButton);
Winsone9f27272015-10-13 10:47:51 -07001361 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1362 } else {
1363 settingsButton.setVisibility(View.GONE);
1364 }
1365
1366 mOverviewPanel.setAlpha(0f);
1367 }
1368
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001370 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001371 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001372 */
1373 public void setAllAppsButton(View allAppsButton) {
1374 mAllAppsButton = allAppsButton;
1375 }
1376
Sunny Goyalbb011da2016-06-15 15:42:29 -07001377 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001378 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001379 }
1380
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001381 public View getWidgetsButton() {
1382 return mWidgetsButton;
1383 }
1384
Anjali Koppal5ad44842014-03-10 20:34:39 -07001385 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 * Creates a view representing a shortcut.
1387 *
1388 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001390 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001391 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 }
1393
1394 /**
1395 * Creates a view representing a shortcut inflated from the specified resource.
1396 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 * @param parent The group the shortcut belongs to.
1398 * @param info The data structure describing the shortcut.
1399 *
1400 * @return A View inflated from layoutResId.
1401 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001402 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001403 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001404 parent, false);
1405 favorite.applyFromShortcutInfo(info, mIconCache);
1406 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001408 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 return favorite;
1410 }
1411
1412 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001413 * Add a shortcut to the workspace.
1414 *
1415 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001417 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001418 int cellY, PendingRequestArgs args) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001419 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001420 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001421
Sunny Goyal5c97f512015-05-19 16:03:28 -07001422 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001423 if (info == null || args.getRequestCode() != REQUEST_CREATE_SHORTCUT ||
1424 args.getPendingIntent().getComponent() == null) {
1425 return;
1426 }
1427 if (!PackageManagerHelper.hasPermissionForActivity(
1428 this, info.intent, args.getPendingIntent().getComponent().getPackageName())) {
1429 // The app is trying to add a shortcut without sufficient permissions
1430 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
Adam Cohend9198822011-11-22 16:42:47 -08001431 return;
1432 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001433 final View view = createShortcut(info);
1434
Sunny Goyal5c97f512015-05-19 16:03:28 -07001435 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001436 // First we check if we already know the exact location where we want to add this item.
1437 if (cellX >= 0 && cellY >= 0) {
1438 cellXY[0] = cellX;
1439 cellXY[1] = cellY;
1440 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001441
1442 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001443 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001444 true, null,null)) {
1445 return;
1446 }
1447 DragObject dragObject = new DragObject();
1448 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001449 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1450 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001451 return;
1452 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001453 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001454 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001455 }
1456
1457 if (!foundCellSpan) {
Jon Miranda9485e5f2016-10-18 11:47:42 -07001458 mWorkspace.onNoCellFound(layout);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001459 return;
1460 }
1461
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001462 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463
Sunny Goyal2100c782016-08-22 16:00:03 -07001464 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
1465 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 }
1467
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001469 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001471 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001473 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001474 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1475
Adam Cohened66b2b2012-01-23 17:28:51 -08001476 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001477 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001478 }
Romain Guycbb89e42009-06-08 15:52:54 -07001479
Adam Cohen59400422014-03-05 18:07:04 -08001480 if (appWidgetInfo.isCustomWidget) {
1481 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001482 }
1483
Adam Cohen59400422014-03-05 18:07:04 -08001484 LauncherAppWidgetInfo launcherInfo;
1485 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001486 launcherInfo.spanX = itemInfo.spanX;
1487 launcherInfo.spanY = itemInfo.spanY;
1488 launcherInfo.minSpanX = itemInfo.minSpanX;
1489 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001490 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001491
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001493 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494
Sunny Goyal2100c782016-08-22 16:00:03 -07001495 if (hostView == null) {
1496 // Perform actual inflation because we're live
1497 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001499 hostView.setVisibility(View.VISIBLE);
1500 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 }
Romain Guycbb89e42009-06-08 15:52:54 -07001502
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001503 private void addAppWidgetToWorkspace(
1504 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001505 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001506 hostView.setTag(item);
1507 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001508
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001509 hostView.setFocusable(true);
1510 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001511
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001512 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001513 item.cellX, item.cellY, item.spanX, item.spanY, insert);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001514 }
1515
Adam Cohended9f8d2010-11-03 13:25:16 -07001516 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1517 @Override
1518 public void onReceive(Context context, Intent intent) {
1519 final String action = intent.getAction();
1520 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Sunny Goyal6ad72f02016-09-23 11:01:10 -07001521 mDragLayer.clearResizeFrame();
Winson Chung337cd9d2011-03-30 10:39:30 -07001522
Winson Chungc100e8e2011-08-09 16:02:43 -07001523 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001524 // processing a multi-step drop
Sunny Goyal2100c782016-08-22 16:00:03 -07001525 if (mAppsView != null && mWidgetsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001526 if (!showWorkspace(false)) {
1527 // If we are already on the workspace, then manually reset all apps
1528 mAppsView.reset();
1529 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001530 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001531 mIsResumeFromActionScreenOff = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001532 }
1533 }
1534 };
1535
1536 @Override
1537 public void onAttachedToWindow() {
1538 super.onAttachedToWindow();
1539
1540 // Listen for broadcasts related to user-presence
1541 final IntentFilter filter = new IntentFilter();
1542 filter.addAction(Intent.ACTION_SCREEN_OFF);
Adam Cohended9f8d2010-11-03 13:25:16 -07001543 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001544 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001545 mAttached = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001546
1547 if (mLauncherCallbacks != null) {
1548 mLauncherCallbacks.onAttachedToWindow();
1549 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001550 }
1551
1552 @Override
1553 public void onDetachedFromWindow() {
1554 super.onDetachedFromWindow();
Adam Cohend113e0c2010-11-11 10:48:05 -08001555 if (mAttached) {
1556 unregisterReceiver(mReceiver);
1557 mAttached = false;
1558 }
Sunny Goyalc86df472016-02-25 09:19:38 -08001559
1560 if (mLauncherCallbacks != null) {
1561 mLauncherCallbacks.onDetachedFromWindow();
1562 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001563 }
1564
1565 public void onWindowVisibilityChanged(int visibility) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001566 // The following code used to be in onResume, but it turns out onResume is called when
1567 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1568 // is a more appropriate event to handle
Sunny Goyal1acc56a2016-09-25 21:23:25 -07001569 if (visibility == View.VISIBLE) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001570 if (!mWorkspaceLoading) {
1571 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001572 // We want to let Launcher draw itself at least once before we force it to build
1573 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001574 // apps is nice and speedy.
1575 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001576 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001577 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001578 if (mStarted) return;
1579 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001580 // We delay the layer building a bit in order to give
1581 // other message processing a time to run. In particular
1582 // this avoids a delay in hiding the IME if it was
1583 // currently shown, because doing that may involve
1584 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001585 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001586 final ViewTreeObserver.OnDrawListener listener = this;
1587 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001588 public void run() {
1589 if (mWorkspace != null &&
1590 mWorkspace.getViewTreeObserver() != null) {
1591 mWorkspace.getViewTreeObserver().
1592 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001593 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001594 }
1595 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001596 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001597 }
1598 });
1599 }
1600 clearTypedText();
1601 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001602 }
1603
Winson Chunga6945242014-01-08 14:04:34 -08001604 public DragLayer getDragLayer() {
1605 return mDragLayer;
1606 }
1607
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001608 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001609 return mAppsView;
1610 }
1611
Hyunyoung Song3f471442015-04-08 19:01:34 -07001612 public WidgetsContainerView getWidgetsView() {
1613 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001614 }
1615
Winson Chunga6945242014-01-08 14:04:34 -08001616 public Workspace getWorkspace() {
1617 return mWorkspace;
1618 }
1619
Sunny Goyal6178f132016-07-11 17:30:03 -07001620 public View getQsbContainer() {
1621 return mQsbContainer;
1622 }
1623
Winson Chunga6945242014-01-08 14:04:34 -08001624 public Hotseat getHotseat() {
1625 return mHotseat;
1626 }
1627
Jorim Jaggid017f882014-01-14 17:08:48 -08001628 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001629 return mOverviewPanel;
1630 }
1631
Sunny Goyal47328fd2016-05-25 18:56:41 -07001632 public DropTargetBar getDropTargetBar() {
1633 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001634 }
1635
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001636 public LauncherAppWidgetHost getAppWidgetHost() {
1637 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 }
Romain Guycbb89e42009-06-08 15:52:54 -07001639
Winson Chunga9abd0e2010-10-27 17:18:37 -07001640 public LauncherModel getModel() {
1641 return mModel;
1642 }
1643
Adam Cohen79d90c52016-04-22 13:29:20 -07001644 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001645 return mSharedPrefs;
1646 }
1647
Adam Cohen2e6da152015-05-06 11:42:25 -07001648 public DeviceProfile getDeviceProfile() {
1649 return mDeviceProfile;
1650 }
1651
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 @Override
1653 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001654 long startTime = 0;
1655 if (DEBUG_RESUME_TIME) {
1656 startTime = System.currentTimeMillis();
1657 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 super.onNewIntent(intent);
1659
Winson15f8b172015-08-19 11:02:39 -07001660 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1661 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1662 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001663
1664 // Check this condition before handling isActionMain, as this will get reset.
1665 boolean shouldMoveToDefaultScreen = alreadyOnHome &&
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001666 mState == State.WORKSPACE && AbstractFloatingView.getTopOpenView(this) == null;
Sunny Goyal85313732016-09-28 12:00:07 -07001667
Winson15f8b172015-08-19 11:02:39 -07001668 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1669 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001670 if (mWorkspace == null) {
1671 // Can be cases where mWorkspace is null, this prevents a NPE
1672 return;
1673 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001674 // In all these cases, only animate if we're already on home
1675 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001676
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001677 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001678 exitSpringLoadedDragMode();
1679
1680 // If we are already on home, then just animate back to the workspace,
1681 // otherwise, just wait until onResume to set the state back to Workspace
1682 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001683 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001684 } else {
1685 mOnResumeState = State.WORKSPACE;
1686 }
1687
1688 final View v = getWindow().peekDecorView();
1689 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001690 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001691 INPUT_METHOD_SERVICE);
1692 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1693 }
1694
Winson Chungb745afb2015-03-02 11:51:23 -08001695 // Reset the apps view
1696 if (!alreadyOnHome && mAppsView != null) {
1697 mAppsView.scrollToTop();
1698 }
1699
Hyunyoung Song3f471442015-04-08 19:01:34 -07001700 // Reset the widgets view
1701 if (!alreadyOnHome && mWidgetsView != null) {
1702 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001703 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001704
Adam Cohen9211d422014-10-07 18:14:53 -07001705 if (mLauncherCallbacks != null) {
1706 mLauncherCallbacks.onHomeIntent();
1707 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001708 }
Adam Cohened307df2013-10-02 09:37:31 -07001709
Adam Cohen9211d422014-10-07 18:14:53 -07001710 if (mLauncherCallbacks != null) {
1711 mLauncherCallbacks.onNewIntent(intent);
1712 }
Winson15f8b172015-08-19 11:02:39 -07001713
1714 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1715 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1716 // animation.
1717 if (isActionMain) {
Sunny Goyal85313732016-09-28 12:00:07 -07001718 boolean callbackAllowsMoveToDefaultScreen = mLauncherCallbacks != null ?
Ivan Lee667d6882015-09-03 10:16:07 -06001719 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Sunny Goyal85313732016-09-28 12:00:07 -07001720 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()
1721 && callbackAllowsMoveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001722
1723 // We use this flag to suppress noisy callbacks above custom content state
1724 // from onResume.
1725 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001726 mWorkspace.post(new Runnable() {
1727 @Override
1728 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001729 if (mWorkspace != null) {
1730 mWorkspace.moveToDefaultScreen(true);
1731 }
Winson15f8b172015-08-19 11:02:39 -07001732 }
1733 });
1734 }
1735 }
1736
1737 if (DEBUG_RESUME_TIME) {
1738 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1739 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001740 }
1741
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001742 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001743 public void onRestoreInstanceState(Bundle state) {
1744 super.onRestoreInstanceState(state);
1745 for (int page: mSynchronouslyBoundPages) {
1746 mWorkspace.restoreInstanceStateForChild(page);
1747 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001748 }
1749
1750 @Override
1751 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001752 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001753 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1754 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001755
Adam Cohen21cd0022013-10-09 18:57:02 -07001756 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001757 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758
Michael Jurka883f55b2010-10-21 15:47:14 -07001759 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001760 // We close any open folders and shortcut containers since they will not be re-opened,
1761 // and we need to make sure this state is reflected.
1762 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001763
Sunny Goyal2100c782016-08-22 16:00:03 -07001764 if (mPendingRequestArgs != null) {
1765 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1766 }
1767 if (mPendingActivityResult != null) {
1768 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001769 }
1770
Adam Cohen9211d422014-10-07 18:14:53 -07001771 if (mLauncherCallbacks != null) {
1772 mLauncherCallbacks.onSaveInstanceState(outState);
1773 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001774 }
1775
1776 @Override
1777 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001778 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001779
Michael Jurka9d906c72011-10-14 06:25:36 -07001780 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001781 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001782
Winson Chungcd2b0142011-06-08 16:02:26 -07001783 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001784 // It's possible to receive onDestroy after a new Launcher activity has
1785 // been created. In this case, don't interfere with the new Launcher.
1786 if (mModel.isCurrentCallbacks(this)) {
1787 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001788 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001789 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001790
Sunny Goyal745bad92016-05-02 10:54:12 -07001791 if (mRotationPrefChangeHandler != null) {
1792 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1793 }
1794
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001795 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001796 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001798 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001800 mAppWidgetHost = null;
1801
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802 TextKeyListener.getInstance().release();
1803
Tony Wickhame2217252016-03-22 16:34:23 -07001804 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1805 .removeAccessibilityStateChangeListener(this);
1806
Michael Jurka2ecf9952012-06-18 12:52:28 -07001807 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001808
1809 if (mLauncherCallbacks != null) {
1810 mLauncherCallbacks.onDestroy();
1811 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 }
1813
Sunny Goyalae502842016-06-17 08:43:56 -07001814 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1815 return mAccessibilityDelegate;
1816 }
1817
Adam Cohena9cf38f2011-05-02 15:36:58 -07001818 public DragController getDragController() {
1819 return mDragController;
1820 }
1821
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001822 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001823 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001824 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001825 }
1826
1827 @Override
1828 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1829 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001830 try {
1831 super.startIntentSenderForResult(intent, requestCode,
1832 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1833 } catch (IntentSender.SendIntentException e) {
1834 throw new ActivityNotFoundException();
1835 }
1836 }
1837
Winson Chung70d72102011-08-12 11:24:35 -07001838 /**
1839 * Indicates that we want global search for this activity by setting the globalSearch
1840 * argument for {@link #startSearch} to true.
1841 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001842 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001843 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001845
Karl Rosaen138a0412009-04-23 19:00:21 -07001846 if (initialQuery == null) {
1847 // Use any text typed in the launcher as the initial query
1848 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001849 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 if (appSearchData == null) {
1851 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001852 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001853 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001854
Sunny Goyal6f28e712016-09-13 14:19:29 -07001855 if (mLauncherCallbacks == null ||
1856 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1857 // Starting search from the callbacks failed. Start the default global search.
1858 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001859 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001860
1861 // We need to show the workspace after starting the search
1862 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001863 }
1864
Ian Parkinson047036e2014-09-01 15:40:53 +01001865 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001866 * Starts the global search activity. This code is a copied from SearchManager
1867 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001868 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001869 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001870 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001871 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1872 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1873 if (globalSearchActivity == null) {
1874 Log.w(TAG, "No global search activity found.");
1875 return;
1876 }
1877 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1878 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1879 intent.setComponent(globalSearchActivity);
1880 // Make sure that we have a Bundle to put source in
1881 if (appSearchData == null) {
1882 appSearchData = new Bundle();
1883 } else {
1884 appSearchData = new Bundle(appSearchData);
1885 }
Adam Cohen9211d422014-10-07 18:14:53 -07001886 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001887 if (!appSearchData.containsKey("source")) {
1888 appSearchData.putString("source", getPackageName());
1889 }
1890 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1891 if (!TextUtils.isEmpty(initialQuery)) {
1892 intent.putExtra(SearchManager.QUERY, initialQuery);
1893 }
1894 if (selectInitialQuery) {
1895 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1896 }
1897 intent.setSourceBounds(sourceBounds);
1898 try {
1899 startActivity(intent);
1900 } catch (ActivityNotFoundException ex) {
1901 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1902 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 }
1904
Yura4f93ec62014-02-04 14:15:21 +00001905 public boolean isOnCustomContent() {
1906 return mWorkspace.isOnOrMovingToCustomContent();
1907 }
1908
Winson Chung70d72102011-08-12 11:24:35 -07001909 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001910 public boolean onPrepareOptionsMenu(Menu menu) {
1911 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001912 if (mLauncherCallbacks != null) {
1913 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1914 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001915 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001916 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001918 @Override
1919 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001920 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001921 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001922 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001923 }
1924
Joe Onorato9c1289c2009-08-17 11:03:03 -04001925 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001926 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001927 }
1928
Adam Cohen517a7f52014-03-01 12:12:59 -08001929 public boolean isWorkspaceLoading() {
1930 return mWorkspaceLoading;
1931 }
1932
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001933 private void setWorkspaceLoading(boolean value) {
1934 boolean isLocked = isWorkspaceLocked();
1935 mWorkspaceLoading = value;
1936 if (isLocked != isWorkspaceLocked()) {
1937 onWorkspaceLockedChanged();
1938 }
1939 }
1940
Sunny Goyal2100c782016-08-22 16:00:03 -07001941 private void setWaitingForResult(PendingRequestArgs args) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001942 boolean isLocked = isWorkspaceLocked();
Sunny Goyal2100c782016-08-22 16:00:03 -07001943 mPendingRequestArgs = args;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001944 if (isLocked != isWorkspaceLocked()) {
1945 onWorkspaceLockedChanged();
1946 }
1947 }
1948
Adam Cohen9211d422014-10-07 18:14:53 -07001949 protected void onWorkspaceLockedChanged() {
1950 if (mLauncherCallbacks != null) {
1951 mLauncherCallbacks.onWorkspaceLockedChanged();
1952 }
1953 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001954
Sunny Goyal2100c782016-08-22 16:00:03 -07001955 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1956 LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001957 if (LOGD) {
1958 Log.d(TAG, "Adding widget from drop");
1959 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07001960 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
1961 }
1962
Sunny Goyal2100c782016-08-22 16:00:03 -07001963 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
1964 AppWidgetHostView boundWidget, LauncherAppWidgetProviderInfo appWidgetInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001965 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001966 if (appWidgetInfo.configure != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001967 setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, appWidgetInfo, info));
Michael Jurkaaf442092010-06-10 17:01:57 -07001968
Bjorn Bringert7984c942009-12-09 15:38:25 +00001969 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07001970 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
1971 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001973 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07001974 Runnable onComplete = new Runnable() {
1975 @Override
1976 public void run() {
1977 // Exit spring loaded mode if necessary after adding the widget
1978 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1979 null);
1980 }
1981 };
Sunny Goyal2100c782016-08-22 16:00:03 -07001982 completeAddAppWidget(appWidgetId, info, boundWidget, appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07001983 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984 }
1985 }
Romain Guycbb89e42009-06-08 15:52:54 -07001986
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001987 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07001988 // Close any folders that may be open.
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001989 AbstractFloatingView.closeAllOpenViews(this, animate);
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001990 mWorkspace.moveToCustomContentScreen(animate);
1991 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001992
1993 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1994 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001995 info.container = container;
1996 info.screenId = screenId;
1997 if (cell != null) {
1998 info.cellX = cell[0];
1999 info.cellY = cell[1];
2000 }
2001 info.spanX = spanX;
2002 info.spanY = spanY;
2003
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002004 switch (info.itemType) {
2005 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2006 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07002007 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002008 break;
2009 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal2100c782016-08-22 16:00:03 -07002010 processShortcutFromDrop(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002011 break;
2012 default:
2013 throw new IllegalStateException("Unknown item type: " + info.itemType);
2014 }
2015 }
2016
Adam Cohenfbba09b2011-07-18 21:43:05 -07002017 /**
2018 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002019 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002020 private void processShortcutFromDrop(PendingAddItemInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07002021 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
2022 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
2023 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002024 }
2025
Adam Cohenfbba09b2011-07-18 21:43:05 -07002026 /**
2027 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002028 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002029 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002030 AppWidgetHostView hostView = info.boundWidget;
2031 int appWidgetId;
2032 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002033 // In the case where we've prebound the widget, we remove it from the DragLayer
2034 if (LOGD) {
2035 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2036 }
2037 getDragLayer().removeView(hostView);
2038
Adam Cohened66b2b2012-01-23 17:28:51 -08002039 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002040 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002041
2042 // Clear the boundWidget so that it doesn't get destroyed.
2043 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002044 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002045 // In this case, we either need to start an activity to get permission to bind
2046 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002047 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002048 Bundle options = info.bindOptions;
2049
Sunny Goyalffe83f12014-08-14 17:39:34 -07002050 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2051 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002052 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002053 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002054 } else {
Sunny Goyal2100c782016-08-22 16:00:03 -07002055 setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, info.info, info));
Michael Jurka8b805b12012-04-18 14:23:14 -07002056 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2057 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2058 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyal2100c782016-08-22 16:00:03 -07002059 mAppWidgetManager.getUser(info.info)
Sunny Goyalffe83f12014-08-14 17:39:34 -07002060 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002061 // TODO: we need to make sure that this accounts for the options bundle.
2062 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002063 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2064 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002065 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002066 }
2067
Adam Cohendcd297f2013-06-18 13:13:40 -07002068 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002069 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002070 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 folderInfo.title = getText(R.string.folder_name);
2072
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002074 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2075 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002076
2077 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002078 FolderIcon newFolder =
2079 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002080 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002081 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002082 // Force measure the new folder icon
2083 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2084 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002085 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002086 }
Romain Guycbb89e42009-06-08 15:52:54 -07002087
Winsonc0b52fe2015-09-09 16:38:15 -07002088 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002089 * Unbinds the view for the specified item, and removes the item and all its children.
2090 *
2091 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002092 * @param itemInfo the {@link ItemInfo} for this view.
2093 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002094 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002095 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002096 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002097 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002098 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2099 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002100 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002101 } else {
2102 mWorkspace.removeWorkspaceItem(v);
2103 }
Winsonc0b52fe2015-09-09 16:38:15 -07002104 if (deleteFromDb) {
2105 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2106 }
2107 } else if (itemInfo instanceof FolderInfo) {
2108 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002109 if (v instanceof FolderIcon) {
2110 ((FolderIcon) v).removeListeners();
2111 }
Winsonc0b52fe2015-09-09 16:38:15 -07002112 mWorkspace.removeWorkspaceItem(v);
2113 if (deleteFromDb) {
2114 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2115 }
2116 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2117 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002118 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002119 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002120 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002121 }
2122 } else {
2123 return false;
2124 }
2125 return true;
2126 }
2127
2128 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002129 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002130 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002131 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002132 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002133 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002134 // Deleting an app widget ID is a void call but writes to disk before returning
2135 // to the caller...
2136 new AsyncTask<Void, Void, Void>() {
2137 public Void doInBackground(Void ... args) {
2138 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2139 return null;
2140 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002141 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002142 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002143 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002144 }
2145
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002146 @Override
2147 public boolean dispatchKeyEvent(KeyEvent event) {
2148 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2149 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002150 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002151 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002152 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002153 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002154 dumpState();
2155 return true;
2156 }
2157 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002158 }
2159 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2160 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002161 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002162 return true;
2163 }
2164 }
2165
2166 return super.dispatchKeyEvent(event);
2167 }
2168
Joe Onorato88ec0992009-11-19 13:16:06 -08002169 @Override
2170 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002171 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2172 return;
2173 }
2174
Sunny Goyal45478022015-06-08 16:52:41 -07002175 if (mDragController.isDragging()) {
2176 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002177 return;
2178 }
2179
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002180 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2181 if (topView != null) {
2182 if (topView.getActiveTextView() != null) {
2183 topView.getActiveTextView().dispatchBackKey();
2184 } else {
2185 topView.close(true);
2186 }
Tony Wickham49c8d292016-07-01 11:44:34 -07002187 } else if (isAppsViewVisible()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002188 showWorkspace(true);
2189 } else if (isWidgetsViewVisible()) {
2190 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002191 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002192 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002193 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002194 mWorkspace.exitWidgetResizeMode();
2195
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002196 // Back button is a no-op here, but give at least some feedback for the button press
2197 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002198 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002199 }
2200
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002201 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002202 * Launches the intent referred by the clicked shortcut.
2203 *
2204 * @param v The view representing the clicked shortcut.
2205 */
2206 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002207 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2208 // view has detached (it's possible for this to happen if the view is removed mid touch).
2209 if (v.getWindowToken() == null) {
2210 return;
2211 }
2212
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002213 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002214 return;
2215 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002216
Adam Cohen1697b792013-09-17 19:08:21 -07002217 if (v instanceof Workspace) {
2218 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002219 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002220 }
2221 return;
2222 }
2223
2224 if (v instanceof CellLayout) {
2225 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002226 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2227 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002228 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002229 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002230 }
2231
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002232 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002233 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002234 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002235 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002236 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002237 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002238 }
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002239 } else if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2240 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002241 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002242 } else if (tag instanceof AppInfo) {
2243 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002244 } else if (tag instanceof LauncherAppWidgetInfo) {
2245 if (v instanceof PendingAppWidgetHostView) {
2246 onClickPendingWidget((PendingAppWidgetHostView) v);
2247 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248 }
2249 }
2250
Sunny Goyal70660032015-05-14 00:07:08 -07002251 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002252 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002253 return false;
2254 }
2255
Michael Jurkaaf442092010-06-10 17:01:57 -07002256 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002257 * Event handler for the app widget view which has not fully restored.
2258 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002259 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002260 if (mIsSafeModeEnabled) {
2261 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2262 return;
2263 }
2264
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002265 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002266 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002267 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2268 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2269 // This should not happen, as we make sure that an Id is allocated during bind.
2270 return;
2271 }
2272 LauncherAppWidgetProviderInfo appWidgetInfo =
2273 mAppWidgetManager.findProvider(info.providerName, info.user);
2274 if (appWidgetInfo != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002275 setWaitingForResult(PendingRequestArgs
2276 .forWidgetInfo(info.appWidgetId, appWidgetInfo, info));
Sunny Goyalff572272014-07-23 13:58:07 -07002277
Sunny Goyald478c832016-04-01 12:04:16 -07002278 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
Sunny Goyal2100c782016-08-22 16:00:03 -07002279 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, info.appWidgetId);
Sunny Goyald478c832016-04-01 12:04:16 -07002280 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07002281 mAppWidgetManager.getUser(appWidgetInfo)
Sunny Goyald478c832016-04-01 12:04:16 -07002282 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2283 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2284 }
2285 } else {
2286 LauncherAppWidgetProviderInfo appWidgetInfo =
2287 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2288 if (appWidgetInfo != null) {
2289 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2290 }
Sunny Goyalff572272014-07-23 13:58:07 -07002291 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002292 } else if (info.installProgress < 0) {
2293 // The install has not been queued
2294 final String packageName = info.providerName.getPackageName();
2295 showBrokenAppInstallDialog(packageName,
2296 new DialogInterface.OnClickListener() {
2297 public void onClick(DialogInterface dialog, int id) {
2298 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2299 }
2300 });
2301 } else {
2302 // Download has started.
2303 final String packageName = info.providerName.getPackageName();
2304 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002305 }
2306 }
2307
Sunny Goyald478c832016-04-01 12:04:16 -07002308 private void startRestoredWidgetReconfigActivity(
2309 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002310 setWaitingForResult(PendingRequestArgs.forWidgetInfo(info.appWidgetId, provider, info));
Sunny Goyald478c832016-04-01 12:04:16 -07002311 mAppWidgetManager.startConfigActivity(provider,
2312 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2313 }
2314
Sunny Goyalff572272014-07-23 13:58:07 -07002315 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002316 * Event handler for the "grid" button that appears on the home screen, which
2317 * enters all apps mode.
2318 *
2319 * @param v The view that was clicked.
2320 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002321 protected void onClickAllAppsButton(View v) {
2322 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002323 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002324 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.TAP,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002325 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002326 showAppsView(true /* animated */, true /* updatePredictedApps */,
2327 false /* focusSearchBar */);
Winson Chung76648c52015-07-10 14:33:23 -07002328 }
2329 }
2330
2331 protected void onLongClickAllAppsButton(View v) {
2332 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2333 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002334 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.LONGPRESS,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002335 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002336 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07002337 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002338 }
2339 }
2340
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002341 private void showBrokenAppInstallDialog(final String packageName,
2342 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002343 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002344 .setTitle(R.string.abandoned_promises_title)
2345 .setMessage(R.string.abandoned_promise_explanation)
2346 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2347 .setNeutralButton(R.string.abandoned_clean_this,
2348 new DialogInterface.OnClickListener() {
2349 public void onClick(DialogInterface dialog, int id) {
2350 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2351 mWorkspace.removeAbandonedPromise(packageName, user);
2352 }
2353 })
2354 .create().show();
2355 return;
2356 }
2357
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002358 /**
2359 * Event handler for an app shortcut click.
2360 *
2361 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2362 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002363 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002364 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2365 Object tag = v.getTag();
2366 if (!(tag instanceof ShortcutInfo)) {
2367 throw new IllegalArgumentException("Input must be a Shortcut");
2368 }
2369
2370 // Open shortcut
2371 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002372
2373 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002374 if ((shortcut.isDisabled &
2375 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2376 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2377 // If the app is only disabled because of the above flags, launch activity anyway.
2378 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002379 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002380 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2381 // Use a message specific to this shortcut, if it has one.
2382 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2383 return;
2384 }
2385 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002386 int error = R.string.activity_not_available;
2387 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2388 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002389 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2390 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002391 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002392 }
2393 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2394 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002395 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002396 }
2397
Chris Wren40c5ed32014-06-24 18:24:23 -04002398 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002399 if ((v instanceof BubbleTextView)
2400 && shortcut.isPromise()
2401 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002402 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002403 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002404 new DialogInterface.OnClickListener() {
2405 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002406 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002407 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002408 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002409 return;
2410 }
2411
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002412 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002413 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002414 }
2415
Sunny Goyala7ce1662016-05-31 15:01:35 -07002416 private void startAppShortcutOrInfoActivity(View v) {
2417 ItemInfo item = (ItemInfo) v.getTag();
2418 Intent intent = item.getIntent();
2419 if (intent == null) {
2420 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002421 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002422 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002423 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002424
2425 if (success && v instanceof BubbleTextView) {
2426 mWaitingForResume = (BubbleTextView) v;
2427 mWaitingForResume.setStayPressed(true);
2428 }
2429 }
2430
2431 /**
2432 * Event handler for a folder icon click.
2433 *
2434 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2435 */
2436 protected void onClickFolderIcon(View v) {
2437 if (LOGD) Log.d(TAG, "onClickFolder");
2438 if (!(v instanceof FolderIcon)){
2439 throw new IllegalArgumentException("Input must be a FolderIcon");
2440 }
2441
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002442 Folder folder = ((FolderIcon) v).getFolder();
2443 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002444 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002445 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002446 }
Michael Jurka5130e402011-10-13 04:55:35 -07002447 }
2448
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002449 /**
2450 * Event handler for the (Add) Widgets button that appears after a long press
2451 * on the home screen.
2452 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002453 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002454 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002455 if (mIsSafeModeEnabled) {
2456 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2457 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002458 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002459 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002460 }
2461
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002462 /**
2463 * Event handler for the wallpaper picker button that appears after a long press
2464 * on the home screen.
2465 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002466 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002467 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002468 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2469 return;
2470 }
2471
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002472 String pickerPackage = getString(R.string.wallpaper_picker_package);
2473 if (TextUtils.isEmpty(pickerPackage)) {
2474 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2475 }
2476
Tony Wickham785f7a52015-08-31 17:28:32 -07002477 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2478 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002479
Sunny Goyal2100c782016-08-22 16:00:03 -07002480 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002481 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002482 .setPackage(pickerPackage)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002483 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
2484 intent.setSourceBounds(getViewBounds(v));
2485 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002486 }
2487
2488 /**
2489 * Event handler for a click on the settings button that appears after a long press
2490 * on the home screen.
2491 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002492 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002493 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002494 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2495 .setPackage(getPackageName());
2496 intent.setSourceBounds(getViewBounds(v));
2497 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002498 }
2499
Adam Cohen61f560d2013-09-30 15:58:20 -07002500 public View.OnTouchListener getHapticFeedbackTouchListener() {
2501 if (mHapticFeedbackTouchListener == null) {
2502 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002503 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002504 @Override
2505 public boolean onTouch(View v, MotionEvent event) {
2506 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2507 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2508 }
2509 return false;
2510 }
2511 };
2512 }
2513 return mHapticFeedbackTouchListener;
2514 }
2515
Tony Wickhame2217252016-03-22 16:34:23 -07002516 @Override
2517 public void onAccessibilityStateChanged(boolean enabled) {
2518 mDragLayer.onAccessibilityStateChanged(enabled);
2519 }
2520
Sunny Goyal06e21a22016-08-11 16:02:02 -07002521 public void onDragStarted() {
Adam Cohen9211d422014-10-07 18:14:53 -07002522 if (isOnCustomContent()) {
2523 // Custom content screen doesn't participate in drag and drop. If on custom
2524 // content screen, move to default.
2525 moveWorkspaceToDefaultScreen();
2526 }
Adam Cohen9211d422014-10-07 18:14:53 -07002527 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002528
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002529 /**
2530 * Called when the user stops interacting with the launcher.
2531 * This implies that the user is now on the homescreen and is not doing housekeeping.
2532 */
Adam Cohen9211d422014-10-07 18:14:53 -07002533 protected void onInteractionEnd() {
2534 if (mLauncherCallbacks != null) {
2535 mLauncherCallbacks.onInteractionEnd();
2536 }
2537 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002538
2539 /**
2540 * Called when the user starts interacting with the launcher.
2541 * The possible interactions are:
2542 * - open all apps
2543 * - reorder an app shortcut, or a widget
2544 * - open the overview mode.
2545 * This is a good time to stop doing things that only make sense
2546 * when the user is on the homescreen and not doing housekeeping.
2547 */
Adam Cohen9211d422014-10-07 18:14:53 -07002548 protected void onInteractionBegin() {
2549 if (mLauncherCallbacks != null) {
2550 mLauncherCallbacks.onInteractionBegin();
2551 }
2552 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002553
Winson Chungcd99cd32015-04-29 11:03:24 -07002554 /** Updates the interaction state. */
2555 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002556 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002557 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002558 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2559 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002560 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002561 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002562 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002563 onInteractionEnd();
2564 }
2565 }
2566
Sunny Goyala7ce1662016-05-31 15:01:35 -07002567 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002568 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002569 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2570 try {
2571 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2572 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2573 // is enabled by default on NYC.
2574 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2575 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002576
2577 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2578 String id = ((ShortcutInfo) info).getDeepShortcutId();
2579 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002580 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2581 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002582 } else {
2583 // Could be launching some bookkeeping activity
2584 startActivity(intent, optsBundle);
2585 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002586 } finally {
2587 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002588 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002589 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002590 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2591 // corresponding permission. Show the appropriate permission prompt if that
2592 // is the case.
2593 if (intent.getComponent() == null
2594 && Intent.ACTION_CALL.equals(intent.getAction())
2595 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2596 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002597
2598 setWaitingForResult(PendingRequestArgs
2599 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002600 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2601 REQUEST_PERMISSION_CALL_PHONE);
2602 } else {
2603 // No idea why this was thrown.
2604 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002605 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002606 }
2607 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002608
Sunny Goyalfe770c92016-09-27 14:38:58 -07002609 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyala7ce1662016-05-31 15:01:35 -07002610 private Bundle getActivityLaunchOptions(View v) {
2611 if (Utilities.ATLEAST_MARSHMALLOW) {
2612 int left = 0, top = 0;
2613 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2614 if (v instanceof TextView) {
2615 // Launch from center of icon, not entire view
2616 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2617 if (icon != null) {
2618 Rect bounds = icon.getBounds();
2619 left = (width - bounds.width()) / 2;
2620 top = v.getPaddingTop();
2621 width = bounds.width();
2622 height = bounds.height();
2623 }
2624 }
2625 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2626 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2627 // On L devices, we use the device default slide-up transition.
2628 // On L MR1 devices, we use a custom version of the slide-up transition which
2629 // doesn't have the delay present in the device default.
2630 return ActivityOptions.makeCustomAnimation(
2631 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2632 }
2633 return null;
2634 }
2635
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002636 private Rect getViewBounds(View v) {
2637 int[] pos = new int[2];
2638 v.getLocationOnScreen(pos);
2639 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2640 }
2641
Sunny Goyala7ce1662016-05-31 15:01:35 -07002642 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002643 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2644 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2645 return false;
2646 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002647 // Only launch using the new animation if the shortcut has not opted out (this is a
2648 // private contract between launcher and may be ignored in the future).
2649 boolean useLaunchAnimation = (v != null) &&
2650 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2651 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2652
2653 UserHandleCompat user = null;
2654 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2655 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2656 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002657 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002658
2659 // Prepare intent
2660 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2661 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002662 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002663 }
2664 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002665 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2666 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07002667 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
2668 && ((ShortcutInfo) item).promisedIntent == null) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002669 // Shortcuts need some special checks due to legacy reasons.
2670 startShortcutIntentSafely(intent, optsBundle, item);
2671 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2672 // Could be launching some bookkeeping activity
2673 startActivity(intent, optsBundle);
2674 } else {
2675 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2676 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2677 }
2678 return true;
2679 } catch (ActivityNotFoundException|SecurityException e) {
2680 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2681 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2682 }
2683 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002684 }
2685
Tony Wickhamdadb3042016-02-24 11:07:00 -08002686 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002687 public boolean dispatchTouchEvent(MotionEvent ev) {
2688 mLastDispatchTouchEventX = ev.getX();
2689 return super.dispatchTouchEvent(ev);
2690 }
2691
2692 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002693 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002694 if (!isDraggingEnabled()) return false;
2695 if (isWorkspaceLocked()) return false;
2696 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002697
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002698 if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2699 (v == mAllAppsButton && mAllAppsButton != null)) {
Winson Chung76648c52015-07-10 14:33:23 -07002700 onLongClickAllAppsButton(v);
2701 return true;
2702 }
2703
Jon Miranda379198e2016-09-23 08:54:40 -07002704 boolean fromEdgeOfScreen = mLastDispatchTouchEventX < mEdgeOfScreenThresholdPx
2705 || mLastDispatchTouchEventX > (mDeviceProfile.widthPx - mEdgeOfScreenThresholdPx);
2706
Adam Cohen1697b792013-09-17 19:08:21 -07002707 if (v instanceof Workspace) {
2708 if (!mWorkspace.isInOverviewMode()) {
Jon Miranda379198e2016-09-23 08:54:40 -07002709 if (!mWorkspace.isTouchActive() && !fromEdgeOfScreen) {
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002710 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.LONGPRESS,
2711 LauncherLogProto.Action.NONE, LauncherLogProto.WORKSPACE,
2712 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002713 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07002714 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2715 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2716 return true;
2717 } else {
2718 return false;
2719 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002720 } else {
2721 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002722 }
Adam Cohen1697b792013-09-17 19:08:21 -07002723 }
2724
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002725 CellLayout.CellInfo longClickCellInfo = null;
2726 View itemUnderLongClick = null;
2727 if (v.getTag() instanceof ItemInfo) {
2728 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002729 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002730 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002731 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002732 }
2733
Winson Chung3d503fb2011-07-13 17:25:49 -07002734 // The hotseat touch handling does not go through Workspace, and we always allow long press
2735 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002736 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002737 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002738 // User long pressed on empty space
Adam Cohend3ecce92013-09-16 14:58:00 -07002739 if (mWorkspace.isInOverviewMode()) {
2740 mWorkspace.startReordering(v);
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002741 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.LONGPRESS,
2742 LauncherLogProto.Action.NONE, LauncherLogProto.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002743 } else {
Jon Miranda379198e2016-09-23 08:54:40 -07002744 if (fromEdgeOfScreen) {
2745 return false;
2746 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002747 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.LONGPRESS,
2748 LauncherLogProto.Action.NONE, LauncherLogProto.WORKSPACE,
2749 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002750 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07002751 }
Jon Miranda379198e2016-09-23 08:54:40 -07002752 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2753 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002754 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002755 final boolean isAllAppsButton =
2756 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2757 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2758 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002759 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002760 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002761 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002762 }
2763 }
2764 }
2765 return true;
2766 }
2767
Winson Chung3d503fb2011-07-13 17:25:49 -07002768 boolean isHotseatLayout(View layout) {
2769 return mHotseat != null && layout != null &&
2770 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2771 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002772
Winson Chung3d503fb2011-07-13 17:25:49 -07002773 /**
2774 * Returns the CellLayout of the specified container at the specified screen.
2775 */
Sunny Goyal92820592015-03-02 11:31:35 -08002776 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002777 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2778 if (mHotseat != null) {
2779 return mHotseat.getLayout();
2780 } else {
2781 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002782 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002783 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002784 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002785 }
2786 }
2787
Winson Chungb745afb2015-03-02 11:51:23 -08002788 /**
2789 * For overridden classes.
2790 */
Daniel Sandler843e8602010-06-07 14:59:01 -04002791 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08002792 return isAppsViewVisible();
2793 }
2794
2795 public boolean isAppsViewVisible() {
2796 return (mState == State.APPS) || (mOnResumeState == State.APPS);
2797 }
2798
2799 public boolean isWidgetsViewVisible() {
2800 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002801 }
2802
Michael Jurkae326f182011-11-21 14:05:46 -08002803 @Override
2804 public void onTrimMemory(int level) {
2805 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002806 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2807 // The widget preview db can result in holding onto over
2808 // 3MB of memory for caching which isn't necessary.
2809 SQLiteDatabase.releaseMemory();
2810
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002811 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002812 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002813 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002814 if (mLauncherCallbacks != null) {
2815 mLauncherCallbacks.onTrimMemory(level);
2816 }
Michael Jurkae326f182011-11-21 14:05:46 -08002817 }
2818
Winson5c6bdbb2015-09-03 11:36:19 -07002819 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07002820 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07002821 }
2822
Winson5c6bdbb2015-09-03 11:36:19 -07002823 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07002824 boolean changed = mState != State.WORKSPACE ||
2825 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002826 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002827 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002828 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002829 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07002830
Michael Jurkab3e22d92011-10-31 15:58:33 -07002831 // Set focus to the AppsCustomize button
2832 if (mAllAppsButton != null) {
2833 mAllAppsButton.requestFocus();
2834 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002835 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002836
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002837 // Change the state *after* we've called all the transition code
2838 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002839
Winson Chung0f785722015-04-08 10:27:49 -07002840 if (changed) {
2841 // Send an accessibility event to announce the context change
2842 getWindow().getDecorView()
2843 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07002844 }
Winson5c6bdbb2015-09-03 11:36:19 -07002845 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01002846 }
2847
Winsone9f27272015-10-13 10:47:51 -07002848 /**
2849 * Shows the overview button.
2850 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002851 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07002852 showOverviewMode(animated, false);
2853 }
2854
2855 /**
2856 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
2857 * onto one of the overview panel buttons.
2858 */
2859 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
2860 Runnable postAnimRunnable = null;
2861 if (requestButtonFocus) {
2862 postAnimRunnable = new Runnable() {
2863 @Override
2864 public void run() {
2865 // Hitting the menu button when in touch mode does not trigger touch mode to
2866 // be disabled, so if requested, force focus on one of the overview panel
2867 // buttons.
2868 mOverviewPanel.requestFocusFromTouch();
2869 }
2870 };
2871 }
Adam Cohened307df2013-10-02 09:37:31 -07002872 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002873 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07002874 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07002875 mState = State.WORKSPACE;
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07002876 // If animated from long press, then don't allow any of the controller in the drag
2877 // layer to intercept any remaining touch.
2878 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04002879 }
2880
Winson Chungb745afb2015-03-02 11:51:23 -08002881 /**
2882 * Shows the apps view.
2883 */
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002884 public void showAppsView(boolean animated, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07002885 boolean focusSearchBar) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07002886 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07002887 if (updatePredictedApps) {
2888 tryAndUpdatePredictedApps();
2889 }
Winson Chung76648c52015-07-10 14:33:23 -07002890 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08002891 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002892
Winson Chungb745afb2015-03-02 11:51:23 -08002893 /**
2894 * Shows the widgets view.
2895 */
2896 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002897 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07002898 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002899 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07002900 }
Winson Chung76648c52015-07-10 14:33:23 -07002901 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07002902
2903 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04002904 @Override
2905 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002906 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04002907 }
2908 });
Winson Chungb745afb2015-03-02 11:51:23 -08002909 }
2910
2911 /**
2912 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07002913 *
2914 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08002915 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07002916 // TODO: calling method should use the return value so that when {@code false} is returned
2917 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07002918 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002919 if (!(mState == State.WORKSPACE ||
2920 mState == State.APPS_SPRING_LOADED ||
2921 mState == State.WIDGETS_SPRING_LOADED ||
2922 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07002923 return false;
2924 }
2925 if (toState != State.APPS && toState != State.WIDGETS) {
2926 return false;
2927 }
Winson Chungb745afb2015-03-02 11:51:23 -08002928
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002929 // This is a safe and supported transition to bypass spring_loaded mode.
2930 if (mExitSpringLoadedModeRunnable != null) {
2931 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2932 mExitSpringLoadedModeRunnable = null;
2933 }
2934
Winson Chungb745afb2015-03-02 11:51:23 -08002935 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07002936 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
2937 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08002938 } else {
Winson Chung006ee262015-08-03 14:40:11 -07002939 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002940 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002941
Michael Jurkab3e22d92011-10-31 15:58:33 -07002942 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08002943 mState = toState;
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002944 AbstractFloatingView.closeAllOpenViews(this);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002945
2946 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002947 getWindow().getDecorView()
2948 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07002949 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07002950 }
2951
Winson Chungcd99cd32015-04-29 11:03:24 -07002952 /**
2953 * Updates the workspace and interaction state on state change, and return the animation to this
2954 * new state.
2955 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002956 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07002957 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002958 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07002959 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07002960 updateInteraction(fromState, toState);
2961 return anim;
2962 }
2963
Hyunyoung Song3f471442015-04-08 19:01:34 -07002964 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002965 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07002966 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002967 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07002968 }
Winson Chungb745afb2015-03-02 11:51:23 -08002969
Winson Chung006ee262015-08-03 14:40:11 -07002970 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002971 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07002972 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07002973
2974 if (isAppsViewVisible()) {
2975 mState = State.APPS_SPRING_LOADED;
2976 } else if (isWidgetsViewVisible()) {
2977 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08002978 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07002979 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08002980 } else {
2981 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07002982 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002983 }
Adam Cohen7777d962011-08-18 18:58:38 -07002984
Hyunyoung Song3f471442015-04-08 19:01:34 -07002985 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08002986 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07002987 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07002988
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002989 if (mExitSpringLoadedModeRunnable != null) {
2990 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2991 }
2992 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002993 @Override
2994 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002995 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002996 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
2997 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002998 // Before we show workspace, hide all apps again because
2999 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3000 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003001 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003002 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003003 } else {
3004 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003005 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003006 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07003007 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003008 };
3009 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003010 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003011
Tony Wickhame0c33232016-02-08 11:37:04 -08003012 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003013 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3014 || mState == State.WIDGETS_SPRING_LOADED;
3015 }
3016
Michael Jurkad3ef3062010-11-23 16:23:58 -08003017 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003018 if (mState == State.APPS_SPRING_LOADED) {
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003019 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07003020 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003021 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003022 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003023 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3024 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003025 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003026 }
3027
Winson Chung4ac30062015-05-08 17:34:17 -07003028 /**
3029 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3030 * resumed.
3031 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003032 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003033 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003034 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003035 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003036 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003037 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003038 }
3039 }
3040 }
3041
Michael Jurkab3e22d92011-10-31 15:58:33 -07003042 void lockAllApps() {
3043 // TODO
3044 }
3045
3046 void unlockAllApps() {
3047 // TODO
3048 }
3049
Michael Jurkad7c28052012-04-27 15:43:36 -07003050 @Override
3051 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003052 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003053 final List<CharSequence> text = event.getText();
3054 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003055 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003056 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003057 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003058 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003059 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003060 } else if (mWorkspace != null) {
3061 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003062 } else {
3063 text.add(getString(R.string.all_apps_home_button_label));
3064 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003065 return result;
3066 }
3067
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003068 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003069 * If the activity is currently paused, signal that we need to run the passed Runnable
3070 * in onResume.
3071 *
3072 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003073 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3074 * wrong when we're not running, and if the activity comes back to what the configuration was
3075 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003076 *
3077 * Implementation of the method from LauncherModel.Callbacks.
3078 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003079 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003080 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003081 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003082 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003083 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003084 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003085 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003086 }
3087 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003088 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003089 return true;
3090 } else {
3091 return false;
3092 }
3093 }
3094
Michael Jurkac402cd92013-05-20 15:49:32 +02003095 private boolean waitUntilResume(Runnable run) {
3096 return waitUntilResume(run, false);
3097 }
3098
Michael Jurka1e2f4652013-07-08 18:03:46 -07003099 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003100 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003101 }
3102
Michael Jurka7607c2f2013-04-03 14:33:19 -07003103 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003104 * If the activity is currently paused, signal that we need to re-run the loader
3105 * in onResume.
3106 *
3107 * This needs to be called from incoming places where resources might have been loaded
3108 * while we are paused. That is becaues the Configuration might be wrong
3109 * when we're not running, and if it comes back to what it was when we
3110 * were paused, we are not restarted.
3111 *
3112 * Implementation of the method from LauncherModel.Callbacks.
3113 *
3114 * @return true if we are currently paused. The caller might be able to
3115 * skip some work in that case since we will come back again.
3116 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003117 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003118 public boolean setLoadOnResume() {
3119 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003120 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003121 mOnResumeNeedsLoad = true;
3122 return true;
3123 } else {
3124 return false;
3125 }
3126 }
3127
3128 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003129 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003130 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003131 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003132 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003133 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003134 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003135 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003136 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003137 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003138 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003139
Joe Onorato9c1289c2009-08-17 11:03:03 -04003140 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003141 * Clear any pending bind callbacks. This is called when is loader is planning to
3142 * perform a full rebind from scratch.
3143 */
3144 @Override
3145 public void clearPendingBinds() {
3146 mBindOnResumeCallbacks.clear();
3147 if (mPendingExecutor != null) {
3148 mPendingExecutor.markCompleted();
3149 mPendingExecutor = null;
3150 }
3151 }
3152
3153 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003154 * Refreshes the shortcuts shown on the workspace.
3155 *
3156 * Implementation of the method from LauncherModel.Callbacks.
3157 */
3158 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003159 if (LauncherAppState.PROFILE_STARTUP) {
3160 Trace.beginSection("Starting page bind");
3161 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003162 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003163
Winson Chungd64d1762013-08-20 14:37:16 -07003164 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003165 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003166 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003167
Winson Chung3d503fb2011-07-13 17:25:49 -07003168 if (mHotseat != null) {
3169 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003170 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003171 if (LauncherAppState.PROFILE_STARTUP) {
3172 Trace.endSection();
3173 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003174 }
3175
Adam Cohendcd297f2013-06-18 13:13:40 -07003176 @Override
3177 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003178 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003179 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3180 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003181 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3182 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3183 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003184 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3185 // If there are no screens, we need to have an empty screen
3186 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003187 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003188 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003189
3190 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003191 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003192 if (hasCustomContentToLeft()) {
3193 mWorkspace.createCustomContentContainer();
3194 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003195 }
Winsonc7d2e832016-07-28 12:24:55 -07003196
3197 // After we have added all the screens, if the wallpaper was locked to the default state,
3198 // then notify to indicate that it can be released and a proper wallpaper offset can be
3199 // computed before the next layout
3200 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07003201 }
3202
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003203 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003204 int count = orderedScreenIds.size();
3205 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003206 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003207 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003208 // No need to bind the first screen, as its always bound.
3209 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3210 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003211 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003212 }
3213
Winson Chungd64d1762013-08-20 14:37:16 -07003214 public void bindAppsAdded(final ArrayList<Long> newScreens,
3215 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003216 final ArrayList<ItemInfo> addAnimated,
3217 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003218 Runnable r = new Runnable() {
3219 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003220 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003221 }
3222 };
3223 if (waitUntilResume(r)) {
3224 return;
3225 }
3226
Winson Chungd64d1762013-08-20 14:37:16 -07003227 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003228 if (newScreens != null) {
3229 bindAddScreens(newScreens);
3230 }
Winson Chungd64d1762013-08-20 14:37:16 -07003231
3232 // We add the items without animation on non-visible pages, and with
3233 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003234 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003235 bindItems(addNotAnimated, 0,
3236 addNotAnimated.size(), false);
3237 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003238 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003239 bindItems(addAnimated, 0,
3240 addAnimated.size(), true);
3241 }
Winson Chungc58497e2013-09-03 17:48:37 -07003242
Winson Chung87412982013-10-03 18:34:14 -07003243 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003244 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003245
Winson Chungb745afb2015-03-02 11:51:23 -08003246 if (addedApps != null && mAppsView != null) {
3247 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003248 }
Winson Chungd64d1762013-08-20 14:37:16 -07003249 }
3250
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003251 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003252 * Bind the items start-end from the list.
3253 *
3254 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003255 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003256 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003257 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3258 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003259 Runnable r = new Runnable() {
3260 public void run() {
3261 bindItems(shortcuts, start, end, forceAnimateIcons);
3262 }
3263 };
3264 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003265 return;
3266 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003267
Winson Chungf0c6ae02012-03-21 16:10:31 -07003268 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003269 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3270 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003271 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003272 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003273 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003274 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003275 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003276
3277 // Short circuit if we are loading dock items for a configuration which has no dock
3278 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3279 mHotseat == null) {
3280 continue;
3281 }
3282
Sunny Goyal639e9062015-08-19 19:17:06 -07003283 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003284 switch (item.itemType) {
3285 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3286 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003287 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003288 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003289 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003290 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003291 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003292 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003293 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003294 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003295 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003296 default:
3297 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003298 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003299
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003300 /*
3301 * Remove colliding items.
3302 */
3303 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3304 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3305 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3306 View v = cl.getChildAt(item.cellX, item.cellY);
3307 Object tag = v.getTag();
3308 String desc = "Collision while binding workspace item: " + item
3309 + ". Collides with " + tag;
3310 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3311 throw (new RuntimeException(desc));
3312 } else {
3313 Log.d(TAG, desc);
3314 LauncherModel.deleteItemFromDatabase(this, item);
3315 continue;
3316 }
3317 }
3318 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003319 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3320 item.cellY, 1, 1);
3321 if (animateIcons) {
3322 // Animate all the applications up now
3323 view.setAlpha(0f);
3324 view.setScaleX(0f);
3325 view.setScaleY(0f);
3326 bounceAnims.add(createNewAppBounceAnimation(view, i));
3327 newShortcutsScreenId = item.screenId;
3328 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003329 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003330
Winson Chung997a9232013-07-24 15:33:46 -07003331 if (animateIcons) {
3332 // Animate to the correct page
3333 if (newShortcutsScreenId > -1) {
3334 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003335 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003336 final Runnable startBounceAnimRunnable = new Runnable() {
3337 public void run() {
3338 anim.playTogether(bounceAnims);
3339 anim.start();
3340 }
3341 };
Winson Chung997a9232013-07-24 15:33:46 -07003342 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003343 // We post the animation slightly delayed to prevent slowdowns
3344 // when we are loading right after we return to launcher.
3345 mWorkspace.postDelayed(new Runnable() {
3346 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003347 if (mWorkspace != null) {
3348 mWorkspace.snapToPage(newScreenIndex);
3349 mWorkspace.postDelayed(startBounceAnimRunnable,
3350 NEW_APPS_ANIMATION_DELAY);
3351 }
Winson Chung94d67682013-09-25 16:29:40 -07003352 }
3353 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003354 } else {
3355 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003356 }
3357 }
Winson Chung64359a52013-07-08 17:17:08 -07003358 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003359 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003360 }
3361
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003362 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3363 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3364 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003365 view.updateAppWidget(null);
3366 view.setOnClickListener(this);
3367 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003368 mWorkspace.requestLayout();
3369 }
3370
Joe Onorato9c1289c2009-08-17 11:03:03 -04003371 /**
3372 * Add the views for a widget to the workspace.
3373 *
3374 * Implementation of the method from LauncherModel.Callbacks.
3375 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003376 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003377 Runnable r = new Runnable() {
3378 public void run() {
3379 bindAppWidget(item);
3380 }
3381 };
3382 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003383 return;
3384 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003385
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003386 if (mIsSafeModeEnabled) {
3387 bindSafeModeWidget(item);
3388 return;
3389 }
3390
Daniel Sandler843e8602010-06-07 14:59:01 -04003391 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3392 if (DEBUG_WIDGETS) {
3393 Log.d(TAG, "bindAppWidget: " + item);
3394 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003395
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003396 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003397
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003398 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3399 // If the provider is not ready, bind as a pending widget.
3400 appWidgetInfo = null;
3401 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3402 // The widget id is not valid. Try to find the widget based on the provider info.
3403 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3404 } else {
3405 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3406 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003407
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003408 // If the provider is ready, but the width is not yet restored, try to restore it.
3409 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3410 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003411 if (appWidgetInfo == null) {
3412 if (DEBUG_WIDGETS) {
3413 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3414 + " belongs to component " + item.providerName
Hyunyoung Song0de01172016-10-05 16:27:48 -07003415 + ", as the provider is null");
Sunny Goyalff572272014-07-23 13:58:07 -07003416 }
3417 LauncherModel.deleteItemFromDatabase(this, item);
3418 return;
3419 }
Sunny Goyalff572272014-07-23 13:58:07 -07003420
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003421 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003422 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003423 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3424 // Id has not been allocated yet. Allocate a new id.
3425 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3426 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003427
Sunny Goyald478c832016-04-01 12:04:16 -07003428 // Also try to bind the widget. If the bind fails, the user will be shown
3429 // a click to setup UI, which will ask for the bind permission.
3430 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3431 pendingInfo.spanX = item.spanX;
3432 pendingInfo.spanY = item.spanY;
3433 pendingInfo.minSpanX = item.minSpanX;
3434 pendingInfo.minSpanY = item.minSpanY;
3435 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003436
3437 boolean isDirectConfig =
3438 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3439 if (isDirectConfig && item.bindOptions != null) {
3440 Bundle newOptions = item.bindOptions.getExtras();
3441 if (options != null) {
3442 newOptions.putAll(options);
3443 }
3444 options = newOptions;
3445 }
Sunny Goyald478c832016-04-01 12:04:16 -07003446 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3447 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003448
Sunny Goyal86df1382016-08-10 15:03:22 -07003449 // We tried to bind once. If we were not able to bind, we would need to
3450 // go through the permission dialog, which means we cannot skip the config
3451 // activity.
3452 item.bindOptions = null;
3453 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3454
Sunny Goyald478c832016-04-01 12:04:16 -07003455 // Bind succeeded
3456 if (success) {
3457 // If the widget has a configure activity, it is still needs to set it up,
3458 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003459 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003460 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3461 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003462 }
Sunny Goyald478c832016-04-01 12:04:16 -07003463
3464 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003465 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003466 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003467 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003468 // The widget was marked as UI not ready, but there is no configure activity to
3469 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003470 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3471 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003472 }
Sunny Goyalff572272014-07-23 13:58:07 -07003473 }
3474
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003475 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003476 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003477 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3478 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003479 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003480
Sunny Goyal56c73602015-09-25 12:55:01 -07003481 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003482 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003483 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003484 deleteWidgetInfo(item);
3485 return;
3486 }
3487
Sunny Goyal233ee962015-08-03 13:05:01 -07003488 item.minSpanX = appWidgetInfo.minSpanX;
3489 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003490 addAppWidgetToWorkspace(
3491 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3492 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003493 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003494 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003495 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003496 view.updateAppWidget(null);
3497 view.setOnClickListener(this);
3498 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003499 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003500 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003501
Daniel Sandler843e8602010-06-07 14:59:01 -04003502 if (DEBUG_WIDGETS) {
3503 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3504 + (SystemClock.uptimeMillis()-start) + "ms");
3505 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003506 }
3507
Sunny Goyalff572272014-07-23 13:58:07 -07003508 /**
3509 * Restores a pending widget.
3510 *
3511 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003512 */
Sunny Goyald478c832016-04-01 12:04:16 -07003513 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003514 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3515 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3516 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003517 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003518 }
3519
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003520 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003521 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003522
3523 mWorkspace.reinflateWidgetsIfNecessary();
3524 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003525 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003526 }
3527
Adam Cohen1462de32012-07-24 22:34:36 -07003528 public void onPageBoundSynchronously(int page) {
3529 mSynchronouslyBoundPages.add(page);
3530 }
3531
Sunny Goyal527c7d32015-08-28 15:19:36 -07003532 @Override
3533 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3534 if (mPendingExecutor != null) {
3535 mPendingExecutor.markCompleted();
3536 }
3537 mPendingExecutor = executor;
3538 executor.attachTo(this);
3539 }
3540
3541 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3542 if (mPendingExecutor == executor) {
3543 mPendingExecutor = null;
3544 }
3545 }
3546
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003547 @Override
3548 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3549 Runnable r = new Runnable() {
3550 public void run() {
3551 finishFirstPageBind(executor);
3552 }
3553 };
3554 if (waitUntilResume(r)) {
3555 return;
3556 }
3557
3558 Runnable onComplete = new Runnable() {
3559 @Override
3560 public void run() {
3561 if (executor != null) {
3562 executor.onLoadAnimationCompleted();
3563 }
3564 }
3565 };
3566 if (mDragLayer.getAlpha() < 1) {
3567 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3568 } else {
3569 onComplete.run();
3570 }
3571 }
3572
Joe Onorato9c1289c2009-08-17 11:03:03 -04003573 /**
3574 * Callback saying that there aren't any more items to bind.
3575 *
3576 * Implementation of the method from LauncherModel.Callbacks.
3577 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003578 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003579 Runnable r = new Runnable() {
3580 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003581 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003582 }
3583 };
3584 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003585 return;
3586 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003587 if (LauncherAppState.PROFILE_STARTUP) {
3588 Trace.beginSection("Page bind completed");
3589 }
Adam Cohen1462de32012-07-24 22:34:36 -07003590 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003591
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003592 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003593
Sunny Goyal2100c782016-08-22 16:00:03 -07003594 if (mPendingActivityResult != null) {
3595 handleActivityResult(mPendingActivityResult.requestCode,
3596 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3597 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003598 }
3599
Sunny Goyal756adbc2015-04-16 15:20:51 -07003600 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003601
3602 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003603 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003604 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003605 if (LauncherAppState.PROFILE_STARTUP) {
3606 Trace.endSection();
3607 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003608 }
3609
Winson Chunga2413752012-04-03 14:22:34 -07003610 private boolean canRunNewAppsAnimation() {
3611 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003612 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003613 }
3614
Winson Chungc9168342013-06-26 14:54:55 -07003615 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003616 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003617 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3618 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003619 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003620 return bounceAnim;
3621 }
3622
Adam Cohen27772732013-11-11 14:00:56 +00003623 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07003624 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00003625 }
3626
Tony Wickham55616cd2015-09-23 14:55:17 -07003627 public int getSearchBarHeight() {
3628 if (mLauncherCallbacks != null) {
3629 return mLauncherCallbacks.getSearchBarHeight();
3630 }
3631 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
3632 }
3633
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003634 /**
Winson Chungbb785c62015-05-05 10:05:08 -07003635 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
3636 * multiple calls to bind the same list.)
3637 */
3638 @Thunk ArrayList<AppInfo> mTmpAppsList;
3639 private Runnable mBindAllApplicationsRunnable = new Runnable() {
3640 public void run() {
3641 bindAllApplications(mTmpAppsList);
3642 mTmpAppsList = null;
3643 }
3644 };
3645
3646 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003647 * Add the icons for all apps.
3648 *
3649 * Implementation of the method from LauncherModel.Callbacks.
3650 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003651 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07003652 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
3653 mTmpAppsList = apps;
3654 return;
3655 }
3656
Winson Chungb745afb2015-03-02 11:51:23 -08003657 if (mAppsView != null) {
3658 mAppsView.setApps(apps);
3659 }
Adam Cohen9211d422014-10-07 18:14:53 -07003660 if (mLauncherCallbacks != null) {
3661 mLauncherCallbacks.bindAllApplications(apps);
3662 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003663 }
3664
3665 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003666 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
3667 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
3668 */
3669 @Override
3670 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
3671 mDeepShortcutMap = deepShortcutMapCopy;
3672 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
3673 }
3674
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003675 public List<String> getShortcutIdsForItem(ItemInfo info) {
3676 if (!DeepShortcutManager.supportsShortcuts(info)) {
3677 return Collections.EMPTY_LIST;
3678 }
3679 ComponentName component = info.getTargetComponent();
Hyunyoung Songd3bf9802016-08-29 14:43:53 -07003680 if (component == null) {
3681 return Collections.EMPTY_LIST;
3682 }
3683
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003684 List<String> ids = mDeepShortcutMap.get(new ComponentKey(component, info.user));
3685 return ids == null ? Collections.EMPTY_LIST : ids;
3686 }
3687
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003688 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003689 * A package was updated.
3690 *
3691 * Implementation of the method from LauncherModel.Callbacks.
3692 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003693 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003694 Runnable r = new Runnable() {
3695 public void run() {
3696 bindAppsUpdated(apps);
3697 }
3698 };
3699 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003700 return;
3701 }
3702
Winson Chungb745afb2015-03-02 11:51:23 -08003703 if (mAppsView != null) {
3704 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07003705 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003706 }
3707
Sunny Goyal4390ace2014-10-13 11:33:11 -07003708 @Override
3709 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
3710 Runnable r = new Runnable() {
3711 public void run() {
3712 bindWidgetsRestored(widgets);
3713 }
3714 };
3715 if (waitUntilResume(r)) {
3716 return;
3717 }
3718 mWorkspace.widgetsRestored(widgets);
3719 }
3720
Joe Onorato9c1289c2009-08-17 11:03:03 -04003721 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003722 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003723 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003724 *
3725 * @param updated list of shortcuts which have changed.
3726 * @param removed list of shortcuts which were deleted in the background. This can happen when
3727 * an app gets removed from the system or some of its components are no longer
3728 * available.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003729 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07003730 @Override
3731 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
3732 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003733 Runnable r = new Runnable() {
3734 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003735 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003736 }
3737 };
3738 if (waitUntilResume(r)) {
3739 return;
3740 }
3741
Sunny Goyal4390ace2014-10-13 11:33:11 -07003742 if (!updated.isEmpty()) {
3743 mWorkspace.updateShortcuts(updated);
3744 }
3745
3746 if (!removed.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003747 HashSet<ComponentName> removedComponents = new HashSet<>();
3748 HashSet<ShortcutKey> removedDeepShortcuts = new HashSet<>();
3749
Sunny Goyal4390ace2014-10-13 11:33:11 -07003750 for (ShortcutInfo si : removed) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003751 if (si.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07003752 removedDeepShortcuts.add(ShortcutKey.fromShortcutInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003753 } else {
3754 removedComponents.add(si.getTargetComponent());
3755 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003756 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003757
3758 if (!removedComponents.isEmpty()) {
3759 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(removedComponents, user);
3760 mWorkspace.removeItemsByMatcher(matcher);
3761 mDragController.onAppsRemoved(matcher);
3762 }
3763
3764 if (!removedDeepShortcuts.isEmpty()) {
3765 ItemInfoMatcher matcher = ItemInfoMatcher.ofShortcutKeys(removedDeepShortcuts);
3766 mWorkspace.removeItemsByMatcher(matcher);
3767 mDragController.onAppsRemoved(matcher);
3768 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003769 }
3770 }
3771
3772 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003773 * Update the state of a package, typically related to install state.
3774 *
3775 * Implementation of the method from LauncherModel.Callbacks.
3776 */
Sunny Goyale755d462014-07-22 13:48:29 -07003777 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003778 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3779 Runnable r = new Runnable() {
3780 public void run() {
3781 bindRestoreItemsChange(updates);
3782 }
3783 };
3784 if (waitUntilResume(r)) {
3785 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003786 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003787
Sunny Goyal756adbc2015-04-16 15:20:51 -07003788 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003789 }
3790
3791 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003792 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003793 * in addition to specific applications to remove, the reason being that
3794 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003795 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003796 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003797 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003798 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003799 public void bindWorkspaceComponentsRemoved(
3800 final HashSet<String> packageNames, final HashSet<ComponentName> components,
3801 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07003802 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003803 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003804 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07003805 }
Winson Chungd64d1762013-08-20 14:37:16 -07003806 };
3807 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003808 return;
3809 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003810 if (!packageNames.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003811 ItemInfoMatcher matcher = ItemInfoMatcher.ofPackages(packageNames, user);
3812 mWorkspace.removeItemsByMatcher(matcher);
3813 mDragController.onAppsRemoved(matcher);
3814
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003815 }
3816 if (!components.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003817 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(components, user);
3818 mWorkspace.removeItemsByMatcher(matcher);
3819 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003820 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003821 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003822
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003823 @Override
3824 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3825 Runnable r = new Runnable() {
3826 public void run() {
3827 bindAppInfosRemoved(appInfos);
3828 }
3829 };
3830 if (waitUntilResume(r)) {
3831 return;
Joe Onorato36115782010-06-17 13:28:48 -04003832 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003833
Winson Chungdf95eb12013-10-16 14:57:07 -07003834 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003835 if (mAppsView != null) {
3836 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003837 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003838 }
Joe Onoratobe386092009-11-17 17:32:16 -08003839
Sunny Goyald164b7f2016-10-12 20:49:31 -07003840 private Runnable mBindAllWidgetsRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003841 public void run() {
Sunny Goyald164b7f2016-10-12 20:49:31 -07003842 bindAllWidgets(mAllWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003843 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003844 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07003845
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003846 @Override
Sunny Goyald164b7f2016-10-12 20:49:31 -07003847 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> allWidgets) {
3848 if (waitUntilResume(mBindAllWidgetsRunnable, true)) {
3849 mAllWidgets = allWidgets;
Winson Chung83892cc2013-05-01 16:53:33 -07003850 return;
3851 }
3852
Sunny Goyald164b7f2016-10-12 20:49:31 -07003853 if (mWidgetsView != null && allWidgets != null) {
3854 mWidgetsView.setWidgets(allWidgets);
3855 mAllWidgets = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003856 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003857 }
3858
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003859 @Override
3860 public void notifyWidgetProvidersChanged() {
3861 if (mWorkspace.getState().shouldUpdateWidget) {
3862 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
3863 }
3864 }
3865
Winson Chung400438b2011-01-16 17:53:48 -08003866 private int mapConfigurationOriActivityInfoOri(int configOri) {
3867 final Display d = getWindowManager().getDefaultDisplay();
3868 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3869 switch (d.getRotation()) {
3870 case Surface.ROTATION_0:
3871 case Surface.ROTATION_180:
3872 // We are currently in the same basic orientation as the natural orientation
3873 naturalOri = configOri;
3874 break;
3875 case Surface.ROTATION_90:
3876 case Surface.ROTATION_270:
3877 // We are currently in the other basic orientation to the natural orientation
3878 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3879 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3880 break;
3881 }
3882
3883 int[] oriMap = {
3884 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3885 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3886 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3887 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3888 };
3889 // Since the map starts at portrait, we need to offset if this device's natural orientation
3890 // is landscape.
3891 int indexOffset = 0;
3892 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3893 indexOffset = 1;
3894 }
3895 return oriMap[(d.getRotation() + indexOffset) % 4];
3896 }
Adam Cohen446e9402011-09-15 18:21:21 -07003897
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003898 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07003899 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003900 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003901 if (Utilities.ATLEAST_JB_MR2) {
3902 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
3903 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08003904 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3905 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08003906 }
Winson Chung4b919f82012-05-01 10:44:08 -07003907 }
3908 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003909
Winson Chung4b919f82012-05-01 10:44:08 -07003910 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003911 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07003912 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003913 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003914 } else {
3915 mHandler.postDelayed(new Runnable() {
3916 public void run() {
3917 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3918 }
Sunny Goyal756cd262015-08-20 12:33:21 -07003919 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07003920 }
Winson Chung4b919f82012-05-01 10:44:08 -07003921 }
Winson Chung400438b2011-01-16 17:53:48 -08003922 }
3923
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003924 private void markAppsViewShown() {
3925 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3926 return;
3927 }
3928 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
3929 }
3930
3931 private boolean shouldShowDiscoveryBounce() {
3932 if (mState != mState.WORKSPACE) {
3933 return false;
3934 }
3935 if (mLauncherCallbacks != null && mLauncherCallbacks.shouldShowDiscoveryBounce()) {
3936 return true;
3937 }
3938 if (!mIsResumeFromActionScreenOff) {
3939 return false;
3940 }
3941 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3942 return false;
3943 }
3944 return true;
3945 }
3946
Winson Chung5ffd51d2015-06-25 11:36:50 -07003947 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003948 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00003949 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07003950 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003951 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003952 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003953 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
3954 if (userHandle != null) {
3955 user = UserHandleCompat.fromUser(userHandle);
3956 }
3957 }
3958 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07003959 }
3960
3961 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003962 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07003963 if (user == null) {
3964 user = UserHandleCompat.myUserHandle();
3965 }
3966
3967 // Called from search suggestion, add the profile extra to the intent to ensure that we
3968 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01003969 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003970 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01003971 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00003972 return null;
3973 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07003974 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00003975 }
3976
Winson Chung5ffd51d2015-06-25 11:36:50 -07003977 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00003978 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
3979 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003980 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01003981 UserHandleCompat.myUserHandle());
3982 }
3983
Winson Chung5ffd51d2015-06-25 11:36:50 -07003984 protected void moveWorkspaceToDefaultScreen() {
3985 mWorkspace.moveToDefaultScreen(false);
3986 }
3987
Winson Chung80baf5a2010-08-09 16:03:15 -07003988 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003989 * Returns a FastBitmapDrawable with the icon, accurately sized.
3990 */
3991 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
3992 FastBitmapDrawable d = new FastBitmapDrawable(icon);
3993 d.setFilterBitmap(true);
3994 resizeIconDrawable(d);
3995 return d;
3996 }
3997
3998 /**
3999 * Resizes an icon drawable to the correct icon size.
4000 */
Winson5fbe0742015-09-30 15:33:00 -07004001 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004002 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004003 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004004 }
4005
4006 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004007 * Prints out out state for debugging.
4008 */
4009 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004010 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004011 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
Sunny Goyal2100c782016-08-22 16:00:03 -07004012 Log.d(TAG, "mPendingRequestArgs=" + mPendingRequestArgs);
4013 Log.d(TAG, "mPendingActivityResult=" + mPendingActivityResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004014 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004015 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004016
Daniel Sandler325dc232013-06-05 22:57:57 -04004017 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004018 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004019
4020 @Override
4021 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4022 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004023 // Dump workspace
4024 writer.println(prefix + "Workspace Items");
4025 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4026 writer.println(prefix + " Homescreen " + i);
4027
4028 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4029 for (int j = 0; j < layout.getChildCount(); j++) {
4030 Object tag = layout.getChildAt(j).getTag();
4031 if (tag != null) {
4032 writer.println(prefix + " " + tag.toString());
4033 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004034 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004035 }
Sunny Goyala1365452015-10-01 15:46:24 -07004036
4037 writer.println(prefix + " Hotseat");
4038 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4039 for (int j = 0; j < layout.getChildCount(); j++) {
4040 Object tag = layout.getChildAt(j).getTag();
4041 if (tag != null) {
4042 writer.println(prefix + " " + tag.toString());
4043 }
4044 }
4045
Sunny Goyal713edfc2016-05-06 09:58:34 -07004046 try {
4047 FileLog.flushAll(writer);
4048 } catch (Exception e) {
4049 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004050 }
4051
Adam Cohen9211d422014-10-07 18:14:53 -07004052 if (mLauncherCallbacks != null) {
4053 mLauncherCallbacks.dump(prefix, fd, writer, args);
4054 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004055 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004056
Sunny Goyal66b24572016-09-21 15:57:55 -07004057 @Override
4058 @TargetApi(Build.VERSION_CODES.N)
4059 public void onProvideKeyboardShortcuts(
4060 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
4061
4062 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
4063 if (mState == State.WORKSPACE) {
4064 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
4065 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
4066 }
4067 View currentFocus = getCurrentFocus();
4068 if (new CustomActionsPopup(this, currentFocus).canShow()) {
4069 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
4070 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
4071 }
4072 if (currentFocus instanceof BubbleTextView &&
4073 ((BubbleTextView) currentFocus).hasDeepShortcuts()) {
4074 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
4075 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
4076 }
4077 if (!shortcutInfos.isEmpty()) {
4078 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
4079 }
4080
4081 super.onProvideKeyboardShortcuts(data, menu, deviceId);
4082 }
4083
4084 @Override
4085 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
4086 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
4087 switch (keyCode) {
4088 case KeyEvent.KEYCODE_A:
4089 if (mState == State.WORKSPACE) {
4090 showAppsView(true, true, false);
4091 return true;
4092 }
4093 break;
4094 case KeyEvent.KEYCODE_S: {
4095 View focusedView = getCurrentFocus();
4096 if (focusedView instanceof BubbleTextView
4097 && focusedView.getTag() instanceof ItemInfo
4098 && mAccessibilityDelegate.performAction(focusedView,
4099 (ItemInfo) focusedView.getTag(),
4100 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -07004101 DeepShortcutsContainer.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07004102 return true;
4103 }
4104 break;
4105 }
4106 case KeyEvent.KEYCODE_O:
4107 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
4108 return true;
4109 }
4110 break;
4111 }
4112 }
4113 return super.onKeyShortcut(keyCode, event);
4114 }
4115
Adam Cohen59400422014-03-05 18:07:04 -08004116 public static CustomAppWidget getCustomAppWidget(String name) {
4117 return sCustomAppWidgets.get(name);
4118 }
4119
4120 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4121 return sCustomAppWidgets;
4122 }
4123
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004124 public static Launcher getLauncher(Context context) {
4125 if (context instanceof Launcher) {
4126 return (Launcher) context;
4127 }
4128 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4129 }
4130
Sunny Goyal745bad92016-05-02 10:54:12 -07004131 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4132
4133 @Override
4134 public void onSharedPreferenceChanged(
4135 SharedPreferences sharedPreferences, String key) {
4136 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4137 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4138 if (!waitUntilResume(this, true)) {
4139 run();
4140 }
4141 }
4142 }
4143
4144 @Override
4145 public void run() {
4146 setOrientation();
4147 }
4148 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004149}