blob: d870f9ee519f14bdb8fd8e35c89fb6a6a4ecdb26 [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 Goyalb5e65c82016-10-26 18:32:38 -070088import com.android.launcher3.anim.AnimationLayerSet;
Sunny Goyalffe83f12014-08-14 17:39:34 -070089import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010090import com.android.launcher3.compat.LauncherActivityInfoCompat;
91import com.android.launcher3.compat.LauncherAppsCompat;
92import com.android.launcher3.compat.UserHandleCompat;
93import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -080094import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -070095import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -070096import com.android.launcher3.dragndrop.DragController;
97import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -070098import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -070099import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -0700100import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000101import com.android.launcher3.folder.Folder;
102import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700103import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700104import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700105import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700106import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyald164b7f2016-10-12 20:49:31 -0700107import com.android.launcher3.model.PackageItemInfo;
108import com.android.launcher3.model.WidgetItem;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700109import com.android.launcher3.pageindicators.PageIndicator;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700110import com.android.launcher3.shortcuts.DeepShortcutManager;
Tony Wickham49c8d292016-07-01 11:44:34 -0700111import com.android.launcher3.shortcuts.DeepShortcutsContainer;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700112import com.android.launcher3.shortcuts.ShortcutKey;
Hyunyoung Song5aa27142016-07-21 11:48:37 -0700113import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal2100c782016-08-22 16:00:03 -0700114import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700115import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700116import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700117import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700118import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal2100c782016-08-22 16:00:03 -0700119import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal322d5562015-06-25 19:35:49 -0700120import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700121import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700122import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700123import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700124import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700125import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700126
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700127import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700128import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700129import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700130import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800131import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700132import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700133import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700134import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700135
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136/**
137 * Default launcher application.
138 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100139public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700140 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
141 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700142 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700143 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700144 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700146 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700147 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400148 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700149
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700151 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700152 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700153 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Michael Jurka8b805b12012-04-18 14:23:14 -0700155 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700156 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700157 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700158
Sunny Goyal28c6b962015-10-12 11:42:05 -0700159 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
160
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700161 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
162
Mathew Inwood876a8462013-06-14 14:12:41 +0100163 /**
164 * IntentStarter uses request codes starting with this. This must be greater than all activity
165 * request codes used internally.
166 */
167 protected static final int REQUEST_LAST = 100;
168
Michael Jurka0a457bf2012-11-19 14:05:05 -0800169 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700170 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800171 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172
Winson Chung2672ff92012-05-04 16:22:30 -0700173 // The Intent extra that defines whether to ignore the launch animation
174 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400175 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700176
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 // Type: int
178 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700179 // Type: int
180 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700181 // Type: PendingRequestArgs
182 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
183 // Type: ActivityResultInfo
184 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700186 static final String APPS_VIEW_SHOWN = "launcher.apps_view_shown";
Adam Cohenb54a5982014-01-08 15:21:04 -0800187
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700188 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700189 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
190 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700191
Adam Cohen091440a2015-03-18 14:16:05 -0700192 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700193 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700194
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700195 private boolean mIsSafeModeEnabled;
196
Joe Onorato9c1289c2009-08-17 11:03:03 -0400197 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800198 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700199 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700200 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201
Winson Chunga2413752012-04-03 14:22:34 -0700202 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700203 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
204 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700205 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700206
Adam Cohen091440a2015-03-18 14:16:05 -0700207 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700208 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700209 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800210 private DragController mDragController;
Sunny Goyal6178f132016-07-11 17:30:03 -0700211 private View mQsbContainer;
Sunny Goyal322d5562015-06-25 19:35:49 -0700212
213 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700214
Sunny Goyalffe83f12014-08-14 17:39:34 -0700215 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700216 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700217
Michael Jurka0280c3b2010-09-17 15:00:07 -0700218 private int[] mTmpAddItemCellCoordinates = new int[2];
219
Sunny Goyal316490e2015-06-02 09:38:28 -0700220 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800221 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700222
Michael Jurka838a4ca2011-02-07 13:33:06 -0800223 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700224 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700225
Sunny Goyal47328fd2016-05-25 18:56:41 -0700226 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700227
228 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700229 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700230 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700231
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700232 // Main container view and the model for the widget tray screen.
233 @Thunk WidgetsContainerView mWidgetsView;
Sunny Goyald164b7f2016-10-12 20:49:31 -0700234 @Thunk MultiHashMap<PackageItemInfo, WidgetItem> mAllWidgets;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700235
Winson Chung4a2afa32012-07-19 14:53:05 -0700236 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
237 // scroll issues (because the workspace may not have been measured yet) and extra work.
238 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
239 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800240
241 private SpannableStringBuilder mDefaultKeySsb = null;
242
Adam Cohen091440a2015-03-18 14:16:05 -0700243 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400244
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800245 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700246 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247
Michael Jurka1e2f4652013-07-08 18:03:46 -0700248 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700249 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700250 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700251
Joe Onorato9c1289c2009-08-17 11:03:03 -0400252 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800253 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700254 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700255 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700256 private Handler mHandler = new Handler();
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700257 private boolean mIsResumeFromActionScreenOff;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700258 private boolean mHasFocus = false;
259 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400260
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700261 /** Maps launcher activity components to their list of shortcut ids. */
262 private MultiHashMap<ComponentKey, String> mDeepShortcutMap = new MultiHashMap<>();
263
Adam Cohen61f560d2013-09-30 15:58:20 -0700264 private View.OnTouchListener mHapticFeedbackTouchListener;
265
Winson Chung400438b2011-01-16 17:53:48 -0800266 // Determines how long to wait after a rotation before restoring the screen orientation to
267 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700268 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800269
Adam Cohen1462de32012-07-24 22:34:36 -0700270 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
271
Winson Chung46353de2012-02-16 14:05:10 -0800272 // We only want to get the SharedPreferences once since it does an FS stat each time we get
273 // it from the context.
274 private SharedPreferences mSharedPrefs;
275
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700276 private DeviceProfile mDeviceProfile;
277
Adam Cohen4b66bf32015-09-18 12:15:19 -0700278 private boolean mMoveToDefaultScreenFromNewIntent;
279
Winson Chung13eb5272015-05-11 16:30:13 -0700280 // This is set to the view that launched the activity that navigated the user away from
281 // launcher. Since there is no callback for when the activity has finished launching, enable
282 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800283 private BubbleTextView mWaitingForResume;
284
Adam Cohen59400422014-03-05 18:07:04 -0800285 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
286 new HashMap<String, CustomAppWidget>();
287
Adam Cohen59400422014-03-05 18:07:04 -0800288 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700289 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
290 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800291 }
292 }
293
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700294 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
295 // state transition. If another state transition happened during this delay,
296 // simply unregister this runnable.
297 private Runnable mExitSpringLoadedModeRunnable;
298
Adam Cohen091440a2015-03-18 14:16:05 -0700299 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700300 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700301 if (mWorkspace != null) {
302 mWorkspace.buildPageHardwareLayers();
303 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700304 }
305 };
306
Sunny Goyal2100c782016-08-22 16:00:03 -0700307 // Activity result which needs to be processed after workspace has loaded.
308 private ActivityResultInfo mPendingActivityResult;
309 /**
310 * Holds extra information required to handle a result from an external call, like
311 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
312 */
313 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800314
Hyunyoung Songaa953652016-04-19 18:30:24 -0700315 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800316
Jon Miranda379198e2016-09-23 08:54:40 -0700317 private float mLastDispatchTouchEventX = 0.0f;
318 private float mEdgeOfScreenThresholdPx = 0.0f;
319
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700320 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700321 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400322
323 @Thunk void setOrientation() {
324 if (mRotationEnabled) {
325 unlockScreenOrientation(true);
326 } else {
327 setRequestedOrientation(
328 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700329 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400330 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700331
Sunny Goyal745bad92016-05-02 10:54:12 -0700332 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700333
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800334 @Override
335 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700336 if (DEBUG_STRICT_MODE) {
337 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
338 .detectDiskReads()
339 .detectDiskWrites()
340 .detectNetwork() // or .detectAll() for all detectable problems
341 .penaltyLog()
342 .build());
343 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
344 .detectLeakedSqlLiteObjects()
345 .detectLeakedClosableObjects()
346 .penaltyLog()
347 .penaltyDeath()
348 .build());
349 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700350 if (LauncherAppState.PROFILE_STARTUP) {
351 Trace.beginSection("Launcher-onCreate");
352 }
Winson Chunga2413752012-04-03 14:22:34 -0700353
Adam Cohen9211d422014-10-07 18:14:53 -0700354 if (mLauncherCallbacks != null) {
355 mLauncherCallbacks.preOnCreate();
356 }
357
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400359
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400360 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700361
Adam Cohen2e6da152015-05-06 11:42:25 -0700362 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700363 mDeviceProfile = getResources().getConfiguration().orientation
364 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700365 app.getInvariantDeviceProfile().landscapeProfile
366 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700367
Sunny Goyalf7258242015-10-19 16:59:07 -0700368 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700369 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800370 mModel = app.setLauncher(this);
371 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700372 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700373
Joe Onorato41a12d22009-10-31 18:30:00 -0400374 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700375 mAllAppsController = new AllAppsTransitionController(this);
376 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700377
Sunny Goyalffe83f12014-08-14 17:39:34 -0700378 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700379
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700380 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
381 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700382
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700383 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
384 // this also ensures that any synchronous binding below doesn't re-trigger another
385 // LauncherModel load.
386 mPaused = false;
387
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700389
Jon Miranda379198e2016-09-23 08:54:40 -0700390 mEdgeOfScreenThresholdPx = getResources()
391 .getDimensionPixelSize(R.dimen.edge_of_screen_threshold);
392
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 setupViews();
Winson1f064272016-07-18 17:18:02 -0700394 mDeviceProfile.layout(this, false /* notifyListeners */);
Tony Wickham827cef22016-03-17 15:39:39 -0700395 mExtractedColors = new ExtractedColors();
396 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397
Tony Wickhame2217252016-03-22 16:34:23 -0700398 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
399 .addAccessibilityStateChangeListener(this);
400
Joe Onorato7c312c12009-08-13 21:36:53 -0700401 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700402
Sunny Goyalfe770c92016-09-27 14:38:58 -0700403 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404
Sunny Goyale26d1002016-06-20 14:52:14 -0700405 if (LauncherAppState.PROFILE_STARTUP) {
406 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 }
408
Sunny Goyal2100c782016-08-22 16:00:03 -0700409 // We only load the page synchronously if the user rotates (or triggers a
410 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700411 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
412 if (savedInstanceState != null) {
413 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
414 }
415 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700416 // If we are not binding synchronously, show a fade in animation when
417 // the first page bind completes.
418 mDragLayer.setAlpha(0);
419 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700420 // Pages bound synchronously.
421 mWorkspace.setCurrentPage(currentScreen);
422
Sunny Goyal2100c782016-08-22 16:00:03 -0700423 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800424 }
425
426 // For handling default keys
427 mDefaultKeySsb = new SpannableStringBuilder();
428 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800429
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800430 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700431 // In case we are on a device with locked rotation, we should look at preferences to check
432 // if the user has specifically allowed rotation.
433 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700434 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700435 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
436 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700437 }
438
439 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
440 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400441 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700442
Adam Cohen9211d422014-10-07 18:14:53 -0700443 if (mLauncherCallbacks != null) {
444 mLauncherCallbacks.onCreate(savedInstanceState);
445 }
446 }
447
Sunny Goyal7779d622015-06-11 16:18:39 -0700448 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700449 public void onExtractedColorsChanged() {
450 loadExtractedColorsAndColorItems();
451 }
452
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700453 @Override
454 public void onAppWidgetHostReset() {
455 if (mAppWidgetHost != null) {
456 mAppWidgetHost.startListening();
457 }
458 }
459
Tony Wickham827cef22016-03-17 15:39:39 -0700460 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700461 // TODO: do this in pre-N as well, once the extraction part is complete.
Tony Wickham345bff32016-09-28 15:34:51 -0700462 if (Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700463 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700464 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700465 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham345bff32016-09-28 15:34:51 -0700466 // It's possible that All Apps is visible when this is run,
467 // so always use light status bar in that case.
468 activateLightStatusBar(isAllAppsVisible());
Tony Wickham827cef22016-03-17 15:39:39 -0700469 }
470 }
471
Tony Wickham345bff32016-09-28 15:34:51 -0700472 /**
473 * Sets the status bar to be light or not. Light status bar means dark icons.
474 * @param activate if true, make sure the status bar is light, otherwise base on wallpaper.
475 */
476 public void activateLightStatusBar(boolean activate) {
Tony Wickham0fed55b2016-10-12 14:55:46 -0700477 boolean lightStatusBar = activate || (FeatureFlags.LIGHT_STATUS_BAR
478 && mExtractedColors.getColor(ExtractedColors.STATUS_BAR_INDEX,
479 ExtractedColors.DEFAULT_DARK) == ExtractedColors.DEFAULT_LIGHT);
Tony Wickham345bff32016-09-28 15:34:51 -0700480 int oldSystemUiFlags = getWindow().getDecorView().getSystemUiVisibility();
481 int newSystemUiFlags = oldSystemUiFlags;
Tony Wickham93113872016-09-14 16:46:32 -0700482 if (lightStatusBar) {
Tony Wickham345bff32016-09-28 15:34:51 -0700483 newSystemUiFlags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
Tony Wickham93113872016-09-14 16:46:32 -0700484 } else {
Tony Wickham345bff32016-09-28 15:34:51 -0700485 newSystemUiFlags &= ~(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Tony Wickham93113872016-09-14 16:46:32 -0700486 }
Tony Wickham345bff32016-09-28 15:34:51 -0700487 if (newSystemUiFlags != oldSystemUiFlags) {
488 getWindow().getDecorView().setSystemUiVisibility(newSystemUiFlags);
489 }
Tony Wickham93113872016-09-14 16:46:32 -0700490 }
491
Adam Cohen9211d422014-10-07 18:14:53 -0700492 private LauncherCallbacks mLauncherCallbacks;
493
494 public void onPostCreate(Bundle savedInstanceState) {
495 super.onPostCreate(savedInstanceState);
496 if (mLauncherCallbacks != null) {
497 mLauncherCallbacks.onPostCreate(savedInstanceState);
498 }
499 }
500
Winson1f064272016-07-18 17:18:02 -0700501 public void onInsetsChanged(Rect insets) {
502 mDeviceProfile.updateInsets(insets);
503 mDeviceProfile.layout(this, true /* notifyListeners */);
504 }
505
Sunny Goyal32554d12015-12-03 15:31:25 -0800506 /**
507 * Call this after onCreate to set or clear overlay.
508 */
509 public void setLauncherOverlay(LauncherOverlay overlay) {
510 if (overlay != null) {
511 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
512 }
513 mWorkspace.setLauncherOverlay(overlay);
514 }
515
Adam Cohen9211d422014-10-07 18:14:53 -0700516 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
517 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700518 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700519 private boolean mWorkspaceImportanceStored = false;
520 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700521 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800522 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700523 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
524
525 @Override
526 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700527 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700528 return;
529 }
530 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700531 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700532 if (mWorkspace != null) {
533 mWorkspaceImportanceForAccessibility =
534 mWorkspace.getImportantForAccessibility();
535 mWorkspace.setImportantForAccessibility(
536 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
537 mWorkspaceImportanceStored = true;
538 }
539 if (mHotseat != null) {
540 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
541 mHotseat.setImportantForAccessibility(
542 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
543 mHotseatImportanceStored = true;
544 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700545 }
546
547 @Override
548 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700549 if (mWorkspaceImportanceStored && mWorkspace != null) {
550 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
551 }
552 if (mHotseatImportanceStored && mHotseat != null) {
553 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
554 }
555 mWorkspaceImportanceStored = false;
556 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700557 }
558 });
Adam Cohen9211d422014-10-07 18:14:53 -0700559 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700560 }
561
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700562 @Override
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700563 public void onLauncherProviderChanged() {
Adam Cohen9211d422014-10-07 18:14:53 -0700564 if (mLauncherCallbacks != null) {
565 mLauncherCallbacks.onLauncherProviderChange();
566 }
567 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700568
Adam Cohen9211d422014-10-07 18:14:53 -0700569 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700570 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700571 if (mLauncherCallbacks != null) {
572 return mLauncherCallbacks.hasCustomContentToLeft();
573 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700574 return false;
575 }
576
Dave Hawkeya8881582013-09-17 15:55:33 -0600577 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500578 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600579 * {@link #addToCustomContentPage}. This will only be invoked if
580 * {@link #hasCustomContentToLeft()} is {@code true}.
581 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500582 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700583 if (mLauncherCallbacks != null) {
584 mLauncherCallbacks.populateCustomContentContainer();
585 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600586 }
587
588 /**
Sunny Goyalfe770c92016-09-27 14:38:58 -0700589 * Invoked by subclasses to signal a change to the {@link #addToCustomContentPage} value to
Dave Hawkeya8881582013-09-17 15:55:33 -0600590 * ensure the custom content page is added or removed if necessary.
591 */
592 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600593 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600594 // Not bound yet, wait for bindScreens to be called.
595 return;
596 }
597
598 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
599 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500600 mWorkspace.createCustomContentContainer();
601 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600602 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
603 mWorkspace.removeCustomContentPage();
604 }
605 }
606
Hyunyoung Songaa953652016-04-19 18:30:24 -0700607 public UserEventDispatcher getUserEventDispatcher() {
608 if (mLauncherCallbacks != null) {
609 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
610 if (dispatcher != null) {
611 return dispatcher;
612 }
613 }
614
Sunny Goyal64976d52016-06-20 14:56:28 -0700615 // Logger object is a singleton and does not have to be coupled with the foreground
616 // activity. Since most user event logging is done on the UI, the object is retrieved
617 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700618 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700619 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700620 }
621 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800622 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100623
Hyunyoung Song3f471442015-04-08 19:01:34 -0700624 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700625 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
626 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700627 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700628 }
629
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000630 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700631 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
632 // This cast is safe as long as the id < 0x00FFFFFF
633 // Since we jail all the dynamically generated views, there should be no clashes
634 // with any other views.
635 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000636 }
637
638 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700639 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
640 * a configuration step, this allows the proper animations to run after other transitions.
641 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700642 private long completeAdd(
643 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
644 long screenId = info.screenId;
645 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700646 // When the screen id represents an actual screen (as opposed to a rank) we make sure
647 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700648 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700649 }
Adam Cohendb364c32014-05-20 14:23:40 -0700650
Sunny Goyal2100c782016-08-22 16:00:03 -0700651 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800652 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700653 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700654 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800655 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700656 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700657 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700658 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700659 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700660 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700661 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700662 int widgetId = appWidgetId;
663 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700664 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700665 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700666 // Since the view was just bound, also launch the configure activity if needed
667 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
668 .getLauncherAppWidgetInfo(widgetId);
669 if (provider != null && provider.configure != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700670 startRestoredWidgetReconfigActivity(provider, widgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -0700671 }
672 }
673 break;
674 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800675 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700676
Adam Cohendb364c32014-05-20 14:23:40 -0700677 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800678 }
679
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800680 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700681 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700682 if (isWorkspaceLoading()) {
683 // process the result once the workspace has loaded.
684 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
685 return;
686 }
687 mPendingActivityResult = null;
688
Winson Chunge341d302013-08-16 14:31:00 -0700689 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700690 final PendingRequestArgs requestArgs = mPendingRequestArgs;
691 setWaitingForResult(null);
692 if (requestArgs == null) {
693 return;
694 }
695
696 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700697
Adam Cohenad4e15c2013-10-17 16:21:35 -0700698 Runnable exitSpringLoaded = new Runnable() {
699 @Override
700 public void run() {
701 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
702 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
703 }
704 };
705
Michael Jurka8b805b12012-04-18 14:23:14 -0700706 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700707 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700708 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700709 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
710 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700711 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700712 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700713 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700714 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700715 addAppWidgetImpl(
716 appWidgetId, requestArgs, null,
717 requestArgs.getWidgetProvider(),
718 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700719 }
720 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200721 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
722 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700723 // User could have free-scrolled between pages before picking a wallpaper; make sure
724 // we move to the closest one now.
725 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700726 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200727 }
728 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700729 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200730
Adam Cohened66b2b2012-01-23 17:28:51 -0800731 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700732 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700733
Adam Cohened66b2b2012-01-23 17:28:51 -0800734 // We have special handling for widgets
735 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800736 final int appWidgetId;
737 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
738 : -1;
739 if (widgetId < 0) {
740 appWidgetId = pendingAddWidgetId;
741 } else {
742 appWidgetId = widgetId;
743 }
744
Adam Cohenad4e15c2013-10-17 16:21:35 -0700745 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800746 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700747 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
748 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700749 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700750 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700751 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700752 @Override
753 public void run() {
754 exitSpringLoadedDragModeDelayed(false, 0, null);
755 }
756 };
Adam Cohendb364c32014-05-20 14:23:40 -0700757
Sunny Goyal2100c782016-08-22 16:00:03 -0700758 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
759 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
760 } else {
761 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
762 // When the screen id represents an actual screen (as opposed to a rank)
763 // we make sure that the drop page actually exists.
764 requestArgs.screenId =
765 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700766 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700767 final CellLayout dropLayout =
768 mWorkspace.getScreenWithId(requestArgs.screenId);
769
770 dropLayout.setDropPending(true);
771 final Runnable onComplete = new Runnable() {
772 @Override
773 public void run() {
774 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
775 dropLayout.setDropPending(false);
776 }
777 };
778 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
779 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700780 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800781 return;
782 }
783
Sunny Goyald478c832016-04-01 12:04:16 -0700784 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
785 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700786 if (resultCode == RESULT_OK) {
787 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700788 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700789 }
790 // Leave the widget in the pending state if the user canceled the configure.
791 return;
792 }
793
Sunny Goyalaad90582015-07-09 14:22:50 -0700794 if (requestCode == REQUEST_CREATE_SHORTCUT) {
795 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700796 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
797 completeAdd(requestCode, data, -1, requestArgs);
798 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
799 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
800
Sunny Goyalaad90582015-07-09 14:22:50 -0700801 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700802 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
803 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800804 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800806 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800807 }
808
809 @Override
810 protected void onActivityResult(
811 final int requestCode, final int resultCode, final Intent data) {
812 handleActivityResult(requestCode, resultCode, data);
813 if (mLauncherCallbacks != null) {
814 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
815 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800816 }
817
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600818 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700819 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600820 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700821 PendingRequestArgs pendingArgs = mPendingRequestArgs;
822 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
823 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
824 setWaitingForResult(null);
825
Sunny Goyal28c6b962015-10-12 11:42:05 -0700826 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700827 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700828 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700829 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700830 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700831 Intent intent = pendingArgs.getPendingIntent();
832
Sunny Goyal28c6b962015-10-12 11:42:05 -0700833 if (grantResults.length > 0
834 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700835 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700836 } else {
837 // TODO: Show a snack bar with link to settings
838 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700839 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700840 }
841 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600842 if (mLauncherCallbacks != null) {
843 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
844 grantResults);
845 }
846 }
847
Adam Cohendb364c32014-05-20 14:23:40 -0700848 /**
849 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
850 *
851 * @param screenId the screen id to check
852 * @return the new screen, or screenId if it exists
853 */
854 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800855 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700856 if (dropLayout == null) {
857 // it's possible that the add screen was removed because it was
858 // empty and a re-bind occurred
859 mWorkspace.addExtraEmptyScreen();
860 return mWorkspace.commitExtraEmptyScreen();
861 } else {
862 return screenId;
863 }
864 }
865
Sunny Goyal2100c782016-08-22 16:00:03 -0700866 @Thunk void completeTwoStageWidgetDrop(
867 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
868 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800869 Runnable onCompleteRunnable = null;
870 int animationType = 0;
871
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700872 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800873 if (resultCode == RESULT_OK) {
874 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
875 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal2100c782016-08-22 16:00:03 -0700876 requestArgs.getWidgetProvider());
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700877 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800878 onCompleteRunnable = new Runnable() {
879 @Override
880 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700881 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700882 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
883 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800884 }
885 };
886 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800887 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800888 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800889 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700890 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700891 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700892 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
893 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700894 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700895 // The animated view may be null in the case of a rotation during widget configuration
896 onCompleteRunnable.run();
897 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 }
899
900 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700901 protected void onStop() {
902 super.onStop();
903 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700904
905 if (mLauncherCallbacks != null) {
906 mLauncherCallbacks.onStop();
907 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700908
909 if (Utilities.isNycMR1OrAbove()) {
910 mAppWidgetHost.stopListening();
911 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700912 }
913
914 @Override
915 protected void onStart() {
916 super.onStart();
917 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700918
919 if (mLauncherCallbacks != null) {
920 mLauncherCallbacks.onStart();
921 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700922
923 if (Utilities.isNycMR1OrAbove()) {
924 mAppWidgetHost.startListening();
925 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700926 }
927
928 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800929 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200930 long startTime = 0;
931 if (DEBUG_RESUME_TIME) {
932 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400933 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200934 }
Adam Cohen9211d422014-10-07 18:14:53 -0700935
936 if (mLauncherCallbacks != null) {
937 mLauncherCallbacks.preOnResume();
938 }
939
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700941 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700942
Winson Chung4a2afa32012-07-19 14:53:05 -0700943 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700944 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700945 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800946 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700947 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700948 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700949 // view after launching an app, as they may be depending on the UI to be static to
950 // switch to another app, otherwise, if it was
Hyunyoung Songdd60ce42016-07-27 17:08:38 -0700951 showAppsView(false /* animated */, !launchedFromApp /* updatePredictedApps */,
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700952 mAppsView.shouldRestoreImeState() /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800953 } else if (mOnResumeState == State.WIDGETS) {
954 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700955 }
956 mOnResumeState = State.NONE;
957
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800958 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700959 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700960 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700961 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700962 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700964 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200965 // We might have postponed some bind calls until onResume (see waitUntilResume) --
966 // execute them here
967 long startTimeCallbacks = 0;
968 if (DEBUG_RESUME_TIME) {
969 startTimeCallbacks = System.currentTimeMillis();
970 }
971
Michael Jurka1e2f4652013-07-08 18:03:46 -0700972 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
973 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200974 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700975 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200976 if (DEBUG_RESUME_TIME) {
977 Log.d(TAG, "Time spent processing callbacks in onResume: " +
978 (System.currentTimeMillis() - startTimeCallbacks));
979 }
Michael Jurka447bf842013-05-15 14:52:15 +0200980 }
Michael Jurka54554252013-08-01 12:52:23 +0200981 if (mOnResumeCallbacks.size() > 0) {
982 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
983 mOnResumeCallbacks.get(i).run();
984 }
985 mOnResumeCallbacks.clear();
986 }
Winson Chunge4e50662012-01-23 14:45:13 -0800987
988 // Reset the pressed state of icons that were locked in the press state while activities
989 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800990 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800991 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800992 mWaitingForResume.setStayPressed(false);
993 }
Winson Chung82963d52013-10-09 11:20:57 -0700994
Adam Cohen06dff352012-06-01 17:17:08 -0700995 // It is possible that widgets can receive updates while launcher is not in the foreground.
996 // Consequently, the widgets will be inflated in the orientation of the foreground activity
997 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
998 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -0700999 if (!isWorkspaceLoading()) {
1000 getWorkspace().reinflateWidgetsIfNecessary();
1001 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001002
Michael Jurka447bf842013-05-15 14:52:15 +02001003 if (DEBUG_RESUME_TIME) {
1004 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1005 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001006
Adam Cohen4b66bf32015-09-18 12:15:19 -07001007 // We want to suppress callbacks about CustomContent being shown if we have just received
1008 // onNewIntent while the user was present within launcher. In that case, we post a call
1009 // to move the user to the main screen (which will occur after onResume). We don't want to
1010 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1011 // suppress here.
1012 if (mWorkspace.getCustomContentCallbacks() != null
1013 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001014 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001015 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001016 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1017 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001018 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001019 }
1020 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001021 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001022 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001023 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001024
Sunny Goyal756adbc2015-04-16 15:20:51 -07001025 if (!isWorkspaceLoading()) {
1026 // Process any items that were added while Launcher was away.
1027 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001028
1029 // Refresh shortcuts if the permission changed.
1030 mModel.refreshShortcutsIfRequired();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001031 }
Adam Cohen9211d422014-10-07 18:14:53 -07001032
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001033 if (shouldShowDiscoveryBounce()) {
1034 mAllAppsController.showDiscoveryBounce();
1035 }
1036 mIsResumeFromActionScreenOff = false;
Adam Cohen9211d422014-10-07 18:14:53 -07001037 if (mLauncherCallbacks != null) {
1038 mLauncherCallbacks.onResume();
1039 }
Adam Cohen06dff352012-06-01 17:17:08 -07001040 }
1041
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001043 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001044 // Ensure that items added to Launcher are queued until Launcher returns
1045 InstallShortcutReceiver.enableInstallQueue();
1046
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001047 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001048 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001049 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001050 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001051
1052 // We call onHide() aggressively. The custom content callbacks should be able to
1053 // debounce excess onHide calls.
1054 if (mWorkspace.getCustomContentCallbacks() != null) {
1055 mWorkspace.getCustomContentCallbacks().onHide();
1056 }
Romain Guycbb89e42009-06-08 15:52:54 -07001057
Adam Cohen9211d422014-10-07 18:14:53 -07001058 if (mLauncherCallbacks != null) {
1059 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001060 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001061 }
1062
1063 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001064 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1065 // by a onResume or by scrolling otherwise.
1066 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001067
1068 // Custom content is completely hidden
1069 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001070
1071 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1072 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001073
1074 // Indicates whether the user is allowed to scroll away from the custom content.
1075 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001076 }
1077
Adam Cohenc2d6e892014-10-16 09:49:24 -07001078 public interface LauncherOverlay {
1079
1080 /**
1081 * Touch interaction leading to overscroll has begun
1082 */
1083 public void onScrollInteractionBegin();
1084
1085 /**
1086 * Touch interaction related to overscroll has ended
1087 */
1088 public void onScrollInteractionEnd();
1089
1090 /**
1091 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1092 * screen (or in the case of RTL, the rightmost screen).
1093 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001094 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001095
1096 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001097 * Called when the launcher is ready to use the overlay
1098 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001099 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001100 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001101 }
1102
Jun Mukai8af0cd82015-05-12 12:32:12 -07001103 public interface LauncherSearchCallbacks {
1104 /**
1105 * Called when the search overlay is shown.
1106 */
1107 public void onSearchOverlayOpened();
1108
1109 /**
1110 * Called when the search overlay is dismissed.
1111 */
1112 public void onSearchOverlayClosed();
1113 }
1114
Adam Cohenc2d6e892014-10-16 09:49:24 -07001115 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001116 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001117 }
1118
1119 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1120
Sunny Goyalc86df472016-02-25 09:19:38 -08001121 public void onScrollChanged(float progress) {
1122 if (mWorkspace != null) {
1123 mWorkspace.onOverlayScrollChanged(progress);
1124 }
1125 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001126 }
1127
Jorim Jaggid017f882014-01-14 17:08:48 -08001128 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001129 if (mLauncherCallbacks != null) {
1130 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001131 } else {
1132 // On devices with a locked orientation, we will at least have the allow rotation
1133 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001134 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001135 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001136 }
1137
Adam Cohen9211d422014-10-07 18:14:53 -07001138 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001139 CustomContentCallbacks callbacks, String description) {
1140 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001141 }
1142
Adam Cohenedb40762013-07-18 16:45:45 -07001143 // The custom content needs to offset its content to account for the QSB
1144 public int getTopOffsetForCustomContent() {
1145 return mWorkspace.getPaddingTop();
1146 }
1147
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001148 @Override
1149 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001150 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001151 if (mModel.isCurrentCallbacks(this)) {
1152 mModel.stopLoader();
1153 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001154 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1155
Romain Guy13c2e7b2010-03-10 19:45:00 -08001156 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001157 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001158
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001159 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001160 @Override
1161 public void onWindowFocusChanged(boolean hasFocus) {
1162 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001163 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001164
1165 if (mLauncherCallbacks != null) {
1166 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1167 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001168 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001169
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 private boolean acceptFilter() {
1171 final InputMethodManager inputManager = (InputMethodManager)
1172 getSystemService(Context.INPUT_METHOD_SERVICE);
1173 return !inputManager.isFullscreenMode();
1174 }
1175
1176 @Override
1177 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001178 final int uniChar = event.getUnicodeChar();
1179 final boolean handled = super.onKeyDown(keyCode, event);
1180 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1181 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1183 keyCode, event);
1184 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001185 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001186 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001187 // showSearchDialog()
1188 // If there are multiple keystrokes before the search dialog takes focus,
1189 // onSearchRequested() will be called for every keystroke,
1190 // but it is idempotent, so it's fine.
1191 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192 }
1193 }
1194
Joe Onorato8a9625e2010-01-28 15:55:35 -08001195 // Eat the long press event so the keyboard doesn't come up.
1196 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1197 return true;
1198 }
1199
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 return handled;
1201 }
1202
Winson46163472015-09-22 17:31:56 -07001203 @Override
1204 public boolean onKeyUp(int keyCode, KeyEvent event) {
1205 if (keyCode == KeyEvent.KEYCODE_MENU) {
1206 // Ignore the menu key if we are currently dragging or are on the custom content screen
1207 if (!isOnCustomContent() && !mDragController.isDragging()) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001208 // Close any open floating view
1209 AbstractFloatingView.closeAllOpenViews(this);
Tony Wickham49c8d292016-07-01 11:44:34 -07001210
Winson46163472015-09-22 17:31:56 -07001211 // Stop resizing any widgets
1212 mWorkspace.exitWidgetResizeMode();
1213
1214 // Show the overview mode if we are on the workspace
1215 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1216 !mWorkspace.isSwitchingState()) {
1217 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001218 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001219 }
1220 }
1221 return true;
1222 }
1223 return super.onKeyUp(keyCode, event);
1224 }
1225
Karl Rosaen138a0412009-04-23 19:00:21 -07001226 private String getTypedText() {
1227 return mDefaultKeySsb.toString();
1228 }
1229
Sunny Goyal6f28e712016-09-13 14:19:29 -07001230 @Override
1231 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001232 mDefaultKeySsb.clear();
1233 mDefaultKeySsb.clearSpans();
1234 Selection.setSelection(mDefaultKeySsb, 0);
1235 }
1236
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237 /**
1238 * Restores the previous state, if it exists.
1239 *
1240 * @param savedState The previous state.
1241 */
1242 private void restoreState(Bundle savedState) {
1243 if (savedState == null) {
1244 return;
1245 }
1246
Sunny Goyalfe770c92016-09-27 14:38:58 -07001247 int stateOrdinal = savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal());
1248 State[] stateValues = State.values();
1249 State state = (stateOrdinal >= 0 && stateOrdinal < stateValues.length)
1250 ? stateValues[stateOrdinal] : State.WORKSPACE;
Winson Chungb745afb2015-03-02 11:51:23 -08001251 if (state == State.APPS || state == State.WIDGETS) {
1252 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001253 }
1254
Sunny Goyal2100c782016-08-22 16:00:03 -07001255 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1256 if (requestArgs != null) {
1257 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001259
1260 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 }
1262
1263 /**
1264 * Finds all the views we need and configure them properly.
1265 */
1266 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001267 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001268 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001269 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001270 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyal6178f132016-07-11 17:30:03 -07001271 mQsbContainer = mDragLayer.findViewById(mDeviceProfile.isVerticalBarLayout()
1272 ? R.id.workspace_blocked_row : R.id.qsb_container);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001273 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001274
Sunny Goyal784f9c32016-03-23 16:56:54 -07001275 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1276 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1277 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278
Winson Chung4c98d922011-05-31 16:50:48 -07001279 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001280 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001281
Winson Chung3d503fb2011-07-13 17:25:49 -07001282 // Setup the hotseat
1283 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1284 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001285 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001286 }
1287
Winsone9f27272015-10-13 10:47:51 -07001288 // Setup the overview panel
1289 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001290
Winson Chung4c98d922011-05-31 16:50:48 -07001291 // Setup the workspace
1292 mWorkspace.setHapticFeedbackEnabled(false);
1293 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001294 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001295 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1296 // default state, otherwise we will update to the wrong offsets in RTL
1297 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001298 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001299 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001300
Tony Wickham34d2c912015-09-11 09:27:58 -07001301 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001302 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001303
Winson Chungef7f8742015-06-04 17:18:17 -07001304 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001305 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001306 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001307 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1308 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1309 } else {
1310 mAppsView.setSearchBarController(new DefaultAppSearchController());
1311 }
Craig Mautner360310b2012-10-26 15:13:08 -07001312
Winson Chung3d503fb2011-07-13 17:25:49 -07001313 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song0de01172016-10-05 16:27:48 -07001314 mDragController.setDragScroller(mWorkspace);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001315 mDragController.setScrollView(mDragLayer);
1316 mDragController.setMoveTarget(mWorkspace);
1317 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001318 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001319
Peter Schiller310a9882016-06-30 13:52:36 -07001320 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1321 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1322 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001323
Sunny Goyal322d5562015-06-25 19:35:49 -07001324 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1325 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001326 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 }
1328
Winsone9f27272015-10-13 10:47:51 -07001329 private void setupOverviewPanel() {
1330 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1331
Winsone9f27272015-10-13 10:47:51 -07001332 // Bind wallpaper button actions
1333 View wallpaperButton = findViewById(R.id.wallpaper_button);
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001334 new OverviewButtonClickListener(LauncherLogProto.WALLPAPER_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001335 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001336 public void handleViewClick(View view) {
1337 onClickWallpaperPicker(view);
Winsone9f27272015-10-13 10:47:51 -07001338 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001339 }.attachTo(wallpaperButton);
Winsone9f27272015-10-13 10:47:51 -07001340 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1341
1342 // Bind widget button actions
1343 mWidgetsButton = findViewById(R.id.widget_button);
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001344 new OverviewButtonClickListener(LauncherLogProto.WIDGETS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001345 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001346 public void handleViewClick(View view) {
1347 onClickAddWidgetButton(view);
Winsone9f27272015-10-13 10:47:51 -07001348 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001349 }.attachTo(mWidgetsButton);
Winsone9f27272015-10-13 10:47:51 -07001350 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1351
1352 // Bind settings actions
1353 View settingsButton = findViewById(R.id.settings_button);
1354 boolean hasSettings = hasSettings();
1355 if (hasSettings) {
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001356 new OverviewButtonClickListener(LauncherLogProto.SETTINGS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001357 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001358 public void handleViewClick(View view) {
1359 onClickSettingsButton(view);
Winsone9f27272015-10-13 10:47:51 -07001360 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001361 }.attachTo(settingsButton);
Winsone9f27272015-10-13 10:47:51 -07001362 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1363 } else {
1364 settingsButton.setVisibility(View.GONE);
1365 }
1366
1367 mOverviewPanel.setAlpha(0f);
1368 }
1369
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001371 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001372 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001373 */
1374 public void setAllAppsButton(View allAppsButton) {
1375 mAllAppsButton = allAppsButton;
1376 }
1377
Sunny Goyalbb011da2016-06-15 15:42:29 -07001378 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001379 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001380 }
1381
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001382 public View getWidgetsButton() {
1383 return mWidgetsButton;
1384 }
1385
Anjali Koppal5ad44842014-03-10 20:34:39 -07001386 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 * Creates a view representing a shortcut.
1388 *
1389 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001391 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001392 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 }
1394
1395 /**
1396 * Creates a view representing a shortcut inflated from the specified resource.
1397 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 * @param parent The group the shortcut belongs to.
1399 * @param info The data structure describing the shortcut.
1400 *
1401 * @return A View inflated from layoutResId.
1402 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001403 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001404 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001405 parent, false);
1406 favorite.applyFromShortcutInfo(info, mIconCache);
1407 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001408 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001409 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410 return favorite;
1411 }
1412
1413 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 * Add a shortcut to the workspace.
1415 *
1416 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001418 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001419 int cellY, PendingRequestArgs args) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001420 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001421 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001422
Sunny Goyal5c97f512015-05-19 16:03:28 -07001423 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001424 if (info == null || args.getRequestCode() != REQUEST_CREATE_SHORTCUT ||
1425 args.getPendingIntent().getComponent() == null) {
1426 return;
1427 }
1428 if (!PackageManagerHelper.hasPermissionForActivity(
1429 this, info.intent, args.getPendingIntent().getComponent().getPackageName())) {
1430 // The app is trying to add a shortcut without sufficient permissions
1431 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
Adam Cohend9198822011-11-22 16:42:47 -08001432 return;
1433 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001434 final View view = createShortcut(info);
1435
Sunny Goyal5c97f512015-05-19 16:03:28 -07001436 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001437 // First we check if we already know the exact location where we want to add this item.
1438 if (cellX >= 0 && cellY >= 0) {
1439 cellXY[0] = cellX;
1440 cellXY[1] = cellY;
1441 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001442
1443 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001444 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001445 true, null,null)) {
1446 return;
1447 }
1448 DragObject dragObject = new DragObject();
1449 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001450 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1451 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001452 return;
1453 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001454 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001455 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001456 }
1457
1458 if (!foundCellSpan) {
Jon Miranda9485e5f2016-10-18 11:47:42 -07001459 mWorkspace.onNoCellFound(layout);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001460 return;
1461 }
1462
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001463 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464
Sunny Goyal2100c782016-08-22 16:00:03 -07001465 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
1466 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 }
1468
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001470 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001472 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001474 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001475 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1476
Adam Cohened66b2b2012-01-23 17:28:51 -08001477 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001478 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001479 }
Romain Guycbb89e42009-06-08 15:52:54 -07001480
Adam Cohen59400422014-03-05 18:07:04 -08001481 if (appWidgetInfo.isCustomWidget) {
1482 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001483 }
1484
Adam Cohen59400422014-03-05 18:07:04 -08001485 LauncherAppWidgetInfo launcherInfo;
1486 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001487 launcherInfo.spanX = itemInfo.spanX;
1488 launcherInfo.spanY = itemInfo.spanY;
1489 launcherInfo.minSpanX = itemInfo.minSpanX;
1490 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001491 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001492
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001494 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495
Sunny Goyal2100c782016-08-22 16:00:03 -07001496 if (hostView == null) {
1497 // Perform actual inflation because we're live
1498 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001500 hostView.setVisibility(View.VISIBLE);
1501 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 }
Romain Guycbb89e42009-06-08 15:52:54 -07001503
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001504 private void addAppWidgetToWorkspace(
1505 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001506 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001507 hostView.setTag(item);
1508 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001509
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001510 hostView.setFocusable(true);
1511 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001512
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001513 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001514 item.cellX, item.cellY, item.spanX, item.spanY, insert);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001515 }
1516
Adam Cohended9f8d2010-11-03 13:25:16 -07001517 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1518 @Override
1519 public void onReceive(Context context, Intent intent) {
1520 final String action = intent.getAction();
1521 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Sunny Goyal6ad72f02016-09-23 11:01:10 -07001522 mDragLayer.clearResizeFrame();
Winson Chung337cd9d2011-03-30 10:39:30 -07001523
Winson Chungc100e8e2011-08-09 16:02:43 -07001524 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001525 // processing a multi-step drop
Sunny Goyal2100c782016-08-22 16:00:03 -07001526 if (mAppsView != null && mWidgetsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001527 if (!showWorkspace(false)) {
1528 // If we are already on the workspace, then manually reset all apps
1529 mAppsView.reset();
1530 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001531 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001532 mIsResumeFromActionScreenOff = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001533 }
1534 }
1535 };
1536
1537 @Override
1538 public void onAttachedToWindow() {
1539 super.onAttachedToWindow();
1540
1541 // Listen for broadcasts related to user-presence
1542 final IntentFilter filter = new IntentFilter();
1543 filter.addAction(Intent.ACTION_SCREEN_OFF);
Adam Cohended9f8d2010-11-03 13:25:16 -07001544 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001545 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001546 mAttached = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001547
1548 if (mLauncherCallbacks != null) {
1549 mLauncherCallbacks.onAttachedToWindow();
1550 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001551 }
1552
1553 @Override
1554 public void onDetachedFromWindow() {
1555 super.onDetachedFromWindow();
Adam Cohend113e0c2010-11-11 10:48:05 -08001556 if (mAttached) {
1557 unregisterReceiver(mReceiver);
1558 mAttached = false;
1559 }
Sunny Goyalc86df472016-02-25 09:19:38 -08001560
1561 if (mLauncherCallbacks != null) {
1562 mLauncherCallbacks.onDetachedFromWindow();
1563 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001564 }
1565
1566 public void onWindowVisibilityChanged(int visibility) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001567 // The following code used to be in onResume, but it turns out onResume is called when
1568 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1569 // is a more appropriate event to handle
Sunny Goyal1acc56a2016-09-25 21:23:25 -07001570 if (visibility == View.VISIBLE) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001571 if (!mWorkspaceLoading) {
1572 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001573 // We want to let Launcher draw itself at least once before we force it to build
1574 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001575 // apps is nice and speedy.
1576 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001577 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001578 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001579 if (mStarted) return;
1580 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001581 // We delay the layer building a bit in order to give
1582 // other message processing a time to run. In particular
1583 // this avoids a delay in hiding the IME if it was
1584 // currently shown, because doing that may involve
1585 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001586 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001587 final ViewTreeObserver.OnDrawListener listener = this;
1588 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001589 public void run() {
1590 if (mWorkspace != null &&
1591 mWorkspace.getViewTreeObserver() != null) {
1592 mWorkspace.getViewTreeObserver().
1593 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001594 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001595 }
1596 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001597 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001598 }
1599 });
1600 }
1601 clearTypedText();
1602 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001603 }
1604
Winson Chunga6945242014-01-08 14:04:34 -08001605 public DragLayer getDragLayer() {
1606 return mDragLayer;
1607 }
1608
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001609 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001610 return mAppsView;
1611 }
1612
Hyunyoung Song3f471442015-04-08 19:01:34 -07001613 public WidgetsContainerView getWidgetsView() {
1614 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001615 }
1616
Winson Chunga6945242014-01-08 14:04:34 -08001617 public Workspace getWorkspace() {
1618 return mWorkspace;
1619 }
1620
Sunny Goyal6178f132016-07-11 17:30:03 -07001621 public View getQsbContainer() {
1622 return mQsbContainer;
1623 }
1624
Winson Chunga6945242014-01-08 14:04:34 -08001625 public Hotseat getHotseat() {
1626 return mHotseat;
1627 }
1628
Jorim Jaggid017f882014-01-14 17:08:48 -08001629 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001630 return mOverviewPanel;
1631 }
1632
Sunny Goyal47328fd2016-05-25 18:56:41 -07001633 public DropTargetBar getDropTargetBar() {
1634 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001635 }
1636
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001637 public LauncherAppWidgetHost getAppWidgetHost() {
1638 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001639 }
Romain Guycbb89e42009-06-08 15:52:54 -07001640
Winson Chunga9abd0e2010-10-27 17:18:37 -07001641 public LauncherModel getModel() {
1642 return mModel;
1643 }
1644
Adam Cohen79d90c52016-04-22 13:29:20 -07001645 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001646 return mSharedPrefs;
1647 }
1648
Adam Cohen2e6da152015-05-06 11:42:25 -07001649 public DeviceProfile getDeviceProfile() {
1650 return mDeviceProfile;
1651 }
1652
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653 @Override
1654 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001655 long startTime = 0;
1656 if (DEBUG_RESUME_TIME) {
1657 startTime = System.currentTimeMillis();
1658 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001659 super.onNewIntent(intent);
1660
Winson15f8b172015-08-19 11:02:39 -07001661 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1662 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1663 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001664
1665 // Check this condition before handling isActionMain, as this will get reset.
1666 boolean shouldMoveToDefaultScreen = alreadyOnHome &&
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001667 mState == State.WORKSPACE && AbstractFloatingView.getTopOpenView(this) == null;
Sunny Goyal85313732016-09-28 12:00:07 -07001668
Winson15f8b172015-08-19 11:02:39 -07001669 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1670 if (isActionMain) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001671 if (mWorkspace == null) {
1672 // Can be cases where mWorkspace is null, this prevents a NPE
1673 return;
1674 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001675 // In all these cases, only animate if we're already on home
1676 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001677
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001678 AbstractFloatingView.closeAllOpenViews(this, alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001679 exitSpringLoadedDragMode();
1680
1681 // If we are already on home, then just animate back to the workspace,
1682 // otherwise, just wait until onResume to set the state back to Workspace
1683 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001684 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001685 } else {
1686 mOnResumeState = State.WORKSPACE;
1687 }
1688
1689 final View v = getWindow().peekDecorView();
1690 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001691 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001692 INPUT_METHOD_SERVICE);
1693 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1694 }
1695
Winson Chungb745afb2015-03-02 11:51:23 -08001696 // Reset the apps view
1697 if (!alreadyOnHome && mAppsView != null) {
1698 mAppsView.scrollToTop();
1699 }
1700
Hyunyoung Song3f471442015-04-08 19:01:34 -07001701 // Reset the widgets view
1702 if (!alreadyOnHome && mWidgetsView != null) {
1703 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001704 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001705
Adam Cohen9211d422014-10-07 18:14:53 -07001706 if (mLauncherCallbacks != null) {
1707 mLauncherCallbacks.onHomeIntent();
1708 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001709 }
Adam Cohened307df2013-10-02 09:37:31 -07001710
Adam Cohen9211d422014-10-07 18:14:53 -07001711 if (mLauncherCallbacks != null) {
1712 mLauncherCallbacks.onNewIntent(intent);
1713 }
Winson15f8b172015-08-19 11:02:39 -07001714
1715 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1716 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1717 // animation.
1718 if (isActionMain) {
Sunny Goyal85313732016-09-28 12:00:07 -07001719 boolean callbackAllowsMoveToDefaultScreen = mLauncherCallbacks != null ?
Ivan Lee667d6882015-09-03 10:16:07 -06001720 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Sunny Goyal85313732016-09-28 12:00:07 -07001721 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()
1722 && callbackAllowsMoveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001723
1724 // We use this flag to suppress noisy callbacks above custom content state
1725 // from onResume.
1726 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001727 mWorkspace.post(new Runnable() {
1728 @Override
1729 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001730 if (mWorkspace != null) {
1731 mWorkspace.moveToDefaultScreen(true);
1732 }
Winson15f8b172015-08-19 11:02:39 -07001733 }
1734 });
1735 }
1736 }
1737
1738 if (DEBUG_RESUME_TIME) {
1739 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1740 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001741 }
1742
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001743 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001744 public void onRestoreInstanceState(Bundle state) {
1745 super.onRestoreInstanceState(state);
1746 for (int page: mSynchronouslyBoundPages) {
1747 mWorkspace.restoreInstanceStateForChild(page);
1748 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749 }
1750
1751 @Override
1752 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001753 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001754 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1755 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001756
Adam Cohen21cd0022013-10-09 18:57:02 -07001757 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001758 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001759
Michael Jurka883f55b2010-10-21 15:47:14 -07001760 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001761 // We close any open folders and shortcut containers since they will not be re-opened,
1762 // and we need to make sure this state is reflected.
1763 AbstractFloatingView.closeAllOpenViews(this, false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001764
Sunny Goyal2100c782016-08-22 16:00:03 -07001765 if (mPendingRequestArgs != null) {
1766 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1767 }
1768 if (mPendingActivityResult != null) {
1769 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770 }
1771
Adam Cohen9211d422014-10-07 18:14:53 -07001772 if (mLauncherCallbacks != null) {
1773 mLauncherCallbacks.onSaveInstanceState(outState);
1774 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001775 }
1776
1777 @Override
1778 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001780
Michael Jurka9d906c72011-10-14 06:25:36 -07001781 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001782 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001783
Winson Chungcd2b0142011-06-08 16:02:26 -07001784 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001785 // It's possible to receive onDestroy after a new Launcher activity has
1786 // been created. In this case, don't interfere with the new Launcher.
1787 if (mModel.isCurrentCallbacks(this)) {
1788 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001789 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001790 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001791
Sunny Goyal745bad92016-05-02 10:54:12 -07001792 if (mRotationPrefChangeHandler != null) {
1793 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1794 }
1795
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001797 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001799 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001800 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001801 mAppWidgetHost = null;
1802
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803 TextKeyListener.getInstance().release();
1804
Tony Wickhame2217252016-03-22 16:34:23 -07001805 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1806 .removeAccessibilityStateChangeListener(this);
1807
Michael Jurka2ecf9952012-06-18 12:52:28 -07001808 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001809
1810 if (mLauncherCallbacks != null) {
1811 mLauncherCallbacks.onDestroy();
1812 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001813 }
1814
Sunny Goyalae502842016-06-17 08:43:56 -07001815 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1816 return mAccessibilityDelegate;
1817 }
1818
Adam Cohena9cf38f2011-05-02 15:36:58 -07001819 public DragController getDragController() {
1820 return mDragController;
1821 }
1822
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001823 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001824 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001825 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001826 }
1827
1828 @Override
1829 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1830 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001831 try {
1832 super.startIntentSenderForResult(intent, requestCode,
1833 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1834 } catch (IntentSender.SendIntentException e) {
1835 throw new ActivityNotFoundException();
1836 }
1837 }
1838
Winson Chung70d72102011-08-12 11:24:35 -07001839 /**
1840 * Indicates that we want global search for this activity by setting the globalSearch
1841 * argument for {@link #startSearch} to true.
1842 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001843 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001844 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001846
Karl Rosaen138a0412009-04-23 19:00:21 -07001847 if (initialQuery == null) {
1848 // Use any text typed in the launcher as the initial query
1849 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001850 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 if (appSearchData == null) {
1852 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001853 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001854 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001855
Sunny Goyal6f28e712016-09-13 14:19:29 -07001856 if (mLauncherCallbacks == null ||
1857 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1858 // Starting search from the callbacks failed. Start the default global search.
1859 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001860 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001861
1862 // We need to show the workspace after starting the search
1863 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001864 }
1865
Ian Parkinson047036e2014-09-01 15:40:53 +01001866 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001867 * Starts the global search activity. This code is a copied from SearchManager
1868 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001869 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001870 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001871 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001872 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1873 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1874 if (globalSearchActivity == null) {
1875 Log.w(TAG, "No global search activity found.");
1876 return;
1877 }
1878 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1879 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1880 intent.setComponent(globalSearchActivity);
1881 // Make sure that we have a Bundle to put source in
1882 if (appSearchData == null) {
1883 appSearchData = new Bundle();
1884 } else {
1885 appSearchData = new Bundle(appSearchData);
1886 }
Adam Cohen9211d422014-10-07 18:14:53 -07001887 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001888 if (!appSearchData.containsKey("source")) {
1889 appSearchData.putString("source", getPackageName());
1890 }
1891 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1892 if (!TextUtils.isEmpty(initialQuery)) {
1893 intent.putExtra(SearchManager.QUERY, initialQuery);
1894 }
1895 if (selectInitialQuery) {
1896 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1897 }
1898 intent.setSourceBounds(sourceBounds);
1899 try {
1900 startActivity(intent);
1901 } catch (ActivityNotFoundException ex) {
1902 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1903 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 }
1905
Yura4f93ec62014-02-04 14:15:21 +00001906 public boolean isOnCustomContent() {
1907 return mWorkspace.isOnOrMovingToCustomContent();
1908 }
1909
Winson Chung70d72102011-08-12 11:24:35 -07001910 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001911 public boolean onPrepareOptionsMenu(Menu menu) {
1912 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001913 if (mLauncherCallbacks != null) {
1914 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1915 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001916 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001917 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001919 @Override
1920 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001921 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001922 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001923 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001924 }
1925
Joe Onorato9c1289c2009-08-17 11:03:03 -04001926 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001927 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001928 }
1929
Adam Cohen517a7f52014-03-01 12:12:59 -08001930 public boolean isWorkspaceLoading() {
1931 return mWorkspaceLoading;
1932 }
1933
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001934 private void setWorkspaceLoading(boolean value) {
1935 boolean isLocked = isWorkspaceLocked();
1936 mWorkspaceLoading = value;
1937 if (isLocked != isWorkspaceLocked()) {
1938 onWorkspaceLockedChanged();
1939 }
1940 }
1941
Sunny Goyal2100c782016-08-22 16:00:03 -07001942 private void setWaitingForResult(PendingRequestArgs args) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001943 boolean isLocked = isWorkspaceLocked();
Sunny Goyal2100c782016-08-22 16:00:03 -07001944 mPendingRequestArgs = args;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001945 if (isLocked != isWorkspaceLocked()) {
1946 onWorkspaceLockedChanged();
1947 }
1948 }
1949
Adam Cohen9211d422014-10-07 18:14:53 -07001950 protected void onWorkspaceLockedChanged() {
1951 if (mLauncherCallbacks != null) {
1952 mLauncherCallbacks.onWorkspaceLockedChanged();
1953 }
1954 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001955
Sunny Goyal2100c782016-08-22 16:00:03 -07001956 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1957 LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001958 if (LOGD) {
1959 Log.d(TAG, "Adding widget from drop");
1960 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07001961 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
1962 }
1963
Sunny Goyal2100c782016-08-22 16:00:03 -07001964 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
1965 AppWidgetHostView boundWidget, LauncherAppWidgetProviderInfo appWidgetInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001966 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001967 if (appWidgetInfo.configure != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001968 setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, appWidgetInfo, info));
Michael Jurkaaf442092010-06-10 17:01:57 -07001969
Bjorn Bringert7984c942009-12-09 15:38:25 +00001970 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07001971 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
1972 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001974 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07001975 Runnable onComplete = new Runnable() {
1976 @Override
1977 public void run() {
1978 // Exit spring loaded mode if necessary after adding the widget
1979 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1980 null);
1981 }
1982 };
Sunny Goyal2100c782016-08-22 16:00:03 -07001983 completeAddAppWidget(appWidgetId, info, boundWidget, appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07001984 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 }
1986 }
Romain Guycbb89e42009-06-08 15:52:54 -07001987
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001988 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07001989 // Close any folders that may be open.
Sunny Goyal740ac7f2016-09-28 16:47:32 -07001990 AbstractFloatingView.closeAllOpenViews(this, animate);
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001991 mWorkspace.moveToCustomContentScreen(animate);
1992 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001993
1994 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
1995 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07001996 info.container = container;
1997 info.screenId = screenId;
1998 if (cell != null) {
1999 info.cellX = cell[0];
2000 info.cellY = cell[1];
2001 }
2002 info.spanX = spanX;
2003 info.spanY = spanY;
2004
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002005 switch (info.itemType) {
2006 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2007 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07002008 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002009 break;
2010 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal2100c782016-08-22 16:00:03 -07002011 processShortcutFromDrop(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002012 break;
2013 default:
2014 throw new IllegalStateException("Unknown item type: " + info.itemType);
2015 }
2016 }
2017
Adam Cohenfbba09b2011-07-18 21:43:05 -07002018 /**
2019 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002020 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002021 private void processShortcutFromDrop(PendingAddItemInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07002022 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
2023 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
2024 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002025 }
2026
Adam Cohenfbba09b2011-07-18 21:43:05 -07002027 /**
2028 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002029 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002030 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002031 AppWidgetHostView hostView = info.boundWidget;
2032 int appWidgetId;
2033 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002034 // In the case where we've prebound the widget, we remove it from the DragLayer
2035 if (LOGD) {
2036 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2037 }
2038 getDragLayer().removeView(hostView);
2039
Adam Cohened66b2b2012-01-23 17:28:51 -08002040 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002041 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002042
2043 // Clear the boundWidget so that it doesn't get destroyed.
2044 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002045 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002046 // In this case, we either need to start an activity to get permission to bind
2047 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002048 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002049 Bundle options = info.bindOptions;
2050
Sunny Goyalffe83f12014-08-14 17:39:34 -07002051 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2052 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002053 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002054 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002055 } else {
Sunny Goyal2100c782016-08-22 16:00:03 -07002056 setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, info.info, info));
Michael Jurka8b805b12012-04-18 14:23:14 -07002057 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2058 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2059 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyal2100c782016-08-22 16:00:03 -07002060 mAppWidgetManager.getUser(info.info)
Sunny Goyalffe83f12014-08-14 17:39:34 -07002061 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002062 // TODO: we need to make sure that this accounts for the options bundle.
2063 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002064 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2065 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002066 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002067 }
2068
Adam Cohendcd297f2013-06-18 13:13:40 -07002069 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002070 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002071 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002072 folderInfo.title = getText(R.string.folder_name);
2073
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002074 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002075 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2076 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002077
2078 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002079 FolderIcon newFolder =
2080 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002081 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002082 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002083 // Force measure the new folder icon
2084 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2085 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002086 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002087 }
Romain Guycbb89e42009-06-08 15:52:54 -07002088
Winsonc0b52fe2015-09-09 16:38:15 -07002089 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002090 * Unbinds the view for the specified item, and removes the item and all its children.
2091 *
2092 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002093 * @param itemInfo the {@link ItemInfo} for this view.
2094 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002095 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002096 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002097 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002098 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002099 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2100 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002101 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002102 } else {
2103 mWorkspace.removeWorkspaceItem(v);
2104 }
Winsonc0b52fe2015-09-09 16:38:15 -07002105 if (deleteFromDb) {
2106 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2107 }
2108 } else if (itemInfo instanceof FolderInfo) {
2109 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002110 if (v instanceof FolderIcon) {
2111 ((FolderIcon) v).removeListeners();
2112 }
Winsonc0b52fe2015-09-09 16:38:15 -07002113 mWorkspace.removeWorkspaceItem(v);
2114 if (deleteFromDb) {
2115 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2116 }
2117 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2118 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002119 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002120 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002121 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002122 }
2123 } else {
2124 return false;
2125 }
2126 return true;
2127 }
2128
2129 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002130 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002131 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002132 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002133 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002134 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002135 // Deleting an app widget ID is a void call but writes to disk before returning
2136 // to the caller...
2137 new AsyncTask<Void, Void, Void>() {
2138 public Void doInBackground(Void ... args) {
2139 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2140 return null;
2141 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002142 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002143 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002144 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002145 }
2146
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002147 @Override
2148 public boolean dispatchKeyEvent(KeyEvent event) {
2149 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2150 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002151 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002152 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002153 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002154 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002155 dumpState();
2156 return true;
2157 }
2158 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002159 }
2160 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2161 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002162 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002163 return true;
2164 }
2165 }
2166
2167 return super.dispatchKeyEvent(event);
2168 }
2169
Joe Onorato88ec0992009-11-19 13:16:06 -08002170 @Override
2171 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002172 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2173 return;
2174 }
2175
Sunny Goyal45478022015-06-08 16:52:41 -07002176 if (mDragController.isDragging()) {
2177 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002178 return;
2179 }
2180
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002181 AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(this);
2182 if (topView != null) {
2183 if (topView.getActiveTextView() != null) {
2184 topView.getActiveTextView().dispatchBackKey();
2185 } else {
2186 topView.close(true);
2187 }
Tony Wickham49c8d292016-07-01 11:44:34 -07002188 } else if (isAppsViewVisible()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002189 showWorkspace(true);
2190 } else if (isWidgetsViewVisible()) {
2191 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002192 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002193 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002194 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002195 mWorkspace.exitWidgetResizeMode();
2196
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002197 // Back button is a no-op here, but give at least some feedback for the button press
2198 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002199 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002200 }
2201
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002202 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002203 * Launches the intent referred by the clicked shortcut.
2204 *
2205 * @param v The view representing the clicked shortcut.
2206 */
2207 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002208 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2209 // view has detached (it's possible for this to happen if the view is removed mid touch).
2210 if (v.getWindowToken() == null) {
2211 return;
2212 }
2213
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002214 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002215 return;
2216 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002217
Adam Cohen1697b792013-09-17 19:08:21 -07002218 if (v instanceof Workspace) {
2219 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002220 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002221 }
2222 return;
2223 }
2224
2225 if (v instanceof CellLayout) {
2226 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002227 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2228 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002229 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002230 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002231 }
2232
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002233 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002234 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002235 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002236 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002237 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002238 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002239 }
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002240 } else if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2241 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002242 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002243 } else if (tag instanceof AppInfo) {
2244 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002245 } else if (tag instanceof LauncherAppWidgetInfo) {
2246 if (v instanceof PendingAppWidgetHostView) {
2247 onClickPendingWidget((PendingAppWidgetHostView) v);
2248 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002249 }
2250 }
2251
Sunny Goyal70660032015-05-14 00:07:08 -07002252 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002253 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002254 return false;
2255 }
2256
Michael Jurkaaf442092010-06-10 17:01:57 -07002257 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002258 * Event handler for the app widget view which has not fully restored.
2259 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002260 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002261 if (mIsSafeModeEnabled) {
2262 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2263 return;
2264 }
2265
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002266 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002267 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002268 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2269 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2270 // This should not happen, as we make sure that an Id is allocated during bind.
2271 return;
2272 }
2273 LauncherAppWidgetProviderInfo appWidgetInfo =
2274 mAppWidgetManager.findProvider(info.providerName, info.user);
2275 if (appWidgetInfo != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002276 setWaitingForResult(PendingRequestArgs
2277 .forWidgetInfo(info.appWidgetId, appWidgetInfo, info));
Sunny Goyalff572272014-07-23 13:58:07 -07002278
Sunny Goyald478c832016-04-01 12:04:16 -07002279 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
Sunny Goyal2100c782016-08-22 16:00:03 -07002280 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, info.appWidgetId);
Sunny Goyald478c832016-04-01 12:04:16 -07002281 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07002282 mAppWidgetManager.getUser(appWidgetInfo)
Sunny Goyald478c832016-04-01 12:04:16 -07002283 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2284 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2285 }
2286 } else {
2287 LauncherAppWidgetProviderInfo appWidgetInfo =
2288 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2289 if (appWidgetInfo != null) {
2290 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2291 }
Sunny Goyalff572272014-07-23 13:58:07 -07002292 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002293 } else if (info.installProgress < 0) {
2294 // The install has not been queued
2295 final String packageName = info.providerName.getPackageName();
2296 showBrokenAppInstallDialog(packageName,
2297 new DialogInterface.OnClickListener() {
2298 public void onClick(DialogInterface dialog, int id) {
2299 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2300 }
2301 });
2302 } else {
2303 // Download has started.
2304 final String packageName = info.providerName.getPackageName();
2305 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002306 }
2307 }
2308
Sunny Goyald478c832016-04-01 12:04:16 -07002309 private void startRestoredWidgetReconfigActivity(
2310 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002311 setWaitingForResult(PendingRequestArgs.forWidgetInfo(info.appWidgetId, provider, info));
Sunny Goyald478c832016-04-01 12:04:16 -07002312 mAppWidgetManager.startConfigActivity(provider,
2313 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2314 }
2315
Sunny Goyalff572272014-07-23 13:58:07 -07002316 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002317 * Event handler for the "grid" button that appears on the home screen, which
2318 * enters all apps mode.
2319 *
2320 * @param v The view that was clicked.
2321 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002322 protected void onClickAllAppsButton(View v) {
2323 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002324 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002325 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.TAP,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002326 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002327 showAppsView(true /* animated */, true /* updatePredictedApps */,
2328 false /* focusSearchBar */);
Winson Chung76648c52015-07-10 14:33:23 -07002329 }
2330 }
2331
2332 protected void onLongClickAllAppsButton(View v) {
2333 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2334 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002335 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.LONGPRESS,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002336 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002337 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07002338 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002339 }
2340 }
2341
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002342 private void showBrokenAppInstallDialog(final String packageName,
2343 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002344 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002345 .setTitle(R.string.abandoned_promises_title)
2346 .setMessage(R.string.abandoned_promise_explanation)
2347 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2348 .setNeutralButton(R.string.abandoned_clean_this,
2349 new DialogInterface.OnClickListener() {
2350 public void onClick(DialogInterface dialog, int id) {
2351 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2352 mWorkspace.removeAbandonedPromise(packageName, user);
2353 }
2354 })
2355 .create().show();
2356 return;
2357 }
2358
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002359 /**
2360 * Event handler for an app shortcut click.
2361 *
2362 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2363 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002364 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002365 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2366 Object tag = v.getTag();
2367 if (!(tag instanceof ShortcutInfo)) {
2368 throw new IllegalArgumentException("Input must be a Shortcut");
2369 }
2370
2371 // Open shortcut
2372 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002373
2374 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002375 if ((shortcut.isDisabled &
2376 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2377 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2378 // If the app is only disabled because of the above flags, launch activity anyway.
2379 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002380 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002381 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2382 // Use a message specific to this shortcut, if it has one.
2383 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2384 return;
2385 }
2386 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002387 int error = R.string.activity_not_available;
2388 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2389 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002390 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2391 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002392 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002393 }
2394 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2395 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002396 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002397 }
2398
Chris Wren40c5ed32014-06-24 18:24:23 -04002399 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002400 if ((v instanceof BubbleTextView)
2401 && shortcut.isPromise()
2402 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002403 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002404 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002405 new DialogInterface.OnClickListener() {
2406 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002407 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002408 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002409 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002410 return;
2411 }
2412
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002413 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002414 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002415 }
2416
Sunny Goyala7ce1662016-05-31 15:01:35 -07002417 private void startAppShortcutOrInfoActivity(View v) {
2418 ItemInfo item = (ItemInfo) v.getTag();
2419 Intent intent = item.getIntent();
2420 if (intent == null) {
2421 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002422 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002423 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002424 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002425
2426 if (success && v instanceof BubbleTextView) {
2427 mWaitingForResume = (BubbleTextView) v;
2428 mWaitingForResume.setStayPressed(true);
2429 }
2430 }
2431
2432 /**
2433 * Event handler for a folder icon click.
2434 *
2435 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2436 */
2437 protected void onClickFolderIcon(View v) {
2438 if (LOGD) Log.d(TAG, "onClickFolder");
2439 if (!(v instanceof FolderIcon)){
2440 throw new IllegalArgumentException("Input must be a FolderIcon");
2441 }
2442
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002443 Folder folder = ((FolderIcon) v).getFolder();
2444 if (!folder.isOpen() && !folder.isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002445 // Open the requested folder
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002446 folder.animateOpen();
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002447 }
Michael Jurka5130e402011-10-13 04:55:35 -07002448 }
2449
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002450 /**
2451 * Event handler for the (Add) Widgets button that appears after a long press
2452 * on the home screen.
2453 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002454 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002455 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002456 if (mIsSafeModeEnabled) {
2457 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2458 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002459 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002460 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002461 }
2462
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002463 /**
2464 * Event handler for the wallpaper picker button that appears after a long press
2465 * on the home screen.
2466 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002467 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002468 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002469 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2470 return;
2471 }
2472
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002473 String pickerPackage = getString(R.string.wallpaper_picker_package);
2474 if (TextUtils.isEmpty(pickerPackage)) {
2475 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2476 }
2477
Tony Wickham785f7a52015-08-31 17:28:32 -07002478 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2479 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002480
Sunny Goyal2100c782016-08-22 16:00:03 -07002481 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002482 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002483 .setPackage(pickerPackage)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002484 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
2485 intent.setSourceBounds(getViewBounds(v));
2486 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002487 }
2488
2489 /**
2490 * Event handler for a click on the settings button that appears after a long press
2491 * on the home screen.
2492 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002493 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002494 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002495 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2496 .setPackage(getPackageName());
2497 intent.setSourceBounds(getViewBounds(v));
2498 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002499 }
2500
Adam Cohen61f560d2013-09-30 15:58:20 -07002501 public View.OnTouchListener getHapticFeedbackTouchListener() {
2502 if (mHapticFeedbackTouchListener == null) {
2503 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002504 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002505 @Override
2506 public boolean onTouch(View v, MotionEvent event) {
2507 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2508 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2509 }
2510 return false;
2511 }
2512 };
2513 }
2514 return mHapticFeedbackTouchListener;
2515 }
2516
Tony Wickhame2217252016-03-22 16:34:23 -07002517 @Override
2518 public void onAccessibilityStateChanged(boolean enabled) {
2519 mDragLayer.onAccessibilityStateChanged(enabled);
2520 }
2521
Sunny Goyal06e21a22016-08-11 16:02:02 -07002522 public void onDragStarted() {
Adam Cohen9211d422014-10-07 18:14:53 -07002523 if (isOnCustomContent()) {
2524 // Custom content screen doesn't participate in drag and drop. If on custom
2525 // content screen, move to default.
2526 moveWorkspaceToDefaultScreen();
2527 }
Adam Cohen9211d422014-10-07 18:14:53 -07002528 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002529
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002530 /**
2531 * Called when the user stops interacting with the launcher.
2532 * This implies that the user is now on the homescreen and is not doing housekeeping.
2533 */
Adam Cohen9211d422014-10-07 18:14:53 -07002534 protected void onInteractionEnd() {
2535 if (mLauncherCallbacks != null) {
2536 mLauncherCallbacks.onInteractionEnd();
2537 }
2538 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002539
2540 /**
2541 * Called when the user starts interacting with the launcher.
2542 * The possible interactions are:
2543 * - open all apps
2544 * - reorder an app shortcut, or a widget
2545 * - open the overview mode.
2546 * This is a good time to stop doing things that only make sense
2547 * when the user is on the homescreen and not doing housekeeping.
2548 */
Adam Cohen9211d422014-10-07 18:14:53 -07002549 protected void onInteractionBegin() {
2550 if (mLauncherCallbacks != null) {
2551 mLauncherCallbacks.onInteractionBegin();
2552 }
2553 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002554
Winson Chungcd99cd32015-04-29 11:03:24 -07002555 /** Updates the interaction state. */
2556 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002557 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002558 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002559 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2560 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002561 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002562 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002563 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002564 onInteractionEnd();
2565 }
2566 }
2567
Sunny Goyala7ce1662016-05-31 15:01:35 -07002568 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002569 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002570 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2571 try {
2572 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2573 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2574 // is enabled by default on NYC.
2575 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2576 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002577
2578 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2579 String id = ((ShortcutInfo) info).getDeepShortcutId();
2580 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002581 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2582 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002583 } else {
2584 // Could be launching some bookkeeping activity
2585 startActivity(intent, optsBundle);
2586 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002587 } finally {
2588 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002589 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002590 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002591 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2592 // corresponding permission. Show the appropriate permission prompt if that
2593 // is the case.
2594 if (intent.getComponent() == null
2595 && Intent.ACTION_CALL.equals(intent.getAction())
2596 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2597 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002598
2599 setWaitingForResult(PendingRequestArgs
2600 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002601 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2602 REQUEST_PERMISSION_CALL_PHONE);
2603 } else {
2604 // No idea why this was thrown.
2605 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002606 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002607 }
2608 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002609
Sunny Goyalfe770c92016-09-27 14:38:58 -07002610 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyala7ce1662016-05-31 15:01:35 -07002611 private Bundle getActivityLaunchOptions(View v) {
2612 if (Utilities.ATLEAST_MARSHMALLOW) {
2613 int left = 0, top = 0;
2614 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2615 if (v instanceof TextView) {
2616 // Launch from center of icon, not entire view
2617 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2618 if (icon != null) {
2619 Rect bounds = icon.getBounds();
2620 left = (width - bounds.width()) / 2;
2621 top = v.getPaddingTop();
2622 width = bounds.width();
2623 height = bounds.height();
2624 }
2625 }
2626 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2627 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2628 // On L devices, we use the device default slide-up transition.
2629 // On L MR1 devices, we use a custom version of the slide-up transition which
2630 // doesn't have the delay present in the device default.
2631 return ActivityOptions.makeCustomAnimation(
2632 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2633 }
2634 return null;
2635 }
2636
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002637 private Rect getViewBounds(View v) {
2638 int[] pos = new int[2];
2639 v.getLocationOnScreen(pos);
2640 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2641 }
2642
Sunny Goyala7ce1662016-05-31 15:01:35 -07002643 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002644 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2645 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2646 return false;
2647 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002648 // Only launch using the new animation if the shortcut has not opted out (this is a
2649 // private contract between launcher and may be ignored in the future).
2650 boolean useLaunchAnimation = (v != null) &&
2651 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2652 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2653
2654 UserHandleCompat user = null;
2655 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2656 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2657 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002658 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002659
2660 // Prepare intent
2661 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2662 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002663 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002664 }
2665 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002666 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2667 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07002668 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
2669 && ((ShortcutInfo) item).promisedIntent == null) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002670 // Shortcuts need some special checks due to legacy reasons.
2671 startShortcutIntentSafely(intent, optsBundle, item);
2672 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2673 // Could be launching some bookkeeping activity
2674 startActivity(intent, optsBundle);
2675 } else {
2676 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2677 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2678 }
2679 return true;
2680 } catch (ActivityNotFoundException|SecurityException e) {
2681 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2682 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2683 }
2684 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002685 }
2686
Tony Wickhamdadb3042016-02-24 11:07:00 -08002687 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002688 public boolean dispatchTouchEvent(MotionEvent ev) {
2689 mLastDispatchTouchEventX = ev.getX();
2690 return super.dispatchTouchEvent(ev);
2691 }
2692
2693 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002694 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002695 if (!isDraggingEnabled()) return false;
2696 if (isWorkspaceLocked()) return false;
2697 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002698
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002699 if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2700 (v == mAllAppsButton && mAllAppsButton != null)) {
Winson Chung76648c52015-07-10 14:33:23 -07002701 onLongClickAllAppsButton(v);
2702 return true;
2703 }
2704
Jon Miranda379198e2016-09-23 08:54:40 -07002705 boolean fromEdgeOfScreen = mLastDispatchTouchEventX < mEdgeOfScreenThresholdPx
2706 || mLastDispatchTouchEventX > (mDeviceProfile.widthPx - mEdgeOfScreenThresholdPx);
2707
Adam Cohen1697b792013-09-17 19:08:21 -07002708 if (v instanceof Workspace) {
2709 if (!mWorkspace.isInOverviewMode()) {
Jon Miranda379198e2016-09-23 08:54:40 -07002710 if (!mWorkspace.isTouchActive() && !fromEdgeOfScreen) {
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002711 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.LONGPRESS,
2712 LauncherLogProto.Action.NONE, LauncherLogProto.WORKSPACE,
2713 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002714 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07002715 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2716 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2717 return true;
2718 } else {
2719 return false;
2720 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002721 } else {
2722 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002723 }
Adam Cohen1697b792013-09-17 19:08:21 -07002724 }
2725
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002726 CellLayout.CellInfo longClickCellInfo = null;
2727 View itemUnderLongClick = null;
2728 if (v.getTag() instanceof ItemInfo) {
2729 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002730 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002731 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07002732 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002733 }
2734
Winson Chung3d503fb2011-07-13 17:25:49 -07002735 // The hotseat touch handling does not go through Workspace, and we always allow long press
2736 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002737 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002738 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002739 // User long pressed on empty space
Adam Cohend3ecce92013-09-16 14:58:00 -07002740 if (mWorkspace.isInOverviewMode()) {
2741 mWorkspace.startReordering(v);
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002742 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.LONGPRESS,
2743 LauncherLogProto.Action.NONE, LauncherLogProto.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07002744 } else {
Jon Miranda379198e2016-09-23 08:54:40 -07002745 if (fromEdgeOfScreen) {
2746 return false;
2747 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002748 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.LONGPRESS,
2749 LauncherLogProto.Action.NONE, LauncherLogProto.WORKSPACE,
2750 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002751 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07002752 }
Jon Miranda379198e2016-09-23 08:54:40 -07002753 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2754 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002755 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07002756 final boolean isAllAppsButton =
2757 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
2758 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
2759 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05002760 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002761 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07002762 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002763 }
2764 }
2765 }
2766 return true;
2767 }
2768
Winson Chung3d503fb2011-07-13 17:25:49 -07002769 boolean isHotseatLayout(View layout) {
2770 return mHotseat != null && layout != null &&
2771 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2772 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002773
Winson Chung3d503fb2011-07-13 17:25:49 -07002774 /**
2775 * Returns the CellLayout of the specified container at the specified screen.
2776 */
Sunny Goyal92820592015-03-02 11:31:35 -08002777 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002778 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2779 if (mHotseat != null) {
2780 return mHotseat.getLayout();
2781 } else {
2782 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002783 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002784 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002785 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002786 }
2787 }
2788
Winson Chungb745afb2015-03-02 11:51:23 -08002789 /**
2790 * For overridden classes.
2791 */
Daniel Sandler843e8602010-06-07 14:59:01 -04002792 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08002793 return isAppsViewVisible();
2794 }
2795
2796 public boolean isAppsViewVisible() {
2797 return (mState == State.APPS) || (mOnResumeState == State.APPS);
2798 }
2799
2800 public boolean isWidgetsViewVisible() {
2801 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002802 }
2803
Michael Jurkae326f182011-11-21 14:05:46 -08002804 @Override
2805 public void onTrimMemory(int level) {
2806 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07002807 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2808 // The widget preview db can result in holding onto over
2809 // 3MB of memory for caching which isn't necessary.
2810 SQLiteDatabase.releaseMemory();
2811
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08002812 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07002813 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08002814 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01002815 if (mLauncherCallbacks != null) {
2816 mLauncherCallbacks.onTrimMemory(level);
2817 }
Michael Jurkae326f182011-11-21 14:05:46 -08002818 }
2819
Winson5c6bdbb2015-09-03 11:36:19 -07002820 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07002821 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07002822 }
2823
Winson5c6bdbb2015-09-03 11:36:19 -07002824 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07002825 boolean changed = mState != State.WORKSPACE ||
2826 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002827 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002828 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002829 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002830 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07002831
Michael Jurkab3e22d92011-10-31 15:58:33 -07002832 // Set focus to the AppsCustomize button
2833 if (mAllAppsButton != null) {
2834 mAllAppsButton.requestFocus();
2835 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002836 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002837
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002838 // Change the state *after* we've called all the transition code
2839 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002840
Winson Chung0f785722015-04-08 10:27:49 -07002841 if (changed) {
2842 // Send an accessibility event to announce the context change
2843 getWindow().getDecorView()
2844 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07002845 }
Winson5c6bdbb2015-09-03 11:36:19 -07002846 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01002847 }
2848
Winsone9f27272015-10-13 10:47:51 -07002849 /**
2850 * Shows the overview button.
2851 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002852 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07002853 showOverviewMode(animated, false);
2854 }
2855
2856 /**
2857 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
2858 * onto one of the overview panel buttons.
2859 */
2860 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
2861 Runnable postAnimRunnable = null;
2862 if (requestButtonFocus) {
2863 postAnimRunnable = new Runnable() {
2864 @Override
2865 public void run() {
2866 // Hitting the menu button when in touch mode does not trigger touch mode to
2867 // be disabled, so if requested, force focus on one of the overview panel
2868 // buttons.
2869 mOverviewPanel.requestFocusFromTouch();
2870 }
2871 };
2872 }
Adam Cohened307df2013-10-02 09:37:31 -07002873 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07002874 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07002875 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07002876 mState = State.WORKSPACE;
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07002877 // If animated from long press, then don't allow any of the controller in the drag
2878 // layer to intercept any remaining touch.
2879 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04002880 }
2881
Winson Chungb745afb2015-03-02 11:51:23 -08002882 /**
2883 * Shows the apps view.
2884 */
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002885 public void showAppsView(boolean animated, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07002886 boolean focusSearchBar) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07002887 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07002888 if (updatePredictedApps) {
2889 tryAndUpdatePredictedApps();
2890 }
Winson Chung76648c52015-07-10 14:33:23 -07002891 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08002892 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002893
Winson Chungb745afb2015-03-02 11:51:23 -08002894 /**
2895 * Shows the widgets view.
2896 */
2897 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002898 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07002899 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002900 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07002901 }
Winson Chung76648c52015-07-10 14:33:23 -07002902 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07002903
2904 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04002905 @Override
2906 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002907 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04002908 }
2909 });
Winson Chungb745afb2015-03-02 11:51:23 -08002910 }
2911
2912 /**
2913 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07002914 *
2915 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08002916 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07002917 // TODO: calling method should use the return value so that when {@code false} is returned
2918 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07002919 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07002920 if (!(mState == State.WORKSPACE ||
2921 mState == State.APPS_SPRING_LOADED ||
2922 mState == State.WIDGETS_SPRING_LOADED ||
2923 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07002924 return false;
2925 }
2926 if (toState != State.APPS && toState != State.WIDGETS) {
2927 return false;
2928 }
Winson Chungb745afb2015-03-02 11:51:23 -08002929
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002930 // This is a safe and supported transition to bypass spring_loaded mode.
2931 if (mExitSpringLoadedModeRunnable != null) {
2932 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2933 mExitSpringLoadedModeRunnable = null;
2934 }
2935
Winson Chungb745afb2015-03-02 11:51:23 -08002936 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07002937 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
2938 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08002939 } else {
Winson Chung006ee262015-08-03 14:40:11 -07002940 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08002941 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002942
Michael Jurkab3e22d92011-10-31 15:58:33 -07002943 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08002944 mState = toState;
Sunny Goyal740ac7f2016-09-28 16:47:32 -07002945 AbstractFloatingView.closeAllOpenViews(this);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002946
2947 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002948 getWindow().getDecorView()
2949 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07002950 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07002951 }
2952
Winson Chungcd99cd32015-04-29 11:03:24 -07002953 /**
2954 * Updates the workspace and interaction state on state change, and return the animation to this
2955 * new state.
2956 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002957 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Sunny Goyalb5e65c82016-10-26 18:32:38 -07002958 boolean animated, AnimationLayerSet layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002959 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07002960 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07002961 updateInteraction(fromState, toState);
2962 return anim;
2963 }
2964
Hyunyoung Song3f471442015-04-08 19:01:34 -07002965 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07002966 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07002967 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002968 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07002969 }
Winson Chungb745afb2015-03-02 11:51:23 -08002970
Winson Chung006ee262015-08-03 14:40:11 -07002971 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07002972 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07002973 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07002974
2975 if (isAppsViewVisible()) {
2976 mState = State.APPS_SPRING_LOADED;
2977 } else if (isWidgetsViewVisible()) {
2978 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08002979 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07002980 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08002981 } else {
2982 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07002983 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002984 }
Adam Cohen7777d962011-08-18 18:58:38 -07002985
Hyunyoung Song3f471442015-04-08 19:01:34 -07002986 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08002987 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07002988 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07002989
Hyunyoung Song06ca7562016-07-29 13:03:54 -07002990 if (mExitSpringLoadedModeRunnable != null) {
2991 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
2992 }
2993 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002994 @Override
2995 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002996 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07002997 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
2998 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002999 // Before we show workspace, hide all apps again because
3000 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3001 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003002 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003003 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003004 } else {
3005 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003006 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003007 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07003008 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003009 };
3010 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003011 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003012
Tony Wickhame0c33232016-02-08 11:37:04 -08003013 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003014 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3015 || mState == State.WIDGETS_SPRING_LOADED;
3016 }
3017
Michael Jurkad3ef3062010-11-23 16:23:58 -08003018 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003019 if (mState == State.APPS_SPRING_LOADED) {
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003020 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07003021 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003022 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003023 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003024 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3025 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003026 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003027 }
3028
Winson Chung4ac30062015-05-08 17:34:17 -07003029 /**
3030 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3031 * resumed.
3032 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003033 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003034 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003035 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003036 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003037 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003038 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003039 }
3040 }
3041 }
3042
Michael Jurkab3e22d92011-10-31 15:58:33 -07003043 void lockAllApps() {
3044 // TODO
3045 }
3046
3047 void unlockAllApps() {
3048 // TODO
3049 }
3050
Michael Jurkad7c28052012-04-27 15:43:36 -07003051 @Override
3052 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003053 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003054 final List<CharSequence> text = event.getText();
3055 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003056 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003057 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003058 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003059 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003060 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003061 } else if (mWorkspace != null) {
3062 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003063 } else {
3064 text.add(getString(R.string.all_apps_home_button_label));
3065 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003066 return result;
3067 }
3068
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003069 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003070 * If the activity is currently paused, signal that we need to run the passed Runnable
3071 * in onResume.
3072 *
3073 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003074 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3075 * wrong when we're not running, and if the activity comes back to what the configuration was
3076 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003077 *
3078 * Implementation of the method from LauncherModel.Callbacks.
3079 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003080 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003081 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003082 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003083 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003084 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003085 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003086 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003087 }
3088 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003089 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003090 return true;
3091 } else {
3092 return false;
3093 }
3094 }
3095
Michael Jurkac402cd92013-05-20 15:49:32 +02003096 private boolean waitUntilResume(Runnable run) {
3097 return waitUntilResume(run, false);
3098 }
3099
Michael Jurka1e2f4652013-07-08 18:03:46 -07003100 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003101 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003102 }
3103
Michael Jurka7607c2f2013-04-03 14:33:19 -07003104 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003105 * If the activity is currently paused, signal that we need to re-run the loader
3106 * in onResume.
3107 *
3108 * This needs to be called from incoming places where resources might have been loaded
3109 * while we are paused. That is becaues the Configuration might be wrong
3110 * when we're not running, and if it comes back to what it was when we
3111 * were paused, we are not restarted.
3112 *
3113 * Implementation of the method from LauncherModel.Callbacks.
3114 *
3115 * @return true if we are currently paused. The caller might be able to
3116 * skip some work in that case since we will come back again.
3117 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003118 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003119 public boolean setLoadOnResume() {
3120 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003121 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003122 mOnResumeNeedsLoad = true;
3123 return true;
3124 } else {
3125 return false;
3126 }
3127 }
3128
3129 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003130 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003131 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003132 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003133 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003134 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003135 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003136 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003137 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003138 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003139 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003140
Joe Onorato9c1289c2009-08-17 11:03:03 -04003141 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003142 * Clear any pending bind callbacks. This is called when is loader is planning to
3143 * perform a full rebind from scratch.
3144 */
3145 @Override
3146 public void clearPendingBinds() {
3147 mBindOnResumeCallbacks.clear();
3148 if (mPendingExecutor != null) {
3149 mPendingExecutor.markCompleted();
3150 mPendingExecutor = null;
3151 }
3152 }
3153
3154 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003155 * Refreshes the shortcuts shown on the workspace.
3156 *
3157 * Implementation of the method from LauncherModel.Callbacks.
3158 */
3159 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003160 if (LauncherAppState.PROFILE_STARTUP) {
3161 Trace.beginSection("Starting page bind");
3162 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003163 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003164
Winson Chungd64d1762013-08-20 14:37:16 -07003165 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003166 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003167 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003168
Winson Chung3d503fb2011-07-13 17:25:49 -07003169 if (mHotseat != null) {
3170 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003171 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003172 if (LauncherAppState.PROFILE_STARTUP) {
3173 Trace.endSection();
3174 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003175 }
3176
Adam Cohendcd297f2013-06-18 13:13:40 -07003177 @Override
3178 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003179 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003180 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3181 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003182 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3183 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3184 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003185 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3186 // If there are no screens, we need to have an empty screen
3187 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003188 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003189 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003190
3191 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003192 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003193 if (hasCustomContentToLeft()) {
3194 mWorkspace.createCustomContentContainer();
3195 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003196 }
Winsonc7d2e832016-07-28 12:24:55 -07003197
3198 // After we have added all the screens, if the wallpaper was locked to the default state,
3199 // then notify to indicate that it can be released and a proper wallpaper offset can be
3200 // computed before the next layout
3201 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07003202 }
3203
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003204 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003205 int count = orderedScreenIds.size();
3206 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003207 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003208 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003209 // No need to bind the first screen, as its always bound.
3210 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3211 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003212 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003213 }
3214
Winson Chungd64d1762013-08-20 14:37:16 -07003215 public void bindAppsAdded(final ArrayList<Long> newScreens,
3216 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003217 final ArrayList<ItemInfo> addAnimated,
3218 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003219 Runnable r = new Runnable() {
3220 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003221 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003222 }
3223 };
3224 if (waitUntilResume(r)) {
3225 return;
3226 }
3227
Winson Chungd64d1762013-08-20 14:37:16 -07003228 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003229 if (newScreens != null) {
3230 bindAddScreens(newScreens);
3231 }
Winson Chungd64d1762013-08-20 14:37:16 -07003232
3233 // We add the items without animation on non-visible pages, and with
3234 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003235 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003236 bindItems(addNotAnimated, 0,
3237 addNotAnimated.size(), false);
3238 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003239 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003240 bindItems(addAnimated, 0,
3241 addAnimated.size(), true);
3242 }
Winson Chungc58497e2013-09-03 17:48:37 -07003243
Winson Chung87412982013-10-03 18:34:14 -07003244 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003245 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003246
Winson Chungb745afb2015-03-02 11:51:23 -08003247 if (addedApps != null && mAppsView != null) {
3248 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003249 }
Winson Chungd64d1762013-08-20 14:37:16 -07003250 }
3251
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003252 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003253 * Bind the items start-end from the list.
3254 *
3255 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003256 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003257 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003258 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3259 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003260 Runnable r = new Runnable() {
3261 public void run() {
3262 bindItems(shortcuts, start, end, forceAnimateIcons);
3263 }
3264 };
3265 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003266 return;
3267 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003268
Winson Chungf0c6ae02012-03-21 16:10:31 -07003269 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003270 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3271 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003272 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003273 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003274 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003275 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003276 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003277
3278 // Short circuit if we are loading dock items for a configuration which has no dock
3279 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3280 mHotseat == null) {
3281 continue;
3282 }
3283
Sunny Goyal639e9062015-08-19 19:17:06 -07003284 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003285 switch (item.itemType) {
3286 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3287 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003288 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003289 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003290 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003291 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003292 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003293 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003294 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003295 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003296 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003297 default:
3298 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003299 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003300
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003301 /*
3302 * Remove colliding items.
3303 */
3304 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3305 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3306 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3307 View v = cl.getChildAt(item.cellX, item.cellY);
3308 Object tag = v.getTag();
3309 String desc = "Collision while binding workspace item: " + item
3310 + ". Collides with " + tag;
3311 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3312 throw (new RuntimeException(desc));
3313 } else {
3314 Log.d(TAG, desc);
3315 LauncherModel.deleteItemFromDatabase(this, item);
3316 continue;
3317 }
3318 }
3319 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003320 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3321 item.cellY, 1, 1);
3322 if (animateIcons) {
3323 // Animate all the applications up now
3324 view.setAlpha(0f);
3325 view.setScaleX(0f);
3326 view.setScaleY(0f);
3327 bounceAnims.add(createNewAppBounceAnimation(view, i));
3328 newShortcutsScreenId = item.screenId;
3329 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003330 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003331
Winson Chung997a9232013-07-24 15:33:46 -07003332 if (animateIcons) {
3333 // Animate to the correct page
3334 if (newShortcutsScreenId > -1) {
3335 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003336 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003337 final Runnable startBounceAnimRunnable = new Runnable() {
3338 public void run() {
3339 anim.playTogether(bounceAnims);
3340 anim.start();
3341 }
3342 };
Winson Chung997a9232013-07-24 15:33:46 -07003343 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003344 // We post the animation slightly delayed to prevent slowdowns
3345 // when we are loading right after we return to launcher.
3346 mWorkspace.postDelayed(new Runnable() {
3347 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003348 if (mWorkspace != null) {
3349 mWorkspace.snapToPage(newScreenIndex);
3350 mWorkspace.postDelayed(startBounceAnimRunnable,
3351 NEW_APPS_ANIMATION_DELAY);
3352 }
Winson Chung94d67682013-09-25 16:29:40 -07003353 }
3354 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003355 } else {
3356 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003357 }
3358 }
Winson Chung64359a52013-07-08 17:17:08 -07003359 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003360 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003361 }
3362
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003363 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3364 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3365 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003366 view.updateAppWidget(null);
3367 view.setOnClickListener(this);
3368 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003369 mWorkspace.requestLayout();
3370 }
3371
Joe Onorato9c1289c2009-08-17 11:03:03 -04003372 /**
3373 * Add the views for a widget to the workspace.
3374 *
3375 * Implementation of the method from LauncherModel.Callbacks.
3376 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003377 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003378 Runnable r = new Runnable() {
3379 public void run() {
3380 bindAppWidget(item);
3381 }
3382 };
3383 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003384 return;
3385 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003386
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003387 if (mIsSafeModeEnabled) {
3388 bindSafeModeWidget(item);
3389 return;
3390 }
3391
Daniel Sandler843e8602010-06-07 14:59:01 -04003392 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3393 if (DEBUG_WIDGETS) {
3394 Log.d(TAG, "bindAppWidget: " + item);
3395 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003396
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003397 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003398
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003399 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3400 // If the provider is not ready, bind as a pending widget.
3401 appWidgetInfo = null;
3402 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3403 // The widget id is not valid. Try to find the widget based on the provider info.
3404 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3405 } else {
3406 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3407 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003408
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003409 // If the provider is ready, but the width is not yet restored, try to restore it.
3410 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3411 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003412 if (appWidgetInfo == null) {
3413 if (DEBUG_WIDGETS) {
3414 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3415 + " belongs to component " + item.providerName
Hyunyoung Song0de01172016-10-05 16:27:48 -07003416 + ", as the provider is null");
Sunny Goyalff572272014-07-23 13:58:07 -07003417 }
3418 LauncherModel.deleteItemFromDatabase(this, item);
3419 return;
3420 }
Sunny Goyalff572272014-07-23 13:58:07 -07003421
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003422 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003423 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003424 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3425 // Id has not been allocated yet. Allocate a new id.
3426 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3427 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003428
Sunny Goyald478c832016-04-01 12:04:16 -07003429 // Also try to bind the widget. If the bind fails, the user will be shown
3430 // a click to setup UI, which will ask for the bind permission.
3431 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3432 pendingInfo.spanX = item.spanX;
3433 pendingInfo.spanY = item.spanY;
3434 pendingInfo.minSpanX = item.minSpanX;
3435 pendingInfo.minSpanY = item.minSpanY;
3436 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003437
3438 boolean isDirectConfig =
3439 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3440 if (isDirectConfig && item.bindOptions != null) {
3441 Bundle newOptions = item.bindOptions.getExtras();
3442 if (options != null) {
3443 newOptions.putAll(options);
3444 }
3445 options = newOptions;
3446 }
Sunny Goyald478c832016-04-01 12:04:16 -07003447 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3448 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003449
Sunny Goyal86df1382016-08-10 15:03:22 -07003450 // We tried to bind once. If we were not able to bind, we would need to
3451 // go through the permission dialog, which means we cannot skip the config
3452 // activity.
3453 item.bindOptions = null;
3454 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3455
Sunny Goyald478c832016-04-01 12:04:16 -07003456 // Bind succeeded
3457 if (success) {
3458 // If the widget has a configure activity, it is still needs to set it up,
3459 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003460 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003461 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3462 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003463 }
Sunny Goyald478c832016-04-01 12:04:16 -07003464
3465 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003466 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003467 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003468 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003469 // The widget was marked as UI not ready, but there is no configure activity to
3470 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003471 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3472 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003473 }
Sunny Goyalff572272014-07-23 13:58:07 -07003474 }
3475
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003476 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003477 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003478 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3479 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003480 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003481
Sunny Goyal56c73602015-09-25 12:55:01 -07003482 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003483 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003484 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003485 deleteWidgetInfo(item);
3486 return;
3487 }
3488
Sunny Goyal233ee962015-08-03 13:05:01 -07003489 item.minSpanX = appWidgetInfo.minSpanX;
3490 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003491 addAppWidgetToWorkspace(
3492 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3493 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003494 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003495 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003496 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003497 view.updateAppWidget(null);
3498 view.setOnClickListener(this);
3499 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003500 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003501 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003502
Daniel Sandler843e8602010-06-07 14:59:01 -04003503 if (DEBUG_WIDGETS) {
3504 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3505 + (SystemClock.uptimeMillis()-start) + "ms");
3506 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003507 }
3508
Sunny Goyalff572272014-07-23 13:58:07 -07003509 /**
3510 * Restores a pending widget.
3511 *
3512 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003513 */
Sunny Goyald478c832016-04-01 12:04:16 -07003514 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003515 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3516 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3517 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003518 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003519 }
3520
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003521 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003522 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003523
3524 mWorkspace.reinflateWidgetsIfNecessary();
3525 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003526 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003527 }
3528
Adam Cohen1462de32012-07-24 22:34:36 -07003529 public void onPageBoundSynchronously(int page) {
3530 mSynchronouslyBoundPages.add(page);
3531 }
3532
Sunny Goyal527c7d32015-08-28 15:19:36 -07003533 @Override
3534 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3535 if (mPendingExecutor != null) {
3536 mPendingExecutor.markCompleted();
3537 }
3538 mPendingExecutor = executor;
3539 executor.attachTo(this);
3540 }
3541
3542 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3543 if (mPendingExecutor == executor) {
3544 mPendingExecutor = null;
3545 }
3546 }
3547
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003548 @Override
3549 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3550 Runnable r = new Runnable() {
3551 public void run() {
3552 finishFirstPageBind(executor);
3553 }
3554 };
3555 if (waitUntilResume(r)) {
3556 return;
3557 }
3558
3559 Runnable onComplete = new Runnable() {
3560 @Override
3561 public void run() {
3562 if (executor != null) {
3563 executor.onLoadAnimationCompleted();
3564 }
3565 }
3566 };
3567 if (mDragLayer.getAlpha() < 1) {
3568 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3569 } else {
3570 onComplete.run();
3571 }
3572 }
3573
Joe Onorato9c1289c2009-08-17 11:03:03 -04003574 /**
3575 * Callback saying that there aren't any more items to bind.
3576 *
3577 * Implementation of the method from LauncherModel.Callbacks.
3578 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003579 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003580 Runnable r = new Runnable() {
3581 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003582 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003583 }
3584 };
3585 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003586 return;
3587 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003588 if (LauncherAppState.PROFILE_STARTUP) {
3589 Trace.beginSection("Page bind completed");
3590 }
Adam Cohen1462de32012-07-24 22:34:36 -07003591 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003592
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003593 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003594
Sunny Goyal2100c782016-08-22 16:00:03 -07003595 if (mPendingActivityResult != null) {
3596 handleActivityResult(mPendingActivityResult.requestCode,
3597 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3598 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003599 }
3600
Sunny Goyal756adbc2015-04-16 15:20:51 -07003601 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003602
3603 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003604 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003605 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003606 if (LauncherAppState.PROFILE_STARTUP) {
3607 Trace.endSection();
3608 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003609 }
3610
Winson Chunga2413752012-04-03 14:22:34 -07003611 private boolean canRunNewAppsAnimation() {
3612 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003613 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003614 }
3615
Winson Chungc9168342013-06-26 14:54:55 -07003616 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003617 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003618 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3619 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003620 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003621 return bounceAnim;
3622 }
3623
Adam Cohen27772732013-11-11 14:00:56 +00003624 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07003625 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00003626 }
3627
Tony Wickham55616cd2015-09-23 14:55:17 -07003628 public int getSearchBarHeight() {
3629 if (mLauncherCallbacks != null) {
3630 return mLauncherCallbacks.getSearchBarHeight();
3631 }
3632 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
3633 }
3634
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003635 /**
Winson Chungbb785c62015-05-05 10:05:08 -07003636 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
3637 * multiple calls to bind the same list.)
3638 */
3639 @Thunk ArrayList<AppInfo> mTmpAppsList;
3640 private Runnable mBindAllApplicationsRunnable = new Runnable() {
3641 public void run() {
3642 bindAllApplications(mTmpAppsList);
3643 mTmpAppsList = null;
3644 }
3645 };
3646
3647 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003648 * Add the icons for all apps.
3649 *
3650 * Implementation of the method from LauncherModel.Callbacks.
3651 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003652 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07003653 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
3654 mTmpAppsList = apps;
3655 return;
3656 }
3657
Winson Chungb745afb2015-03-02 11:51:23 -08003658 if (mAppsView != null) {
3659 mAppsView.setApps(apps);
3660 }
Adam Cohen9211d422014-10-07 18:14:53 -07003661 if (mLauncherCallbacks != null) {
3662 mLauncherCallbacks.bindAllApplications(apps);
3663 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003664 }
3665
3666 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003667 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
3668 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
3669 */
3670 @Override
3671 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
3672 mDeepShortcutMap = deepShortcutMapCopy;
3673 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
3674 }
3675
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003676 public List<String> getShortcutIdsForItem(ItemInfo info) {
3677 if (!DeepShortcutManager.supportsShortcuts(info)) {
3678 return Collections.EMPTY_LIST;
3679 }
3680 ComponentName component = info.getTargetComponent();
Hyunyoung Songd3bf9802016-08-29 14:43:53 -07003681 if (component == null) {
3682 return Collections.EMPTY_LIST;
3683 }
3684
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003685 List<String> ids = mDeepShortcutMap.get(new ComponentKey(component, info.user));
3686 return ids == null ? Collections.EMPTY_LIST : ids;
3687 }
3688
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003689 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003690 * A package was updated.
3691 *
3692 * Implementation of the method from LauncherModel.Callbacks.
3693 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003694 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003695 Runnable r = new Runnable() {
3696 public void run() {
3697 bindAppsUpdated(apps);
3698 }
3699 };
3700 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003701 return;
3702 }
3703
Winson Chungb745afb2015-03-02 11:51:23 -08003704 if (mAppsView != null) {
3705 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07003706 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003707 }
3708
Sunny Goyal4390ace2014-10-13 11:33:11 -07003709 @Override
3710 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
3711 Runnable r = new Runnable() {
3712 public void run() {
3713 bindWidgetsRestored(widgets);
3714 }
3715 };
3716 if (waitUntilResume(r)) {
3717 return;
3718 }
3719 mWorkspace.widgetsRestored(widgets);
3720 }
3721
Joe Onorato9c1289c2009-08-17 11:03:03 -04003722 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003723 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003724 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003725 *
3726 * @param updated list of shortcuts which have changed.
3727 * @param removed list of shortcuts which were deleted in the background. This can happen when
3728 * an app gets removed from the system or some of its components are no longer
3729 * available.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003730 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07003731 @Override
3732 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
3733 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003734 Runnable r = new Runnable() {
3735 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003736 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003737 }
3738 };
3739 if (waitUntilResume(r)) {
3740 return;
3741 }
3742
Sunny Goyal4390ace2014-10-13 11:33:11 -07003743 if (!updated.isEmpty()) {
3744 mWorkspace.updateShortcuts(updated);
3745 }
3746
3747 if (!removed.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003748 HashSet<ComponentName> removedComponents = new HashSet<>();
3749 HashSet<ShortcutKey> removedDeepShortcuts = new HashSet<>();
3750
Sunny Goyal4390ace2014-10-13 11:33:11 -07003751 for (ShortcutInfo si : removed) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003752 if (si.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07003753 removedDeepShortcuts.add(ShortcutKey.fromShortcutInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003754 } else {
3755 removedComponents.add(si.getTargetComponent());
3756 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003757 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003758
3759 if (!removedComponents.isEmpty()) {
3760 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(removedComponents, user);
3761 mWorkspace.removeItemsByMatcher(matcher);
3762 mDragController.onAppsRemoved(matcher);
3763 }
3764
3765 if (!removedDeepShortcuts.isEmpty()) {
3766 ItemInfoMatcher matcher = ItemInfoMatcher.ofShortcutKeys(removedDeepShortcuts);
3767 mWorkspace.removeItemsByMatcher(matcher);
3768 mDragController.onAppsRemoved(matcher);
3769 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003770 }
3771 }
3772
3773 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003774 * Update the state of a package, typically related to install state.
3775 *
3776 * Implementation of the method from LauncherModel.Callbacks.
3777 */
Sunny Goyale755d462014-07-22 13:48:29 -07003778 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07003779 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
3780 Runnable r = new Runnable() {
3781 public void run() {
3782 bindRestoreItemsChange(updates);
3783 }
3784 };
3785 if (waitUntilResume(r)) {
3786 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003787 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05003788
Sunny Goyal756adbc2015-04-16 15:20:51 -07003789 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07003790 }
3791
3792 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003793 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07003794 * in addition to specific applications to remove, the reason being that
3795 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003796 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07003797 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003798 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07003799 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003800 public void bindWorkspaceComponentsRemoved(
3801 final HashSet<String> packageNames, final HashSet<ComponentName> components,
3802 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07003803 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003804 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003805 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07003806 }
Winson Chungd64d1762013-08-20 14:37:16 -07003807 };
3808 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003809 return;
3810 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003811 if (!packageNames.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003812 ItemInfoMatcher matcher = ItemInfoMatcher.ofPackages(packageNames, user);
3813 mWorkspace.removeItemsByMatcher(matcher);
3814 mDragController.onAppsRemoved(matcher);
3815
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003816 }
3817 if (!components.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003818 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(components, user);
3819 mWorkspace.removeItemsByMatcher(matcher);
3820 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003821 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003822 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003823
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003824 @Override
3825 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
3826 Runnable r = new Runnable() {
3827 public void run() {
3828 bindAppInfosRemoved(appInfos);
3829 }
3830 };
3831 if (waitUntilResume(r)) {
3832 return;
Joe Onorato36115782010-06-17 13:28:48 -04003833 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003834
Winson Chungdf95eb12013-10-16 14:57:07 -07003835 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08003836 if (mAppsView != null) {
3837 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07003838 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003839 }
Joe Onoratobe386092009-11-17 17:32:16 -08003840
Sunny Goyald164b7f2016-10-12 20:49:31 -07003841 private Runnable mBindAllWidgetsRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003842 public void run() {
Sunny Goyald164b7f2016-10-12 20:49:31 -07003843 bindAllWidgets(mAllWidgets);
Winson Chung83892cc2013-05-01 16:53:33 -07003844 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003845 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07003846
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003847 @Override
Sunny Goyald164b7f2016-10-12 20:49:31 -07003848 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> allWidgets) {
3849 if (waitUntilResume(mBindAllWidgetsRunnable, true)) {
3850 mAllWidgets = allWidgets;
Winson Chung83892cc2013-05-01 16:53:33 -07003851 return;
3852 }
3853
Sunny Goyald164b7f2016-10-12 20:49:31 -07003854 if (mWidgetsView != null && allWidgets != null) {
3855 mWidgetsView.setWidgets(allWidgets);
3856 mAllWidgets = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003857 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003858 }
3859
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003860 @Override
3861 public void notifyWidgetProvidersChanged() {
3862 if (mWorkspace.getState().shouldUpdateWidget) {
3863 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
3864 }
3865 }
3866
Winson Chung400438b2011-01-16 17:53:48 -08003867 private int mapConfigurationOriActivityInfoOri(int configOri) {
3868 final Display d = getWindowManager().getDefaultDisplay();
3869 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3870 switch (d.getRotation()) {
3871 case Surface.ROTATION_0:
3872 case Surface.ROTATION_180:
3873 // We are currently in the same basic orientation as the natural orientation
3874 naturalOri = configOri;
3875 break;
3876 case Surface.ROTATION_90:
3877 case Surface.ROTATION_270:
3878 // We are currently in the other basic orientation to the natural orientation
3879 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3880 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3881 break;
3882 }
3883
3884 int[] oriMap = {
3885 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3886 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3887 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3888 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3889 };
3890 // Since the map starts at portrait, we need to offset if this device's natural orientation
3891 // is landscape.
3892 int indexOffset = 0;
3893 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3894 indexOffset = 1;
3895 }
3896 return oriMap[(d.getRotation() + indexOffset) % 4];
3897 }
Adam Cohen446e9402011-09-15 18:21:21 -07003898
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003899 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07003900 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003901 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003902 if (Utilities.ATLEAST_JB_MR2) {
3903 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
3904 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08003905 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3906 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08003907 }
Winson Chung4b919f82012-05-01 10:44:08 -07003908 }
3909 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003910
Winson Chung4b919f82012-05-01 10:44:08 -07003911 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07003912 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07003913 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003914 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003915 } else {
3916 mHandler.postDelayed(new Runnable() {
3917 public void run() {
3918 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3919 }
Sunny Goyal756cd262015-08-20 12:33:21 -07003920 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07003921 }
Winson Chung4b919f82012-05-01 10:44:08 -07003922 }
Winson Chung400438b2011-01-16 17:53:48 -08003923 }
3924
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003925 private void markAppsViewShown() {
3926 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3927 return;
3928 }
3929 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
3930 }
3931
3932 private boolean shouldShowDiscoveryBounce() {
3933 if (mState != mState.WORKSPACE) {
3934 return false;
3935 }
3936 if (mLauncherCallbacks != null && mLauncherCallbacks.shouldShowDiscoveryBounce()) {
3937 return true;
3938 }
3939 if (!mIsResumeFromActionScreenOff) {
3940 return false;
3941 }
3942 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
3943 return false;
3944 }
3945 return true;
3946 }
3947
Winson Chung5ffd51d2015-06-25 11:36:50 -07003948 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003949 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00003950 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07003951 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003952 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003953 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003954 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
3955 if (userHandle != null) {
3956 user = UserHandleCompat.fromUser(userHandle);
3957 }
3958 }
3959 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07003960 }
3961
3962 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003963 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07003964 if (user == null) {
3965 user = UserHandleCompat.myUserHandle();
3966 }
3967
3968 // Called from search suggestion, add the profile extra to the intent to ensure that we
3969 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01003970 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003971 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01003972 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00003973 return null;
3974 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07003975 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00003976 }
3977
Winson Chung5ffd51d2015-06-25 11:36:50 -07003978 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00003979 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
3980 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07003981 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01003982 UserHandleCompat.myUserHandle());
3983 }
3984
Winson Chung5ffd51d2015-06-25 11:36:50 -07003985 protected void moveWorkspaceToDefaultScreen() {
3986 mWorkspace.moveToDefaultScreen(false);
3987 }
3988
Winson Chung80baf5a2010-08-09 16:03:15 -07003989 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003990 * Returns a FastBitmapDrawable with the icon, accurately sized.
3991 */
3992 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
3993 FastBitmapDrawable d = new FastBitmapDrawable(icon);
3994 d.setFilterBitmap(true);
3995 resizeIconDrawable(d);
3996 return d;
3997 }
3998
3999 /**
4000 * Resizes an icon drawable to the correct icon size.
4001 */
Winson5fbe0742015-09-30 15:33:00 -07004002 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004003 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004004 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004005 }
4006
4007 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004008 * Prints out out state for debugging.
4009 */
4010 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004011 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004012 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
Sunny Goyal2100c782016-08-22 16:00:03 -07004013 Log.d(TAG, "mPendingRequestArgs=" + mPendingRequestArgs);
4014 Log.d(TAG, "mPendingActivityResult=" + mPendingActivityResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004015 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004016 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004017
Daniel Sandler325dc232013-06-05 22:57:57 -04004018 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004019 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004020
4021 @Override
4022 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4023 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004024 // Dump workspace
4025 writer.println(prefix + "Workspace Items");
4026 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4027 writer.println(prefix + " Homescreen " + i);
4028
4029 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4030 for (int j = 0; j < layout.getChildCount(); j++) {
4031 Object tag = layout.getChildAt(j).getTag();
4032 if (tag != null) {
4033 writer.println(prefix + " " + tag.toString());
4034 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004035 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004036 }
Sunny Goyala1365452015-10-01 15:46:24 -07004037
4038 writer.println(prefix + " Hotseat");
4039 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4040 for (int j = 0; j < layout.getChildCount(); j++) {
4041 Object tag = layout.getChildAt(j).getTag();
4042 if (tag != null) {
4043 writer.println(prefix + " " + tag.toString());
4044 }
4045 }
4046
Sunny Goyal713edfc2016-05-06 09:58:34 -07004047 try {
4048 FileLog.flushAll(writer);
4049 } catch (Exception e) {
4050 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004051 }
4052
Adam Cohen9211d422014-10-07 18:14:53 -07004053 if (mLauncherCallbacks != null) {
4054 mLauncherCallbacks.dump(prefix, fd, writer, args);
4055 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004056 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004057
Sunny Goyal66b24572016-09-21 15:57:55 -07004058 @Override
4059 @TargetApi(Build.VERSION_CODES.N)
4060 public void onProvideKeyboardShortcuts(
4061 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
4062
4063 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
4064 if (mState == State.WORKSPACE) {
4065 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
4066 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
4067 }
4068 View currentFocus = getCurrentFocus();
4069 if (new CustomActionsPopup(this, currentFocus).canShow()) {
4070 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
4071 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
4072 }
4073 if (currentFocus instanceof BubbleTextView &&
4074 ((BubbleTextView) currentFocus).hasDeepShortcuts()) {
4075 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
4076 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
4077 }
4078 if (!shortcutInfos.isEmpty()) {
4079 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
4080 }
4081
4082 super.onProvideKeyboardShortcuts(data, menu, deviceId);
4083 }
4084
4085 @Override
4086 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
4087 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
4088 switch (keyCode) {
4089 case KeyEvent.KEYCODE_A:
4090 if (mState == State.WORKSPACE) {
4091 showAppsView(true, true, false);
4092 return true;
4093 }
4094 break;
4095 case KeyEvent.KEYCODE_S: {
4096 View focusedView = getCurrentFocus();
4097 if (focusedView instanceof BubbleTextView
4098 && focusedView.getTag() instanceof ItemInfo
4099 && mAccessibilityDelegate.performAction(focusedView,
4100 (ItemInfo) focusedView.getTag(),
4101 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
Sunny Goyal740ac7f2016-09-28 16:47:32 -07004102 DeepShortcutsContainer.getOpen(this).requestFocus();
Sunny Goyal66b24572016-09-21 15:57:55 -07004103 return true;
4104 }
4105 break;
4106 }
4107 case KeyEvent.KEYCODE_O:
4108 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
4109 return true;
4110 }
4111 break;
4112 }
4113 }
4114 return super.onKeyShortcut(keyCode, event);
4115 }
4116
Adam Cohen59400422014-03-05 18:07:04 -08004117 public static CustomAppWidget getCustomAppWidget(String name) {
4118 return sCustomAppWidgets.get(name);
4119 }
4120
4121 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4122 return sCustomAppWidgets;
4123 }
4124
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004125 public static Launcher getLauncher(Context context) {
4126 if (context instanceof Launcher) {
4127 return (Launcher) context;
4128 }
4129 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4130 }
4131
Sunny Goyal745bad92016-05-02 10:54:12 -07004132 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4133
4134 @Override
4135 public void onSharedPreferenceChanged(
4136 SharedPreferences sharedPreferences, String key) {
4137 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4138 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4139 if (!waitUntilResume(this, true)) {
4140 run();
4141 }
4142 }
4143 }
4144
4145 @Override
4146 public void run() {
4147 setOrientation();
4148 }
4149 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004150}