blob: 30d5b17c51fbb7d32e1e3da8ed47bc71366fbdec [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 Song645764e2016-06-06 14:19:02 -07001343 mDragController.setDragScoller(mWorkspace);
1344 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
1361 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1362 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1363 @Override
1364 public boolean onLongClick(View v) {
1365 return v.performClick();
1366 }
1367 };
1368
1369 // Bind wallpaper button actions
1370 View wallpaperButton = findViewById(R.id.wallpaper_button);
1371 wallpaperButton.setOnClickListener(new OnClickListener() {
1372 @Override
1373 public void onClick(View view) {
1374 if (!mWorkspace.isSwitchingState()) {
1375 onClickWallpaperPicker(view);
1376 }
1377 }
1378 });
1379 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1380 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1381
1382 // Bind widget button actions
1383 mWidgetsButton = findViewById(R.id.widget_button);
1384 mWidgetsButton.setOnClickListener(new OnClickListener() {
1385 @Override
1386 public void onClick(View view) {
1387 if (!mWorkspace.isSwitchingState()) {
1388 onClickAddWidgetButton(view);
1389 }
1390 }
1391 });
1392 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1393 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1394
1395 // Bind settings actions
1396 View settingsButton = findViewById(R.id.settings_button);
1397 boolean hasSettings = hasSettings();
1398 if (hasSettings) {
1399 settingsButton.setOnClickListener(new OnClickListener() {
1400 @Override
1401 public void onClick(View view) {
1402 if (!mWorkspace.isSwitchingState()) {
1403 onClickSettingsButton(view);
1404 }
1405 }
1406 });
1407 settingsButton.setOnLongClickListener(performClickOnLongClick);
1408 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1409 } else {
1410 settingsButton.setVisibility(View.GONE);
1411 }
1412
1413 mOverviewPanel.setAlpha(0f);
1414 }
1415
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001417 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001418 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001419 */
1420 public void setAllAppsButton(View allAppsButton) {
1421 mAllAppsButton = allAppsButton;
1422 }
1423
Sunny Goyalbb011da2016-06-15 15:42:29 -07001424 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001425 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001426 }
1427
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001428 public View getWidgetsButton() {
1429 return mWidgetsButton;
1430 }
1431
Anjali Koppal5ad44842014-03-10 20:34:39 -07001432 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433 * Creates a view representing a shortcut.
1434 *
1435 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001437 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001438 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001439 }
1440
1441 /**
1442 * Creates a view representing a shortcut inflated from the specified resource.
1443 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 * @param parent The group the shortcut belongs to.
1445 * @param info The data structure describing the shortcut.
1446 *
1447 * @return A View inflated from layoutResId.
1448 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001449 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001450 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001451 parent, false);
1452 favorite.applyFromShortcutInfo(info, mIconCache);
1453 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001455 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 return favorite;
1457 }
1458
1459 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 * Add a shortcut to the workspace.
1461 *
1462 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001464 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001465 int cellY, PendingRequestArgs args) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001466 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001467 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001468
Sunny Goyal5c97f512015-05-19 16:03:28 -07001469 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001470 if (info == null || args.getRequestCode() != REQUEST_CREATE_SHORTCUT ||
1471 args.getPendingIntent().getComponent() == null) {
1472 return;
1473 }
1474 if (!PackageManagerHelper.hasPermissionForActivity(
1475 this, info.intent, args.getPendingIntent().getComponent().getPackageName())) {
1476 // The app is trying to add a shortcut without sufficient permissions
1477 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
Adam Cohend9198822011-11-22 16:42:47 -08001478 return;
1479 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001480 final View view = createShortcut(info);
1481
Sunny Goyal5c97f512015-05-19 16:03:28 -07001482 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001483 // First we check if we already know the exact location where we want to add this item.
1484 if (cellX >= 0 && cellY >= 0) {
1485 cellXY[0] = cellX;
1486 cellXY[1] = cellY;
1487 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001488
1489 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001490 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001491 true, null,null)) {
1492 return;
1493 }
1494 DragObject dragObject = new DragObject();
1495 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001496 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1497 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001498 return;
1499 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001500 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001501 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001502 }
1503
1504 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001505 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001506 return;
1507 }
1508
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001509 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510
Sunny Goyal2100c782016-08-22 16:00:03 -07001511 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
1512 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 }
1514
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001516 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001518 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001520 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001521 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1522
Adam Cohened66b2b2012-01-23 17:28:51 -08001523 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001524 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001525 }
Romain Guycbb89e42009-06-08 15:52:54 -07001526
Adam Cohen59400422014-03-05 18:07:04 -08001527 if (appWidgetInfo.isCustomWidget) {
1528 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001529 }
1530
Adam Cohen59400422014-03-05 18:07:04 -08001531 LauncherAppWidgetInfo launcherInfo;
1532 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001533 launcherInfo.spanX = itemInfo.spanX;
1534 launcherInfo.spanY = itemInfo.spanY;
1535 launcherInfo.minSpanX = itemInfo.minSpanX;
1536 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001537 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001538
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001540 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541
Sunny Goyal2100c782016-08-22 16:00:03 -07001542 if (hostView == null) {
1543 // Perform actual inflation because we're live
1544 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001546 hostView.setVisibility(View.VISIBLE);
1547 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 }
Romain Guycbb89e42009-06-08 15:52:54 -07001549
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001550 private void addAppWidgetToWorkspace(
1551 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001552 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001553 hostView.setTag(item);
1554 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001555
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001556 hostView.setFocusable(true);
1557 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001558
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001559 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001560 item.cellX, item.cellY, item.spanX, item.spanY, insert);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001561 }
1562
Adam Cohended9f8d2010-11-03 13:25:16 -07001563 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1564 @Override
1565 public void onReceive(Context context, Intent intent) {
1566 final String action = intent.getAction();
1567 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Sunny Goyal6ad72f02016-09-23 11:01:10 -07001568 mDragLayer.clearResizeFrame();
Winson Chung337cd9d2011-03-30 10:39:30 -07001569
Winson Chungc100e8e2011-08-09 16:02:43 -07001570 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001571 // processing a multi-step drop
Sunny Goyal2100c782016-08-22 16:00:03 -07001572 if (mAppsView != null && mWidgetsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001573 if (!showWorkspace(false)) {
1574 // If we are already on the workspace, then manually reset all apps
1575 mAppsView.reset();
1576 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001577 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001578 mIsResumeFromActionScreenOff = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001579 }
1580 }
1581 };
1582
1583 @Override
1584 public void onAttachedToWindow() {
1585 super.onAttachedToWindow();
1586
1587 // Listen for broadcasts related to user-presence
1588 final IntentFilter filter = new IntentFilter();
1589 filter.addAction(Intent.ACTION_SCREEN_OFF);
Adam Cohended9f8d2010-11-03 13:25:16 -07001590 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001591 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001592 mAttached = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001593
1594 if (mLauncherCallbacks != null) {
1595 mLauncherCallbacks.onAttachedToWindow();
1596 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001597 }
1598
1599 @Override
1600 public void onDetachedFromWindow() {
1601 super.onDetachedFromWindow();
Adam Cohend113e0c2010-11-11 10:48:05 -08001602 if (mAttached) {
1603 unregisterReceiver(mReceiver);
1604 mAttached = false;
1605 }
Sunny Goyalc86df472016-02-25 09:19:38 -08001606
1607 if (mLauncherCallbacks != null) {
1608 mLauncherCallbacks.onDetachedFromWindow();
1609 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001610 }
1611
1612 public void onWindowVisibilityChanged(int visibility) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001613 // The following code used to be in onResume, but it turns out onResume is called when
1614 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1615 // is a more appropriate event to handle
Sunny Goyal1acc56a2016-09-25 21:23:25 -07001616 if (visibility == View.VISIBLE) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001617 if (!mWorkspaceLoading) {
1618 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001619 // We want to let Launcher draw itself at least once before we force it to build
1620 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001621 // apps is nice and speedy.
1622 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001623 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001624 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001625 if (mStarted) return;
1626 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001627 // We delay the layer building a bit in order to give
1628 // other message processing a time to run. In particular
1629 // this avoids a delay in hiding the IME if it was
1630 // currently shown, because doing that may involve
1631 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001632 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001633 final ViewTreeObserver.OnDrawListener listener = this;
1634 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001635 public void run() {
1636 if (mWorkspace != null &&
1637 mWorkspace.getViewTreeObserver() != null) {
1638 mWorkspace.getViewTreeObserver().
1639 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001640 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001641 }
1642 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001643 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001644 }
1645 });
1646 }
1647 clearTypedText();
1648 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001649 }
1650
Hyunyoung Song3f471442015-04-08 19:01:34 -07001651 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001652 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1653 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001654 }
1655
Winson Chunga6945242014-01-08 14:04:34 -08001656 public DragLayer getDragLayer() {
1657 return mDragLayer;
1658 }
1659
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001660 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001661 return mAppsView;
1662 }
1663
Hyunyoung Song3f471442015-04-08 19:01:34 -07001664 public WidgetsContainerView getWidgetsView() {
1665 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001666 }
1667
Winson Chunga6945242014-01-08 14:04:34 -08001668 public Workspace getWorkspace() {
1669 return mWorkspace;
1670 }
1671
Sunny Goyal6178f132016-07-11 17:30:03 -07001672 public View getQsbContainer() {
1673 return mQsbContainer;
1674 }
1675
Winson Chunga6945242014-01-08 14:04:34 -08001676 public Hotseat getHotseat() {
1677 return mHotseat;
1678 }
1679
Jorim Jaggid017f882014-01-14 17:08:48 -08001680 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001681 return mOverviewPanel;
1682 }
1683
Sunny Goyal47328fd2016-05-25 18:56:41 -07001684 public DropTargetBar getDropTargetBar() {
1685 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001686 }
1687
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001688 public LauncherAppWidgetHost getAppWidgetHost() {
1689 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001690 }
Romain Guycbb89e42009-06-08 15:52:54 -07001691
Winson Chunga9abd0e2010-10-27 17:18:37 -07001692 public LauncherModel getModel() {
1693 return mModel;
1694 }
1695
Adam Cohen79d90c52016-04-22 13:29:20 -07001696 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001697 return mSharedPrefs;
1698 }
1699
Adam Cohen2e6da152015-05-06 11:42:25 -07001700 public DeviceProfile getDeviceProfile() {
1701 return mDeviceProfile;
1702 }
1703
Bjorn Bringertc459e522013-06-07 19:36:01 +01001704 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001705 getWindow().closeAllPanels();
1706
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001707 // Whatever we were doing is hereby canceled.
Sunny Goyal2100c782016-08-22 16:00:03 -07001708 setWaitingForResult(null);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001709 }
1710
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001711 @Override
1712 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001713 long startTime = 0;
1714 if (DEBUG_RESUME_TIME) {
1715 startTime = System.currentTimeMillis();
1716 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001717 super.onNewIntent(intent);
1718
Winson15f8b172015-08-19 11:02:39 -07001719 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1720 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1721 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001722
1723 // Check this condition before handling isActionMain, as this will get reset.
1724 boolean shouldMoveToDefaultScreen = alreadyOnHome &&
1725 mState == State.WORKSPACE && getTopFloatingView() == null;
1726
Winson15f8b172015-08-19 11:02:39 -07001727 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1728 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001729 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001730 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001731
Adam Cohen6fecd412013-10-02 17:41:50 -07001732 if (mWorkspace == null) {
1733 // Can be cases where mWorkspace is null, this prevents a NPE
1734 return;
1735 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001736 // In all these cases, only animate if we're already on home
1737 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001738
Sunny Goyal935fca12015-10-13 10:19:01 -07001739 closeFolder(alreadyOnHome);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001740 closeShortcutsContainer(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001741 exitSpringLoadedDragMode();
1742
1743 // If we are already on home, then just animate back to the workspace,
1744 // otherwise, just wait until onResume to set the state back to Workspace
1745 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001746 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001747 } else {
1748 mOnResumeState = State.WORKSPACE;
1749 }
1750
1751 final View v = getWindow().peekDecorView();
1752 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001753 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001754 INPUT_METHOD_SERVICE);
1755 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1756 }
1757
Winson Chungb745afb2015-03-02 11:51:23 -08001758 // Reset the apps view
1759 if (!alreadyOnHome && mAppsView != null) {
1760 mAppsView.scrollToTop();
1761 }
1762
Hyunyoung Song3f471442015-04-08 19:01:34 -07001763 // Reset the widgets view
1764 if (!alreadyOnHome && mWidgetsView != null) {
1765 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001766 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001767
Adam Cohen9211d422014-10-07 18:14:53 -07001768 if (mLauncherCallbacks != null) {
1769 mLauncherCallbacks.onHomeIntent();
1770 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001771 }
Adam Cohened307df2013-10-02 09:37:31 -07001772
Adam Cohen9211d422014-10-07 18:14:53 -07001773 if (mLauncherCallbacks != null) {
1774 mLauncherCallbacks.onNewIntent(intent);
1775 }
Winson15f8b172015-08-19 11:02:39 -07001776
1777 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1778 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1779 // animation.
1780 if (isActionMain) {
Sunny Goyal85313732016-09-28 12:00:07 -07001781 boolean callbackAllowsMoveToDefaultScreen = mLauncherCallbacks != null ?
Ivan Lee667d6882015-09-03 10:16:07 -06001782 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Sunny Goyal85313732016-09-28 12:00:07 -07001783 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()
1784 && callbackAllowsMoveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001785
1786 // We use this flag to suppress noisy callbacks above custom content state
1787 // from onResume.
1788 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001789 mWorkspace.post(new Runnable() {
1790 @Override
1791 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001792 if (mWorkspace != null) {
1793 mWorkspace.moveToDefaultScreen(true);
1794 }
Winson15f8b172015-08-19 11:02:39 -07001795 }
1796 });
1797 }
1798 }
1799
1800 if (DEBUG_RESUME_TIME) {
1801 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1802 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001803 }
1804
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001805 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001806 public void onRestoreInstanceState(Bundle state) {
1807 super.onRestoreInstanceState(state);
1808 for (int page: mSynchronouslyBoundPages) {
1809 mWorkspace.restoreInstanceStateForChild(page);
1810 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811 }
1812
1813 @Override
1814 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001815 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001816 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1817 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001818
Adam Cohen21cd0022013-10-09 18:57:02 -07001819 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001820 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821
Michael Jurka883f55b2010-10-21 15:47:14 -07001822 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001823 // We close any open folder since it will not be re-opened, and we need to make sure
1824 // this state is reflected.
1825 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1826 closeFolder(false);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001827 closeShortcutsContainer(false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001828
Sunny Goyal2100c782016-08-22 16:00:03 -07001829 if (mPendingRequestArgs != null) {
1830 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1831 }
1832 if (mPendingActivityResult != null) {
1833 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834 }
1835
Adam Cohen9211d422014-10-07 18:14:53 -07001836 if (mLauncherCallbacks != null) {
1837 mLauncherCallbacks.onSaveInstanceState(outState);
1838 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001839 }
1840
1841 @Override
1842 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001843 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001844
Michael Jurka9d906c72011-10-14 06:25:36 -07001845 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001846 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001847
Winson Chungcd2b0142011-06-08 16:02:26 -07001848 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001849 // It's possible to receive onDestroy after a new Launcher activity has
1850 // been created. In this case, don't interfere with the new Launcher.
1851 if (mModel.isCurrentCallbacks(this)) {
1852 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001853 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001854 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001855
Sunny Goyal745bad92016-05-02 10:54:12 -07001856 if (mRotationPrefChangeHandler != null) {
1857 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1858 }
1859
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001860 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001861 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001862 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001863 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001865 mAppWidgetHost = null;
1866
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001867 TextKeyListener.getInstance().release();
1868
Tony Wickhame2217252016-03-22 16:34:23 -07001869 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1870 .removeAccessibilityStateChangeListener(this);
1871
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001872 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001873
Michael Jurka2ecf9952012-06-18 12:52:28 -07001874 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001875
1876 if (mLauncherCallbacks != null) {
1877 mLauncherCallbacks.onDestroy();
1878 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001879 }
1880
Sunny Goyalae502842016-06-17 08:43:56 -07001881 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1882 return mAccessibilityDelegate;
1883 }
1884
Adam Cohena9cf38f2011-05-02 15:36:58 -07001885 public DragController getDragController() {
1886 return mDragController;
1887 }
1888
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001890 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001891 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001892 }
1893
1894 @Override
1895 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1896 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001897 try {
1898 super.startIntentSenderForResult(intent, requestCode,
1899 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1900 } catch (IntentSender.SendIntentException e) {
1901 throw new ActivityNotFoundException();
1902 }
1903 }
1904
Winson Chung70d72102011-08-12 11:24:35 -07001905 /**
1906 * Indicates that we want global search for this activity by setting the globalSearch
1907 * argument for {@link #startSearch} to true.
1908 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001910 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001912
Karl Rosaen138a0412009-04-23 19:00:21 -07001913 if (initialQuery == null) {
1914 // Use any text typed in the launcher as the initial query
1915 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001916 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 if (appSearchData == null) {
1918 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001919 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001921
Sunny Goyal6f28e712016-09-13 14:19:29 -07001922 if (mLauncherCallbacks == null ||
1923 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1924 // Starting search from the callbacks failed. Start the default global search.
1925 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001926 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001927
1928 // We need to show the workspace after starting the search
1929 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01001930 }
1931
Ian Parkinson047036e2014-09-01 15:40:53 +01001932 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07001933 * Starts the global search activity. This code is a copied from SearchManager
1934 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07001935 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001936 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001937 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001938 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1939 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1940 if (globalSearchActivity == null) {
1941 Log.w(TAG, "No global search activity found.");
1942 return;
1943 }
1944 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1945 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1946 intent.setComponent(globalSearchActivity);
1947 // Make sure that we have a Bundle to put source in
1948 if (appSearchData == null) {
1949 appSearchData = new Bundle();
1950 } else {
1951 appSearchData = new Bundle(appSearchData);
1952 }
Adam Cohen9211d422014-10-07 18:14:53 -07001953 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07001954 if (!appSearchData.containsKey("source")) {
1955 appSearchData.putString("source", getPackageName());
1956 }
1957 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1958 if (!TextUtils.isEmpty(initialQuery)) {
1959 intent.putExtra(SearchManager.QUERY, initialQuery);
1960 }
1961 if (selectInitialQuery) {
1962 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1963 }
1964 intent.setSourceBounds(sourceBounds);
1965 try {
1966 startActivity(intent);
1967 } catch (ActivityNotFoundException ex) {
1968 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1969 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001970 }
1971
Yura4f93ec62014-02-04 14:15:21 +00001972 public boolean isOnCustomContent() {
1973 return mWorkspace.isOnOrMovingToCustomContent();
1974 }
1975
Winson Chung70d72102011-08-12 11:24:35 -07001976 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001977 public boolean onPrepareOptionsMenu(Menu menu) {
1978 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07001979 if (mLauncherCallbacks != null) {
1980 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
1981 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001982 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001983 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001985 @Override
1986 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001987 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001988 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001989 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001990 }
1991
Joe Onorato9c1289c2009-08-17 11:03:03 -04001992 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07001993 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001994 }
1995
Adam Cohen517a7f52014-03-01 12:12:59 -08001996 public boolean isWorkspaceLoading() {
1997 return mWorkspaceLoading;
1998 }
1999
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002000 private void setWorkspaceLoading(boolean value) {
2001 boolean isLocked = isWorkspaceLocked();
2002 mWorkspaceLoading = value;
2003 if (isLocked != isWorkspaceLocked()) {
2004 onWorkspaceLockedChanged();
2005 }
2006 }
2007
Sunny Goyal2100c782016-08-22 16:00:03 -07002008 private void setWaitingForResult(PendingRequestArgs args) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002009 boolean isLocked = isWorkspaceLocked();
Sunny Goyal2100c782016-08-22 16:00:03 -07002010 mPendingRequestArgs = args;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002011 if (isLocked != isWorkspaceLocked()) {
2012 onWorkspaceLockedChanged();
2013 }
2014 }
2015
Adam Cohen9211d422014-10-07 18:14:53 -07002016 protected void onWorkspaceLockedChanged() {
2017 if (mLauncherCallbacks != null) {
2018 mLauncherCallbacks.onWorkspaceLockedChanged();
2019 }
2020 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002021
Sunny Goyal2100c782016-08-22 16:00:03 -07002022 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
2023 LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002024 if (LOGD) {
2025 Log.d(TAG, "Adding widget from drop");
2026 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002027 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2028 }
2029
Sunny Goyal2100c782016-08-22 16:00:03 -07002030 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
2031 AppWidgetHostView boundWidget, LauncherAppWidgetProviderInfo appWidgetInfo,
Adam Cohen59400422014-03-05 18:07:04 -08002032 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002033 if (appWidgetInfo.configure != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002034 setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, appWidgetInfo, info));
Michael Jurkaaf442092010-06-10 17:01:57 -07002035
Bjorn Bringert7984c942009-12-09 15:38:25 +00002036 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002037 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2038 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002040 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002041 Runnable onComplete = new Runnable() {
2042 @Override
2043 public void run() {
2044 // Exit spring loaded mode if necessary after adding the widget
2045 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2046 null);
2047 }
2048 };
Sunny Goyal2100c782016-08-22 16:00:03 -07002049 completeAddAppWidget(appWidgetId, info, boundWidget, appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002050 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 }
2052 }
Romain Guycbb89e42009-06-08 15:52:54 -07002053
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002054 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002055 // Close any folders that may be open.
2056 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002057 mWorkspace.moveToCustomContentScreen(animate);
2058 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002059
2060 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2061 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002062 info.container = container;
2063 info.screenId = screenId;
2064 if (cell != null) {
2065 info.cellX = cell[0];
2066 info.cellY = cell[1];
2067 }
2068 info.spanX = spanX;
2069 info.spanY = spanY;
2070
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002071 switch (info.itemType) {
2072 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2073 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07002074 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002075 break;
2076 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal2100c782016-08-22 16:00:03 -07002077 processShortcutFromDrop(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002078 break;
2079 default:
2080 throw new IllegalStateException("Unknown item type: " + info.itemType);
2081 }
2082 }
2083
Adam Cohenfbba09b2011-07-18 21:43:05 -07002084 /**
2085 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002086 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002087 private void processShortcutFromDrop(PendingAddItemInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07002088 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
2089 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
2090 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002091 }
2092
Adam Cohenfbba09b2011-07-18 21:43:05 -07002093 /**
2094 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002095 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002096 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002097 AppWidgetHostView hostView = info.boundWidget;
2098 int appWidgetId;
2099 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002100 // In the case where we've prebound the widget, we remove it from the DragLayer
2101 if (LOGD) {
2102 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2103 }
2104 getDragLayer().removeView(hostView);
2105
Adam Cohened66b2b2012-01-23 17:28:51 -08002106 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002107 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002108
2109 // Clear the boundWidget so that it doesn't get destroyed.
2110 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002111 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002112 // In this case, we either need to start an activity to get permission to bind
2113 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002114 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002115 Bundle options = info.bindOptions;
2116
Sunny Goyalffe83f12014-08-14 17:39:34 -07002117 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2118 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002119 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002120 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002121 } else {
Sunny Goyal2100c782016-08-22 16:00:03 -07002122 setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, info.info, info));
Michael Jurka8b805b12012-04-18 14:23:14 -07002123 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2124 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2125 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyal2100c782016-08-22 16:00:03 -07002126 mAppWidgetManager.getUser(info.info)
Sunny Goyalffe83f12014-08-14 17:39:34 -07002127 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002128 // TODO: we need to make sure that this accounts for the options bundle.
2129 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002130 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2131 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002132 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002133 }
2134
Adam Cohendcd297f2013-06-18 13:13:40 -07002135 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002136 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002137 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002138 folderInfo.title = getText(R.string.folder_name);
2139
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002140 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002141 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2142 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002143
2144 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002145 FolderIcon newFolder =
2146 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002147 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002148 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002149 // Force measure the new folder icon
2150 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2151 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002152 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002153 }
Romain Guycbb89e42009-06-08 15:52:54 -07002154
Winsonc0b52fe2015-09-09 16:38:15 -07002155 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002156 * Unbinds the view for the specified item, and removes the item and all its children.
2157 *
2158 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002159 * @param itemInfo the {@link ItemInfo} for this view.
2160 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002161 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002162 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002163 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002164 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002165 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2166 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002167 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002168 } else {
2169 mWorkspace.removeWorkspaceItem(v);
2170 }
Winsonc0b52fe2015-09-09 16:38:15 -07002171 if (deleteFromDb) {
2172 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2173 }
2174 } else if (itemInfo instanceof FolderInfo) {
2175 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002176 if (v instanceof FolderIcon) {
2177 ((FolderIcon) v).removeListeners();
2178 }
Winsonc0b52fe2015-09-09 16:38:15 -07002179 mWorkspace.removeWorkspaceItem(v);
2180 if (deleteFromDb) {
2181 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2182 }
2183 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2184 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002185 mWorkspace.removeWorkspaceItem(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002186 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002187 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002188 }
2189 } else {
2190 return false;
2191 }
2192 return true;
2193 }
2194
2195 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002196 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002197 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002198 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002199 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002200 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002201 // Deleting an app widget ID is a void call but writes to disk before returning
2202 // to the caller...
2203 new AsyncTask<Void, Void, Void>() {
2204 public Void doInBackground(Void ... args) {
2205 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2206 return null;
2207 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002208 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002209 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002210 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002211 }
2212
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002213 @Override
2214 public boolean dispatchKeyEvent(KeyEvent event) {
2215 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2216 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002217 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002218 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002219 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002220 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002221 dumpState();
2222 return true;
2223 }
2224 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002225 }
2226 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2227 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002228 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002229 return true;
2230 }
2231 }
2232
2233 return super.dispatchKeyEvent(event);
2234 }
2235
Joe Onorato88ec0992009-11-19 13:16:06 -08002236 @Override
2237 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002238 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2239 return;
2240 }
2241
Sunny Goyal45478022015-06-08 16:52:41 -07002242 if (mDragController.isDragging()) {
2243 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002244 return;
2245 }
2246
Tony Wickham49c8d292016-07-01 11:44:34 -07002247 if (getOpenShortcutsContainer() != null) {
2248 closeShortcutsContainer();
2249 } else if (isAppsViewVisible()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002250 showWorkspace(true);
2251 } else if (isWidgetsViewVisible()) {
2252 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002253 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002254 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002255 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002256 Folder openFolder = mWorkspace.getOpenFolder();
2257 if (openFolder.isEditingName()) {
2258 openFolder.dismissEditingName();
2259 } else {
2260 closeFolder();
2261 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002262 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002263 mWorkspace.exitWidgetResizeMode();
2264
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002265 // Back button is a no-op here, but give at least some feedback for the button press
2266 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002267 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002268 }
2269
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002270 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002271 * Launches the intent referred by the clicked shortcut.
2272 *
2273 * @param v The view representing the clicked shortcut.
2274 */
2275 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002276 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2277 // view has detached (it's possible for this to happen if the view is removed mid touch).
2278 if (v.getWindowToken() == null) {
2279 return;
2280 }
2281
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002282 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002283 return;
2284 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002285
Adam Cohen1697b792013-09-17 19:08:21 -07002286 if (v instanceof Workspace) {
2287 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002288 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002289 }
2290 return;
2291 }
2292
2293 if (v instanceof CellLayout) {
2294 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002295 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2296 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002297 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002298 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002299 }
2300
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002301 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002302 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002303 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002304 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002305 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002306 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002307 }
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002308 } else if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2309 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002310 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002311 } else if (tag instanceof AppInfo) {
2312 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002313 } else if (tag instanceof LauncherAppWidgetInfo) {
2314 if (v instanceof PendingAppWidgetHostView) {
2315 onClickPendingWidget((PendingAppWidgetHostView) v);
2316 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002317 }
2318 }
2319
Sunny Goyal70660032015-05-14 00:07:08 -07002320 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002321 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002322 return false;
2323 }
2324
Michael Jurkaaf442092010-06-10 17:01:57 -07002325 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002326 * Event handler for the app widget view which has not fully restored.
2327 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002328 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002329 if (mIsSafeModeEnabled) {
2330 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2331 return;
2332 }
2333
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002334 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002335 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002336 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2337 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2338 // This should not happen, as we make sure that an Id is allocated during bind.
2339 return;
2340 }
2341 LauncherAppWidgetProviderInfo appWidgetInfo =
2342 mAppWidgetManager.findProvider(info.providerName, info.user);
2343 if (appWidgetInfo != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002344 setWaitingForResult(PendingRequestArgs
2345 .forWidgetInfo(info.appWidgetId, appWidgetInfo, info));
Sunny Goyalff572272014-07-23 13:58:07 -07002346
Sunny Goyald478c832016-04-01 12:04:16 -07002347 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
Sunny Goyal2100c782016-08-22 16:00:03 -07002348 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, info.appWidgetId);
Sunny Goyald478c832016-04-01 12:04:16 -07002349 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07002350 mAppWidgetManager.getUser(appWidgetInfo)
Sunny Goyald478c832016-04-01 12:04:16 -07002351 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2352 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2353 }
2354 } else {
2355 LauncherAppWidgetProviderInfo appWidgetInfo =
2356 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2357 if (appWidgetInfo != null) {
2358 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2359 }
Sunny Goyalff572272014-07-23 13:58:07 -07002360 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002361 } else if (info.installProgress < 0) {
2362 // The install has not been queued
2363 final String packageName = info.providerName.getPackageName();
2364 showBrokenAppInstallDialog(packageName,
2365 new DialogInterface.OnClickListener() {
2366 public void onClick(DialogInterface dialog, int id) {
2367 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2368 }
2369 });
2370 } else {
2371 // Download has started.
2372 final String packageName = info.providerName.getPackageName();
2373 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002374 }
2375 }
2376
Sunny Goyald478c832016-04-01 12:04:16 -07002377 private void startRestoredWidgetReconfigActivity(
2378 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002379 setWaitingForResult(PendingRequestArgs.forWidgetInfo(info.appWidgetId, provider, info));
Sunny Goyald478c832016-04-01 12:04:16 -07002380 mAppWidgetManager.startConfigActivity(provider,
2381 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2382 }
2383
Sunny Goyalff572272014-07-23 13:58:07 -07002384 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002385 * Event handler for the "grid" button that appears on the home screen, which
2386 * enters all apps mode.
2387 *
2388 * @param v The view that was clicked.
2389 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002390 protected void onClickAllAppsButton(View v) {
2391 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002392 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002393 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.TAP,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002394 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002395 showAppsView(true /* animated */, true /* updatePredictedApps */,
2396 false /* focusSearchBar */);
Winson Chung76648c52015-07-10 14:33:23 -07002397 }
2398 }
2399
2400 protected void onLongClickAllAppsButton(View v) {
2401 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2402 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002403 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.LONGPRESS,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002404 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002405 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07002406 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002407 }
2408 }
2409
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002410 private void showBrokenAppInstallDialog(final String packageName,
2411 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002412 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002413 .setTitle(R.string.abandoned_promises_title)
2414 .setMessage(R.string.abandoned_promise_explanation)
2415 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2416 .setNeutralButton(R.string.abandoned_clean_this,
2417 new DialogInterface.OnClickListener() {
2418 public void onClick(DialogInterface dialog, int id) {
2419 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2420 mWorkspace.removeAbandonedPromise(packageName, user);
2421 }
2422 })
2423 .create().show();
2424 return;
2425 }
2426
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002427 /**
2428 * Event handler for an app shortcut click.
2429 *
2430 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2431 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002432 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002433 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2434 Object tag = v.getTag();
2435 if (!(tag instanceof ShortcutInfo)) {
2436 throw new IllegalArgumentException("Input must be a Shortcut");
2437 }
2438
2439 // Open shortcut
2440 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002441
2442 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002443 if ((shortcut.isDisabled &
2444 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2445 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2446 // If the app is only disabled because of the above flags, launch activity anyway.
2447 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002448 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002449 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2450 // Use a message specific to this shortcut, if it has one.
2451 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2452 return;
2453 }
2454 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002455 int error = R.string.activity_not_available;
2456 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2457 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002458 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2459 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002460 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002461 }
2462 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2463 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002464 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002465 }
2466
Chris Wren40c5ed32014-06-24 18:24:23 -04002467 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002468 if ((v instanceof BubbleTextView)
2469 && shortcut.isPromise()
2470 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002471 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002472 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002473 new DialogInterface.OnClickListener() {
2474 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002475 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002476 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002477 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002478 return;
2479 }
2480
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002481 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002482 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002483 }
2484
Sunny Goyala7ce1662016-05-31 15:01:35 -07002485 private void startAppShortcutOrInfoActivity(View v) {
2486 ItemInfo item = (ItemInfo) v.getTag();
2487 Intent intent = item.getIntent();
2488 if (intent == null) {
2489 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002490 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002491 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002492 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002493
2494 if (success && v instanceof BubbleTextView) {
2495 mWaitingForResume = (BubbleTextView) v;
2496 mWaitingForResume.setStayPressed(true);
2497 }
2498 }
2499
2500 /**
2501 * Event handler for a folder icon click.
2502 *
2503 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2504 */
2505 protected void onClickFolderIcon(View v) {
2506 if (LOGD) Log.d(TAG, "onClickFolder");
2507 if (!(v instanceof FolderIcon)){
2508 throw new IllegalArgumentException("Input must be a FolderIcon");
2509 }
2510
2511 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002512 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002513 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002514 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002515 }
Michael Jurka5130e402011-10-13 04:55:35 -07002516 }
2517
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002518 /**
2519 * Event handler for the (Add) Widgets button that appears after a long press
2520 * on the home screen.
2521 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002522 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002523 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002524 if (mIsSafeModeEnabled) {
2525 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2526 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002527 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002528 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002529 }
2530
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002531 /**
2532 * Event handler for the wallpaper picker button that appears after a long press
2533 * on the home screen.
2534 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002535 public void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002536 if (!Utilities.isWallapaperAllowed(this)) {
2537 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2538 return;
2539 }
2540
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002541 String pickerPackage = getString(R.string.wallpaper_picker_package);
2542 if (TextUtils.isEmpty(pickerPackage)) {
2543 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2544 }
2545
Tony Wickham785f7a52015-08-31 17:28:32 -07002546 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2547 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002548
Sunny Goyal2100c782016-08-22 16:00:03 -07002549 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002550 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002551 .setPackage(pickerPackage)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002552 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
2553 intent.setSourceBounds(getViewBounds(v));
2554 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002555 }
2556
2557 /**
2558 * Event handler for a click on the settings button that appears after a long press
2559 * on the home screen.
2560 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002561 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002562 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002563 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2564 .setPackage(getPackageName());
2565 intent.setSourceBounds(getViewBounds(v));
2566 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002567 }
2568
Adam Cohen61f560d2013-09-30 15:58:20 -07002569 public View.OnTouchListener getHapticFeedbackTouchListener() {
2570 if (mHapticFeedbackTouchListener == null) {
2571 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002572 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002573 @Override
2574 public boolean onTouch(View v, MotionEvent event) {
2575 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2576 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2577 }
2578 return false;
2579 }
2580 };
2581 }
2582 return mHapticFeedbackTouchListener;
2583 }
2584
Tony Wickhame2217252016-03-22 16:34:23 -07002585 @Override
2586 public void onAccessibilityStateChanged(boolean enabled) {
2587 mDragLayer.onAccessibilityStateChanged(enabled);
2588 }
2589
Sunny Goyal06e21a22016-08-11 16:02:02 -07002590 public void onDragStarted() {
Adam Cohen9211d422014-10-07 18:14:53 -07002591 if (isOnCustomContent()) {
2592 // Custom content screen doesn't participate in drag and drop. If on custom
2593 // content screen, move to default.
2594 moveWorkspaceToDefaultScreen();
2595 }
Adam Cohen9211d422014-10-07 18:14:53 -07002596 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002597
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002598 /**
2599 * Called when the user stops interacting with the launcher.
2600 * This implies that the user is now on the homescreen and is not doing housekeeping.
2601 */
Adam Cohen9211d422014-10-07 18:14:53 -07002602 protected void onInteractionEnd() {
2603 if (mLauncherCallbacks != null) {
2604 mLauncherCallbacks.onInteractionEnd();
2605 }
2606 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002607
2608 /**
2609 * Called when the user starts interacting with the launcher.
2610 * The possible interactions are:
2611 * - open all apps
2612 * - reorder an app shortcut, or a widget
2613 * - open the overview mode.
2614 * This is a good time to stop doing things that only make sense
2615 * when the user is on the homescreen and not doing housekeeping.
2616 */
Adam Cohen9211d422014-10-07 18:14:53 -07002617 protected void onInteractionBegin() {
2618 if (mLauncherCallbacks != null) {
2619 mLauncherCallbacks.onInteractionBegin();
2620 }
2621 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002622
Winson Chungcd99cd32015-04-29 11:03:24 -07002623 /** Updates the interaction state. */
2624 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002625 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002626 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002627 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2628 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002629 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002630 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002631 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002632 onInteractionEnd();
2633 }
2634 }
2635
Sunny Goyala7ce1662016-05-31 15:01:35 -07002636 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002637 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002638 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2639 try {
2640 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2641 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2642 // is enabled by default on NYC.
2643 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2644 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002645
2646 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2647 String id = ((ShortcutInfo) info).getDeepShortcutId();
2648 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002649 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2650 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002651 } else {
2652 // Could be launching some bookkeeping activity
2653 startActivity(intent, optsBundle);
2654 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002655 } finally {
2656 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002657 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002658 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002659 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2660 // corresponding permission. Show the appropriate permission prompt if that
2661 // is the case.
2662 if (intent.getComponent() == null
2663 && Intent.ACTION_CALL.equals(intent.getAction())
2664 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2665 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002666
2667 setWaitingForResult(PendingRequestArgs
2668 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002669 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2670 REQUEST_PERMISSION_CALL_PHONE);
2671 } else {
2672 // No idea why this was thrown.
2673 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002674 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002675 }
2676 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002677
Sunny Goyalfe770c92016-09-27 14:38:58 -07002678 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyala7ce1662016-05-31 15:01:35 -07002679 private Bundle getActivityLaunchOptions(View v) {
2680 if (Utilities.ATLEAST_MARSHMALLOW) {
2681 int left = 0, top = 0;
2682 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2683 if (v instanceof TextView) {
2684 // Launch from center of icon, not entire view
2685 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2686 if (icon != null) {
2687 Rect bounds = icon.getBounds();
2688 left = (width - bounds.width()) / 2;
2689 top = v.getPaddingTop();
2690 width = bounds.width();
2691 height = bounds.height();
2692 }
2693 }
2694 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2695 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2696 // On L devices, we use the device default slide-up transition.
2697 // On L MR1 devices, we use a custom version of the slide-up transition which
2698 // doesn't have the delay present in the device default.
2699 return ActivityOptions.makeCustomAnimation(
2700 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2701 }
2702 return null;
2703 }
2704
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002705 private Rect getViewBounds(View v) {
2706 int[] pos = new int[2];
2707 v.getLocationOnScreen(pos);
2708 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2709 }
2710
Sunny Goyala7ce1662016-05-31 15:01:35 -07002711 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002712 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2713 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2714 return false;
2715 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002716 // Only launch using the new animation if the shortcut has not opted out (this is a
2717 // private contract between launcher and may be ignored in the future).
2718 boolean useLaunchAnimation = (v != null) &&
2719 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2720 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2721
2722 UserHandleCompat user = null;
2723 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2724 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2725 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002726 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002727
2728 // Prepare intent
2729 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2730 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002731 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002732 }
2733 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002734 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2735 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07002736 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
2737 && ((ShortcutInfo) item).promisedIntent == null) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002738 // Shortcuts need some special checks due to legacy reasons.
2739 startShortcutIntentSafely(intent, optsBundle, item);
2740 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2741 // Could be launching some bookkeeping activity
2742 startActivity(intent, optsBundle);
2743 } else {
2744 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2745 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2746 }
2747 return true;
2748 } catch (ActivityNotFoundException|SecurityException e) {
2749 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2750 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2751 }
2752 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002753 }
2754
Adam Cohen268c4752012-06-06 17:47:33 -07002755 /**
2756 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2757 * in the DragLayer in the exact absolute location of the original FolderIcon.
2758 */
2759 private void copyFolderIconToImage(FolderIcon fi) {
2760 final int width = fi.getMeasuredWidth();
2761 final int height = fi.getMeasuredHeight();
2762
2763 // Lazy load ImageView, Bitmap and Canvas
2764 if (mFolderIconImageView == null) {
2765 mFolderIconImageView = new ImageView(this);
2766 }
2767 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2768 mFolderIconBitmap.getHeight() != height) {
2769 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2770 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2771 }
2772
2773 DragLayer.LayoutParams lp;
2774 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2775 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2776 } else {
2777 lp = new DragLayer.LayoutParams(width, height);
2778 }
2779
Adam Cohen307fe232012-08-16 17:55:58 -07002780 // The layout from which the folder is being opened may be scaled, adjust the starting
2781 // view size by this scale factor.
2782 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002783 lp.customPosition = true;
2784 lp.x = mRectForFolderAnimation.left;
2785 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002786 lp.width = (int) (scale * width);
2787 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002788
2789 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2790 fi.draw(mFolderIconCanvas);
2791 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002792 if (fi.getFolder() != null) {
2793 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2794 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002795 }
Adam Cohen268c4752012-06-06 17:47:33 -07002796 // Just in case this image view is still in the drag layer from a previous animation,
2797 // we remove it and re-add it.
2798 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2799 mDragLayer.removeView(mFolderIconImageView);
2800 }
2801 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002802 if (fi.getFolder() != null) {
2803 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002804 }
Adam Cohen268c4752012-06-06 17:47:33 -07002805 }
2806
Adam Cohen37b2a492016-04-06 18:36:06 -07002807 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002808 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002809 FolderInfo info = (FolderInfo) fi.getTag();
2810 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2811 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002812 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2813 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002814 }
2815
Adam Cohen268c4752012-06-06 17:47:33 -07002816 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2817 copyFolderIconToImage(fi);
2818 fi.setVisibility(View.INVISIBLE);
2819
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002820 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2821 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002822 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002823 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2824 }
2825 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002826 oa.start();
2827 }
2828
Sunny Goyal935fca12015-10-13 10:19:01 -07002829 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002830 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002831 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002832
Adam Cohen268c4752012-06-06 17:47:33 -07002833 // We remove and re-draw the FolderIcon in-case it has changed
2834 mDragLayer.removeView(mFolderIconImageView);
2835 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08002836
2837 if (cl != null) {
2838 cl.clearFolderLeaveBehind();
2839 }
2840
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002841 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002842 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002843 oa.addListener(new AnimatorListenerAdapter() {
2844 @Override
2845 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002846 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07002847 // Remove the ImageView copy of the FolderIcon and make the original visible.
2848 mDragLayer.removeView(mFolderIconImageView);
2849 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002850 }
2851 }
2852 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002853 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07002854 if (!animate) {
2855 oa.end();
2856 }
Adam Cohen2801caf2011-05-13 20:57:39 -07002857 }
2858
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002859 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002860 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002861 * is animated relative to the specified View. If the View is null, no animation
2862 * is played.
2863 *
Sunny Goyal08442b82015-10-21 17:15:08 -07002864 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002865 */
Adam Cohen37b2a492016-04-06 18:36:06 -07002866 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07002867
Adam Cohenfb91f302012-06-11 15:45:18 -07002868 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07002869 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
2870 if (openFolder != null && openFolder != folder) {
2871 // Close any open folder before opening a folder.
2872 closeFolder();
2873 }
2874
Adam Cohena9cf38f2011-05-02 15:36:58 -07002875 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002876
Adam Cohena9cf38f2011-05-02 15:36:58 -07002877 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002878
Sunny Goyalf4066152015-04-15 09:42:19 -07002879 // While the folder is open, the position of the icon cannot change.
2880 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
2881
Adam Cohen4554ee12011-08-03 16:13:21 -07002882 // Just verify that the folder hasn't already been added to the DragLayer.
2883 // There was a one-off crash where the folder had a parent already.
2884 if (folder.getParent() == null) {
2885 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07002886 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07002887 } else {
2888 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2889 folder.getParent() + ").");
2890 }
Adam Cohen37b2a492016-04-06 18:36:06 -07002891 folder.animateOpen();
2892
2893 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002894
2895 // Notify the accessibility manager that this folder "window" has appeared and occluded
2896 // the workspace items
2897 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2898 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002899 }
2900
2901 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07002902 closeFolder(true);
2903 }
2904
2905 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08002906 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002907 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002908 if (folder.isEditingName()) {
2909 folder.dismissEditingName();
2910 }
Sunny Goyal935fca12015-10-13 10:19:01 -07002911 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07002912 }
2913 }
2914
Sunny Goyal935fca12015-10-13 10:19:01 -07002915 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07002916 animate &= !Utilities.isPowerSaverOn(this);
2917
Adam Cohen4554ee12011-08-03 16:13:21 -07002918 folder.getInfo().opened = false;
2919
2920 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2921 if (parent != null) {
2922 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07002923 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07002924 if (fi != null) {
2925 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
2926 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002927 }
Sunny Goyal935fca12015-10-13 10:19:01 -07002928 if (animate) {
2929 folder.animateClosed();
2930 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08002931 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07002932 }
Winson Chung83ca4802013-04-12 15:10:52 -07002933
Sunny Goyal935fca12015-10-13 10:19:01 -07002934 // Notify the accessibility manager that this folder "window" has disappeared and no
2935 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07002936 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002937 }
2938
Tony Wickham49c8d292016-07-01 11:44:34 -07002939 public void closeShortcutsContainer() {
Sunny Goyala2454ad2016-07-22 10:50:11 -07002940 closeShortcutsContainer(true);
2941 }
2942
2943 public void closeShortcutsContainer(boolean animate) {
Tony Wickham49c8d292016-07-01 11:44:34 -07002944 DeepShortcutsContainer deepShortcutsContainer = getOpenShortcutsContainer();
2945 if (deepShortcutsContainer != null) {
Sunny Goyala2454ad2016-07-22 10:50:11 -07002946 if (animate) {
2947 deepShortcutsContainer.animateClose();
2948 } else {
2949 deepShortcutsContainer.close();
2950 }
Tony Wickham49c8d292016-07-01 11:44:34 -07002951 }
2952 }
2953
Sunny Goyal52851aa2016-09-02 10:41:43 -07002954 public View getTopFloatingView() {
2955 View topView = getOpenShortcutsContainer();
2956 if (topView == null) {
2957 topView = getWorkspace().getOpenFolder();
2958 }
2959 return topView;
2960 }
2961
Tony Wickham49c8d292016-07-01 11:44:34 -07002962 /**
2963 * @return The open shortcuts container, or null if there is none
2964 */
2965 public DeepShortcutsContainer getOpenShortcutsContainer() {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07002966 // Iterate in reverse order. Shortcuts container is added later to the dragLayer,
2967 // and will be one of the last views.
2968 for (int i = mDragLayer.getChildCount() - 1; i >= 0; i--) {
2969 View child = mDragLayer.getChildAt(i);
Sunny Goyala2454ad2016-07-22 10:50:11 -07002970 if (child instanceof DeepShortcutsContainer
2971 && ((DeepShortcutsContainer) child).isOpen()) {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07002972 return (DeepShortcutsContainer) child;
2973 }
2974 }
2975 return null;
Tony Wickham49c8d292016-07-01 11:44:34 -07002976 }
2977
Tony Wickhamdadb3042016-02-24 11:07:00 -08002978 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07002979 public boolean dispatchTouchEvent(MotionEvent ev) {
2980 mLastDispatchTouchEventX = ev.getX();
2981 return super.dispatchTouchEvent(ev);
2982 }
2983
2984 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002985 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002986 if (!isDraggingEnabled()) return false;
2987 if (isWorkspaceLocked()) return false;
2988 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002989
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002990 if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2991 (v == mAllAppsButton && mAllAppsButton != null)) {
Winson Chung76648c52015-07-10 14:33:23 -07002992 onLongClickAllAppsButton(v);
2993 return true;
2994 }
2995
Jon Miranda379198e2016-09-23 08:54:40 -07002996 boolean fromEdgeOfScreen = mLastDispatchTouchEventX < mEdgeOfScreenThresholdPx
2997 || mLastDispatchTouchEventX > (mDeviceProfile.widthPx - mEdgeOfScreenThresholdPx);
2998
Adam Cohen1697b792013-09-17 19:08:21 -07002999 if (v instanceof Workspace) {
3000 if (!mWorkspace.isInOverviewMode()) {
Jon Miranda379198e2016-09-23 08:54:40 -07003001 if (!mWorkspace.isTouchActive() && !fromEdgeOfScreen) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003002 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003003 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3004 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3005 return true;
3006 } else {
3007 return false;
3008 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003009 } else {
3010 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003011 }
Adam Cohen1697b792013-09-17 19:08:21 -07003012 }
3013
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003014 CellLayout.CellInfo longClickCellInfo = null;
3015 View itemUnderLongClick = null;
3016 if (v.getTag() instanceof ItemInfo) {
3017 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003018 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003019 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07003020 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003021 }
3022
Winson Chung3d503fb2011-07-13 17:25:49 -07003023 // The hotseat touch handling does not go through Workspace, and we always allow long press
3024 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003025 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003026 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003027 // User long pressed on empty space
Adam Cohend3ecce92013-09-16 14:58:00 -07003028 if (mWorkspace.isInOverviewMode()) {
3029 mWorkspace.startReordering(v);
3030 } else {
Jon Miranda379198e2016-09-23 08:54:40 -07003031 if (fromEdgeOfScreen) {
3032 return false;
3033 }
Winson Chungdc61c4d2015-04-20 18:26:57 -07003034 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003035 }
Jon Miranda379198e2016-09-23 08:54:40 -07003036 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3037 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003038 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003039 final boolean isAllAppsButton =
3040 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3041 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3042 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003043 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003044 // User long pressed on an item
Tony Wickham10236d62016-09-28 12:49:25 -07003045 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003046 }
3047 }
3048 }
3049 return true;
3050 }
3051
Winson Chung3d503fb2011-07-13 17:25:49 -07003052 boolean isHotseatLayout(View layout) {
3053 return mHotseat != null && layout != null &&
3054 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3055 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003056
Winson Chung3d503fb2011-07-13 17:25:49 -07003057 /**
3058 * Returns the CellLayout of the specified container at the specified screen.
3059 */
Sunny Goyal92820592015-03-02 11:31:35 -08003060 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003061 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3062 if (mHotseat != null) {
3063 return mHotseat.getLayout();
3064 } else {
3065 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003066 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003067 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003068 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003069 }
3070 }
3071
Winson Chungb745afb2015-03-02 11:51:23 -08003072 /**
3073 * For overridden classes.
3074 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003075 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003076 return isAppsViewVisible();
3077 }
3078
3079 public boolean isAppsViewVisible() {
3080 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3081 }
3082
3083 public boolean isWidgetsViewVisible() {
3084 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003085 }
3086
Michael Jurkae326f182011-11-21 14:05:46 -08003087 @Override
3088 public void onTrimMemory(int level) {
3089 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003090 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3091 // The widget preview db can result in holding onto over
3092 // 3MB of memory for caching which isn't necessary.
3093 SQLiteDatabase.releaseMemory();
3094
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003095 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003096 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003097 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003098 if (mLauncherCallbacks != null) {
3099 mLauncherCallbacks.onTrimMemory(level);
3100 }
Michael Jurkae326f182011-11-21 14:05:46 -08003101 }
3102
Winson5c6bdbb2015-09-03 11:36:19 -07003103 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003104 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003105 }
3106
Winson5c6bdbb2015-09-03 11:36:19 -07003107 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003108 boolean changed = mState != State.WORKSPACE ||
3109 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003110 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003111 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003112 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003113 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003114
Michael Jurkab3e22d92011-10-31 15:58:33 -07003115 // Set focus to the AppsCustomize button
3116 if (mAllAppsButton != null) {
3117 mAllAppsButton.requestFocus();
3118 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003119 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003120
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003121 // Change the state *after* we've called all the transition code
3122 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003123
Winson Chung0f785722015-04-08 10:27:49 -07003124 if (changed) {
3125 // Send an accessibility event to announce the context change
3126 getWindow().getDecorView()
3127 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003128 }
Winson5c6bdbb2015-09-03 11:36:19 -07003129 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003130 }
3131
Winsone9f27272015-10-13 10:47:51 -07003132 /**
3133 * Shows the overview button.
3134 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07003135 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003136 showOverviewMode(animated, false);
3137 }
3138
3139 /**
3140 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3141 * onto one of the overview panel buttons.
3142 */
3143 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3144 Runnable postAnimRunnable = null;
3145 if (requestButtonFocus) {
3146 postAnimRunnable = new Runnable() {
3147 @Override
3148 public void run() {
3149 // Hitting the menu button when in touch mode does not trigger touch mode to
3150 // be disabled, so if requested, force focus on one of the overview panel
3151 // buttons.
3152 mOverviewPanel.requestFocusFromTouch();
3153 }
3154 };
3155 }
Adam Cohened307df2013-10-02 09:37:31 -07003156 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003157 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003158 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003159 mState = State.WORKSPACE;
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07003160 // If animated from long press, then don't allow any of the controller in the drag
3161 // layer to intercept any remaining touch.
3162 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04003163 }
3164
Winson Chungb745afb2015-03-02 11:51:23 -08003165 /**
3166 * Shows the apps view.
3167 */
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003168 public void showAppsView(boolean animated, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003169 boolean focusSearchBar) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003170 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07003171 if (updatePredictedApps) {
3172 tryAndUpdatePredictedApps();
3173 }
Winson Chung76648c52015-07-10 14:33:23 -07003174 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003175 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003176
Winson Chungb745afb2015-03-02 11:51:23 -08003177 /**
3178 * Shows the widgets view.
3179 */
3180 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003181 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003182 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003183 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003184 }
Winson Chung76648c52015-07-10 14:33:23 -07003185 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003186
3187 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003188 @Override
3189 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003190 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003191 }
3192 });
Winson Chungb745afb2015-03-02 11:51:23 -08003193 }
3194
3195 /**
3196 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003197 *
3198 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003199 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003200 // TODO: calling method should use the return value so that when {@code false} is returned
3201 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003202 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003203 if (!(mState == State.WORKSPACE ||
3204 mState == State.APPS_SPRING_LOADED ||
3205 mState == State.WIDGETS_SPRING_LOADED ||
3206 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003207 return false;
3208 }
3209 if (toState != State.APPS && toState != State.WIDGETS) {
3210 return false;
3211 }
Winson Chungb745afb2015-03-02 11:51:23 -08003212
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003213 // This is a safe and supported transition to bypass spring_loaded mode.
3214 if (mExitSpringLoadedModeRunnable != null) {
3215 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3216 mExitSpringLoadedModeRunnable = null;
3217 }
3218
Winson Chungb745afb2015-03-02 11:51:23 -08003219 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003220 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3221 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003222 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003223 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003224 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003225
Michael Jurkab3e22d92011-10-31 15:58:33 -07003226 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003227 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003228
Michael Jurkab3e22d92011-10-31 15:58:33 -07003229 closeFolder();
Tony Wickham49c8d292016-07-01 11:44:34 -07003230 closeShortcutsContainer();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003231
3232 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003233 getWindow().getDecorView()
3234 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003235 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003236 }
3237
Winson Chungcd99cd32015-04-29 11:03:24 -07003238 /**
3239 * Updates the workspace and interaction state on state change, and return the animation to this
3240 * new state.
3241 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003242 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003243 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003244 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003245 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003246 updateInteraction(fromState, toState);
3247 return anim;
3248 }
3249
Hyunyoung Song3f471442015-04-08 19:01:34 -07003250 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003251 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003252 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003253 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003254 }
Winson Chungb745afb2015-03-02 11:51:23 -08003255
Winson Chung006ee262015-08-03 14:40:11 -07003256 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003257 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003258 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003259
3260 if (isAppsViewVisible()) {
3261 mState = State.APPS_SPRING_LOADED;
3262 } else if (isWidgetsViewVisible()) {
3263 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003264 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003265 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003266 } else {
3267 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003268 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003269 }
Adam Cohen7777d962011-08-18 18:58:38 -07003270
Hyunyoung Song3f471442015-04-08 19:01:34 -07003271 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003272 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003273 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003274
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003275 if (mExitSpringLoadedModeRunnable != null) {
3276 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3277 }
3278 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003279 @Override
3280 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003281 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003282 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3283 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003284 // Before we show workspace, hide all apps again because
3285 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3286 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003287 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003288 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003289 } else {
3290 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003291 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003292 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07003293 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003294 };
3295 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003296 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003297
Tony Wickhame0c33232016-02-08 11:37:04 -08003298 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003299 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3300 || mState == State.WIDGETS_SPRING_LOADED;
3301 }
3302
Michael Jurkad3ef3062010-11-23 16:23:58 -08003303 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003304 if (mState == State.APPS_SPRING_LOADED) {
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003305 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07003306 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003307 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003308 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003309 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3310 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003311 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003312 }
3313
Winson Chung4ac30062015-05-08 17:34:17 -07003314 /**
3315 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3316 * resumed.
3317 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003318 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003319 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003320 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003321 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003322 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003323 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003324 }
3325 }
3326 }
3327
Michael Jurkab3e22d92011-10-31 15:58:33 -07003328 void lockAllApps() {
3329 // TODO
3330 }
3331
3332 void unlockAllApps() {
3333 // TODO
3334 }
3335
Michael Jurkad7c28052012-04-27 15:43:36 -07003336 @Override
3337 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003338 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003339 final List<CharSequence> text = event.getText();
3340 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003341 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003342 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003343 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003344 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003345 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003346 } else if (mWorkspace != null) {
3347 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003348 } else {
3349 text.add(getString(R.string.all_apps_home_button_label));
3350 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003351 return result;
3352 }
3353
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003354 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003355 * If the activity is currently paused, signal that we need to run the passed Runnable
3356 * in onResume.
3357 *
3358 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003359 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3360 * wrong when we're not running, and if the activity comes back to what the configuration was
3361 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003362 *
3363 * Implementation of the method from LauncherModel.Callbacks.
3364 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003365 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003366 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003367 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003368 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003369 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003370 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003371 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003372 }
3373 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003374 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003375 return true;
3376 } else {
3377 return false;
3378 }
3379 }
3380
Michael Jurkac402cd92013-05-20 15:49:32 +02003381 private boolean waitUntilResume(Runnable run) {
3382 return waitUntilResume(run, false);
3383 }
3384
Michael Jurka1e2f4652013-07-08 18:03:46 -07003385 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003386 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003387 }
3388
Michael Jurka7607c2f2013-04-03 14:33:19 -07003389 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003390 * If the activity is currently paused, signal that we need to re-run the loader
3391 * in onResume.
3392 *
3393 * This needs to be called from incoming places where resources might have been loaded
3394 * while we are paused. That is becaues the Configuration might be wrong
3395 * when we're not running, and if it comes back to what it was when we
3396 * were paused, we are not restarted.
3397 *
3398 * Implementation of the method from LauncherModel.Callbacks.
3399 *
3400 * @return true if we are currently paused. The caller might be able to
3401 * skip some work in that case since we will come back again.
3402 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003403 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003404 public boolean setLoadOnResume() {
3405 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003406 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003407 mOnResumeNeedsLoad = true;
3408 return true;
3409 } else {
3410 return false;
3411 }
3412 }
3413
3414 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003415 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003416 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003417 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003418 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003419 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003420 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003421 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003422 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003423 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003424 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003425
Joe Onorato9c1289c2009-08-17 11:03:03 -04003426 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003427 * Clear any pending bind callbacks. This is called when is loader is planning to
3428 * perform a full rebind from scratch.
3429 */
3430 @Override
3431 public void clearPendingBinds() {
3432 mBindOnResumeCallbacks.clear();
3433 if (mPendingExecutor != null) {
3434 mPendingExecutor.markCompleted();
3435 mPendingExecutor = null;
3436 }
3437 }
3438
3439 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003440 * Refreshes the shortcuts shown on the workspace.
3441 *
3442 * Implementation of the method from LauncherModel.Callbacks.
3443 */
3444 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003445 if (LauncherAppState.PROFILE_STARTUP) {
3446 Trace.beginSection("Starting page bind");
3447 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003448 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003449
Winson Chungd64d1762013-08-20 14:37:16 -07003450 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003451 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003452 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003453
Winson Chung3d503fb2011-07-13 17:25:49 -07003454 if (mHotseat != null) {
3455 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003456 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003457 if (LauncherAppState.PROFILE_STARTUP) {
3458 Trace.endSection();
3459 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003460 }
3461
Adam Cohendcd297f2013-06-18 13:13:40 -07003462 @Override
3463 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003464 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003465 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3466 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003467 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3468 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3469 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003470 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3471 // If there are no screens, we need to have an empty screen
3472 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003473 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003474 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003475
3476 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003477 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003478 if (hasCustomContentToLeft()) {
3479 mWorkspace.createCustomContentContainer();
3480 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003481 }
Winsonc7d2e832016-07-28 12:24:55 -07003482
3483 // After we have added all the screens, if the wallpaper was locked to the default state,
3484 // then notify to indicate that it can be released and a proper wallpaper offset can be
3485 // computed before the next layout
3486 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07003487 }
3488
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003489 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003490 int count = orderedScreenIds.size();
3491 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003492 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003493 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003494 // No need to bind the first screen, as its always bound.
3495 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3496 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003497 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003498 }
3499
Winson Chungd64d1762013-08-20 14:37:16 -07003500 public void bindAppsAdded(final ArrayList<Long> newScreens,
3501 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003502 final ArrayList<ItemInfo> addAnimated,
3503 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003504 Runnable r = new Runnable() {
3505 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003506 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003507 }
3508 };
3509 if (waitUntilResume(r)) {
3510 return;
3511 }
3512
Winson Chungd64d1762013-08-20 14:37:16 -07003513 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003514 if (newScreens != null) {
3515 bindAddScreens(newScreens);
3516 }
Winson Chungd64d1762013-08-20 14:37:16 -07003517
3518 // We add the items without animation on non-visible pages, and with
3519 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003520 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003521 bindItems(addNotAnimated, 0,
3522 addNotAnimated.size(), false);
3523 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003524 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003525 bindItems(addAnimated, 0,
3526 addAnimated.size(), true);
3527 }
Winson Chungc58497e2013-09-03 17:48:37 -07003528
Winson Chung87412982013-10-03 18:34:14 -07003529 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003530 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003531
Winson Chungb745afb2015-03-02 11:51:23 -08003532 if (addedApps != null && mAppsView != null) {
3533 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003534 }
Winson Chungd64d1762013-08-20 14:37:16 -07003535 }
3536
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003537 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003538 * Bind the items start-end from the list.
3539 *
3540 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003541 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003542 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003543 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3544 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003545 Runnable r = new Runnable() {
3546 public void run() {
3547 bindItems(shortcuts, start, end, forceAnimateIcons);
3548 }
3549 };
3550 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003551 return;
3552 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003553
Winson Chungf0c6ae02012-03-21 16:10:31 -07003554 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003555 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3556 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003557 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003558 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003559 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003560 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003561 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003562
3563 // Short circuit if we are loading dock items for a configuration which has no dock
3564 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3565 mHotseat == null) {
3566 continue;
3567 }
3568
Sunny Goyal639e9062015-08-19 19:17:06 -07003569 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003570 switch (item.itemType) {
3571 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3572 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003573 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003574 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003575 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003576 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003577 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003578 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003579 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003580 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003581 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003582 default:
3583 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003584 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003585
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003586 /*
3587 * Remove colliding items.
3588 */
3589 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3590 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3591 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3592 View v = cl.getChildAt(item.cellX, item.cellY);
3593 Object tag = v.getTag();
3594 String desc = "Collision while binding workspace item: " + item
3595 + ". Collides with " + tag;
3596 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3597 throw (new RuntimeException(desc));
3598 } else {
3599 Log.d(TAG, desc);
3600 LauncherModel.deleteItemFromDatabase(this, item);
3601 continue;
3602 }
3603 }
3604 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003605 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3606 item.cellY, 1, 1);
3607 if (animateIcons) {
3608 // Animate all the applications up now
3609 view.setAlpha(0f);
3610 view.setScaleX(0f);
3611 view.setScaleY(0f);
3612 bounceAnims.add(createNewAppBounceAnimation(view, i));
3613 newShortcutsScreenId = item.screenId;
3614 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003615 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003616
Winson Chung997a9232013-07-24 15:33:46 -07003617 if (animateIcons) {
3618 // Animate to the correct page
3619 if (newShortcutsScreenId > -1) {
3620 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003621 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003622 final Runnable startBounceAnimRunnable = new Runnable() {
3623 public void run() {
3624 anim.playTogether(bounceAnims);
3625 anim.start();
3626 }
3627 };
Winson Chung997a9232013-07-24 15:33:46 -07003628 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003629 // We post the animation slightly delayed to prevent slowdowns
3630 // when we are loading right after we return to launcher.
3631 mWorkspace.postDelayed(new Runnable() {
3632 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003633 if (mWorkspace != null) {
3634 mWorkspace.snapToPage(newScreenIndex);
3635 mWorkspace.postDelayed(startBounceAnimRunnable,
3636 NEW_APPS_ANIMATION_DELAY);
3637 }
Winson Chung94d67682013-09-25 16:29:40 -07003638 }
3639 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003640 } else {
3641 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003642 }
3643 }
Winson Chung64359a52013-07-08 17:17:08 -07003644 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003645 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003646 }
3647
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003648 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3649 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3650 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003651 view.updateAppWidget(null);
3652 view.setOnClickListener(this);
3653 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003654 mWorkspace.requestLayout();
3655 }
3656
Joe Onorato9c1289c2009-08-17 11:03:03 -04003657 /**
3658 * Add the views for a widget to the workspace.
3659 *
3660 * Implementation of the method from LauncherModel.Callbacks.
3661 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003662 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003663 Runnable r = new Runnable() {
3664 public void run() {
3665 bindAppWidget(item);
3666 }
3667 };
3668 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003669 return;
3670 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003671
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003672 if (mIsSafeModeEnabled) {
3673 bindSafeModeWidget(item);
3674 return;
3675 }
3676
Daniel Sandler843e8602010-06-07 14:59:01 -04003677 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3678 if (DEBUG_WIDGETS) {
3679 Log.d(TAG, "bindAppWidget: " + item);
3680 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003681
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003682 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003683
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003684 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3685 // If the provider is not ready, bind as a pending widget.
3686 appWidgetInfo = null;
3687 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3688 // The widget id is not valid. Try to find the widget based on the provider info.
3689 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3690 } else {
3691 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3692 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003693
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003694 // If the provider is ready, but the width is not yet restored, try to restore it.
3695 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3696 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003697 if (appWidgetInfo == null) {
3698 if (DEBUG_WIDGETS) {
3699 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3700 + " belongs to component " + item.providerName
3701 + ", as the povider is null");
3702 }
3703 LauncherModel.deleteItemFromDatabase(this, item);
3704 return;
3705 }
Sunny Goyalff572272014-07-23 13:58:07 -07003706
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003707 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003708 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003709 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3710 // Id has not been allocated yet. Allocate a new id.
3711 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3712 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003713
Sunny Goyald478c832016-04-01 12:04:16 -07003714 // Also try to bind the widget. If the bind fails, the user will be shown
3715 // a click to setup UI, which will ask for the bind permission.
3716 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3717 pendingInfo.spanX = item.spanX;
3718 pendingInfo.spanY = item.spanY;
3719 pendingInfo.minSpanX = item.minSpanX;
3720 pendingInfo.minSpanY = item.minSpanY;
3721 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003722
3723 boolean isDirectConfig =
3724 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3725 if (isDirectConfig && item.bindOptions != null) {
3726 Bundle newOptions = item.bindOptions.getExtras();
3727 if (options != null) {
3728 newOptions.putAll(options);
3729 }
3730 options = newOptions;
3731 }
Sunny Goyald478c832016-04-01 12:04:16 -07003732 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3733 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003734
Sunny Goyal86df1382016-08-10 15:03:22 -07003735 // We tried to bind once. If we were not able to bind, we would need to
3736 // go through the permission dialog, which means we cannot skip the config
3737 // activity.
3738 item.bindOptions = null;
3739 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3740
Sunny Goyald478c832016-04-01 12:04:16 -07003741 // Bind succeeded
3742 if (success) {
3743 // If the widget has a configure activity, it is still needs to set it up,
3744 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003745 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003746 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3747 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003748 }
Sunny Goyald478c832016-04-01 12:04:16 -07003749
3750 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003751 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003752 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003753 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003754 // The widget was marked as UI not ready, but there is no configure activity to
3755 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003756 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3757 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003758 }
Sunny Goyalff572272014-07-23 13:58:07 -07003759 }
3760
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003761 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003762 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003763 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3764 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003765 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003766
Sunny Goyal56c73602015-09-25 12:55:01 -07003767 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003768 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003769 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003770 deleteWidgetInfo(item);
3771 return;
3772 }
3773
Sunny Goyal233ee962015-08-03 13:05:01 -07003774 item.minSpanX = appWidgetInfo.minSpanX;
3775 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003776 addAppWidgetToWorkspace(
3777 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3778 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003779 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003780 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003781 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003782 view.updateAppWidget(null);
3783 view.setOnClickListener(this);
3784 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003785 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003786 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003787
Daniel Sandler843e8602010-06-07 14:59:01 -04003788 if (DEBUG_WIDGETS) {
3789 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3790 + (SystemClock.uptimeMillis()-start) + "ms");
3791 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003792 }
3793
Sunny Goyalff572272014-07-23 13:58:07 -07003794 /**
3795 * Restores a pending widget.
3796 *
3797 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003798 */
Sunny Goyald478c832016-04-01 12:04:16 -07003799 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003800 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3801 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3802 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003803 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003804 }
3805
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003806 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003807 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003808
3809 mWorkspace.reinflateWidgetsIfNecessary();
3810 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003811 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003812 }
3813
Adam Cohen1462de32012-07-24 22:34:36 -07003814 public void onPageBoundSynchronously(int page) {
3815 mSynchronouslyBoundPages.add(page);
3816 }
3817
Sunny Goyal527c7d32015-08-28 15:19:36 -07003818 @Override
3819 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3820 if (mPendingExecutor != null) {
3821 mPendingExecutor.markCompleted();
3822 }
3823 mPendingExecutor = executor;
3824 executor.attachTo(this);
3825 }
3826
3827 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3828 if (mPendingExecutor == executor) {
3829 mPendingExecutor = null;
3830 }
3831 }
3832
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003833 @Override
3834 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3835 Runnable r = new Runnable() {
3836 public void run() {
3837 finishFirstPageBind(executor);
3838 }
3839 };
3840 if (waitUntilResume(r)) {
3841 return;
3842 }
3843
3844 Runnable onComplete = new Runnable() {
3845 @Override
3846 public void run() {
3847 if (executor != null) {
3848 executor.onLoadAnimationCompleted();
3849 }
3850 }
3851 };
3852 if (mDragLayer.getAlpha() < 1) {
3853 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3854 } else {
3855 onComplete.run();
3856 }
3857 }
3858
Joe Onorato9c1289c2009-08-17 11:03:03 -04003859 /**
3860 * Callback saying that there aren't any more items to bind.
3861 *
3862 * Implementation of the method from LauncherModel.Callbacks.
3863 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003864 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003865 Runnable r = new Runnable() {
3866 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003867 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003868 }
3869 };
3870 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003871 return;
3872 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003873 if (LauncherAppState.PROFILE_STARTUP) {
3874 Trace.beginSection("Page bind completed");
3875 }
Adam Cohen1462de32012-07-24 22:34:36 -07003876 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003877
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003878 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003879
Sunny Goyal2100c782016-08-22 16:00:03 -07003880 if (mPendingActivityResult != null) {
3881 handleActivityResult(mPendingActivityResult.requestCode,
3882 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3883 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003884 }
3885
Sunny Goyal756adbc2015-04-16 15:20:51 -07003886 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003887
3888 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003889 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003890 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003891 if (LauncherAppState.PROFILE_STARTUP) {
3892 Trace.endSection();
3893 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003894 }
3895
Winson Chunga2413752012-04-03 14:22:34 -07003896 private boolean canRunNewAppsAnimation() {
3897 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003898 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003899 }
3900
Winson Chungc9168342013-06-26 14:54:55 -07003901 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003902 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003903 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3904 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003905 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003906 return bounceAnim;
3907 }
3908
Adam Cohen27772732013-11-11 14:00:56 +00003909 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07003910 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00003911 }
3912
Tony Wickham55616cd2015-09-23 14:55:17 -07003913 public int getSearchBarHeight() {
3914 if (mLauncherCallbacks != null) {
3915 return mLauncherCallbacks.getSearchBarHeight();
3916 }
3917 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
3918 }
3919
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003920 /**
Winson Chungbb785c62015-05-05 10:05:08 -07003921 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
3922 * multiple calls to bind the same list.)
3923 */
3924 @Thunk ArrayList<AppInfo> mTmpAppsList;
3925 private Runnable mBindAllApplicationsRunnable = new Runnable() {
3926 public void run() {
3927 bindAllApplications(mTmpAppsList);
3928 mTmpAppsList = null;
3929 }
3930 };
3931
3932 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003933 * Add the icons for all apps.
3934 *
3935 * Implementation of the method from LauncherModel.Callbacks.
3936 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003937 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07003938 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
3939 mTmpAppsList = apps;
3940 return;
3941 }
3942
Winson Chungb745afb2015-03-02 11:51:23 -08003943 if (mAppsView != null) {
3944 mAppsView.setApps(apps);
3945 }
Adam Cohen9211d422014-10-07 18:14:53 -07003946 if (mLauncherCallbacks != null) {
3947 mLauncherCallbacks.bindAllApplications(apps);
3948 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003949 }
3950
3951 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003952 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
3953 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
3954 */
3955 @Override
3956 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
3957 mDeepShortcutMap = deepShortcutMapCopy;
3958 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
3959 }
3960
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003961 public List<String> getShortcutIdsForItem(ItemInfo info) {
3962 if (!DeepShortcutManager.supportsShortcuts(info)) {
3963 return Collections.EMPTY_LIST;
3964 }
3965 ComponentName component = info.getTargetComponent();
Hyunyoung Songd3bf9802016-08-29 14:43:53 -07003966 if (component == null) {
3967 return Collections.EMPTY_LIST;
3968 }
3969
Tony Wickham1bce7fd2016-04-28 17:39:03 -07003970 List<String> ids = mDeepShortcutMap.get(new ComponentKey(component, info.user));
3971 return ids == null ? Collections.EMPTY_LIST : ids;
3972 }
3973
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003974 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003975 * A package was updated.
3976 *
3977 * Implementation of the method from LauncherModel.Callbacks.
3978 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003979 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003980 Runnable r = new Runnable() {
3981 public void run() {
3982 bindAppsUpdated(apps);
3983 }
3984 };
3985 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003986 return;
3987 }
3988
Winson Chungb745afb2015-03-02 11:51:23 -08003989 if (mAppsView != null) {
3990 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07003991 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003992 }
3993
Sunny Goyal4390ace2014-10-13 11:33:11 -07003994 @Override
3995 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
3996 Runnable r = new Runnable() {
3997 public void run() {
3998 bindWidgetsRestored(widgets);
3999 }
4000 };
4001 if (waitUntilResume(r)) {
4002 return;
4003 }
4004 mWorkspace.widgetsRestored(widgets);
4005 }
4006
Joe Onorato9c1289c2009-08-17 11:03:03 -04004007 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004008 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004009 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004010 *
4011 * @param updated list of shortcuts which have changed.
4012 * @param removed list of shortcuts which were deleted in the background. This can happen when
4013 * an app gets removed from the system or some of its components are no longer
4014 * available.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004015 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004016 @Override
4017 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4018 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004019 Runnable r = new Runnable() {
4020 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004021 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004022 }
4023 };
4024 if (waitUntilResume(r)) {
4025 return;
4026 }
4027
Sunny Goyal4390ace2014-10-13 11:33:11 -07004028 if (!updated.isEmpty()) {
4029 mWorkspace.updateShortcuts(updated);
4030 }
4031
4032 if (!removed.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004033 HashSet<ComponentName> removedComponents = new HashSet<>();
4034 HashSet<ShortcutKey> removedDeepShortcuts = new HashSet<>();
4035
Sunny Goyal4390ace2014-10-13 11:33:11 -07004036 for (ShortcutInfo si : removed) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004037 if (si.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07004038 removedDeepShortcuts.add(ShortcutKey.fromShortcutInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004039 } else {
4040 removedComponents.add(si.getTargetComponent());
4041 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004042 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004043
4044 if (!removedComponents.isEmpty()) {
4045 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(removedComponents, user);
4046 mWorkspace.removeItemsByMatcher(matcher);
4047 mDragController.onAppsRemoved(matcher);
4048 }
4049
4050 if (!removedDeepShortcuts.isEmpty()) {
4051 ItemInfoMatcher matcher = ItemInfoMatcher.ofShortcutKeys(removedDeepShortcuts);
4052 mWorkspace.removeItemsByMatcher(matcher);
4053 mDragController.onAppsRemoved(matcher);
4054 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004055 }
4056 }
4057
4058 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004059 * Update the state of a package, typically related to install state.
4060 *
4061 * Implementation of the method from LauncherModel.Callbacks.
4062 */
Sunny Goyale755d462014-07-22 13:48:29 -07004063 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004064 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4065 Runnable r = new Runnable() {
4066 public void run() {
4067 bindRestoreItemsChange(updates);
4068 }
4069 };
4070 if (waitUntilResume(r)) {
4071 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004072 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004073
Sunny Goyal756adbc2015-04-16 15:20:51 -07004074 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004075 }
4076
4077 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004078 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004079 * in addition to specific applications to remove, the reason being that
4080 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004081 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004082 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004083 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004084 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004085 public void bindWorkspaceComponentsRemoved(
4086 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4087 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004088 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004089 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004090 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004091 }
Winson Chungd64d1762013-08-20 14:37:16 -07004092 };
4093 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004094 return;
4095 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004096 if (!packageNames.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004097 ItemInfoMatcher matcher = ItemInfoMatcher.ofPackages(packageNames, user);
4098 mWorkspace.removeItemsByMatcher(matcher);
4099 mDragController.onAppsRemoved(matcher);
4100
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004101 }
4102 if (!components.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004103 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(components, user);
4104 mWorkspace.removeItemsByMatcher(matcher);
4105 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004106 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004107 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004108
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004109 @Override
4110 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4111 Runnable r = new Runnable() {
4112 public void run() {
4113 bindAppInfosRemoved(appInfos);
4114 }
4115 };
4116 if (waitUntilResume(r)) {
4117 return;
Joe Onorato36115782010-06-17 13:28:48 -04004118 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004119
Winson Chungdf95eb12013-10-16 14:57:07 -07004120 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004121 if (mAppsView != null) {
4122 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004123 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004124 }
Joe Onoratobe386092009-11-17 17:32:16 -08004125
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004126 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004127 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004128 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004129 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004130 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004131
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004132 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004133 public void bindWidgetsModel(WidgetsModel model) {
4134 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004135 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004136 return;
4137 }
4138
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004139 if (mWidgetsView != null && model != null) {
4140 mWidgetsView.addWidgets(model);
4141 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004142 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004143 }
4144
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004145 @Override
4146 public void notifyWidgetProvidersChanged() {
4147 if (mWorkspace.getState().shouldUpdateWidget) {
4148 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4149 }
4150 }
4151
Winson Chung400438b2011-01-16 17:53:48 -08004152 private int mapConfigurationOriActivityInfoOri(int configOri) {
4153 final Display d = getWindowManager().getDefaultDisplay();
4154 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4155 switch (d.getRotation()) {
4156 case Surface.ROTATION_0:
4157 case Surface.ROTATION_180:
4158 // We are currently in the same basic orientation as the natural orientation
4159 naturalOri = configOri;
4160 break;
4161 case Surface.ROTATION_90:
4162 case Surface.ROTATION_270:
4163 // We are currently in the other basic orientation to the natural orientation
4164 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4165 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4166 break;
4167 }
4168
4169 int[] oriMap = {
4170 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4171 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4172 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4173 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4174 };
4175 // Since the map starts at portrait, we need to offset if this device's natural orientation
4176 // is landscape.
4177 int indexOffset = 0;
4178 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4179 indexOffset = 1;
4180 }
4181 return oriMap[(d.getRotation() + indexOffset) % 4];
4182 }
Adam Cohen446e9402011-09-15 18:21:21 -07004183
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004184 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004185 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004186 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004187 if (Utilities.ATLEAST_JB_MR2) {
4188 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4189 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004190 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4191 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004192 }
Winson Chung4b919f82012-05-01 10:44:08 -07004193 }
4194 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004195
Winson Chung4b919f82012-05-01 10:44:08 -07004196 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004197 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004198 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004199 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004200 } else {
4201 mHandler.postDelayed(new Runnable() {
4202 public void run() {
4203 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4204 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004205 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004206 }
Winson Chung4b919f82012-05-01 10:44:08 -07004207 }
Winson Chung400438b2011-01-16 17:53:48 -08004208 }
4209
Hyunyoung Songc001cf52016-07-21 17:32:43 -07004210 private void markAppsViewShown() {
4211 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4212 return;
4213 }
4214 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
4215 }
4216
4217 private boolean shouldShowDiscoveryBounce() {
4218 if (mState != mState.WORKSPACE) {
4219 return false;
4220 }
4221 if (mLauncherCallbacks != null && mLauncherCallbacks.shouldShowDiscoveryBounce()) {
4222 return true;
4223 }
4224 if (!mIsResumeFromActionScreenOff) {
4225 return false;
4226 }
4227 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4228 return false;
4229 }
4230 return true;
4231 }
4232
Winson Chung5ffd51d2015-06-25 11:36:50 -07004233 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004234 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004235 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004236 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004237 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004238 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004239 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4240 if (userHandle != null) {
4241 user = UserHandleCompat.fromUser(userHandle);
4242 }
4243 }
4244 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004245 }
4246
4247 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004248 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004249 if (user == null) {
4250 user = UserHandleCompat.myUserHandle();
4251 }
4252
4253 // Called from search suggestion, add the profile extra to the intent to ensure that we
4254 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004255 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004256 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004257 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004258 return null;
4259 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004260 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004261 }
4262
Winson Chung5ffd51d2015-06-25 11:36:50 -07004263 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004264 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4265 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004266 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004267 UserHandleCompat.myUserHandle());
4268 }
4269
Winson Chung5ffd51d2015-06-25 11:36:50 -07004270 protected void moveWorkspaceToDefaultScreen() {
4271 mWorkspace.moveToDefaultScreen(false);
4272 }
4273
Winson Chung80baf5a2010-08-09 16:03:15 -07004274 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004275 * Returns a FastBitmapDrawable with the icon, accurately sized.
4276 */
4277 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4278 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4279 d.setFilterBitmap(true);
4280 resizeIconDrawable(d);
4281 return d;
4282 }
4283
4284 /**
4285 * Resizes an icon drawable to the correct icon size.
4286 */
Winson5fbe0742015-09-30 15:33:00 -07004287 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004288 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004289 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004290 }
4291
4292 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004293 * Prints out out state for debugging.
4294 */
4295 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004296 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004297 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
Sunny Goyal2100c782016-08-22 16:00:03 -07004298 Log.d(TAG, "mPendingRequestArgs=" + mPendingRequestArgs);
4299 Log.d(TAG, "mPendingActivityResult=" + mPendingActivityResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004300 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004301 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004302
Daniel Sandler325dc232013-06-05 22:57:57 -04004303 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004304 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004305
4306 @Override
4307 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4308 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004309 // Dump workspace
4310 writer.println(prefix + "Workspace Items");
4311 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4312 writer.println(prefix + " Homescreen " + i);
4313
4314 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4315 for (int j = 0; j < layout.getChildCount(); j++) {
4316 Object tag = layout.getChildAt(j).getTag();
4317 if (tag != null) {
4318 writer.println(prefix + " " + tag.toString());
4319 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004320 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004321 }
Sunny Goyala1365452015-10-01 15:46:24 -07004322
4323 writer.println(prefix + " Hotseat");
4324 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4325 for (int j = 0; j < layout.getChildCount(); j++) {
4326 Object tag = layout.getChildAt(j).getTag();
4327 if (tag != null) {
4328 writer.println(prefix + " " + tag.toString());
4329 }
4330 }
4331
Sunny Goyal713edfc2016-05-06 09:58:34 -07004332 try {
4333 FileLog.flushAll(writer);
4334 } catch (Exception e) {
4335 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004336 }
4337
Adam Cohen9211d422014-10-07 18:14:53 -07004338 if (mLauncherCallbacks != null) {
4339 mLauncherCallbacks.dump(prefix, fd, writer, args);
4340 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004341 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004342
Sunny Goyal66b24572016-09-21 15:57:55 -07004343 @Override
4344 @TargetApi(Build.VERSION_CODES.N)
4345 public void onProvideKeyboardShortcuts(
4346 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
4347
4348 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
4349 if (mState == State.WORKSPACE) {
4350 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
4351 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
4352 }
4353 View currentFocus = getCurrentFocus();
4354 if (new CustomActionsPopup(this, currentFocus).canShow()) {
4355 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
4356 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
4357 }
4358 if (currentFocus instanceof BubbleTextView &&
4359 ((BubbleTextView) currentFocus).hasDeepShortcuts()) {
4360 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
4361 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
4362 }
4363 if (!shortcutInfos.isEmpty()) {
4364 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
4365 }
4366
4367 super.onProvideKeyboardShortcuts(data, menu, deviceId);
4368 }
4369
4370 @Override
4371 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
4372 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
4373 switch (keyCode) {
4374 case KeyEvent.KEYCODE_A:
4375 if (mState == State.WORKSPACE) {
4376 showAppsView(true, true, false);
4377 return true;
4378 }
4379 break;
4380 case KeyEvent.KEYCODE_S: {
4381 View focusedView = getCurrentFocus();
4382 if (focusedView instanceof BubbleTextView
4383 && focusedView.getTag() instanceof ItemInfo
4384 && mAccessibilityDelegate.performAction(focusedView,
4385 (ItemInfo) focusedView.getTag(),
4386 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
4387 getOpenShortcutsContainer().requestFocus();
4388 return true;
4389 }
4390 break;
4391 }
4392 case KeyEvent.KEYCODE_O:
4393 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
4394 return true;
4395 }
4396 break;
4397 }
4398 }
4399 return super.onKeyShortcut(keyCode, event);
4400 }
4401
Adam Cohen59400422014-03-05 18:07:04 -08004402 public static CustomAppWidget getCustomAppWidget(String name) {
4403 return sCustomAppWidgets.get(name);
4404 }
4405
4406 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4407 return sCustomAppWidgets;
4408 }
4409
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004410 public static Launcher getLauncher(Context context) {
4411 if (context instanceof Launcher) {
4412 return (Launcher) context;
4413 }
4414 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4415 }
4416
Sunny Goyal745bad92016-05-02 10:54:12 -07004417 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4418
4419 @Override
4420 public void onSharedPreferenceChanged(
4421 SharedPreferences sharedPreferences, String key) {
4422 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4423 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4424 if (!waitUntilResume(this, true)) {
4425 run();
4426 }
4427 }
4428 }
4429
4430 @Override
4431 public void run() {
4432 setOrientation();
4433 }
4434 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004435}