blob: b1700ccbbcf891fdd141fd41dc25731d5d92a37d [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;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070025import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000026import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Winson Chungc7450e32012-04-17 17:34:08 -070028import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040029import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080031import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080034import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080035import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040036import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070038import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040039import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070041import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070042import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070043import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070044import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070048import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070049import android.graphics.Bitmap;
50import android.graphics.Canvas;
51import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070052import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070053import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070054import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020055import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080057import android.os.Handler;
Winson Chunga2413752012-04-03 14:22:34 -070058import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040059import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070060import android.os.Trace;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070061import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.text.Selection;
63import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070064import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070066import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070067import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070068import android.view.HapticFeedbackConstants;
69import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070070import android.view.KeyboardShortcutGroup;
71import android.view.KeyboardShortcutInfo;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.MotionEvent;
74import android.view.Surface;
75import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070076import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080077import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.ViewGroup;
79import android.view.ViewTreeObserver;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080080import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070081import android.view.accessibility.AccessibilityManager;
Sunny Goyal66b24572016-09-21 15:57:55 -070082import android.view.accessibility.AccessibilityNodeInfo;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070083import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080086import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070088
Sunny Goyal651077b2014-06-30 14:15:31 -070089import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070090import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyalae502842016-06-17 08:43:56 -070091import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070092import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070093import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080094import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -070095import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010096import com.android.launcher3.compat.LauncherActivityInfoCompat;
97import com.android.launcher3.compat.LauncherAppsCompat;
98import com.android.launcher3.compat.UserHandleCompat;
99import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800100import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700101import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700102import com.android.launcher3.dragndrop.DragController;
103import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700104import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700105import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -0700106import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000107import com.android.launcher3.folder.Folder;
108import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700109import com.android.launcher3.keyboard.CustomActionsPopup;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700110import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700111import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700112import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700113import com.android.launcher3.model.WidgetsModel;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700114import com.android.launcher3.pageindicators.PageIndicator;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700115import com.android.launcher3.shortcuts.DeepShortcutManager;
Tony Wickham49c8d292016-07-01 11:44:34 -0700116import com.android.launcher3.shortcuts.DeepShortcutsContainer;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700117import com.android.launcher3.shortcuts.ShortcutKey;
Hyunyoung Song5aa27142016-07-21 11:48:37 -0700118import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal2100c782016-08-22 16:00:03 -0700119import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700120import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700121import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700122import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700123import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal2100c782016-08-22 16:00:03 -0700124import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal322d5562015-06-25 19:35:49 -0700125import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700126import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700127import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700128import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700129import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700130import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700131
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700132import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700133import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700134import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700135import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800136import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700137import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700138import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700139import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700140
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141/**
142 * Default launcher application.
143 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100144public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700145 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
146 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700147 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700148 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700149 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700151 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700152 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400153 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700156 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700157 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700158 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159
Michael Jurka8b805b12012-04-18 14:23:14 -0700160 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700161 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700162 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700163
Sunny Goyal28c6b962015-10-12 11:42:05 -0700164 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
165
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700166 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
167
Mathew Inwood876a8462013-06-14 14:12:41 +0100168 /**
169 * IntentStarter uses request codes starting with this. This must be greater than all activity
170 * request codes used internally.
171 */
172 protected static final int REQUEST_LAST = 100;
173
Michael Jurka0a457bf2012-11-19 14:05:05 -0800174 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700175 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800176 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177
Winson Chung2672ff92012-05-04 16:22:30 -0700178 // The Intent extra that defines whether to ignore the launch animation
179 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400180 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700181
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700182 public static final String ACTION_APPWIDGET_HOST_RESET =
183 "com.android.launcher3.intent.ACTION_APPWIDGET_HOST_RESET";
184
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 // Type: int
186 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700187 // Type: int
188 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700189 // Type: PendingRequestArgs
190 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
191 // Type: ActivityResultInfo
192 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700194 static final String APPS_VIEW_SHOWN = "launcher.apps_view_shown";
Adam Cohenb54a5982014-01-08 15:21:04 -0800195
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700196 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700197 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
198 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700199
Adam Cohen091440a2015-03-18 14:16:05 -0700200 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700201 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700202
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700203 private boolean mIsSafeModeEnabled;
204
Joe Onorato9c1289c2009-08-17 11:03:03 -0400205 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800206 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700207 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700208 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209
Winson Chunga2413752012-04-03 14:22:34 -0700210 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700211 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
212 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700213 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700214
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700215 private final BroadcastReceiver mUiBroadcastReceiver = new BroadcastReceiver() {
216
217 @Override
218 public void onReceive(Context context, Intent intent) {
219 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
220 closeSystemDialogs();
221 } else if (ACTION_APPWIDGET_HOST_RESET.equals(intent.getAction())) {
222 if (mAppWidgetHost != null) {
223 mAppWidgetHost.startListening();
224 }
225 }
226 }
227 };
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800228
Adam Cohen091440a2015-03-18 14:16:05 -0700229 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700230 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700231 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800232 private DragController mDragController;
Sunny Goyal6178f132016-07-11 17:30:03 -0700233 private View mQsbContainer;
Sunny Goyal322d5562015-06-25 19:35:49 -0700234
235 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700236
Sunny Goyalffe83f12014-08-14 17:39:34 -0700237 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700238 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700239
Michael Jurka0280c3b2010-09-17 15:00:07 -0700240 private int[] mTmpAddItemCellCoordinates = new int[2];
241
Sunny Goyal316490e2015-06-02 09:38:28 -0700242 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800243 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700244
Michael Jurka838a4ca2011-02-07 13:33:06 -0800245 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700246 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700247
Sunny Goyal47328fd2016-05-25 18:56:41 -0700248 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700249
250 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700251 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700252 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700253
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700254 // Main container view and the model for the widget tray screen.
255 @Thunk WidgetsContainerView mWidgetsView;
256 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700257
Winson Chung4a2afa32012-07-19 14:53:05 -0700258 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
259 // scroll issues (because the workspace may not have been measured yet) and extra work.
260 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
261 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262
263 private SpannableStringBuilder mDefaultKeySsb = null;
264
Adam Cohen091440a2015-03-18 14:16:05 -0700265 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400266
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800267 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700268 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269
Michael Jurka1e2f4652013-07-08 18:03:46 -0700270 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700271 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700272 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700273
Joe Onorato9c1289c2009-08-17 11:03:03 -0400274 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800275 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700276 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700277 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700278 private Handler mHandler = new Handler();
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700279 private boolean mIsResumeFromActionScreenOff;
Sunny Goyal1acc56a2016-09-25 21:23:25 -0700280 private boolean mHasFocus = false;
281 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400282
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700283 /** Maps launcher activity components to their list of shortcut ids. */
284 private MultiHashMap<ComponentKey, String> mDeepShortcutMap = new MultiHashMap<>();
285
Adam Cohen61f560d2013-09-30 15:58:20 -0700286 private View.OnTouchListener mHapticFeedbackTouchListener;
287
Winson Chung400438b2011-01-16 17:53:48 -0800288 // Determines how long to wait after a rotation before restoring the screen orientation to
289 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700290 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800291
Adam Cohen1462de32012-07-24 22:34:36 -0700292 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
293
Winson Chung46353de2012-02-16 14:05:10 -0800294 // We only want to get the SharedPreferences once since it does an FS stat each time we get
295 // it from the context.
296 private SharedPreferences mSharedPrefs;
297
Winson Chungf0c6ae02012-03-21 16:10:31 -0700298 // Holds the page that we need to animate to, and the icon views that we need to animate up
299 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700300 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700301 private Bitmap mFolderIconBitmap;
302 private Canvas mFolderIconCanvas;
303 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700304
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700305 private DeviceProfile mDeviceProfile;
306
Adam Cohen4b66bf32015-09-18 12:15:19 -0700307 private boolean mMoveToDefaultScreenFromNewIntent;
308
Winson Chung13eb5272015-05-11 16:30:13 -0700309 // This is set to the view that launched the activity that navigated the user away from
310 // launcher. Since there is no callback for when the activity has finished launching, enable
311 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800312 private BubbleTextView mWaitingForResume;
313
Adam Cohen59400422014-03-05 18:07:04 -0800314 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
315 new HashMap<String, CustomAppWidget>();
316
Adam Cohen59400422014-03-05 18:07:04 -0800317 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700318 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
319 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800320 }
321 }
322
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700323 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
324 // state transition. If another state transition happened during this delay,
325 // simply unregister this runnable.
326 private Runnable mExitSpringLoadedModeRunnable;
327
Adam Cohen091440a2015-03-18 14:16:05 -0700328 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700329 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700330 if (mWorkspace != null) {
331 mWorkspace.buildPageHardwareLayers();
332 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700333 }
334 };
335
Sunny Goyal2100c782016-08-22 16:00:03 -0700336 // Activity result which needs to be processed after workspace has loaded.
337 private ActivityResultInfo mPendingActivityResult;
338 /**
339 * Holds extra information required to handle a result from an external call, like
340 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
341 */
342 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800343
Hyunyoung Songaa953652016-04-19 18:30:24 -0700344 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800345
Jon Miranda379198e2016-09-23 08:54:40 -0700346 private float mLastDispatchTouchEventX = 0.0f;
347 private float mEdgeOfScreenThresholdPx = 0.0f;
348
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700349 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700350 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400351
352 @Thunk void setOrientation() {
353 if (mRotationEnabled) {
354 unlockScreenOrientation(true);
355 } else {
356 setRequestedOrientation(
357 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700358 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400359 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700360
Sunny Goyal745bad92016-05-02 10:54:12 -0700361 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700362
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 @Override
364 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700365 if (DEBUG_STRICT_MODE) {
366 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
367 .detectDiskReads()
368 .detectDiskWrites()
369 .detectNetwork() // or .detectAll() for all detectable problems
370 .penaltyLog()
371 .build());
372 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
373 .detectLeakedSqlLiteObjects()
374 .detectLeakedClosableObjects()
375 .penaltyLog()
376 .penaltyDeath()
377 .build());
378 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700379 if (LauncherAppState.PROFILE_STARTUP) {
380 Trace.beginSection("Launcher-onCreate");
381 }
Winson Chunga2413752012-04-03 14:22:34 -0700382
Adam Cohen9211d422014-10-07 18:14:53 -0700383 if (mLauncherCallbacks != null) {
384 mLauncherCallbacks.preOnCreate();
385 }
386
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400388
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400389 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700390
Adam Cohen2e6da152015-05-06 11:42:25 -0700391 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700392 mDeviceProfile = getResources().getConfiguration().orientation
393 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700394 app.getInvariantDeviceProfile().landscapeProfile
395 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700396
Sunny Goyalf7258242015-10-19 16:59:07 -0700397 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700398 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800399 mModel = app.setLauncher(this);
400 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700401 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700402
Joe Onorato41a12d22009-10-31 18:30:00 -0400403 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700404 mAllAppsController = new AllAppsTransitionController(this);
405 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700406
Sunny Goyalffe83f12014-08-14 17:39:34 -0700407 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700408
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700409 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
410 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700411
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700412 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
413 // this also ensures that any synchronous binding below doesn't re-trigger another
414 // LauncherModel load.
415 mPaused = false;
416
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700418
Jon Miranda379198e2016-09-23 08:54:40 -0700419 mEdgeOfScreenThresholdPx = getResources()
420 .getDimensionPixelSize(R.dimen.edge_of_screen_threshold);
421
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800422 setupViews();
Winson1f064272016-07-18 17:18:02 -0700423 mDeviceProfile.layout(this, false /* notifyListeners */);
Tony Wickham827cef22016-03-17 15:39:39 -0700424 mExtractedColors = new ExtractedColors();
425 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800426
Tony Wickhame2217252016-03-22 16:34:23 -0700427 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
428 .addAccessibilityStateChangeListener(this);
429
Joe Onorato7c312c12009-08-13 21:36:53 -0700430 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700431
Sunny Goyalfe770c92016-09-27 14:38:58 -0700432 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433
Sunny Goyale26d1002016-06-20 14:52:14 -0700434 if (LauncherAppState.PROFILE_STARTUP) {
435 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436 }
437
Sunny Goyal2100c782016-08-22 16:00:03 -0700438 // We only load the page synchronously if the user rotates (or triggers a
439 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700440 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
441 if (savedInstanceState != null) {
442 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
443 }
444 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700445 // If we are not binding synchronously, show a fade in animation when
446 // the first page bind completes.
447 mDragLayer.setAlpha(0);
448 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700449 // Pages bound synchronously.
450 mWorkspace.setCurrentPage(currentScreen);
451
Sunny Goyal2100c782016-08-22 16:00:03 -0700452 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 }
454
455 // For handling default keys
456 mDefaultKeySsb = new SpannableStringBuilder();
457 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800458
459 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700460 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
461 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800462
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800463 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700464 // In case we are on a device with locked rotation, we should look at preferences to check
465 // if the user has specifically allowed rotation.
466 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700467 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700468 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
469 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700470 }
471
472 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
473 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400474 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700475
Adam Cohen9211d422014-10-07 18:14:53 -0700476 if (mLauncherCallbacks != null) {
477 mLauncherCallbacks.onCreate(savedInstanceState);
478 }
479 }
480
Sunny Goyal7779d622015-06-11 16:18:39 -0700481 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700482 public void onExtractedColorsChanged() {
483 loadExtractedColorsAndColorItems();
484 }
485
486 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700487 // TODO: do this in pre-N as well, once the extraction part is complete.
Tony Wickham345bff32016-09-28 15:34:51 -0700488 if (Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700489 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700490 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700491 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham345bff32016-09-28 15:34:51 -0700492 // It's possible that All Apps is visible when this is run,
493 // so always use light status bar in that case.
494 activateLightStatusBar(isAllAppsVisible());
Tony Wickham827cef22016-03-17 15:39:39 -0700495 }
496 }
497
Tony Wickham345bff32016-09-28 15:34:51 -0700498 /**
499 * Sets the status bar to be light or not. Light status bar means dark icons.
500 * @param activate if true, make sure the status bar is light, otherwise base on wallpaper.
501 */
502 public void activateLightStatusBar(boolean activate) {
503 boolean lightStatusBar = activate
504 || mExtractedColors.getColor(ExtractedColors.STATUS_BAR_INDEX,
505 ExtractedColors.DEFAULT_DARK) == ExtractedColors.DEFAULT_LIGHT;
506 int oldSystemUiFlags = getWindow().getDecorView().getSystemUiVisibility();
507 int newSystemUiFlags = oldSystemUiFlags;
Tony Wickham93113872016-09-14 16:46:32 -0700508 if (lightStatusBar) {
Tony Wickham345bff32016-09-28 15:34:51 -0700509 newSystemUiFlags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
Tony Wickham93113872016-09-14 16:46:32 -0700510 } else {
Tony Wickham345bff32016-09-28 15:34:51 -0700511 newSystemUiFlags &= ~(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Tony Wickham93113872016-09-14 16:46:32 -0700512 }
Tony Wickham345bff32016-09-28 15:34:51 -0700513 if (newSystemUiFlags != oldSystemUiFlags) {
514 getWindow().getDecorView().setSystemUiVisibility(newSystemUiFlags);
515 }
Tony Wickham93113872016-09-14 16:46:32 -0700516 }
517
Adam Cohen9211d422014-10-07 18:14:53 -0700518 private LauncherCallbacks mLauncherCallbacks;
519
520 public void onPostCreate(Bundle savedInstanceState) {
521 super.onPostCreate(savedInstanceState);
522 if (mLauncherCallbacks != null) {
523 mLauncherCallbacks.onPostCreate(savedInstanceState);
524 }
525 }
526
Winson1f064272016-07-18 17:18:02 -0700527 public void onInsetsChanged(Rect insets) {
528 mDeviceProfile.updateInsets(insets);
529 mDeviceProfile.layout(this, true /* notifyListeners */);
530 }
531
Sunny Goyal32554d12015-12-03 15:31:25 -0800532 /**
533 * Call this after onCreate to set or clear overlay.
534 */
535 public void setLauncherOverlay(LauncherOverlay overlay) {
536 if (overlay != null) {
537 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
538 }
539 mWorkspace.setLauncherOverlay(overlay);
540 }
541
Adam Cohen9211d422014-10-07 18:14:53 -0700542 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
543 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700544 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700545 private boolean mWorkspaceImportanceStored = false;
546 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700547 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800548 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700549 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
550
551 @Override
552 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700553 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700554 return;
555 }
556 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700557 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700558 if (mWorkspace != null) {
559 mWorkspaceImportanceForAccessibility =
560 mWorkspace.getImportantForAccessibility();
561 mWorkspace.setImportantForAccessibility(
562 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
563 mWorkspaceImportanceStored = true;
564 }
565 if (mHotseat != null) {
566 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
567 mHotseat.setImportantForAccessibility(
568 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
569 mHotseatImportanceStored = true;
570 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700571 }
572
573 @Override
574 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700575 if (mWorkspaceImportanceStored && mWorkspace != null) {
576 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
577 }
578 if (mHotseatImportanceStored && mHotseat != null) {
579 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
580 }
581 mWorkspaceImportanceStored = false;
582 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700583 }
584 });
Adam Cohen9211d422014-10-07 18:14:53 -0700585 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700586 }
587
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700588 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700589 public void onLauncherProviderChange() {
590 if (mLauncherCallbacks != null) {
591 mLauncherCallbacks.onLauncherProviderChange();
592 }
593 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700594
Adam Cohen9211d422014-10-07 18:14:53 -0700595 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700596 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700597 if (mLauncherCallbacks != null) {
598 return mLauncherCallbacks.hasCustomContentToLeft();
599 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700600 return false;
601 }
602
Dave Hawkeya8881582013-09-17 15:55:33 -0600603 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500604 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600605 * {@link #addToCustomContentPage}. This will only be invoked if
606 * {@link #hasCustomContentToLeft()} is {@code true}.
607 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500608 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700609 if (mLauncherCallbacks != null) {
610 mLauncherCallbacks.populateCustomContentContainer();
611 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600612 }
613
614 /**
Sunny Goyalfe770c92016-09-27 14:38:58 -0700615 * Invoked by subclasses to signal a change to the {@link #addToCustomContentPage} value to
Dave Hawkeya8881582013-09-17 15:55:33 -0600616 * ensure the custom content page is added or removed if necessary.
617 */
618 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600619 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600620 // Not bound yet, wait for bindScreens to be called.
621 return;
622 }
623
624 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
625 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500626 mWorkspace.createCustomContentContainer();
627 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600628 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
629 mWorkspace.removeCustomContentPage();
630 }
631 }
632
Hyunyoung Songaa953652016-04-19 18:30:24 -0700633 public UserEventDispatcher getUserEventDispatcher() {
634 if (mLauncherCallbacks != null) {
635 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
636 if (dispatcher != null) {
637 return dispatcher;
638 }
639 }
640
Sunny Goyal64976d52016-06-20 14:56:28 -0700641 // Logger object is a singleton and does not have to be coupled with the foreground
642 // activity. Since most user event logging is done on the UI, the object is retrieved
643 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700644 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700645 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700646 }
647 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800648 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100649
Hyunyoung Song3f471442015-04-08 19:01:34 -0700650 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700651 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
652 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700653 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700654 }
655
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000656 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700657 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
658 // This cast is safe as long as the id < 0x00FFFFFF
659 // Since we jail all the dynamically generated views, there should be no clashes
660 // with any other views.
661 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000662 }
663
664 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700665 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
666 * a configuration step, this allows the proper animations to run after other transitions.
667 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700668 private long completeAdd(
669 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
670 long screenId = info.screenId;
671 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700672 // When the screen id represents an actual screen (as opposed to a rank) we make sure
673 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700674 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700675 }
Adam Cohendb364c32014-05-20 14:23:40 -0700676
Sunny Goyal2100c782016-08-22 16:00:03 -0700677 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800678 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700679 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700680 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800681 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700682 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700683 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700684 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700685 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700686 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700687 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700688 int widgetId = appWidgetId;
689 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700690 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700691 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700692 // Since the view was just bound, also launch the configure activity if needed
693 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
694 .getLauncherAppWidgetInfo(widgetId);
695 if (provider != null && provider.configure != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700696 startRestoredWidgetReconfigActivity(provider, widgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -0700697 }
698 }
699 break;
700 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800701 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700702
Adam Cohendb364c32014-05-20 14:23:40 -0700703 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800704 }
705
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800706 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700707 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700708 if (isWorkspaceLoading()) {
709 // process the result once the workspace has loaded.
710 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
711 return;
712 }
713 mPendingActivityResult = null;
714
Winson Chunge341d302013-08-16 14:31:00 -0700715 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700716 final PendingRequestArgs requestArgs = mPendingRequestArgs;
717 setWaitingForResult(null);
718 if (requestArgs == null) {
719 return;
720 }
721
722 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700723
Adam Cohenad4e15c2013-10-17 16:21:35 -0700724 Runnable exitSpringLoaded = new Runnable() {
725 @Override
726 public void run() {
727 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
728 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
729 }
730 };
731
Michael Jurka8b805b12012-04-18 14:23:14 -0700732 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700733 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700734 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700735 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
736 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700737 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700738 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700739 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700740 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700741 addAppWidgetImpl(
742 appWidgetId, requestArgs, null,
743 requestArgs.getWidgetProvider(),
744 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700745 }
746 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200747 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
748 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700749 // User could have free-scrolled between pages before picking a wallpaper; make sure
750 // we move to the closest one now.
751 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700752 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200753 }
754 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700755 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200756
Adam Cohened66b2b2012-01-23 17:28:51 -0800757 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700758 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700759
Adam Cohened66b2b2012-01-23 17:28:51 -0800760 // We have special handling for widgets
761 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800762 final int appWidgetId;
763 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
764 : -1;
765 if (widgetId < 0) {
766 appWidgetId = pendingAddWidgetId;
767 } else {
768 appWidgetId = widgetId;
769 }
770
Adam Cohenad4e15c2013-10-17 16:21:35 -0700771 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800772 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700773 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
774 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700775 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700776 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700777 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700778 @Override
779 public void run() {
780 exitSpringLoadedDragModeDelayed(false, 0, null);
781 }
782 };
Adam Cohendb364c32014-05-20 14:23:40 -0700783
Sunny Goyal2100c782016-08-22 16:00:03 -0700784 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
785 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
786 } else {
787 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
788 // When the screen id represents an actual screen (as opposed to a rank)
789 // we make sure that the drop page actually exists.
790 requestArgs.screenId =
791 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700792 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700793 final CellLayout dropLayout =
794 mWorkspace.getScreenWithId(requestArgs.screenId);
795
796 dropLayout.setDropPending(true);
797 final Runnable onComplete = new Runnable() {
798 @Override
799 public void run() {
800 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
801 dropLayout.setDropPending(false);
802 }
803 };
804 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
805 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700806 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800807 return;
808 }
809
Sunny Goyald478c832016-04-01 12:04:16 -0700810 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
811 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700812 if (resultCode == RESULT_OK) {
813 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700814 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700815 }
816 // Leave the widget in the pending state if the user canceled the configure.
817 return;
818 }
819
Sunny Goyalaad90582015-07-09 14:22:50 -0700820 if (requestCode == REQUEST_CREATE_SHORTCUT) {
821 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700822 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
823 completeAdd(requestCode, data, -1, requestArgs);
824 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
825 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
826
Sunny Goyalaad90582015-07-09 14:22:50 -0700827 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700828 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
829 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800832 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800833 }
834
835 @Override
836 protected void onActivityResult(
837 final int requestCode, final int resultCode, final Intent data) {
838 handleActivityResult(requestCode, resultCode, data);
839 if (mLauncherCallbacks != null) {
840 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
841 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800842 }
843
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600844 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700845 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600846 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700847 PendingRequestArgs pendingArgs = mPendingRequestArgs;
848 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
849 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
850 setWaitingForResult(null);
851
Sunny Goyal28c6b962015-10-12 11:42:05 -0700852 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700853 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700854 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700855 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700856 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700857 Intent intent = pendingArgs.getPendingIntent();
858
Sunny Goyal28c6b962015-10-12 11:42:05 -0700859 if (grantResults.length > 0
860 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700861 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700862 } else {
863 // TODO: Show a snack bar with link to settings
864 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700865 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700866 }
867 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600868 if (mLauncherCallbacks != null) {
869 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
870 grantResults);
871 }
872 }
873
Adam Cohendb364c32014-05-20 14:23:40 -0700874 /**
875 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
876 *
877 * @param screenId the screen id to check
878 * @return the new screen, or screenId if it exists
879 */
880 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800881 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700882 if (dropLayout == null) {
883 // it's possible that the add screen was removed because it was
884 // empty and a re-bind occurred
885 mWorkspace.addExtraEmptyScreen();
886 return mWorkspace.commitExtraEmptyScreen();
887 } else {
888 return screenId;
889 }
890 }
891
Sunny Goyal2100c782016-08-22 16:00:03 -0700892 @Thunk void completeTwoStageWidgetDrop(
893 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
894 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800895 Runnable onCompleteRunnable = null;
896 int animationType = 0;
897
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700898 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800899 if (resultCode == RESULT_OK) {
900 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
901 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal2100c782016-08-22 16:00:03 -0700902 requestArgs.getWidgetProvider());
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700903 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800904 onCompleteRunnable = new Runnable() {
905 @Override
906 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700907 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700908 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
909 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800910 }
911 };
912 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800913 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800914 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800915 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700916 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700917 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700918 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
919 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700920 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700921 // The animated view may be null in the case of a rotation during widget configuration
922 onCompleteRunnable.run();
923 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924 }
925
926 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700927 protected void onStop() {
928 super.onStop();
929 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700930
931 if (mLauncherCallbacks != null) {
932 mLauncherCallbacks.onStop();
933 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700934
935 if (Utilities.isNycMR1OrAbove()) {
936 mAppWidgetHost.stopListening();
937 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700938 }
939
940 @Override
941 protected void onStart() {
942 super.onStart();
943 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700944
945 if (mLauncherCallbacks != null) {
946 mLauncherCallbacks.onStart();
947 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700948
949 if (Utilities.isNycMR1OrAbove()) {
950 mAppWidgetHost.startListening();
951 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700952 }
953
954 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800955 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200956 long startTime = 0;
957 if (DEBUG_RESUME_TIME) {
958 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400959 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200960 }
Adam Cohen9211d422014-10-07 18:14:53 -0700961
962 if (mLauncherCallbacks != null) {
963 mLauncherCallbacks.preOnResume();
964 }
965
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700967 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700968
Winson Chung4a2afa32012-07-19 14:53:05 -0700969 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700970 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700971 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800972 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700973 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700974 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700975 // view after launching an app, as they may be depending on the UI to be static to
976 // switch to another app, otherwise, if it was
Hyunyoung Songdd60ce42016-07-27 17:08:38 -0700977 showAppsView(false /* animated */, !launchedFromApp /* updatePredictedApps */,
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700978 mAppsView.shouldRestoreImeState() /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800979 } else if (mOnResumeState == State.WIDGETS) {
980 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700981 }
982 mOnResumeState = State.NONE;
983
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800984 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700985 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700986 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700987 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700988 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700990 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200991 // We might have postponed some bind calls until onResume (see waitUntilResume) --
992 // execute them here
993 long startTimeCallbacks = 0;
994 if (DEBUG_RESUME_TIME) {
995 startTimeCallbacks = System.currentTimeMillis();
996 }
997
Michael Jurka1e2f4652013-07-08 18:03:46 -0700998 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
999 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001000 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001001 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001002 if (DEBUG_RESUME_TIME) {
1003 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1004 (System.currentTimeMillis() - startTimeCallbacks));
1005 }
Michael Jurka447bf842013-05-15 14:52:15 +02001006 }
Michael Jurka54554252013-08-01 12:52:23 +02001007 if (mOnResumeCallbacks.size() > 0) {
1008 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1009 mOnResumeCallbacks.get(i).run();
1010 }
1011 mOnResumeCallbacks.clear();
1012 }
Winson Chunge4e50662012-01-23 14:45:13 -08001013
1014 // Reset the pressed state of icons that were locked in the press state while activities
1015 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001016 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001017 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001018 mWaitingForResume.setStayPressed(false);
1019 }
Winson Chung82963d52013-10-09 11:20:57 -07001020
Adam Cohen06dff352012-06-01 17:17:08 -07001021 // It is possible that widgets can receive updates while launcher is not in the foreground.
1022 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1023 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1024 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001025 if (!isWorkspaceLoading()) {
1026 getWorkspace().reinflateWidgetsIfNecessary();
1027 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001028
Michael Jurka447bf842013-05-15 14:52:15 +02001029 if (DEBUG_RESUME_TIME) {
1030 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1031 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001032
Adam Cohen4b66bf32015-09-18 12:15:19 -07001033 // We want to suppress callbacks about CustomContent being shown if we have just received
1034 // onNewIntent while the user was present within launcher. In that case, we post a call
1035 // to move the user to the main screen (which will occur after onResume). We don't want to
1036 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1037 // suppress here.
1038 if (mWorkspace.getCustomContentCallbacks() != null
1039 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001040 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001041 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001042 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1043 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001044 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001045 }
1046 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001047 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001048 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001049 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001050
Sunny Goyal756adbc2015-04-16 15:20:51 -07001051 if (!isWorkspaceLoading()) {
1052 // Process any items that were added while Launcher was away.
1053 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001054
1055 // Refresh shortcuts if the permission changed.
1056 mModel.refreshShortcutsIfRequired();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001057 }
Adam Cohen9211d422014-10-07 18:14:53 -07001058
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001059 if (shouldShowDiscoveryBounce()) {
1060 mAllAppsController.showDiscoveryBounce();
1061 }
1062 mIsResumeFromActionScreenOff = false;
Adam Cohen9211d422014-10-07 18:14:53 -07001063 if (mLauncherCallbacks != null) {
1064 mLauncherCallbacks.onResume();
1065 }
Adam Cohen06dff352012-06-01 17:17:08 -07001066 }
1067
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001069 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001070 // Ensure that items added to Launcher are queued until Launcher returns
1071 InstallShortcutReceiver.enableInstallQueue();
1072
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001073 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001074 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001075 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001076 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001077
1078 // We call onHide() aggressively. The custom content callbacks should be able to
1079 // debounce excess onHide calls.
1080 if (mWorkspace.getCustomContentCallbacks() != null) {
1081 mWorkspace.getCustomContentCallbacks().onHide();
1082 }
Romain Guycbb89e42009-06-08 15:52:54 -07001083
Adam Cohen9211d422014-10-07 18:14:53 -07001084 if (mLauncherCallbacks != null) {
1085 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001086 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001087 }
1088
1089 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001090 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1091 // by a onResume or by scrolling otherwise.
1092 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001093
1094 // Custom content is completely hidden
1095 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001096
1097 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1098 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001099
1100 // Indicates whether the user is allowed to scroll away from the custom content.
1101 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001102 }
1103
Adam Cohenc2d6e892014-10-16 09:49:24 -07001104 public interface LauncherOverlay {
1105
1106 /**
1107 * Touch interaction leading to overscroll has begun
1108 */
1109 public void onScrollInteractionBegin();
1110
1111 /**
1112 * Touch interaction related to overscroll has ended
1113 */
1114 public void onScrollInteractionEnd();
1115
1116 /**
1117 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1118 * screen (or in the case of RTL, the rightmost screen).
1119 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001120 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001121
1122 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001123 * Called when the launcher is ready to use the overlay
1124 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001125 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001126 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001127 }
1128
Jun Mukai8af0cd82015-05-12 12:32:12 -07001129 public interface LauncherSearchCallbacks {
1130 /**
1131 * Called when the search overlay is shown.
1132 */
1133 public void onSearchOverlayOpened();
1134
1135 /**
1136 * Called when the search overlay is dismissed.
1137 */
1138 public void onSearchOverlayClosed();
1139 }
1140
Adam Cohenc2d6e892014-10-16 09:49:24 -07001141 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001142 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001143 }
1144
1145 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1146
Sunny Goyalc86df472016-02-25 09:19:38 -08001147 public void onScrollChanged(float progress) {
1148 if (mWorkspace != null) {
1149 mWorkspace.onOverlayScrollChanged(progress);
1150 }
1151 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001152 }
1153
Jorim Jaggid017f882014-01-14 17:08:48 -08001154 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001155 if (mLauncherCallbacks != null) {
1156 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001157 } else {
1158 // On devices with a locked orientation, we will at least have the allow rotation
1159 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001160 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001161 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001162 }
1163
Adam Cohen9211d422014-10-07 18:14:53 -07001164 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001165 CustomContentCallbacks callbacks, String description) {
1166 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001167 }
1168
Adam Cohenedb40762013-07-18 16:45:45 -07001169 // The custom content needs to offset its content to account for the QSB
1170 public int getTopOffsetForCustomContent() {
1171 return mWorkspace.getPaddingTop();
1172 }
1173
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001174 @Override
1175 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001176 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001177 if (mModel.isCurrentCallbacks(this)) {
1178 mModel.stopLoader();
1179 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001180 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1181
Romain Guy13c2e7b2010-03-10 19:45:00 -08001182 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001183 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001184
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001185 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001186 @Override
1187 public void onWindowFocusChanged(boolean hasFocus) {
1188 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001189 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001190
1191 if (mLauncherCallbacks != null) {
1192 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1193 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001194 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001195
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 private boolean acceptFilter() {
1197 final InputMethodManager inputManager = (InputMethodManager)
1198 getSystemService(Context.INPUT_METHOD_SERVICE);
1199 return !inputManager.isFullscreenMode();
1200 }
1201
1202 @Override
1203 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001204 final int uniChar = event.getUnicodeChar();
1205 final boolean handled = super.onKeyDown(keyCode, event);
1206 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1207 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001208 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1209 keyCode, event);
1210 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001211 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001212 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001213 // showSearchDialog()
1214 // If there are multiple keystrokes before the search dialog takes focus,
1215 // onSearchRequested() will be called for every keystroke,
1216 // but it is idempotent, so it's fine.
1217 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 }
1219 }
1220
Joe Onorato8a9625e2010-01-28 15:55:35 -08001221 // Eat the long press event so the keyboard doesn't come up.
1222 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1223 return true;
1224 }
1225
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226 return handled;
1227 }
1228
Winson46163472015-09-22 17:31:56 -07001229 @Override
1230 public boolean onKeyUp(int keyCode, KeyEvent event) {
1231 if (keyCode == KeyEvent.KEYCODE_MENU) {
1232 // Ignore the menu key if we are currently dragging or are on the custom content screen
1233 if (!isOnCustomContent() && !mDragController.isDragging()) {
1234 // Close any open folders
1235 closeFolder();
1236
Tony Wickham49c8d292016-07-01 11:44:34 -07001237 // Close any shortcuts containers
1238 closeShortcutsContainer();
1239
Winson46163472015-09-22 17:31:56 -07001240 // Stop resizing any widgets
1241 mWorkspace.exitWidgetResizeMode();
1242
1243 // Show the overview mode if we are on the workspace
1244 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1245 !mWorkspace.isSwitchingState()) {
1246 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001247 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001248 }
1249 }
1250 return true;
1251 }
1252 return super.onKeyUp(keyCode, event);
1253 }
1254
Karl Rosaen138a0412009-04-23 19:00:21 -07001255 private String getTypedText() {
1256 return mDefaultKeySsb.toString();
1257 }
1258
Sunny Goyal6f28e712016-09-13 14:19:29 -07001259 @Override
1260 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001261 mDefaultKeySsb.clear();
1262 mDefaultKeySsb.clearSpans();
1263 Selection.setSelection(mDefaultKeySsb, 0);
1264 }
1265
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 /**
1267 * Restores the previous state, if it exists.
1268 *
1269 * @param savedState The previous state.
1270 */
1271 private void restoreState(Bundle savedState) {
1272 if (savedState == null) {
1273 return;
1274 }
1275
Sunny Goyalfe770c92016-09-27 14:38:58 -07001276 int stateOrdinal = savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal());
1277 State[] stateValues = State.values();
1278 State state = (stateOrdinal >= 0 && stateOrdinal < stateValues.length)
1279 ? stateValues[stateOrdinal] : State.WORKSPACE;
Winson Chungb745afb2015-03-02 11:51:23 -08001280 if (state == State.APPS || state == State.WIDGETS) {
1281 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001282 }
1283
Sunny Goyal2100c782016-08-22 16:00:03 -07001284 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1285 if (requestArgs != null) {
1286 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001288
1289 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290 }
1291
1292 /**
1293 * Finds all the views we need and configure them properly.
1294 */
1295 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001296 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001297 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001298 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001299 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyal6178f132016-07-11 17:30:03 -07001300 mQsbContainer = mDragLayer.findViewById(mDeviceProfile.isVerticalBarLayout()
1301 ? R.id.workspace_blocked_row : R.id.qsb_container);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001302 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001303
Sunny Goyal784f9c32016-03-23 16:56:54 -07001304 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1305 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1306 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307
Winson Chung4c98d922011-05-31 16:50:48 -07001308 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001309 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001310
Winson Chung3d503fb2011-07-13 17:25:49 -07001311 // Setup the hotseat
1312 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1313 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001314 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001315 }
1316
Winsone9f27272015-10-13 10:47:51 -07001317 // Setup the overview panel
1318 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001319
Winson Chung4c98d922011-05-31 16:50:48 -07001320 // Setup the workspace
1321 mWorkspace.setHapticFeedbackEnabled(false);
1322 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001323 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001324 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1325 // default state, otherwise we will update to the wrong offsets in RTL
1326 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001327 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001328 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001329
Tony Wickham34d2c912015-09-11 09:27:58 -07001330 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001331 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001332
Winson Chungef7f8742015-06-04 17:18:17 -07001333 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001334 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001335 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001336 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1337 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1338 } else {
1339 mAppsView.setSearchBarController(new DefaultAppSearchController());
1340 }
Craig Mautner360310b2012-10-26 15:13:08 -07001341
Winson Chung3d503fb2011-07-13 17:25:49 -07001342 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song0de01172016-10-05 16:27:48 -07001343 mDragController.setDragScroller(mWorkspace);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001344 mDragController.setScrollView(mDragLayer);
1345 mDragController.setMoveTarget(mWorkspace);
1346 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001347 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001348
Peter Schiller310a9882016-06-30 13:52:36 -07001349 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1350 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1351 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001352
Sunny Goyal322d5562015-06-25 19:35:49 -07001353 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1354 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001355 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356 }
1357
Winsone9f27272015-10-13 10:47:51 -07001358 private void setupOverviewPanel() {
1359 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1360
Winsone9f27272015-10-13 10:47:51 -07001361 // Bind wallpaper button actions
1362 View wallpaperButton = findViewById(R.id.wallpaper_button);
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001363 new OverviewButtonClickListener(LauncherLogProto.WALLPAPER_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001364 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001365 public void handleViewClick(View view) {
1366 onClickWallpaperPicker(view);
Winsone9f27272015-10-13 10:47:51 -07001367 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001368 }.attachTo(wallpaperButton);
Winsone9f27272015-10-13 10:47:51 -07001369 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1370
1371 // Bind widget button actions
1372 mWidgetsButton = findViewById(R.id.widget_button);
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001373 new OverviewButtonClickListener(LauncherLogProto.WIDGETS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001374 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001375 public void handleViewClick(View view) {
1376 onClickAddWidgetButton(view);
Winsone9f27272015-10-13 10:47:51 -07001377 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001378 }.attachTo(mWidgetsButton);
Winsone9f27272015-10-13 10:47:51 -07001379 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1380
1381 // Bind settings actions
1382 View settingsButton = findViewById(R.id.settings_button);
1383 boolean hasSettings = hasSettings();
1384 if (hasSettings) {
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001385 new OverviewButtonClickListener(LauncherLogProto.SETTINGS_BUTTON) {
Winsone9f27272015-10-13 10:47:51 -07001386 @Override
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001387 public void handleViewClick(View view) {
1388 onClickSettingsButton(view);
Winsone9f27272015-10-13 10:47:51 -07001389 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07001390 }.attachTo(settingsButton);
Winsone9f27272015-10-13 10:47:51 -07001391 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1392 } else {
1393 settingsButton.setVisibility(View.GONE);
1394 }
1395
1396 mOverviewPanel.setAlpha(0f);
1397 }
1398
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001400 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001401 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001402 */
1403 public void setAllAppsButton(View allAppsButton) {
1404 mAllAppsButton = allAppsButton;
1405 }
1406
Sunny Goyalbb011da2016-06-15 15:42:29 -07001407 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001408 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001409 }
1410
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001411 public View getWidgetsButton() {
1412 return mWidgetsButton;
1413 }
1414
Anjali Koppal5ad44842014-03-10 20:34:39 -07001415 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 * Creates a view representing a shortcut.
1417 *
1418 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001420 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001421 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422 }
1423
1424 /**
1425 * Creates a view representing a shortcut inflated from the specified resource.
1426 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001427 * @param parent The group the shortcut belongs to.
1428 * @param info The data structure describing the shortcut.
1429 *
1430 * @return A View inflated from layoutResId.
1431 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001432 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001433 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001434 parent, false);
1435 favorite.applyFromShortcutInfo(info, mIconCache);
1436 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001437 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001438 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001439 return favorite;
1440 }
1441
1442 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 * Add a shortcut to the workspace.
1444 *
1445 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001447 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001448 int cellY, PendingRequestArgs args) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001449 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001450 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001451
Sunny Goyal5c97f512015-05-19 16:03:28 -07001452 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001453 if (info == null || args.getRequestCode() != REQUEST_CREATE_SHORTCUT ||
1454 args.getPendingIntent().getComponent() == null) {
1455 return;
1456 }
1457 if (!PackageManagerHelper.hasPermissionForActivity(
1458 this, info.intent, args.getPendingIntent().getComponent().getPackageName())) {
1459 // The app is trying to add a shortcut without sufficient permissions
1460 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
Adam Cohend9198822011-11-22 16:42:47 -08001461 return;
1462 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001463 final View view = createShortcut(info);
1464
Sunny Goyal5c97f512015-05-19 16:03:28 -07001465 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001466 // First we check if we already know the exact location where we want to add this item.
1467 if (cellX >= 0 && cellY >= 0) {
1468 cellXY[0] = cellX;
1469 cellXY[1] = cellY;
1470 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001471
1472 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001473 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001474 true, null,null)) {
1475 return;
1476 }
1477 DragObject dragObject = new DragObject();
1478 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001479 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1480 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001481 return;
1482 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001483 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001484 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001485 }
1486
1487 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001488 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001489 return;
1490 }
1491
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001492 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493
Sunny Goyal2100c782016-08-22 16:00:03 -07001494 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
1495 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 }
1497
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001499 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001501 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001503 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001504 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1505
Adam Cohened66b2b2012-01-23 17:28:51 -08001506 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001507 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001508 }
Romain Guycbb89e42009-06-08 15:52:54 -07001509
Adam Cohen59400422014-03-05 18:07:04 -08001510 if (appWidgetInfo.isCustomWidget) {
1511 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001512 }
1513
Adam Cohen59400422014-03-05 18:07:04 -08001514 LauncherAppWidgetInfo launcherInfo;
1515 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001516 launcherInfo.spanX = itemInfo.spanX;
1517 launcherInfo.spanY = itemInfo.spanY;
1518 launcherInfo.minSpanX = itemInfo.minSpanX;
1519 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001520 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001521
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001523 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524
Sunny Goyal2100c782016-08-22 16:00:03 -07001525 if (hostView == null) {
1526 // Perform actual inflation because we're live
1527 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001529 hostView.setVisibility(View.VISIBLE);
1530 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 }
Romain Guycbb89e42009-06-08 15:52:54 -07001532
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001533 private void addAppWidgetToWorkspace(
1534 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001535 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001536 hostView.setTag(item);
1537 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001538
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001539 hostView.setFocusable(true);
1540 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001541
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001542 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001543 item.cellX, item.cellY, item.spanX, item.spanY, insert);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001544 }
1545
Adam Cohended9f8d2010-11-03 13:25:16 -07001546 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1547 @Override
1548 public void onReceive(Context context, Intent intent) {
1549 final String action = intent.getAction();
1550 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Sunny Goyal6ad72f02016-09-23 11:01:10 -07001551 mDragLayer.clearResizeFrame();
Winson Chung337cd9d2011-03-30 10:39:30 -07001552
Winson Chungc100e8e2011-08-09 16:02:43 -07001553 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001554 // processing a multi-step drop
Sunny Goyal2100c782016-08-22 16:00:03 -07001555 if (mAppsView != null && mWidgetsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001556 if (!showWorkspace(false)) {
1557 // If we are already on the workspace, then manually reset all apps
1558 mAppsView.reset();
1559 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001560 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001561 mIsResumeFromActionScreenOff = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001562 }
1563 }
1564 };
1565
1566 @Override
1567 public void onAttachedToWindow() {
1568 super.onAttachedToWindow();
1569
1570 // Listen for broadcasts related to user-presence
1571 final IntentFilter filter = new IntentFilter();
1572 filter.addAction(Intent.ACTION_SCREEN_OFF);
Adam Cohended9f8d2010-11-03 13:25:16 -07001573 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001574 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001575 mAttached = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001576
1577 if (mLauncherCallbacks != null) {
1578 mLauncherCallbacks.onAttachedToWindow();
1579 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001580 }
1581
1582 @Override
1583 public void onDetachedFromWindow() {
1584 super.onDetachedFromWindow();
Adam Cohend113e0c2010-11-11 10:48:05 -08001585 if (mAttached) {
1586 unregisterReceiver(mReceiver);
1587 mAttached = false;
1588 }
Sunny Goyalc86df472016-02-25 09:19:38 -08001589
1590 if (mLauncherCallbacks != null) {
1591 mLauncherCallbacks.onDetachedFromWindow();
1592 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001593 }
1594
1595 public void onWindowVisibilityChanged(int visibility) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001596 // The following code used to be in onResume, but it turns out onResume is called when
1597 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1598 // is a more appropriate event to handle
Sunny Goyal1acc56a2016-09-25 21:23:25 -07001599 if (visibility == View.VISIBLE) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001600 if (!mWorkspaceLoading) {
1601 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001602 // We want to let Launcher draw itself at least once before we force it to build
1603 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001604 // apps is nice and speedy.
1605 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001606 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001607 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001608 if (mStarted) return;
1609 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001610 // We delay the layer building a bit in order to give
1611 // other message processing a time to run. In particular
1612 // this avoids a delay in hiding the IME if it was
1613 // currently shown, because doing that may involve
1614 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001615 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001616 final ViewTreeObserver.OnDrawListener listener = this;
1617 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001618 public void run() {
1619 if (mWorkspace != null &&
1620 mWorkspace.getViewTreeObserver() != null) {
1621 mWorkspace.getViewTreeObserver().
1622 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001623 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001624 }
1625 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001626 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001627 }
1628 });
1629 }
1630 clearTypedText();
1631 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001632 }
1633
Hyunyoung Song3f471442015-04-08 19:01:34 -07001634 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001635 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1636 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001637 }
1638
Winson Chunga6945242014-01-08 14:04:34 -08001639 public DragLayer getDragLayer() {
1640 return mDragLayer;
1641 }
1642
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001643 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001644 return mAppsView;
1645 }
1646
Hyunyoung Song3f471442015-04-08 19:01:34 -07001647 public WidgetsContainerView getWidgetsView() {
1648 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001649 }
1650
Winson Chunga6945242014-01-08 14:04:34 -08001651 public Workspace getWorkspace() {
1652 return mWorkspace;
1653 }
1654
Sunny Goyal6178f132016-07-11 17:30:03 -07001655 public View getQsbContainer() {
1656 return mQsbContainer;
1657 }
1658
Winson Chunga6945242014-01-08 14:04:34 -08001659 public Hotseat getHotseat() {
1660 return mHotseat;
1661 }
1662
Jorim Jaggid017f882014-01-14 17:08:48 -08001663 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001664 return mOverviewPanel;
1665 }
1666
Sunny Goyal47328fd2016-05-25 18:56:41 -07001667 public DropTargetBar getDropTargetBar() {
1668 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001669 }
1670
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001671 public LauncherAppWidgetHost getAppWidgetHost() {
1672 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673 }
Romain Guycbb89e42009-06-08 15:52:54 -07001674
Winson Chunga9abd0e2010-10-27 17:18:37 -07001675 public LauncherModel getModel() {
1676 return mModel;
1677 }
1678
Adam Cohen79d90c52016-04-22 13:29:20 -07001679 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001680 return mSharedPrefs;
1681 }
1682
Adam Cohen2e6da152015-05-06 11:42:25 -07001683 public DeviceProfile getDeviceProfile() {
1684 return mDeviceProfile;
1685 }
1686
Bjorn Bringertc459e522013-06-07 19:36:01 +01001687 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001688 getWindow().closeAllPanels();
1689
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001690 // Whatever we were doing is hereby canceled.
Sunny Goyal2100c782016-08-22 16:00:03 -07001691 setWaitingForResult(null);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001692 }
1693
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001694 @Override
1695 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001696 long startTime = 0;
1697 if (DEBUG_RESUME_TIME) {
1698 startTime = System.currentTimeMillis();
1699 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001700 super.onNewIntent(intent);
1701
Winson15f8b172015-08-19 11:02:39 -07001702 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1703 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1704 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001705
1706 // Check this condition before handling isActionMain, as this will get reset.
1707 boolean shouldMoveToDefaultScreen = alreadyOnHome &&
1708 mState == State.WORKSPACE && getTopFloatingView() == null;
1709
Winson15f8b172015-08-19 11:02:39 -07001710 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1711 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001712 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001713 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001714
Adam Cohen6fecd412013-10-02 17:41:50 -07001715 if (mWorkspace == null) {
1716 // Can be cases where mWorkspace is null, this prevents a NPE
1717 return;
1718 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001719 // In all these cases, only animate if we're already on home
1720 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001721
Sunny Goyal935fca12015-10-13 10:19:01 -07001722 closeFolder(alreadyOnHome);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001723 closeShortcutsContainer(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001724 exitSpringLoadedDragMode();
1725
1726 // If we are already on home, then just animate back to the workspace,
1727 // otherwise, just wait until onResume to set the state back to Workspace
1728 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001729 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001730 } else {
1731 mOnResumeState = State.WORKSPACE;
1732 }
1733
1734 final View v = getWindow().peekDecorView();
1735 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001736 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001737 INPUT_METHOD_SERVICE);
1738 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1739 }
1740
Winson Chungb745afb2015-03-02 11:51:23 -08001741 // Reset the apps view
1742 if (!alreadyOnHome && mAppsView != null) {
1743 mAppsView.scrollToTop();
1744 }
1745
Hyunyoung Song3f471442015-04-08 19:01:34 -07001746 // Reset the widgets view
1747 if (!alreadyOnHome && mWidgetsView != null) {
1748 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001749 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001750
Adam Cohen9211d422014-10-07 18:14:53 -07001751 if (mLauncherCallbacks != null) {
1752 mLauncherCallbacks.onHomeIntent();
1753 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001754 }
Adam Cohened307df2013-10-02 09:37:31 -07001755
Adam Cohen9211d422014-10-07 18:14:53 -07001756 if (mLauncherCallbacks != null) {
1757 mLauncherCallbacks.onNewIntent(intent);
1758 }
Winson15f8b172015-08-19 11:02:39 -07001759
1760 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1761 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1762 // animation.
1763 if (isActionMain) {
Sunny Goyal85313732016-09-28 12:00:07 -07001764 boolean callbackAllowsMoveToDefaultScreen = mLauncherCallbacks != null ?
Ivan Lee667d6882015-09-03 10:16:07 -06001765 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Sunny Goyal85313732016-09-28 12:00:07 -07001766 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()
1767 && callbackAllowsMoveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001768
1769 // We use this flag to suppress noisy callbacks above custom content state
1770 // from onResume.
1771 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001772 mWorkspace.post(new Runnable() {
1773 @Override
1774 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001775 if (mWorkspace != null) {
1776 mWorkspace.moveToDefaultScreen(true);
1777 }
Winson15f8b172015-08-19 11:02:39 -07001778 }
1779 });
1780 }
1781 }
1782
1783 if (DEBUG_RESUME_TIME) {
1784 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1785 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001786 }
1787
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001788 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001789 public void onRestoreInstanceState(Bundle state) {
1790 super.onRestoreInstanceState(state);
1791 for (int page: mSynchronouslyBoundPages) {
1792 mWorkspace.restoreInstanceStateForChild(page);
1793 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 }
1795
1796 @Override
1797 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001798 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001799 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1800 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001801
Adam Cohen21cd0022013-10-09 18:57:02 -07001802 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001803 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001804
Michael Jurka883f55b2010-10-21 15:47:14 -07001805 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001806 // We close any open folder since it will not be re-opened, and we need to make sure
1807 // this state is reflected.
1808 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1809 closeFolder(false);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001810 closeShortcutsContainer(false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001811
Sunny Goyal2100c782016-08-22 16:00:03 -07001812 if (mPendingRequestArgs != null) {
1813 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1814 }
1815 if (mPendingActivityResult != null) {
1816 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 }
1818
Adam Cohen9211d422014-10-07 18:14:53 -07001819 if (mLauncherCallbacks != null) {
1820 mLauncherCallbacks.onSaveInstanceState(outState);
1821 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001822 }
1823
1824 @Override
1825 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001826 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001827
Michael Jurka9d906c72011-10-14 06:25:36 -07001828 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001829 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001830
Winson Chungcd2b0142011-06-08 16:02:26 -07001831 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001832 // It's possible to receive onDestroy after a new Launcher activity has
1833 // been created. In this case, don't interfere with the new Launcher.
1834 if (mModel.isCurrentCallbacks(this)) {
1835 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001836 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001837 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001838
Sunny Goyal745bad92016-05-02 10:54:12 -07001839 if (mRotationPrefChangeHandler != null) {
1840 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1841 }
1842
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001843 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001844 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001846 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001848 mAppWidgetHost = null;
1849
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 TextKeyListener.getInstance().release();
1851
Tony Wickhame2217252016-03-22 16:34:23 -07001852 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1853 .removeAccessibilityStateChangeListener(this);
1854
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001855 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001856
Michael Jurka2ecf9952012-06-18 12:52:28 -07001857 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001858
1859 if (mLauncherCallbacks != null) {
1860 mLauncherCallbacks.onDestroy();
1861 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001862 }
1863
Sunny Goyalae502842016-06-17 08:43:56 -07001864 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1865 return mAccessibilityDelegate;
1866 }
1867
Adam Cohena9cf38f2011-05-02 15:36:58 -07001868 public DragController getDragController() {
1869 return mDragController;
1870 }
1871
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001873 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001874 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001875 }
1876
1877 @Override
1878 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1879 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001880 try {
1881 super.startIntentSenderForResult(intent, requestCode,
1882 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1883 } catch (IntentSender.SendIntentException e) {
1884 throw new ActivityNotFoundException();
1885 }
1886 }
1887
Winson Chung70d72102011-08-12 11:24:35 -07001888 /**
1889 * Indicates that we want global search for this activity by setting the globalSearch
1890 * argument for {@link #startSearch} to true.
1891 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001892 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001893 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001894 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001895
Karl Rosaen138a0412009-04-23 19:00:21 -07001896 if (initialQuery == null) {
1897 // Use any text typed in the launcher as the initial query
1898 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001899 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 if (appSearchData == null) {
1901 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001902 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001904
Sunny Goyal6f28e712016-09-13 14:19:29 -07001905 if (mLauncherCallbacks == null ||
1906 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1907 // Starting search from the callbacks failed. Start the default global search.
1908 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001909 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001910
1911 // We need to show the workspace after starting the search
1912 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001913 }
1914
Ian Parkinson047036e2014-09-01 15:40:53 +01001915 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001916 * Starts the global search activity. This code is a copied from SearchManager
1917 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001918 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001919 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001920 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001921 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1922 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1923 if (globalSearchActivity == null) {
1924 Log.w(TAG, "No global search activity found.");
1925 return;
1926 }
1927 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1928 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1929 intent.setComponent(globalSearchActivity);
1930 // Make sure that we have a Bundle to put source in
1931 if (appSearchData == null) {
1932 appSearchData = new Bundle();
1933 } else {
1934 appSearchData = new Bundle(appSearchData);
1935 }
Adam Cohen9211d422014-10-07 18:14:53 -07001936 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001937 if (!appSearchData.containsKey("source")) {
1938 appSearchData.putString("source", getPackageName());
1939 }
1940 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1941 if (!TextUtils.isEmpty(initialQuery)) {
1942 intent.putExtra(SearchManager.QUERY, initialQuery);
1943 }
1944 if (selectInitialQuery) {
1945 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1946 }
1947 intent.setSourceBounds(sourceBounds);
1948 try {
1949 startActivity(intent);
1950 } catch (ActivityNotFoundException ex) {
1951 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1952 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953 }
1954
Yura4f93ec62014-02-04 14:15:21 +00001955 public boolean isOnCustomContent() {
1956 return mWorkspace.isOnOrMovingToCustomContent();
1957 }
1958
Winson Chung70d72102011-08-12 11:24:35 -07001959 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001960 public boolean onPrepareOptionsMenu(Menu menu) {
1961 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001962 if (mLauncherCallbacks != null) {
1963 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1964 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001965 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001966 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001968 @Override
1969 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001970 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001971 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001972 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001973 }
1974
Joe Onorato9c1289c2009-08-17 11:03:03 -04001975 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001976 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001977 }
1978
Adam Cohen517a7f52014-03-01 12:12:59 -08001979 public boolean isWorkspaceLoading() {
1980 return mWorkspaceLoading;
1981 }
1982
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001983 private void setWorkspaceLoading(boolean value) {
1984 boolean isLocked = isWorkspaceLocked();
1985 mWorkspaceLoading = value;
1986 if (isLocked != isWorkspaceLocked()) {
1987 onWorkspaceLockedChanged();
1988 }
1989 }
1990
Sunny Goyal2100c782016-08-22 16:00:03 -07001991 private void setWaitingForResult(PendingRequestArgs args) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001992 boolean isLocked = isWorkspaceLocked();
Sunny Goyal2100c782016-08-22 16:00:03 -07001993 mPendingRequestArgs = args;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001994 if (isLocked != isWorkspaceLocked()) {
1995 onWorkspaceLockedChanged();
1996 }
1997 }
1998
Adam Cohen9211d422014-10-07 18:14:53 -07001999 protected void onWorkspaceLockedChanged() {
2000 if (mLauncherCallbacks != null) {
2001 mLauncherCallbacks.onWorkspaceLockedChanged();
2002 }
2003 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002004
Sunny Goyal2100c782016-08-22 16:00:03 -07002005 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
2006 LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002007 if (LOGD) {
2008 Log.d(TAG, "Adding widget from drop");
2009 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002010 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2011 }
2012
Sunny Goyal2100c782016-08-22 16:00:03 -07002013 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
2014 AppWidgetHostView boundWidget, LauncherAppWidgetProviderInfo appWidgetInfo,
Adam Cohen59400422014-03-05 18:07:04 -08002015 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002016 if (appWidgetInfo.configure != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002017 setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, appWidgetInfo, info));
Michael Jurkaaf442092010-06-10 17:01:57 -07002018
Bjorn Bringert7984c942009-12-09 15:38:25 +00002019 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002020 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2021 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002023 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002024 Runnable onComplete = new Runnable() {
2025 @Override
2026 public void run() {
2027 // Exit spring loaded mode if necessary after adding the widget
2028 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2029 null);
2030 }
2031 };
Sunny Goyal2100c782016-08-22 16:00:03 -07002032 completeAddAppWidget(appWidgetId, info, boundWidget, appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002033 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 }
2035 }
Romain Guycbb89e42009-06-08 15:52:54 -07002036
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002037 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002038 // Close any folders that may be open.
2039 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002040 mWorkspace.moveToCustomContentScreen(animate);
2041 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002042
2043 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2044 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002045 info.container = container;
2046 info.screenId = screenId;
2047 if (cell != null) {
2048 info.cellX = cell[0];
2049 info.cellY = cell[1];
2050 }
2051 info.spanX = spanX;
2052 info.spanY = spanY;
2053
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002054 switch (info.itemType) {
2055 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2056 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07002057 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002058 break;
2059 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal2100c782016-08-22 16:00:03 -07002060 processShortcutFromDrop(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002061 break;
2062 default:
2063 throw new IllegalStateException("Unknown item type: " + info.itemType);
2064 }
2065 }
2066
Adam Cohenfbba09b2011-07-18 21:43:05 -07002067 /**
2068 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002069 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002070 private void processShortcutFromDrop(PendingAddItemInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07002071 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
2072 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
2073 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002074 }
2075
Adam Cohenfbba09b2011-07-18 21:43:05 -07002076 /**
2077 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002078 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002079 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002080 AppWidgetHostView hostView = info.boundWidget;
2081 int appWidgetId;
2082 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002083 // In the case where we've prebound the widget, we remove it from the DragLayer
2084 if (LOGD) {
2085 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2086 }
2087 getDragLayer().removeView(hostView);
2088
Adam Cohened66b2b2012-01-23 17:28:51 -08002089 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002090 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002091
2092 // Clear the boundWidget so that it doesn't get destroyed.
2093 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002094 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002095 // In this case, we either need to start an activity to get permission to bind
2096 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002097 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002098 Bundle options = info.bindOptions;
2099
Sunny Goyalffe83f12014-08-14 17:39:34 -07002100 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2101 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002102 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002103 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002104 } else {
Sunny Goyal2100c782016-08-22 16:00:03 -07002105 setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, info.info, info));
Michael Jurka8b805b12012-04-18 14:23:14 -07002106 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2107 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2108 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyal2100c782016-08-22 16:00:03 -07002109 mAppWidgetManager.getUser(info.info)
Sunny Goyalffe83f12014-08-14 17:39:34 -07002110 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002111 // TODO: we need to make sure that this accounts for the options bundle.
2112 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002113 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2114 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002115 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002116 }
2117
Adam Cohendcd297f2013-06-18 13:13:40 -07002118 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002119 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002120 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002121 folderInfo.title = getText(R.string.folder_name);
2122
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002124 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2125 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126
2127 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002128 FolderIcon newFolder =
2129 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002130 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002131 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002132 // Force measure the new folder icon
2133 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2134 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002135 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002136 }
Romain Guycbb89e42009-06-08 15:52:54 -07002137
Winsonc0b52fe2015-09-09 16:38:15 -07002138 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002139 * Unbinds the view for the specified item, and removes the item and all its children.
2140 *
2141 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002142 * @param itemInfo the {@link ItemInfo} for this view.
2143 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002144 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002145 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002146 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002147 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002148 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2149 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002150 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002151 } else {
2152 mWorkspace.removeWorkspaceItem(v);
2153 }
Winsonc0b52fe2015-09-09 16:38:15 -07002154 if (deleteFromDb) {
2155 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2156 }
2157 } else if (itemInfo instanceof FolderInfo) {
2158 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002159 if (v instanceof FolderIcon) {
2160 ((FolderIcon) v).removeListeners();
2161 }
Winsonc0b52fe2015-09-09 16:38:15 -07002162 mWorkspace.removeWorkspaceItem(v);
2163 if (deleteFromDb) {
2164 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2165 }
2166 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2167 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002168 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002169 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002170 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002171 }
2172 } else {
2173 return false;
2174 }
2175 return true;
2176 }
2177
2178 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002179 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002180 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002181 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002182 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002183 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002184 // Deleting an app widget ID is a void call but writes to disk before returning
2185 // to the caller...
2186 new AsyncTask<Void, Void, Void>() {
2187 public Void doInBackground(Void ... args) {
2188 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2189 return null;
2190 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002191 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002192 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002193 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002194 }
2195
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002196 @Override
2197 public boolean dispatchKeyEvent(KeyEvent event) {
2198 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2199 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002200 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002201 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002202 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002203 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002204 dumpState();
2205 return true;
2206 }
2207 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002208 }
2209 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2210 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002211 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002212 return true;
2213 }
2214 }
2215
2216 return super.dispatchKeyEvent(event);
2217 }
2218
Joe Onorato88ec0992009-11-19 13:16:06 -08002219 @Override
2220 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002221 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2222 return;
2223 }
2224
Sunny Goyal45478022015-06-08 16:52:41 -07002225 if (mDragController.isDragging()) {
2226 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002227 return;
2228 }
2229
Tony Wickham49c8d292016-07-01 11:44:34 -07002230 if (getOpenShortcutsContainer() != null) {
2231 closeShortcutsContainer();
2232 } else if (isAppsViewVisible()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002233 showWorkspace(true);
2234 } else if (isWidgetsViewVisible()) {
2235 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002236 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002237 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002238 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002239 Folder openFolder = mWorkspace.getOpenFolder();
2240 if (openFolder.isEditingName()) {
2241 openFolder.dismissEditingName();
2242 } else {
2243 closeFolder();
2244 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002245 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002246 mWorkspace.exitWidgetResizeMode();
2247
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002248 // Back button is a no-op here, but give at least some feedback for the button press
2249 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002250 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002251 }
2252
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002253 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002254 * Launches the intent referred by the clicked shortcut.
2255 *
2256 * @param v The view representing the clicked shortcut.
2257 */
2258 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002259 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2260 // view has detached (it's possible for this to happen if the view is removed mid touch).
2261 if (v.getWindowToken() == null) {
2262 return;
2263 }
2264
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002265 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002266 return;
2267 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002268
Adam Cohen1697b792013-09-17 19:08:21 -07002269 if (v instanceof Workspace) {
2270 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002271 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002272 }
2273 return;
2274 }
2275
2276 if (v instanceof CellLayout) {
2277 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002278 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2279 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002280 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002281 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002282 }
2283
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002284 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002285 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002286 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002287 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002288 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002289 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002290 }
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002291 } else if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2292 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002293 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002294 } else if (tag instanceof AppInfo) {
2295 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002296 } else if (tag instanceof LauncherAppWidgetInfo) {
2297 if (v instanceof PendingAppWidgetHostView) {
2298 onClickPendingWidget((PendingAppWidgetHostView) v);
2299 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002300 }
2301 }
2302
Sunny Goyal70660032015-05-14 00:07:08 -07002303 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002304 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002305 return false;
2306 }
2307
Michael Jurkaaf442092010-06-10 17:01:57 -07002308 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002309 * Event handler for the app widget view which has not fully restored.
2310 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002311 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002312 if (mIsSafeModeEnabled) {
2313 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2314 return;
2315 }
2316
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002317 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002318 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002319 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2320 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2321 // This should not happen, as we make sure that an Id is allocated during bind.
2322 return;
2323 }
2324 LauncherAppWidgetProviderInfo appWidgetInfo =
2325 mAppWidgetManager.findProvider(info.providerName, info.user);
2326 if (appWidgetInfo != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002327 setWaitingForResult(PendingRequestArgs
2328 .forWidgetInfo(info.appWidgetId, appWidgetInfo, info));
Sunny Goyalff572272014-07-23 13:58:07 -07002329
Sunny Goyald478c832016-04-01 12:04:16 -07002330 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
Sunny Goyal2100c782016-08-22 16:00:03 -07002331 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, info.appWidgetId);
Sunny Goyald478c832016-04-01 12:04:16 -07002332 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07002333 mAppWidgetManager.getUser(appWidgetInfo)
Sunny Goyald478c832016-04-01 12:04:16 -07002334 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2335 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2336 }
2337 } else {
2338 LauncherAppWidgetProviderInfo appWidgetInfo =
2339 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2340 if (appWidgetInfo != null) {
2341 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2342 }
Sunny Goyalff572272014-07-23 13:58:07 -07002343 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002344 } else if (info.installProgress < 0) {
2345 // The install has not been queued
2346 final String packageName = info.providerName.getPackageName();
2347 showBrokenAppInstallDialog(packageName,
2348 new DialogInterface.OnClickListener() {
2349 public void onClick(DialogInterface dialog, int id) {
2350 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2351 }
2352 });
2353 } else {
2354 // Download has started.
2355 final String packageName = info.providerName.getPackageName();
2356 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002357 }
2358 }
2359
Sunny Goyald478c832016-04-01 12:04:16 -07002360 private void startRestoredWidgetReconfigActivity(
2361 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002362 setWaitingForResult(PendingRequestArgs.forWidgetInfo(info.appWidgetId, provider, info));
Sunny Goyald478c832016-04-01 12:04:16 -07002363 mAppWidgetManager.startConfigActivity(provider,
2364 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2365 }
2366
Sunny Goyalff572272014-07-23 13:58:07 -07002367 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002368 * Event handler for the "grid" button that appears on the home screen, which
2369 * enters all apps mode.
2370 *
2371 * @param v The view that was clicked.
2372 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002373 protected void onClickAllAppsButton(View v) {
2374 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002375 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002376 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.TAP,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002377 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002378 showAppsView(true /* animated */, true /* updatePredictedApps */,
2379 false /* focusSearchBar */);
Winson Chung76648c52015-07-10 14:33:23 -07002380 }
2381 }
2382
2383 protected void onLongClickAllAppsButton(View v) {
2384 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2385 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002386 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.LONGPRESS,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002387 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002388 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07002389 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002390 }
2391 }
2392
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002393 private void showBrokenAppInstallDialog(final String packageName,
2394 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002395 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002396 .setTitle(R.string.abandoned_promises_title)
2397 .setMessage(R.string.abandoned_promise_explanation)
2398 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2399 .setNeutralButton(R.string.abandoned_clean_this,
2400 new DialogInterface.OnClickListener() {
2401 public void onClick(DialogInterface dialog, int id) {
2402 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2403 mWorkspace.removeAbandonedPromise(packageName, user);
2404 }
2405 })
2406 .create().show();
2407 return;
2408 }
2409
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002410 /**
2411 * Event handler for an app shortcut click.
2412 *
2413 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2414 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002415 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002416 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2417 Object tag = v.getTag();
2418 if (!(tag instanceof ShortcutInfo)) {
2419 throw new IllegalArgumentException("Input must be a Shortcut");
2420 }
2421
2422 // Open shortcut
2423 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002424
2425 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002426 if ((shortcut.isDisabled &
2427 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2428 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2429 // If the app is only disabled because of the above flags, launch activity anyway.
2430 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002431 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002432 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2433 // Use a message specific to this shortcut, if it has one.
2434 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2435 return;
2436 }
2437 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002438 int error = R.string.activity_not_available;
2439 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2440 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002441 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2442 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002443 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002444 }
2445 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2446 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002447 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002448 }
2449
Chris Wren40c5ed32014-06-24 18:24:23 -04002450 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002451 if ((v instanceof BubbleTextView)
2452 && shortcut.isPromise()
2453 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002454 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002455 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002456 new DialogInterface.OnClickListener() {
2457 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002458 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002459 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002460 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002461 return;
2462 }
2463
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002464 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002465 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002466 }
2467
Sunny Goyala7ce1662016-05-31 15:01:35 -07002468 private void startAppShortcutOrInfoActivity(View v) {
2469 ItemInfo item = (ItemInfo) v.getTag();
2470 Intent intent = item.getIntent();
2471 if (intent == null) {
2472 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002473 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002474 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002475 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002476
2477 if (success && v instanceof BubbleTextView) {
2478 mWaitingForResume = (BubbleTextView) v;
2479 mWaitingForResume.setStayPressed(true);
2480 }
2481 }
2482
2483 /**
2484 * Event handler for a folder icon click.
2485 *
2486 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2487 */
2488 protected void onClickFolderIcon(View v) {
2489 if (LOGD) Log.d(TAG, "onClickFolder");
2490 if (!(v instanceof FolderIcon)){
2491 throw new IllegalArgumentException("Input must be a FolderIcon");
2492 }
2493
2494 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002495 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002496 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002497 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002498 }
Michael Jurka5130e402011-10-13 04:55:35 -07002499 }
2500
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002501 /**
2502 * Event handler for the (Add) Widgets button that appears after a long press
2503 * on the home screen.
2504 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002505 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002506 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002507 if (mIsSafeModeEnabled) {
2508 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2509 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002510 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002511 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002512 }
2513
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002514 /**
2515 * Event handler for the wallpaper picker button that appears after a long press
2516 * on the home screen.
2517 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002518 public void onClickWallpaperPicker(View v) {
Hyunyoung Song0de01172016-10-05 16:27:48 -07002519 if (!Utilities.isWallpaperAllowed(this)) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002520 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2521 return;
2522 }
2523
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002524 String pickerPackage = getString(R.string.wallpaper_picker_package);
2525 if (TextUtils.isEmpty(pickerPackage)) {
2526 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2527 }
2528
Tony Wickham785f7a52015-08-31 17:28:32 -07002529 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2530 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002531
Sunny Goyal2100c782016-08-22 16:00:03 -07002532 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002533 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002534 .setPackage(pickerPackage)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002535 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
2536 intent.setSourceBounds(getViewBounds(v));
2537 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002538 }
2539
2540 /**
2541 * Event handler for a click on the settings button that appears after a long press
2542 * on the home screen.
2543 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002544 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002545 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002546 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2547 .setPackage(getPackageName());
2548 intent.setSourceBounds(getViewBounds(v));
2549 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002550 }
2551
Adam Cohen61f560d2013-09-30 15:58:20 -07002552 public View.OnTouchListener getHapticFeedbackTouchListener() {
2553 if (mHapticFeedbackTouchListener == null) {
2554 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002555 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002556 @Override
2557 public boolean onTouch(View v, MotionEvent event) {
2558 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2559 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2560 }
2561 return false;
2562 }
2563 };
2564 }
2565 return mHapticFeedbackTouchListener;
2566 }
2567
Tony Wickhame2217252016-03-22 16:34:23 -07002568 @Override
2569 public void onAccessibilityStateChanged(boolean enabled) {
2570 mDragLayer.onAccessibilityStateChanged(enabled);
2571 }
2572
Sunny Goyal06e21a22016-08-11 16:02:02 -07002573 public void onDragStarted() {
Adam Cohen9211d422014-10-07 18:14:53 -07002574 if (isOnCustomContent()) {
2575 // Custom content screen doesn't participate in drag and drop. If on custom
2576 // content screen, move to default.
2577 moveWorkspaceToDefaultScreen();
2578 }
Adam Cohen9211d422014-10-07 18:14:53 -07002579 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002580
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002581 /**
2582 * Called when the user stops interacting with the launcher.
2583 * This implies that the user is now on the homescreen and is not doing housekeeping.
2584 */
Adam Cohen9211d422014-10-07 18:14:53 -07002585 protected void onInteractionEnd() {
2586 if (mLauncherCallbacks != null) {
2587 mLauncherCallbacks.onInteractionEnd();
2588 }
2589 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002590
2591 /**
2592 * Called when the user starts interacting with the launcher.
2593 * The possible interactions are:
2594 * - open all apps
2595 * - reorder an app shortcut, or a widget
2596 * - open the overview mode.
2597 * This is a good time to stop doing things that only make sense
2598 * when the user is on the homescreen and not doing housekeeping.
2599 */
Adam Cohen9211d422014-10-07 18:14:53 -07002600 protected void onInteractionBegin() {
2601 if (mLauncherCallbacks != null) {
2602 mLauncherCallbacks.onInteractionBegin();
2603 }
2604 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002605
Winson Chungcd99cd32015-04-29 11:03:24 -07002606 /** Updates the interaction state. */
2607 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002608 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002609 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002610 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2611 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002612 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002613 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002614 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002615 onInteractionEnd();
2616 }
2617 }
2618
Sunny Goyala7ce1662016-05-31 15:01:35 -07002619 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002620 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002621 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2622 try {
2623 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2624 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2625 // is enabled by default on NYC.
2626 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2627 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002628
2629 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2630 String id = ((ShortcutInfo) info).getDeepShortcutId();
2631 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002632 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2633 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002634 } else {
2635 // Could be launching some bookkeeping activity
2636 startActivity(intent, optsBundle);
2637 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002638 } finally {
2639 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002640 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002641 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002642 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2643 // corresponding permission. Show the appropriate permission prompt if that
2644 // is the case.
2645 if (intent.getComponent() == null
2646 && Intent.ACTION_CALL.equals(intent.getAction())
2647 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2648 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002649
2650 setWaitingForResult(PendingRequestArgs
2651 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002652 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2653 REQUEST_PERMISSION_CALL_PHONE);
2654 } else {
2655 // No idea why this was thrown.
2656 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002657 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002658 }
2659 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002660
Sunny Goyalfe770c92016-09-27 14:38:58 -07002661 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyala7ce1662016-05-31 15:01:35 -07002662 private Bundle getActivityLaunchOptions(View v) {
2663 if (Utilities.ATLEAST_MARSHMALLOW) {
2664 int left = 0, top = 0;
2665 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2666 if (v instanceof TextView) {
2667 // Launch from center of icon, not entire view
2668 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2669 if (icon != null) {
2670 Rect bounds = icon.getBounds();
2671 left = (width - bounds.width()) / 2;
2672 top = v.getPaddingTop();
2673 width = bounds.width();
2674 height = bounds.height();
2675 }
2676 }
2677 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2678 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2679 // On L devices, we use the device default slide-up transition.
2680 // On L MR1 devices, we use a custom version of the slide-up transition which
2681 // doesn't have the delay present in the device default.
2682 return ActivityOptions.makeCustomAnimation(
2683 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2684 }
2685 return null;
2686 }
2687
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002688 private Rect getViewBounds(View v) {
2689 int[] pos = new int[2];
2690 v.getLocationOnScreen(pos);
2691 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2692 }
2693
Sunny Goyala7ce1662016-05-31 15:01:35 -07002694 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002695 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2696 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2697 return false;
2698 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002699 // Only launch using the new animation if the shortcut has not opted out (this is a
2700 // private contract between launcher and may be ignored in the future).
2701 boolean useLaunchAnimation = (v != null) &&
2702 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2703 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2704
2705 UserHandleCompat user = null;
2706 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2707 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2708 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002709 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002710
2711 // Prepare intent
2712 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2713 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002714 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002715 }
2716 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002717 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2718 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07002719 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
2720 && ((ShortcutInfo) item).promisedIntent == null) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002721 // Shortcuts need some special checks due to legacy reasons.
2722 startShortcutIntentSafely(intent, optsBundle, item);
2723 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2724 // Could be launching some bookkeeping activity
2725 startActivity(intent, optsBundle);
2726 } else {
2727 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2728 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2729 }
2730 return true;
2731 } catch (ActivityNotFoundException|SecurityException e) {
2732 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2733 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2734 }
2735 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002736 }
2737
Adam Cohen268c4752012-06-06 17:47:33 -07002738 /**
2739 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2740 * in the DragLayer in the exact absolute location of the original FolderIcon.
2741 */
2742 private void copyFolderIconToImage(FolderIcon fi) {
2743 final int width = fi.getMeasuredWidth();
2744 final int height = fi.getMeasuredHeight();
2745
2746 // Lazy load ImageView, Bitmap and Canvas
2747 if (mFolderIconImageView == null) {
2748 mFolderIconImageView = new ImageView(this);
2749 }
2750 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2751 mFolderIconBitmap.getHeight() != height) {
2752 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2753 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2754 }
2755
2756 DragLayer.LayoutParams lp;
2757 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2758 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2759 } else {
2760 lp = new DragLayer.LayoutParams(width, height);
2761 }
2762
Adam Cohen307fe232012-08-16 17:55:58 -07002763 // The layout from which the folder is being opened may be scaled, adjust the starting
2764 // view size by this scale factor.
2765 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002766 lp.customPosition = true;
2767 lp.x = mRectForFolderAnimation.left;
2768 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002769 lp.width = (int) (scale * width);
2770 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002771
2772 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2773 fi.draw(mFolderIconCanvas);
2774 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002775 if (fi.getFolder() != null) {
2776 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2777 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002778 }
Adam Cohen268c4752012-06-06 17:47:33 -07002779 // Just in case this image view is still in the drag layer from a previous animation,
2780 // we remove it and re-add it.
2781 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2782 mDragLayer.removeView(mFolderIconImageView);
2783 }
2784 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002785 if (fi.getFolder() != null) {
2786 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002787 }
Adam Cohen268c4752012-06-06 17:47:33 -07002788 }
2789
Adam Cohen37b2a492016-04-06 18:36:06 -07002790 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002791 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002792 FolderInfo info = (FolderInfo) fi.getTag();
2793 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2794 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002795 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2796 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002797 }
2798
Adam Cohen268c4752012-06-06 17:47:33 -07002799 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2800 copyFolderIconToImage(fi);
2801 fi.setVisibility(View.INVISIBLE);
2802
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002803 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2804 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002805 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002806 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2807 }
2808 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002809 oa.start();
2810 }
2811
Sunny Goyal935fca12015-10-13 10:19:01 -07002812 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002813 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002814 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002815
Adam Cohen268c4752012-06-06 17:47:33 -07002816 // We remove and re-draw the FolderIcon in-case it has changed
2817 mDragLayer.removeView(mFolderIconImageView);
2818 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08002819
2820 if (cl != null) {
2821 cl.clearFolderLeaveBehind();
2822 }
2823
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002824 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002825 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002826 oa.addListener(new AnimatorListenerAdapter() {
2827 @Override
2828 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002829 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07002830 // Remove the ImageView copy of the FolderIcon and make the original visible.
2831 mDragLayer.removeView(mFolderIconImageView);
2832 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002833 }
2834 }
2835 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002836 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07002837 if (!animate) {
2838 oa.end();
2839 }
Adam Cohen2801caf2011-05-13 20:57:39 -07002840 }
2841
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002842 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002843 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002844 * is animated relative to the specified View. If the View is null, no animation
2845 * is played.
2846 *
Sunny Goyal08442b82015-10-21 17:15:08 -07002847 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002848 */
Adam Cohen37b2a492016-04-06 18:36:06 -07002849 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07002850
Adam Cohenfb91f302012-06-11 15:45:18 -07002851 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07002852 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
2853 if (openFolder != null && openFolder != folder) {
2854 // Close any open folder before opening a folder.
2855 closeFolder();
2856 }
2857
Adam Cohena9cf38f2011-05-02 15:36:58 -07002858 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002859
Adam Cohena9cf38f2011-05-02 15:36:58 -07002860 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002861
Sunny Goyalf4066152015-04-15 09:42:19 -07002862 // While the folder is open, the position of the icon cannot change.
2863 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
2864
Adam Cohen4554ee12011-08-03 16:13:21 -07002865 // Just verify that the folder hasn't already been added to the DragLayer.
2866 // There was a one-off crash where the folder had a parent already.
2867 if (folder.getParent() == null) {
2868 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07002869 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07002870 } else {
2871 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2872 folder.getParent() + ").");
2873 }
Adam Cohen37b2a492016-04-06 18:36:06 -07002874 folder.animateOpen();
2875
2876 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002877
2878 // Notify the accessibility manager that this folder "window" has appeared and occluded
2879 // the workspace items
2880 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2881 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002882
2883 getUserEventDispatcher().resetElapsedContainerMillis();
Adam Cohen4554ee12011-08-03 16:13:21 -07002884 }
2885
2886 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07002887 closeFolder(true);
2888 }
2889
2890 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08002891 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002892 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002893 if (folder.isEditingName()) {
2894 folder.dismissEditingName();
2895 }
Sunny Goyal935fca12015-10-13 10:19:01 -07002896 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07002897 }
2898 }
2899
Sunny Goyal935fca12015-10-13 10:19:01 -07002900 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07002901 animate &= !Utilities.isPowerSaverOn(this);
2902
Adam Cohen4554ee12011-08-03 16:13:21 -07002903 folder.getInfo().opened = false;
2904
2905 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2906 if (parent != null) {
2907 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07002908 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07002909 if (fi != null) {
2910 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
2911 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002912 }
Sunny Goyal935fca12015-10-13 10:19:01 -07002913 if (animate) {
2914 folder.animateClosed();
2915 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08002916 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07002917 }
Winson Chung83ca4802013-04-12 15:10:52 -07002918
Sunny Goyal935fca12015-10-13 10:19:01 -07002919 // Notify the accessibility manager that this folder "window" has disappeared and no
2920 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07002921 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002922
2923 getUserEventDispatcher().resetElapsedContainerMillis();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002924 }
2925
Tony Wickham49c8d292016-07-01 11:44:34 -07002926 public void closeShortcutsContainer() {
Sunny Goyala2454ad2016-07-22 10:50:11 -07002927 closeShortcutsContainer(true);
2928 }
2929
2930 public void closeShortcutsContainer(boolean animate) {
Tony Wickham49c8d292016-07-01 11:44:34 -07002931 DeepShortcutsContainer deepShortcutsContainer = getOpenShortcutsContainer();
2932 if (deepShortcutsContainer != null) {
Sunny Goyala2454ad2016-07-22 10:50:11 -07002933 if (animate) {
2934 deepShortcutsContainer.animateClose();
2935 } else {
2936 deepShortcutsContainer.close();
2937 }
Tony Wickham49c8d292016-07-01 11:44:34 -07002938 }
2939 }
2940
Sunny Goyal52851aa2016-09-02 10:41:43 -07002941 public View getTopFloatingView() {
2942 View topView = getOpenShortcutsContainer();
2943 if (topView == null) {
2944 topView = getWorkspace().getOpenFolder();
2945 }
2946 return topView;
2947 }
2948
Tony Wickham49c8d292016-07-01 11:44:34 -07002949 /**
2950 * @return The open shortcuts container, or null if there is none
2951 */
2952 public DeepShortcutsContainer getOpenShortcutsContainer() {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07002953 // Iterate in reverse order. Shortcuts container is added later to the dragLayer,
2954 // and will be one of the last views.
2955 for (int i = mDragLayer.getChildCount() - 1; i >= 0; i--) {
2956 View child = mDragLayer.getChildAt(i);
Sunny Goyala2454ad2016-07-22 10:50:11 -07002957 if (child instanceof DeepShortcutsContainer
2958 && ((DeepShortcutsContainer) child).isOpen()) {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07002959 return (DeepShortcutsContainer) child;
2960 }
2961 }
2962 return null;
Tony Wickham49c8d292016-07-01 11:44:34 -07002963 }
2964
Tony Wickhamdadb3042016-02-24 11:07:00 -08002965 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002966 public boolean dispatchTouchEvent(MotionEvent ev) {
2967 mLastDispatchTouchEventX = ev.getX();
2968 return super.dispatchTouchEvent(ev);
2969 }
2970
2971 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002972 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002973 if (!isDraggingEnabled()) return false;
2974 if (isWorkspaceLocked()) return false;
2975 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002976
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002977 if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2978 (v == mAllAppsButton && mAllAppsButton != null)) {
Winson Chung76648c52015-07-10 14:33:23 -07002979 onLongClickAllAppsButton(v);
2980 return true;
2981 }
2982
Jon Miranda379198e2016-09-23 08:54:40 -07002983 boolean fromEdgeOfScreen = mLastDispatchTouchEventX < mEdgeOfScreenThresholdPx
2984 || mLastDispatchTouchEventX > (mDeviceProfile.widthPx - mEdgeOfScreenThresholdPx);
2985
Adam Cohen1697b792013-09-17 19:08:21 -07002986 if (v instanceof Workspace) {
2987 if (!mWorkspace.isInOverviewMode()) {
Jon Miranda379198e2016-09-23 08:54:40 -07002988 if (!mWorkspace.isTouchActive() && !fromEdgeOfScreen) {
Jon Mirandaf3e35d92016-10-05 14:08:11 -07002989 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.LONGPRESS,
2990 LauncherLogProto.Action.NONE, LauncherLogProto.WORKSPACE,
2991 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07002992 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07002993 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2994 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2995 return true;
2996 } else {
2997 return false;
2998 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002999 } else {
3000 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003001 }
Adam Cohen1697b792013-09-17 19:08:21 -07003002 }
3003
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003004 CellLayout.CellInfo longClickCellInfo = null;
3005 View itemUnderLongClick = null;
3006 if (v.getTag() instanceof ItemInfo) {
3007 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003008 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003009 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07003010 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003011 }
3012
Winson Chung3d503fb2011-07-13 17:25:49 -07003013 // The hotseat touch handling does not go through Workspace, and we always allow long press
3014 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003015 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003016 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003017 // User long pressed on empty space
Adam Cohend3ecce92013-09-16 14:58:00 -07003018 if (mWorkspace.isInOverviewMode()) {
3019 mWorkspace.startReordering(v);
Jon Mirandaf3e35d92016-10-05 14:08:11 -07003020 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.LONGPRESS,
3021 LauncherLogProto.Action.NONE, LauncherLogProto.OVERVIEW);
Adam Cohend3ecce92013-09-16 14:58:00 -07003022 } else {
Jon Miranda379198e2016-09-23 08:54:40 -07003023 if (fromEdgeOfScreen) {
3024 return false;
3025 }
Jon Mirandaf3e35d92016-10-05 14:08:11 -07003026 getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.LONGPRESS,
3027 LauncherLogProto.Action.NONE, LauncherLogProto.WORKSPACE,
3028 mWorkspace.getCurrentPage());
Winson Chungdc61c4d2015-04-20 18:26:57 -07003029 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003030 }
Jon Miranda379198e2016-09-23 08:54:40 -07003031 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3032 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003033 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003034 final boolean isAllAppsButton =
3035 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3036 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3037 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003038 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003039 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07003040 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003041 }
3042 }
3043 }
3044 return true;
3045 }
3046
Winson Chung3d503fb2011-07-13 17:25:49 -07003047 boolean isHotseatLayout(View layout) {
3048 return mHotseat != null && layout != null &&
3049 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3050 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003051
Winson Chung3d503fb2011-07-13 17:25:49 -07003052 /**
3053 * Returns the CellLayout of the specified container at the specified screen.
3054 */
Sunny Goyal92820592015-03-02 11:31:35 -08003055 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003056 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3057 if (mHotseat != null) {
3058 return mHotseat.getLayout();
3059 } else {
3060 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003061 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003062 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003063 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003064 }
3065 }
3066
Winson Chungb745afb2015-03-02 11:51:23 -08003067 /**
3068 * For overridden classes.
3069 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003070 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003071 return isAppsViewVisible();
3072 }
3073
3074 public boolean isAppsViewVisible() {
3075 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3076 }
3077
3078 public boolean isWidgetsViewVisible() {
3079 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003080 }
3081
Michael Jurkae326f182011-11-21 14:05:46 -08003082 @Override
3083 public void onTrimMemory(int level) {
3084 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003085 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3086 // The widget preview db can result in holding onto over
3087 // 3MB of memory for caching which isn't necessary.
3088 SQLiteDatabase.releaseMemory();
3089
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003090 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003091 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003092 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003093 if (mLauncherCallbacks != null) {
3094 mLauncherCallbacks.onTrimMemory(level);
3095 }
Michael Jurkae326f182011-11-21 14:05:46 -08003096 }
3097
Winson5c6bdbb2015-09-03 11:36:19 -07003098 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003099 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003100 }
3101
Winson5c6bdbb2015-09-03 11:36:19 -07003102 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003103 boolean changed = mState != State.WORKSPACE ||
3104 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003105 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003106 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003107 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003108 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003109
Michael Jurkab3e22d92011-10-31 15:58:33 -07003110 // Set focus to the AppsCustomize button
3111 if (mAllAppsButton != null) {
3112 mAllAppsButton.requestFocus();
3113 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003114 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003115
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003116 // Change the state *after* we've called all the transition code
3117 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003118
Winson Chung0f785722015-04-08 10:27:49 -07003119 if (changed) {
3120 // Send an accessibility event to announce the context change
3121 getWindow().getDecorView()
3122 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003123 }
Winson5c6bdbb2015-09-03 11:36:19 -07003124 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003125 }
3126
Winsone9f27272015-10-13 10:47:51 -07003127 /**
3128 * Shows the overview button.
3129 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07003130 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003131 showOverviewMode(animated, false);
3132 }
3133
3134 /**
3135 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3136 * onto one of the overview panel buttons.
3137 */
3138 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3139 Runnable postAnimRunnable = null;
3140 if (requestButtonFocus) {
3141 postAnimRunnable = new Runnable() {
3142 @Override
3143 public void run() {
3144 // Hitting the menu button when in touch mode does not trigger touch mode to
3145 // be disabled, so if requested, force focus on one of the overview panel
3146 // buttons.
3147 mOverviewPanel.requestFocusFromTouch();
3148 }
3149 };
3150 }
Adam Cohened307df2013-10-02 09:37:31 -07003151 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003152 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003153 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003154 mState = State.WORKSPACE;
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07003155 // If animated from long press, then don't allow any of the controller in the drag
3156 // layer to intercept any remaining touch.
3157 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04003158 }
3159
Winson Chungb745afb2015-03-02 11:51:23 -08003160 /**
3161 * Shows the apps view.
3162 */
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003163 public void showAppsView(boolean animated, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003164 boolean focusSearchBar) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003165 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07003166 if (updatePredictedApps) {
3167 tryAndUpdatePredictedApps();
3168 }
Winson Chung76648c52015-07-10 14:33:23 -07003169 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003170 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003171
Winson Chungb745afb2015-03-02 11:51:23 -08003172 /**
3173 * Shows the widgets view.
3174 */
3175 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003176 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003177 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003178 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003179 }
Winson Chung76648c52015-07-10 14:33:23 -07003180 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003181
3182 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003183 @Override
3184 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003185 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003186 }
3187 });
Winson Chungb745afb2015-03-02 11:51:23 -08003188 }
3189
3190 /**
3191 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003192 *
3193 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003194 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003195 // TODO: calling method should use the return value so that when {@code false} is returned
3196 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003197 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003198 if (!(mState == State.WORKSPACE ||
3199 mState == State.APPS_SPRING_LOADED ||
3200 mState == State.WIDGETS_SPRING_LOADED ||
3201 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003202 return false;
3203 }
3204 if (toState != State.APPS && toState != State.WIDGETS) {
3205 return false;
3206 }
Winson Chungb745afb2015-03-02 11:51:23 -08003207
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003208 // This is a safe and supported transition to bypass spring_loaded mode.
3209 if (mExitSpringLoadedModeRunnable != null) {
3210 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3211 mExitSpringLoadedModeRunnable = null;
3212 }
3213
Winson Chungb745afb2015-03-02 11:51:23 -08003214 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003215 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3216 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003217 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003218 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003219 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003220
Michael Jurkab3e22d92011-10-31 15:58:33 -07003221 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003222 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003223
Michael Jurkab3e22d92011-10-31 15:58:33 -07003224 closeFolder();
Tony Wickham49c8d292016-07-01 11:44:34 -07003225 closeShortcutsContainer();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003226
3227 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003228 getWindow().getDecorView()
3229 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003230 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003231 }
3232
Winson Chungcd99cd32015-04-29 11:03:24 -07003233 /**
3234 * Updates the workspace and interaction state on state change, and return the animation to this
3235 * new state.
3236 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003237 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003238 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003239 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003240 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003241 updateInteraction(fromState, toState);
3242 return anim;
3243 }
3244
Hyunyoung Song3f471442015-04-08 19:01:34 -07003245 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003246 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003247 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003248 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003249 }
Winson Chungb745afb2015-03-02 11:51:23 -08003250
Winson Chung006ee262015-08-03 14:40:11 -07003251 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003252 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003253 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003254
3255 if (isAppsViewVisible()) {
3256 mState = State.APPS_SPRING_LOADED;
3257 } else if (isWidgetsViewVisible()) {
3258 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003259 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003260 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003261 } else {
3262 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003263 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003264 }
Adam Cohen7777d962011-08-18 18:58:38 -07003265
Hyunyoung Song3f471442015-04-08 19:01:34 -07003266 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003267 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003268 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003269
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003270 if (mExitSpringLoadedModeRunnable != null) {
3271 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3272 }
3273 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003274 @Override
3275 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003276 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003277 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3278 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003279 // Before we show workspace, hide all apps again because
3280 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3281 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003282 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003283 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003284 } else {
3285 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003286 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003287 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07003288 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003289 };
3290 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003291 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003292
Tony Wickhame0c33232016-02-08 11:37:04 -08003293 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003294 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3295 || mState == State.WIDGETS_SPRING_LOADED;
3296 }
3297
Michael Jurkad3ef3062010-11-23 16:23:58 -08003298 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003299 if (mState == State.APPS_SPRING_LOADED) {
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003300 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07003301 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003302 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003303 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003304 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3305 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003306 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003307 }
3308
Winson Chung4ac30062015-05-08 17:34:17 -07003309 /**
3310 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3311 * resumed.
3312 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003313 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003314 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003315 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003316 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003317 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003318 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003319 }
3320 }
3321 }
3322
Michael Jurkab3e22d92011-10-31 15:58:33 -07003323 void lockAllApps() {
3324 // TODO
3325 }
3326
3327 void unlockAllApps() {
3328 // TODO
3329 }
3330
Michael Jurkad7c28052012-04-27 15:43:36 -07003331 @Override
3332 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003333 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003334 final List<CharSequence> text = event.getText();
3335 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003336 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003337 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003338 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003339 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003340 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003341 } else if (mWorkspace != null) {
3342 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003343 } else {
3344 text.add(getString(R.string.all_apps_home_button_label));
3345 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003346 return result;
3347 }
3348
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003349 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003350 * If the activity is currently paused, signal that we need to run the passed Runnable
3351 * in onResume.
3352 *
3353 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003354 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3355 * wrong when we're not running, and if the activity comes back to what the configuration was
3356 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003357 *
3358 * Implementation of the method from LauncherModel.Callbacks.
3359 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003360 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003361 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003362 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003363 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003364 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003365 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003366 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003367 }
3368 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003369 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003370 return true;
3371 } else {
3372 return false;
3373 }
3374 }
3375
Michael Jurkac402cd92013-05-20 15:49:32 +02003376 private boolean waitUntilResume(Runnable run) {
3377 return waitUntilResume(run, false);
3378 }
3379
Michael Jurka1e2f4652013-07-08 18:03:46 -07003380 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003381 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003382 }
3383
Michael Jurka7607c2f2013-04-03 14:33:19 -07003384 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003385 * If the activity is currently paused, signal that we need to re-run the loader
3386 * in onResume.
3387 *
3388 * This needs to be called from incoming places where resources might have been loaded
3389 * while we are paused. That is becaues the Configuration might be wrong
3390 * when we're not running, and if it comes back to what it was when we
3391 * were paused, we are not restarted.
3392 *
3393 * Implementation of the method from LauncherModel.Callbacks.
3394 *
3395 * @return true if we are currently paused. The caller might be able to
3396 * skip some work in that case since we will come back again.
3397 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003398 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003399 public boolean setLoadOnResume() {
3400 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003401 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003402 mOnResumeNeedsLoad = true;
3403 return true;
3404 } else {
3405 return false;
3406 }
3407 }
3408
3409 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003410 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003411 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003412 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003413 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003414 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003415 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003416 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003417 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003418 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003419 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003420
Joe Onorato9c1289c2009-08-17 11:03:03 -04003421 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003422 * Clear any pending bind callbacks. This is called when is loader is planning to
3423 * perform a full rebind from scratch.
3424 */
3425 @Override
3426 public void clearPendingBinds() {
3427 mBindOnResumeCallbacks.clear();
3428 if (mPendingExecutor != null) {
3429 mPendingExecutor.markCompleted();
3430 mPendingExecutor = null;
3431 }
3432 }
3433
3434 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003435 * Refreshes the shortcuts shown on the workspace.
3436 *
3437 * Implementation of the method from LauncherModel.Callbacks.
3438 */
3439 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003440 if (LauncherAppState.PROFILE_STARTUP) {
3441 Trace.beginSection("Starting page bind");
3442 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003443 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003444
Winson Chungd64d1762013-08-20 14:37:16 -07003445 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003446 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003447 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003448
Winson Chung3d503fb2011-07-13 17:25:49 -07003449 if (mHotseat != null) {
3450 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003451 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003452 if (LauncherAppState.PROFILE_STARTUP) {
3453 Trace.endSection();
3454 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003455 }
3456
Adam Cohendcd297f2013-06-18 13:13:40 -07003457 @Override
3458 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003459 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003460 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3461 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003462 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3463 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3464 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003465 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3466 // If there are no screens, we need to have an empty screen
3467 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003468 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003469 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003470
3471 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003472 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003473 if (hasCustomContentToLeft()) {
3474 mWorkspace.createCustomContentContainer();
3475 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003476 }
Winsonc7d2e832016-07-28 12:24:55 -07003477
3478 // After we have added all the screens, if the wallpaper was locked to the default state,
3479 // then notify to indicate that it can be released and a proper wallpaper offset can be
3480 // computed before the next layout
3481 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07003482 }
3483
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003484 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003485 int count = orderedScreenIds.size();
3486 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003487 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003488 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003489 // No need to bind the first screen, as its always bound.
3490 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3491 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003492 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003493 }
3494
Winson Chungd64d1762013-08-20 14:37:16 -07003495 public void bindAppsAdded(final ArrayList<Long> newScreens,
3496 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003497 final ArrayList<ItemInfo> addAnimated,
3498 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003499 Runnable r = new Runnable() {
3500 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003501 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003502 }
3503 };
3504 if (waitUntilResume(r)) {
3505 return;
3506 }
3507
Winson Chungd64d1762013-08-20 14:37:16 -07003508 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003509 if (newScreens != null) {
3510 bindAddScreens(newScreens);
3511 }
Winson Chungd64d1762013-08-20 14:37:16 -07003512
3513 // We add the items without animation on non-visible pages, and with
3514 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003515 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003516 bindItems(addNotAnimated, 0,
3517 addNotAnimated.size(), false);
3518 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003519 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003520 bindItems(addAnimated, 0,
3521 addAnimated.size(), true);
3522 }
Winson Chungc58497e2013-09-03 17:48:37 -07003523
Winson Chung87412982013-10-03 18:34:14 -07003524 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003525 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003526
Winson Chungb745afb2015-03-02 11:51:23 -08003527 if (addedApps != null && mAppsView != null) {
3528 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003529 }
Winson Chungd64d1762013-08-20 14:37:16 -07003530 }
3531
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003532 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003533 * Bind the items start-end from the list.
3534 *
3535 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003536 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003537 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003538 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3539 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003540 Runnable r = new Runnable() {
3541 public void run() {
3542 bindItems(shortcuts, start, end, forceAnimateIcons);
3543 }
3544 };
3545 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003546 return;
3547 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003548
Winson Chungf0c6ae02012-03-21 16:10:31 -07003549 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003550 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3551 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003552 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003553 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003554 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003555 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003556 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003557
3558 // Short circuit if we are loading dock items for a configuration which has no dock
3559 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3560 mHotseat == null) {
3561 continue;
3562 }
3563
Sunny Goyal639e9062015-08-19 19:17:06 -07003564 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003565 switch (item.itemType) {
3566 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3567 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003568 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003569 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003570 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003571 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003572 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003573 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003574 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003575 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003576 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003577 default:
3578 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003579 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003580
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003581 /*
3582 * Remove colliding items.
3583 */
3584 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3585 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3586 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3587 View v = cl.getChildAt(item.cellX, item.cellY);
3588 Object tag = v.getTag();
3589 String desc = "Collision while binding workspace item: " + item
3590 + ". Collides with " + tag;
3591 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3592 throw (new RuntimeException(desc));
3593 } else {
3594 Log.d(TAG, desc);
3595 LauncherModel.deleteItemFromDatabase(this, item);
3596 continue;
3597 }
3598 }
3599 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003600 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3601 item.cellY, 1, 1);
3602 if (animateIcons) {
3603 // Animate all the applications up now
3604 view.setAlpha(0f);
3605 view.setScaleX(0f);
3606 view.setScaleY(0f);
3607 bounceAnims.add(createNewAppBounceAnimation(view, i));
3608 newShortcutsScreenId = item.screenId;
3609 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003610 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003611
Winson Chung997a9232013-07-24 15:33:46 -07003612 if (animateIcons) {
3613 // Animate to the correct page
3614 if (newShortcutsScreenId > -1) {
3615 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003616 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003617 final Runnable startBounceAnimRunnable = new Runnable() {
3618 public void run() {
3619 anim.playTogether(bounceAnims);
3620 anim.start();
3621 }
3622 };
Winson Chung997a9232013-07-24 15:33:46 -07003623 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003624 // We post the animation slightly delayed to prevent slowdowns
3625 // when we are loading right after we return to launcher.
3626 mWorkspace.postDelayed(new Runnable() {
3627 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003628 if (mWorkspace != null) {
3629 mWorkspace.snapToPage(newScreenIndex);
3630 mWorkspace.postDelayed(startBounceAnimRunnable,
3631 NEW_APPS_ANIMATION_DELAY);
3632 }
Winson Chung94d67682013-09-25 16:29:40 -07003633 }
3634 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003635 } else {
3636 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003637 }
3638 }
Winson Chung64359a52013-07-08 17:17:08 -07003639 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003640 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003641 }
3642
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003643 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3644 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3645 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003646 view.updateAppWidget(null);
3647 view.setOnClickListener(this);
3648 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003649 mWorkspace.requestLayout();
3650 }
3651
Joe Onorato9c1289c2009-08-17 11:03:03 -04003652 /**
3653 * Add the views for a widget to the workspace.
3654 *
3655 * Implementation of the method from LauncherModel.Callbacks.
3656 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003657 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003658 Runnable r = new Runnable() {
3659 public void run() {
3660 bindAppWidget(item);
3661 }
3662 };
3663 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003664 return;
3665 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003666
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003667 if (mIsSafeModeEnabled) {
3668 bindSafeModeWidget(item);
3669 return;
3670 }
3671
Daniel Sandler843e8602010-06-07 14:59:01 -04003672 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3673 if (DEBUG_WIDGETS) {
3674 Log.d(TAG, "bindAppWidget: " + item);
3675 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003676
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003677 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003678
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003679 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3680 // If the provider is not ready, bind as a pending widget.
3681 appWidgetInfo = null;
3682 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3683 // The widget id is not valid. Try to find the widget based on the provider info.
3684 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3685 } else {
3686 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3687 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003688
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003689 // If the provider is ready, but the width is not yet restored, try to restore it.
3690 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3691 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003692 if (appWidgetInfo == null) {
3693 if (DEBUG_WIDGETS) {
3694 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3695 + " belongs to component " + item.providerName
Hyunyoung Song0de01172016-10-05 16:27:48 -07003696 + ", as the provider is null");
Sunny Goyalff572272014-07-23 13:58:07 -07003697 }
3698 LauncherModel.deleteItemFromDatabase(this, item);
3699 return;
3700 }
Sunny Goyalff572272014-07-23 13:58:07 -07003701
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003702 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003703 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003704 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3705 // Id has not been allocated yet. Allocate a new id.
3706 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3707 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003708
Sunny Goyald478c832016-04-01 12:04:16 -07003709 // Also try to bind the widget. If the bind fails, the user will be shown
3710 // a click to setup UI, which will ask for the bind permission.
3711 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3712 pendingInfo.spanX = item.spanX;
3713 pendingInfo.spanY = item.spanY;
3714 pendingInfo.minSpanX = item.minSpanX;
3715 pendingInfo.minSpanY = item.minSpanY;
3716 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003717
3718 boolean isDirectConfig =
3719 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3720 if (isDirectConfig && item.bindOptions != null) {
3721 Bundle newOptions = item.bindOptions.getExtras();
3722 if (options != null) {
3723 newOptions.putAll(options);
3724 }
3725 options = newOptions;
3726 }
Sunny Goyald478c832016-04-01 12:04:16 -07003727 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3728 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003729
Sunny Goyal86df1382016-08-10 15:03:22 -07003730 // We tried to bind once. If we were not able to bind, we would need to
3731 // go through the permission dialog, which means we cannot skip the config
3732 // activity.
3733 item.bindOptions = null;
3734 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3735
Sunny Goyald478c832016-04-01 12:04:16 -07003736 // Bind succeeded
3737 if (success) {
3738 // If the widget has a configure activity, it is still needs to set it up,
3739 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003740 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003741 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3742 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003743 }
Sunny Goyald478c832016-04-01 12:04:16 -07003744
3745 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003746 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003747 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003748 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003749 // The widget was marked as UI not ready, but there is no configure activity to
3750 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003751 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3752 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003753 }
Sunny Goyalff572272014-07-23 13:58:07 -07003754 }
3755
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003756 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003757 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003758 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3759 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003760 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003761
Sunny Goyal56c73602015-09-25 12:55:01 -07003762 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003763 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003764 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003765 deleteWidgetInfo(item);
3766 return;
3767 }
3768
Sunny Goyal233ee962015-08-03 13:05:01 -07003769 item.minSpanX = appWidgetInfo.minSpanX;
3770 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003771 addAppWidgetToWorkspace(
3772 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3773 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003774 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003775 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003776 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003777 view.updateAppWidget(null);
3778 view.setOnClickListener(this);
3779 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003780 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003781 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003782
Daniel Sandler843e8602010-06-07 14:59:01 -04003783 if (DEBUG_WIDGETS) {
3784 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3785 + (SystemClock.uptimeMillis()-start) + "ms");
3786 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003787 }
3788
Sunny Goyalff572272014-07-23 13:58:07 -07003789 /**
3790 * Restores a pending widget.
3791 *
3792 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003793 */
Sunny Goyald478c832016-04-01 12:04:16 -07003794 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003795 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3796 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3797 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003798 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003799 }
3800
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003801 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003802 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003803
3804 mWorkspace.reinflateWidgetsIfNecessary();
3805 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003806 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003807 }
3808
Adam Cohen1462de32012-07-24 22:34:36 -07003809 public void onPageBoundSynchronously(int page) {
3810 mSynchronouslyBoundPages.add(page);
3811 }
3812
Sunny Goyal527c7d32015-08-28 15:19:36 -07003813 @Override
3814 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3815 if (mPendingExecutor != null) {
3816 mPendingExecutor.markCompleted();
3817 }
3818 mPendingExecutor = executor;
3819 executor.attachTo(this);
3820 }
3821
3822 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3823 if (mPendingExecutor == executor) {
3824 mPendingExecutor = null;
3825 }
3826 }
3827
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003828 @Override
3829 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3830 Runnable r = new Runnable() {
3831 public void run() {
3832 finishFirstPageBind(executor);
3833 }
3834 };
3835 if (waitUntilResume(r)) {
3836 return;
3837 }
3838
3839 Runnable onComplete = new Runnable() {
3840 @Override
3841 public void run() {
3842 if (executor != null) {
3843 executor.onLoadAnimationCompleted();
3844 }
3845 }
3846 };
3847 if (mDragLayer.getAlpha() < 1) {
3848 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3849 } else {
3850 onComplete.run();
3851 }
3852 }
3853
Joe Onorato9c1289c2009-08-17 11:03:03 -04003854 /**
3855 * Callback saying that there aren't any more items to bind.
3856 *
3857 * Implementation of the method from LauncherModel.Callbacks.
3858 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003859 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003860 Runnable r = new Runnable() {
3861 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003862 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003863 }
3864 };
3865 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003866 return;
3867 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003868 if (LauncherAppState.PROFILE_STARTUP) {
3869 Trace.beginSection("Page bind completed");
3870 }
Adam Cohen1462de32012-07-24 22:34:36 -07003871 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003872
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003873 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003874
Sunny Goyal2100c782016-08-22 16:00:03 -07003875 if (mPendingActivityResult != null) {
3876 handleActivityResult(mPendingActivityResult.requestCode,
3877 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3878 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003879 }
3880
Sunny Goyal756adbc2015-04-16 15:20:51 -07003881 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003882
3883 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003884 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003885 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003886 if (LauncherAppState.PROFILE_STARTUP) {
3887 Trace.endSection();
3888 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003889 }
3890
Winson Chunga2413752012-04-03 14:22:34 -07003891 private boolean canRunNewAppsAnimation() {
3892 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003893 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003894 }
3895
Winson Chungc9168342013-06-26 14:54:55 -07003896 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003897 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003898 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3899 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003900 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003901 return bounceAnim;
3902 }
3903
Adam Cohen27772732013-11-11 14:00:56 +00003904 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07003905 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00003906 }
3907
Tony Wickham55616cd2015-09-23 14:55:17 -07003908 public int getSearchBarHeight() {
3909 if (mLauncherCallbacks != null) {
3910 return mLauncherCallbacks.getSearchBarHeight();
3911 }
3912 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
3913 }
3914
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003915 /**
Winson Chungbb785c62015-05-05 10:05:08 -07003916 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
3917 * multiple calls to bind the same list.)
3918 */
3919 @Thunk ArrayList<AppInfo> mTmpAppsList;
3920 private Runnable mBindAllApplicationsRunnable = new Runnable() {
3921 public void run() {
3922 bindAllApplications(mTmpAppsList);
3923 mTmpAppsList = null;
3924 }
3925 };
3926
3927 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003928 * Add the icons for all apps.
3929 *
3930 * Implementation of the method from LauncherModel.Callbacks.
3931 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003932 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07003933 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
3934 mTmpAppsList = apps;
3935 return;
3936 }
3937
Winson Chungb745afb2015-03-02 11:51:23 -08003938 if (mAppsView != null) {
3939 mAppsView.setApps(apps);
3940 }
Adam Cohen9211d422014-10-07 18:14:53 -07003941 if (mLauncherCallbacks != null) {
3942 mLauncherCallbacks.bindAllApplications(apps);
3943 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003944 }
3945
3946 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003947 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
3948 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
3949 */
3950 @Override
3951 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
3952 mDeepShortcutMap = deepShortcutMapCopy;
3953 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
3954 }
3955
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003956 public List<String> getShortcutIdsForItem(ItemInfo info) {
3957 if (!DeepShortcutManager.supportsShortcuts(info)) {
3958 return Collections.EMPTY_LIST;
3959 }
3960 ComponentName component = info.getTargetComponent();
Hyunyoung Songd3bf9802016-08-29 14:43:53 -07003961 if (component == null) {
3962 return Collections.EMPTY_LIST;
3963 }
3964
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003965 List<String> ids = mDeepShortcutMap.get(new ComponentKey(component, info.user));
3966 return ids == null ? Collections.EMPTY_LIST : ids;
3967 }
3968
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003969 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003970 * A package was updated.
3971 *
3972 * Implementation of the method from LauncherModel.Callbacks.
3973 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003974 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003975 Runnable r = new Runnable() {
3976 public void run() {
3977 bindAppsUpdated(apps);
3978 }
3979 };
3980 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003981 return;
3982 }
3983
Winson Chungb745afb2015-03-02 11:51:23 -08003984 if (mAppsView != null) {
3985 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07003986 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003987 }
3988
Sunny Goyal4390ace2014-10-13 11:33:11 -07003989 @Override
3990 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
3991 Runnable r = new Runnable() {
3992 public void run() {
3993 bindWidgetsRestored(widgets);
3994 }
3995 };
3996 if (waitUntilResume(r)) {
3997 return;
3998 }
3999 mWorkspace.widgetsRestored(widgets);
4000 }
4001
Joe Onorato9c1289c2009-08-17 11:03:03 -04004002 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004003 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004004 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004005 *
4006 * @param updated list of shortcuts which have changed.
4007 * @param removed list of shortcuts which were deleted in the background. This can happen when
4008 * an app gets removed from the system or some of its components are no longer
4009 * available.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004010 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004011 @Override
4012 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4013 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004014 Runnable r = new Runnable() {
4015 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004016 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004017 }
4018 };
4019 if (waitUntilResume(r)) {
4020 return;
4021 }
4022
Sunny Goyal4390ace2014-10-13 11:33:11 -07004023 if (!updated.isEmpty()) {
4024 mWorkspace.updateShortcuts(updated);
4025 }
4026
4027 if (!removed.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004028 HashSet<ComponentName> removedComponents = new HashSet<>();
4029 HashSet<ShortcutKey> removedDeepShortcuts = new HashSet<>();
4030
Sunny Goyal4390ace2014-10-13 11:33:11 -07004031 for (ShortcutInfo si : removed) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004032 if (si.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07004033 removedDeepShortcuts.add(ShortcutKey.fromShortcutInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004034 } else {
4035 removedComponents.add(si.getTargetComponent());
4036 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004037 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004038
4039 if (!removedComponents.isEmpty()) {
4040 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(removedComponents, user);
4041 mWorkspace.removeItemsByMatcher(matcher);
4042 mDragController.onAppsRemoved(matcher);
4043 }
4044
4045 if (!removedDeepShortcuts.isEmpty()) {
4046 ItemInfoMatcher matcher = ItemInfoMatcher.ofShortcutKeys(removedDeepShortcuts);
4047 mWorkspace.removeItemsByMatcher(matcher);
4048 mDragController.onAppsRemoved(matcher);
4049 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004050 }
4051 }
4052
4053 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004054 * Update the state of a package, typically related to install state.
4055 *
4056 * Implementation of the method from LauncherModel.Callbacks.
4057 */
Sunny Goyale755d462014-07-22 13:48:29 -07004058 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004059 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4060 Runnable r = new Runnable() {
4061 public void run() {
4062 bindRestoreItemsChange(updates);
4063 }
4064 };
4065 if (waitUntilResume(r)) {
4066 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004067 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004068
Sunny Goyal756adbc2015-04-16 15:20:51 -07004069 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004070 }
4071
4072 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004073 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004074 * in addition to specific applications to remove, the reason being that
4075 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004076 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004077 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004078 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004079 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004080 public void bindWorkspaceComponentsRemoved(
4081 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4082 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004083 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004084 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004085 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004086 }
Winson Chungd64d1762013-08-20 14:37:16 -07004087 };
4088 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004089 return;
4090 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004091 if (!packageNames.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004092 ItemInfoMatcher matcher = ItemInfoMatcher.ofPackages(packageNames, user);
4093 mWorkspace.removeItemsByMatcher(matcher);
4094 mDragController.onAppsRemoved(matcher);
4095
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004096 }
4097 if (!components.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004098 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(components, user);
4099 mWorkspace.removeItemsByMatcher(matcher);
4100 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004101 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004102 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004103
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004104 @Override
4105 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4106 Runnable r = new Runnable() {
4107 public void run() {
4108 bindAppInfosRemoved(appInfos);
4109 }
4110 };
4111 if (waitUntilResume(r)) {
4112 return;
Joe Onorato36115782010-06-17 13:28:48 -04004113 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004114
Winson Chungdf95eb12013-10-16 14:57:07 -07004115 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004116 if (mAppsView != null) {
4117 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004118 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004119 }
Joe Onoratobe386092009-11-17 17:32:16 -08004120
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004121 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004122 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004123 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004124 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004125 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004126
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004127 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004128 public void bindWidgetsModel(WidgetsModel model) {
4129 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004130 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004131 return;
4132 }
4133
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004134 if (mWidgetsView != null && model != null) {
4135 mWidgetsView.addWidgets(model);
4136 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004137 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004138 }
4139
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004140 @Override
4141 public void notifyWidgetProvidersChanged() {
4142 if (mWorkspace.getState().shouldUpdateWidget) {
4143 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4144 }
4145 }
4146
Winson Chung400438b2011-01-16 17:53:48 -08004147 private int mapConfigurationOriActivityInfoOri(int configOri) {
4148 final Display d = getWindowManager().getDefaultDisplay();
4149 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4150 switch (d.getRotation()) {
4151 case Surface.ROTATION_0:
4152 case Surface.ROTATION_180:
4153 // We are currently in the same basic orientation as the natural orientation
4154 naturalOri = configOri;
4155 break;
4156 case Surface.ROTATION_90:
4157 case Surface.ROTATION_270:
4158 // We are currently in the other basic orientation to the natural orientation
4159 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4160 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4161 break;
4162 }
4163
4164 int[] oriMap = {
4165 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4166 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4167 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4168 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4169 };
4170 // Since the map starts at portrait, we need to offset if this device's natural orientation
4171 // is landscape.
4172 int indexOffset = 0;
4173 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4174 indexOffset = 1;
4175 }
4176 return oriMap[(d.getRotation() + indexOffset) % 4];
4177 }
Adam Cohen446e9402011-09-15 18:21:21 -07004178
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004179 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004180 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004181 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004182 if (Utilities.ATLEAST_JB_MR2) {
4183 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4184 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004185 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4186 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004187 }
Winson Chung4b919f82012-05-01 10:44:08 -07004188 }
4189 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004190
Winson Chung4b919f82012-05-01 10:44:08 -07004191 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004192 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004193 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004194 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004195 } else {
4196 mHandler.postDelayed(new Runnable() {
4197 public void run() {
4198 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4199 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004200 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004201 }
Winson Chung4b919f82012-05-01 10:44:08 -07004202 }
Winson Chung400438b2011-01-16 17:53:48 -08004203 }
4204
Hyunyoung Songc001cf52016-07-21 17:32:43 -07004205 private void markAppsViewShown() {
4206 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4207 return;
4208 }
4209 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
4210 }
4211
4212 private boolean shouldShowDiscoveryBounce() {
4213 if (mState != mState.WORKSPACE) {
4214 return false;
4215 }
4216 if (mLauncherCallbacks != null && mLauncherCallbacks.shouldShowDiscoveryBounce()) {
4217 return true;
4218 }
4219 if (!mIsResumeFromActionScreenOff) {
4220 return false;
4221 }
4222 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4223 return false;
4224 }
4225 return true;
4226 }
4227
Winson Chung5ffd51d2015-06-25 11:36:50 -07004228 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004229 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004230 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004231 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004232 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004233 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004234 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4235 if (userHandle != null) {
4236 user = UserHandleCompat.fromUser(userHandle);
4237 }
4238 }
4239 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004240 }
4241
4242 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004243 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004244 if (user == null) {
4245 user = UserHandleCompat.myUserHandle();
4246 }
4247
4248 // Called from search suggestion, add the profile extra to the intent to ensure that we
4249 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004250 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004251 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004252 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004253 return null;
4254 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004255 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004256 }
4257
Winson Chung5ffd51d2015-06-25 11:36:50 -07004258 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004259 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4260 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004261 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004262 UserHandleCompat.myUserHandle());
4263 }
4264
Winson Chung5ffd51d2015-06-25 11:36:50 -07004265 protected void moveWorkspaceToDefaultScreen() {
4266 mWorkspace.moveToDefaultScreen(false);
4267 }
4268
Winson Chung80baf5a2010-08-09 16:03:15 -07004269 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004270 * Returns a FastBitmapDrawable with the icon, accurately sized.
4271 */
4272 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4273 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4274 d.setFilterBitmap(true);
4275 resizeIconDrawable(d);
4276 return d;
4277 }
4278
4279 /**
4280 * Resizes an icon drawable to the correct icon size.
4281 */
Winson5fbe0742015-09-30 15:33:00 -07004282 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004283 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004284 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004285 }
4286
4287 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004288 * Prints out out state for debugging.
4289 */
4290 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004291 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004292 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
Sunny Goyal2100c782016-08-22 16:00:03 -07004293 Log.d(TAG, "mPendingRequestArgs=" + mPendingRequestArgs);
4294 Log.d(TAG, "mPendingActivityResult=" + mPendingActivityResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004295 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004296 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004297
Daniel Sandler325dc232013-06-05 22:57:57 -04004298 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004299 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004300
4301 @Override
4302 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4303 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004304 // Dump workspace
4305 writer.println(prefix + "Workspace Items");
4306 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4307 writer.println(prefix + " Homescreen " + i);
4308
4309 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4310 for (int j = 0; j < layout.getChildCount(); j++) {
4311 Object tag = layout.getChildAt(j).getTag();
4312 if (tag != null) {
4313 writer.println(prefix + " " + tag.toString());
4314 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004315 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004316 }
Sunny Goyala1365452015-10-01 15:46:24 -07004317
4318 writer.println(prefix + " Hotseat");
4319 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4320 for (int j = 0; j < layout.getChildCount(); j++) {
4321 Object tag = layout.getChildAt(j).getTag();
4322 if (tag != null) {
4323 writer.println(prefix + " " + tag.toString());
4324 }
4325 }
4326
Sunny Goyal713edfc2016-05-06 09:58:34 -07004327 try {
4328 FileLog.flushAll(writer);
4329 } catch (Exception e) {
4330 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004331 }
4332
Adam Cohen9211d422014-10-07 18:14:53 -07004333 if (mLauncherCallbacks != null) {
4334 mLauncherCallbacks.dump(prefix, fd, writer, args);
4335 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004336 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004337
Sunny Goyal66b24572016-09-21 15:57:55 -07004338 @Override
4339 @TargetApi(Build.VERSION_CODES.N)
4340 public void onProvideKeyboardShortcuts(
4341 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
4342
4343 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
4344 if (mState == State.WORKSPACE) {
4345 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
4346 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
4347 }
4348 View currentFocus = getCurrentFocus();
4349 if (new CustomActionsPopup(this, currentFocus).canShow()) {
4350 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
4351 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
4352 }
4353 if (currentFocus instanceof BubbleTextView &&
4354 ((BubbleTextView) currentFocus).hasDeepShortcuts()) {
4355 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
4356 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
4357 }
4358 if (!shortcutInfos.isEmpty()) {
4359 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
4360 }
4361
4362 super.onProvideKeyboardShortcuts(data, menu, deviceId);
4363 }
4364
4365 @Override
4366 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
4367 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
4368 switch (keyCode) {
4369 case KeyEvent.KEYCODE_A:
4370 if (mState == State.WORKSPACE) {
4371 showAppsView(true, true, false);
4372 return true;
4373 }
4374 break;
4375 case KeyEvent.KEYCODE_S: {
4376 View focusedView = getCurrentFocus();
4377 if (focusedView instanceof BubbleTextView
4378 && focusedView.getTag() instanceof ItemInfo
4379 && mAccessibilityDelegate.performAction(focusedView,
4380 (ItemInfo) focusedView.getTag(),
4381 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
4382 getOpenShortcutsContainer().requestFocus();
4383 return true;
4384 }
4385 break;
4386 }
4387 case KeyEvent.KEYCODE_O:
4388 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
4389 return true;
4390 }
4391 break;
4392 }
4393 }
4394 return super.onKeyShortcut(keyCode, event);
4395 }
4396
Adam Cohen59400422014-03-05 18:07:04 -08004397 public static CustomAppWidget getCustomAppWidget(String name) {
4398 return sCustomAppWidgets.get(name);
4399 }
4400
4401 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4402 return sCustomAppWidgets;
4403 }
4404
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004405 public static Launcher getLauncher(Context context) {
4406 if (context instanceof Launcher) {
4407 return (Launcher) context;
4408 }
4409 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4410 }
4411
Sunny Goyal745bad92016-05-02 10:54:12 -07004412 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4413
4414 @Override
4415 public void onSharedPreferenceChanged(
4416 SharedPreferences sharedPreferences, String key) {
4417 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4418 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4419 if (!waitUntilResume(this, true)) {
4420 run();
4421 }
4422 }
4423 }
4424
4425 @Override
4426 public void run() {
4427 setOrientation();
4428 }
4429 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004430}