blob: 469e78e719fddf07883f5f3f6707537f46fa2fae [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;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080036import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070039import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040040import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070042import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070043import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070045import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.pm.PackageManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070049import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070050import android.graphics.Bitmap;
51import android.graphics.Canvas;
52import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070053import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070054import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070055import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020056import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080058import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070059import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070060import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040061import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070062import android.os.Trace;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070063import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.text.Selection;
65import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070066import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070068import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070069import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070070import android.view.HapticFeedbackConstants;
71import android.view.KeyEvent;
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 Goyal5a1f53b2015-05-27 10:24:24 -070082import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.widget.Advanceable;
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 Goyal3333b0c2016-05-09 20:43:21 -0700109import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700110import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700111import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700112import com.android.launcher3.model.WidgetsModel;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700113import com.android.launcher3.pageindicators.PageIndicator;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700114import com.android.launcher3.shortcuts.DeepShortcutManager;
Tony Wickham49c8d292016-07-01 11:44:34 -0700115import com.android.launcher3.shortcuts.DeepShortcutsContainer;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700116import com.android.launcher3.shortcuts.ShortcutKey;
Hyunyoung Song5aa27142016-07-21 11:48:37 -0700117import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal2100c782016-08-22 16:00:03 -0700118import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700119import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700120import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700121import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700122import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal2100c782016-08-22 16:00:03 -0700123import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal322d5562015-06-25 19:35:49 -0700124import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700125import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700126import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700127import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700128import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700129import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700130
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700131import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700132import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700133import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700134import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800135import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700136import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700137import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700138import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700139
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140/**
141 * Default launcher application.
142 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100143public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700144 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
145 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700146 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700147 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700148 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700150 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700151 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400152 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700153
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700155 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700156 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700157 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158
Michael Jurka8b805b12012-04-18 14:23:14 -0700159 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700160 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700161 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700162
Sunny Goyal28c6b962015-10-12 11:42:05 -0700163 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
164
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700165 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
166
Mathew Inwood876a8462013-06-14 14:12:41 +0100167 /**
168 * IntentStarter uses request codes starting with this. This must be greater than all activity
169 * request codes used internally.
170 */
171 protected static final int REQUEST_LAST = 100;
172
Michael Jurka0a457bf2012-11-19 14:05:05 -0800173 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700174 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800175 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
Winson Chung2672ff92012-05-04 16:22:30 -0700177 // The Intent extra that defines whether to ignore the launch animation
178 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400179 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700180
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700181 public static final String ACTION_APPWIDGET_HOST_RESET =
182 "com.android.launcher3.intent.ACTION_APPWIDGET_HOST_RESET";
183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 // Type: int
185 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700186 // Type: int
187 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700188 // Type: PendingRequestArgs
189 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
190 // Type: ActivityResultInfo
191 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700193 static final String APPS_VIEW_SHOWN = "launcher.apps_view_shown";
Adam Cohenb54a5982014-01-08 15:21:04 -0800194
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700195 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700196 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
197 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700198
Adam Cohen091440a2015-03-18 14:16:05 -0700199 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700200 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700201
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700202 private boolean mIsSafeModeEnabled;
203
Joe Onorato9c1289c2009-08-17 11:03:03 -0400204 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800205 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700206 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700207 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208
Winson Chunga2413752012-04-03 14:22:34 -0700209 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700210 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
211 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700212 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700213
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700214 private final BroadcastReceiver mUiBroadcastReceiver = new BroadcastReceiver() {
215
216 @Override
217 public void onReceive(Context context, Intent intent) {
218 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
219 closeSystemDialogs();
220 } else if (ACTION_APPWIDGET_HOST_RESET.equals(intent.getAction())) {
221 if (mAppWidgetHost != null) {
222 mAppWidgetHost.startListening();
223 }
224 }
225 }
226 };
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800227
Adam Cohen091440a2015-03-18 14:16:05 -0700228 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700229 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700230 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800231 private DragController mDragController;
Sunny Goyal6178f132016-07-11 17:30:03 -0700232 private View mQsbContainer;
Sunny Goyal322d5562015-06-25 19:35:49 -0700233
234 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700235
Sunny Goyalffe83f12014-08-14 17:39:34 -0700236 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700237 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700238
Michael Jurka0280c3b2010-09-17 15:00:07 -0700239 private int[] mTmpAddItemCellCoordinates = new int[2];
240
Sunny Goyal316490e2015-06-02 09:38:28 -0700241 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800242 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700243
Michael Jurka838a4ca2011-02-07 13:33:06 -0800244 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700245 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700246
Sunny Goyal47328fd2016-05-25 18:56:41 -0700247 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700248
249 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700250 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700251 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700252
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700253 // Main container view and the model for the widget tray screen.
254 @Thunk WidgetsContainerView mWidgetsView;
255 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700256
Winson Chung4a2afa32012-07-19 14:53:05 -0700257 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
258 // scroll issues (because the workspace may not have been measured yet) and extra work.
259 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
260 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261
262 private SpannableStringBuilder mDefaultKeySsb = null;
263
Adam Cohen091440a2015-03-18 14:16:05 -0700264 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400265
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800266 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700267 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268
Michael Jurka1e2f4652013-07-08 18:03:46 -0700269 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700270 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700271 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700272
Joe Onorato9c1289c2009-08-17 11:03:03 -0400273 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800274 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700275 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700276 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700277 private boolean mIsResumeFromActionScreenOff;
Adam Cohen091440a2015-03-18 14:16:05 -0700278 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800279 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700280 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800281 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
Adam Cohended9f8d2010-11-03 13:25:16 -0700288 // Related to the auto-advancing of widgets
289 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700290 private static final int ADVANCE_INTERVAL = 20000;
291 private static final int ADVANCE_STAGGER = 250;
292
293 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700294 private long mAutoAdvanceSentTime;
295 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700296 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700297
Winson Chung400438b2011-01-16 17:53:48 -0800298 // Determines how long to wait after a rotation before restoring the screen orientation to
299 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700300 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800301
Adam Cohen1462de32012-07-24 22:34:36 -0700302 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
303
Winson Chung46353de2012-02-16 14:05:10 -0800304 // We only want to get the SharedPreferences once since it does an FS stat each time we get
305 // it from the context.
306 private SharedPreferences mSharedPrefs;
307
Winson Chungf0c6ae02012-03-21 16:10:31 -0700308 // Holds the page that we need to animate to, and the icon views that we need to animate up
309 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700310 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700311 private Bitmap mFolderIconBitmap;
312 private Canvas mFolderIconCanvas;
313 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700314
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700315 private DeviceProfile mDeviceProfile;
316
Adam Cohen4b66bf32015-09-18 12:15:19 -0700317 private boolean mMoveToDefaultScreenFromNewIntent;
318
Winson Chung13eb5272015-05-11 16:30:13 -0700319 // This is set to the view that launched the activity that navigated the user away from
320 // launcher. Since there is no callback for when the activity has finished launching, enable
321 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800322 private BubbleTextView mWaitingForResume;
323
Adam Cohen59400422014-03-05 18:07:04 -0800324 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
325 new HashMap<String, CustomAppWidget>();
326
Adam Cohen59400422014-03-05 18:07:04 -0800327 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700328 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
329 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800330 }
331 }
332
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700333 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
334 // state transition. If another state transition happened during this delay,
335 // simply unregister this runnable.
336 private Runnable mExitSpringLoadedModeRunnable;
337
Adam Cohen091440a2015-03-18 14:16:05 -0700338 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700339 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700340 if (mWorkspace != null) {
341 mWorkspace.buildPageHardwareLayers();
342 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700343 }
344 };
345
Sunny Goyal2100c782016-08-22 16:00:03 -0700346 // Activity result which needs to be processed after workspace has loaded.
347 private ActivityResultInfo mPendingActivityResult;
348 /**
349 * Holds extra information required to handle a result from an external call, like
350 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
351 */
352 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800353
Hyunyoung Songaa953652016-04-19 18:30:24 -0700354 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800355
Jon Miranda379198e2016-09-23 08:54:40 -0700356 private float mLastDispatchTouchEventX = 0.0f;
357 private float mEdgeOfScreenThresholdPx = 0.0f;
358
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700359 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700360 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400361
362 @Thunk void setOrientation() {
363 if (mRotationEnabled) {
364 unlockScreenOrientation(true);
365 } else {
366 setRequestedOrientation(
367 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700368 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400369 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700370
Sunny Goyal745bad92016-05-02 10:54:12 -0700371 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700372
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 @Override
374 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700375 if (DEBUG_STRICT_MODE) {
376 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
377 .detectDiskReads()
378 .detectDiskWrites()
379 .detectNetwork() // or .detectAll() for all detectable problems
380 .penaltyLog()
381 .build());
382 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
383 .detectLeakedSqlLiteObjects()
384 .detectLeakedClosableObjects()
385 .penaltyLog()
386 .penaltyDeath()
387 .build());
388 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700389 if (LauncherAppState.PROFILE_STARTUP) {
390 Trace.beginSection("Launcher-onCreate");
391 }
Winson Chunga2413752012-04-03 14:22:34 -0700392
Adam Cohen9211d422014-10-07 18:14:53 -0700393 if (mLauncherCallbacks != null) {
394 mLauncherCallbacks.preOnCreate();
395 }
396
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400398
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400399 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700400
Adam Cohen2e6da152015-05-06 11:42:25 -0700401 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700402 mDeviceProfile = getResources().getConfiguration().orientation
403 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700404 app.getInvariantDeviceProfile().landscapeProfile
405 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700406
Sunny Goyalf7258242015-10-19 16:59:07 -0700407 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700408 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800409 mModel = app.setLauncher(this);
410 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700411 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700412
Joe Onorato41a12d22009-10-31 18:30:00 -0400413 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700414 mAllAppsController = new AllAppsTransitionController(this);
415 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700416
Sunny Goyalffe83f12014-08-14 17:39:34 -0700417 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700418
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700419 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
420 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700421
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700422 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
423 // this also ensures that any synchronous binding below doesn't re-trigger another
424 // LauncherModel load.
425 mPaused = false;
426
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800427 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700428
Jon Miranda379198e2016-09-23 08:54:40 -0700429 mEdgeOfScreenThresholdPx = getResources()
430 .getDimensionPixelSize(R.dimen.edge_of_screen_threshold);
431
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432 setupViews();
Winson1f064272016-07-18 17:18:02 -0700433 mDeviceProfile.layout(this, false /* notifyListeners */);
Tony Wickham827cef22016-03-17 15:39:39 -0700434 mExtractedColors = new ExtractedColors();
435 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436
Tony Wickhame2217252016-03-22 16:34:23 -0700437 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
438 .addAccessibilityStateChangeListener(this);
439
Joe Onorato7c312c12009-08-13 21:36:53 -0700440 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700441
Sunny Goyalfe770c92016-09-27 14:38:58 -0700442 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443
Sunny Goyale26d1002016-06-20 14:52:14 -0700444 if (LauncherAppState.PROFILE_STARTUP) {
445 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 }
447
Sunny Goyal2100c782016-08-22 16:00:03 -0700448 // We only load the page synchronously if the user rotates (or triggers a
449 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700450 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
451 if (savedInstanceState != null) {
452 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
453 }
454 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700455 // If we are not binding synchronously, show a fade in animation when
456 // the first page bind completes.
457 mDragLayer.setAlpha(0);
458 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700459 // Pages bound synchronously.
460 mWorkspace.setCurrentPage(currentScreen);
461
Sunny Goyal2100c782016-08-22 16:00:03 -0700462 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800463 }
464
465 // For handling default keys
466 mDefaultKeySsb = new SpannableStringBuilder();
467 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800468
469 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700470 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
471 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800472
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800473 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700474 // In case we are on a device with locked rotation, we should look at preferences to check
475 // if the user has specifically allowed rotation.
476 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700477 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700478 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
479 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700480 }
481
482 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
483 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400484 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700485
Adam Cohen9211d422014-10-07 18:14:53 -0700486 if (mLauncherCallbacks != null) {
487 mLauncherCallbacks.onCreate(savedInstanceState);
488 }
489 }
490
Sunny Goyal7779d622015-06-11 16:18:39 -0700491 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700492 public void onExtractedColorsChanged() {
493 loadExtractedColorsAndColorItems();
494 }
495
496 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700497 // TODO: do this in pre-N as well, once the extraction part is complete.
498 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700499 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700500 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700501 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham827cef22016-03-17 15:39:39 -0700502 }
503 }
504
Adam Cohen9211d422014-10-07 18:14:53 -0700505 private LauncherCallbacks mLauncherCallbacks;
506
507 public void onPostCreate(Bundle savedInstanceState) {
508 super.onPostCreate(savedInstanceState);
509 if (mLauncherCallbacks != null) {
510 mLauncherCallbacks.onPostCreate(savedInstanceState);
511 }
512 }
513
Winson1f064272016-07-18 17:18:02 -0700514 public void onInsetsChanged(Rect insets) {
515 mDeviceProfile.updateInsets(insets);
516 mDeviceProfile.layout(this, true /* notifyListeners */);
517 }
518
Sunny Goyal32554d12015-12-03 15:31:25 -0800519 /**
520 * Call this after onCreate to set or clear overlay.
521 */
522 public void setLauncherOverlay(LauncherOverlay overlay) {
523 if (overlay != null) {
524 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
525 }
526 mWorkspace.setLauncherOverlay(overlay);
527 }
528
Adam Cohen9211d422014-10-07 18:14:53 -0700529 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
530 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700531 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700532 private boolean mWorkspaceImportanceStored = false;
533 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700534 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800535 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700536 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
537
538 @Override
539 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700540 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700541 return;
542 }
543 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700544 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700545 if (mWorkspace != null) {
546 mWorkspaceImportanceForAccessibility =
547 mWorkspace.getImportantForAccessibility();
548 mWorkspace.setImportantForAccessibility(
549 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
550 mWorkspaceImportanceStored = true;
551 }
552 if (mHotseat != null) {
553 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
554 mHotseat.setImportantForAccessibility(
555 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
556 mHotseatImportanceStored = true;
557 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700558 }
559
560 @Override
561 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700562 if (mWorkspaceImportanceStored && mWorkspace != null) {
563 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
564 }
565 if (mHotseatImportanceStored && mHotseat != null) {
566 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
567 }
568 mWorkspaceImportanceStored = false;
569 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700570 }
571 });
Adam Cohen9211d422014-10-07 18:14:53 -0700572 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700573 }
574
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700575 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700576 public void onLauncherProviderChange() {
577 if (mLauncherCallbacks != null) {
578 mLauncherCallbacks.onLauncherProviderChange();
579 }
580 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700581
Adam Cohen9211d422014-10-07 18:14:53 -0700582 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700583 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700584 if (mLauncherCallbacks != null) {
585 return mLauncherCallbacks.hasCustomContentToLeft();
586 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700587 return false;
588 }
589
Dave Hawkeya8881582013-09-17 15:55:33 -0600590 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500591 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600592 * {@link #addToCustomContentPage}. This will only be invoked if
593 * {@link #hasCustomContentToLeft()} is {@code true}.
594 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500595 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700596 if (mLauncherCallbacks != null) {
597 mLauncherCallbacks.populateCustomContentContainer();
598 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600599 }
600
601 /**
Sunny Goyalfe770c92016-09-27 14:38:58 -0700602 * Invoked by subclasses to signal a change to the {@link #addToCustomContentPage} value to
Dave Hawkeya8881582013-09-17 15:55:33 -0600603 * ensure the custom content page is added or removed if necessary.
604 */
605 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600606 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600607 // Not bound yet, wait for bindScreens to be called.
608 return;
609 }
610
611 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
612 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500613 mWorkspace.createCustomContentContainer();
614 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600615 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
616 mWorkspace.removeCustomContentPage();
617 }
618 }
619
Hyunyoung Songaa953652016-04-19 18:30:24 -0700620 public UserEventDispatcher getUserEventDispatcher() {
621 if (mLauncherCallbacks != null) {
622 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
623 if (dispatcher != null) {
624 return dispatcher;
625 }
626 }
627
Sunny Goyal64976d52016-06-20 14:56:28 -0700628 // Logger object is a singleton and does not have to be coupled with the foreground
629 // activity. Since most user event logging is done on the UI, the object is retrieved
630 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700631 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700632 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700633 }
634 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800635 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100636
Hyunyoung Song3f471442015-04-08 19:01:34 -0700637 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700638 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
639 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700640 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700641 }
642
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000643 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700644 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
645 // This cast is safe as long as the id < 0x00FFFFFF
646 // Since we jail all the dynamically generated views, there should be no clashes
647 // with any other views.
648 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000649 }
650
651 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700652 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
653 * a configuration step, this allows the proper animations to run after other transitions.
654 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700655 private long completeAdd(
656 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
657 long screenId = info.screenId;
658 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700659 // When the screen id represents an actual screen (as opposed to a rank) we make sure
660 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700661 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700662 }
Adam Cohendb364c32014-05-20 14:23:40 -0700663
Sunny Goyal2100c782016-08-22 16:00:03 -0700664 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800665 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700666 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700667 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800668 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700669 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700670 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700671 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700672 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700673 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700674 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700675 int widgetId = appWidgetId;
676 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700677 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700678 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700679 // Since the view was just bound, also launch the configure activity if needed
680 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
681 .getLauncherAppWidgetInfo(widgetId);
682 if (provider != null && provider.configure != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700683 startRestoredWidgetReconfigActivity(provider, widgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -0700684 }
685 }
686 break;
687 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800688 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700689
Adam Cohendb364c32014-05-20 14:23:40 -0700690 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800691 }
692
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800693 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700694 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700695 if (isWorkspaceLoading()) {
696 // process the result once the workspace has loaded.
697 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
698 return;
699 }
700 mPendingActivityResult = null;
701
Winson Chunge341d302013-08-16 14:31:00 -0700702 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700703 final PendingRequestArgs requestArgs = mPendingRequestArgs;
704 setWaitingForResult(null);
705 if (requestArgs == null) {
706 return;
707 }
708
709 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700710
Adam Cohenad4e15c2013-10-17 16:21:35 -0700711 Runnable exitSpringLoaded = new Runnable() {
712 @Override
713 public void run() {
714 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
715 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
716 }
717 };
718
Michael Jurka8b805b12012-04-18 14:23:14 -0700719 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700720 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700721 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700722 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
723 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700724 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700725 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700726 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700727 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700728 addAppWidgetImpl(
729 appWidgetId, requestArgs, null,
730 requestArgs.getWidgetProvider(),
731 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700732 }
733 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200734 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
735 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700736 // User could have free-scrolled between pages before picking a wallpaper; make sure
737 // we move to the closest one now.
738 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700739 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200740 }
741 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700742 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200743
Adam Cohened66b2b2012-01-23 17:28:51 -0800744 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700745 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700746
Adam Cohened66b2b2012-01-23 17:28:51 -0800747 // We have special handling for widgets
748 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800749 final int appWidgetId;
750 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
751 : -1;
752 if (widgetId < 0) {
753 appWidgetId = pendingAddWidgetId;
754 } else {
755 appWidgetId = widgetId;
756 }
757
Adam Cohenad4e15c2013-10-17 16:21:35 -0700758 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800759 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700760 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
761 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700762 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700763 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700764 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700765 @Override
766 public void run() {
767 exitSpringLoadedDragModeDelayed(false, 0, null);
768 }
769 };
Adam Cohendb364c32014-05-20 14:23:40 -0700770
Sunny Goyal2100c782016-08-22 16:00:03 -0700771 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
772 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
773 } else {
774 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
775 // When the screen id represents an actual screen (as opposed to a rank)
776 // we make sure that the drop page actually exists.
777 requestArgs.screenId =
778 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700779 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700780 final CellLayout dropLayout =
781 mWorkspace.getScreenWithId(requestArgs.screenId);
782
783 dropLayout.setDropPending(true);
784 final Runnable onComplete = new Runnable() {
785 @Override
786 public void run() {
787 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
788 dropLayout.setDropPending(false);
789 }
790 };
791 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
792 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700793 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800794 return;
795 }
796
Sunny Goyald478c832016-04-01 12:04:16 -0700797 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
798 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700799 if (resultCode == RESULT_OK) {
800 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700801 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700802 }
803 // Leave the widget in the pending state if the user canceled the configure.
804 return;
805 }
806
Sunny Goyalaad90582015-07-09 14:22:50 -0700807 if (requestCode == REQUEST_CREATE_SHORTCUT) {
808 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700809 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
810 completeAdd(requestCode, data, -1, requestArgs);
811 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
812 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
813
Sunny Goyalaad90582015-07-09 14:22:50 -0700814 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700815 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
816 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800819 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800820 }
821
822 @Override
823 protected void onActivityResult(
824 final int requestCode, final int resultCode, final Intent data) {
825 handleActivityResult(requestCode, resultCode, data);
826 if (mLauncherCallbacks != null) {
827 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
828 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800829 }
830
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600831 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700832 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600833 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700834 PendingRequestArgs pendingArgs = mPendingRequestArgs;
835 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
836 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
837 setWaitingForResult(null);
838
Sunny Goyal28c6b962015-10-12 11:42:05 -0700839 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700840 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700841 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700842 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700843 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700844 Intent intent = pendingArgs.getPendingIntent();
845
Sunny Goyal28c6b962015-10-12 11:42:05 -0700846 if (grantResults.length > 0
847 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700848 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700849 } else {
850 // TODO: Show a snack bar with link to settings
851 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700852 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700853 }
854 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600855 if (mLauncherCallbacks != null) {
856 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
857 grantResults);
858 }
859 }
860
Adam Cohendb364c32014-05-20 14:23:40 -0700861 /**
862 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
863 *
864 * @param screenId the screen id to check
865 * @return the new screen, or screenId if it exists
866 */
867 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800868 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700869 if (dropLayout == null) {
870 // it's possible that the add screen was removed because it was
871 // empty and a re-bind occurred
872 mWorkspace.addExtraEmptyScreen();
873 return mWorkspace.commitExtraEmptyScreen();
874 } else {
875 return screenId;
876 }
877 }
878
Sunny Goyal2100c782016-08-22 16:00:03 -0700879 @Thunk void completeTwoStageWidgetDrop(
880 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
881 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800882 Runnable onCompleteRunnable = null;
883 int animationType = 0;
884
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700885 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800886 if (resultCode == RESULT_OK) {
887 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
888 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal2100c782016-08-22 16:00:03 -0700889 requestArgs.getWidgetProvider());
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700890 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800891 onCompleteRunnable = new Runnable() {
892 @Override
893 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700894 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700895 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
896 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800897 }
898 };
899 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800900 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800901 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800902 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700903 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700904 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700905 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
906 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700907 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700908 // The animated view may be null in the case of a rotation during widget configuration
909 onCompleteRunnable.run();
910 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 }
912
913 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700914 protected void onStop() {
915 super.onStop();
916 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700917
918 if (mLauncherCallbacks != null) {
919 mLauncherCallbacks.onStop();
920 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700921
922 if (Utilities.isNycMR1OrAbove()) {
923 mAppWidgetHost.stopListening();
924 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700925 }
926
927 @Override
928 protected void onStart() {
929 super.onStart();
930 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700931
932 if (mLauncherCallbacks != null) {
933 mLauncherCallbacks.onStart();
934 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700935
936 if (Utilities.isNycMR1OrAbove()) {
937 mAppWidgetHost.startListening();
938 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700939 }
940
941 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200943 long startTime = 0;
944 if (DEBUG_RESUME_TIME) {
945 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400946 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200947 }
Adam Cohen9211d422014-10-07 18:14:53 -0700948
949 if (mLauncherCallbacks != null) {
950 mLauncherCallbacks.preOnResume();
951 }
952
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700954 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700955
Winson Chung4a2afa32012-07-19 14:53:05 -0700956 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700957 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700958 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800959 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700960 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700961 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700962 // view after launching an app, as they may be depending on the UI to be static to
963 // switch to another app, otherwise, if it was
Hyunyoung Songdd60ce42016-07-27 17:08:38 -0700964 showAppsView(false /* animated */, !launchedFromApp /* updatePredictedApps */,
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700965 mAppsView.shouldRestoreImeState() /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800966 } else if (mOnResumeState == State.WIDGETS) {
967 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700968 }
969 mOnResumeState = State.NONE;
970
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800971 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700972 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700973 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700974 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700975 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700977 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200978 // We might have postponed some bind calls until onResume (see waitUntilResume) --
979 // execute them here
980 long startTimeCallbacks = 0;
981 if (DEBUG_RESUME_TIME) {
982 startTimeCallbacks = System.currentTimeMillis();
983 }
984
Michael Jurka1e2f4652013-07-08 18:03:46 -0700985 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
986 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200987 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700988 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200989 if (DEBUG_RESUME_TIME) {
990 Log.d(TAG, "Time spent processing callbacks in onResume: " +
991 (System.currentTimeMillis() - startTimeCallbacks));
992 }
Michael Jurka447bf842013-05-15 14:52:15 +0200993 }
Michael Jurka54554252013-08-01 12:52:23 +0200994 if (mOnResumeCallbacks.size() > 0) {
995 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
996 mOnResumeCallbacks.get(i).run();
997 }
998 mOnResumeCallbacks.clear();
999 }
Winson Chunge4e50662012-01-23 14:45:13 -08001000
1001 // Reset the pressed state of icons that were locked in the press state while activities
1002 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001003 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001004 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001005 mWaitingForResume.setStayPressed(false);
1006 }
Winson Chung82963d52013-10-09 11:20:57 -07001007
Adam Cohen06dff352012-06-01 17:17:08 -07001008 // It is possible that widgets can receive updates while launcher is not in the foreground.
1009 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1010 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1011 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001012 if (!isWorkspaceLoading()) {
1013 getWorkspace().reinflateWidgetsIfNecessary();
1014 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001015
Michael Jurka447bf842013-05-15 14:52:15 +02001016 if (DEBUG_RESUME_TIME) {
1017 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1018 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001019
Adam Cohen4b66bf32015-09-18 12:15:19 -07001020 // We want to suppress callbacks about CustomContent being shown if we have just received
1021 // onNewIntent while the user was present within launcher. In that case, we post a call
1022 // to move the user to the main screen (which will occur after onResume). We don't want to
1023 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1024 // suppress here.
1025 if (mWorkspace.getCustomContentCallbacks() != null
1026 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001027 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001028 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001029 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1030 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001031 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001032 }
1033 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001034 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001035 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001036 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001037
Sunny Goyal756adbc2015-04-16 15:20:51 -07001038 if (!isWorkspaceLoading()) {
1039 // Process any items that were added while Launcher was away.
1040 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001041
1042 // Refresh shortcuts if the permission changed.
1043 mModel.refreshShortcutsIfRequired();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001044 }
Adam Cohen9211d422014-10-07 18:14:53 -07001045
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001046 if (shouldShowDiscoveryBounce()) {
1047 mAllAppsController.showDiscoveryBounce();
1048 }
1049 mIsResumeFromActionScreenOff = false;
Adam Cohen9211d422014-10-07 18:14:53 -07001050 if (mLauncherCallbacks != null) {
1051 mLauncherCallbacks.onResume();
1052 }
Adam Cohen06dff352012-06-01 17:17:08 -07001053 }
1054
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001056 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001057 // Ensure that items added to Launcher are queued until Launcher returns
1058 InstallShortcutReceiver.enableInstallQueue();
1059
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001060 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001061 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001062 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001063 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001064
1065 // We call onHide() aggressively. The custom content callbacks should be able to
1066 // debounce excess onHide calls.
1067 if (mWorkspace.getCustomContentCallbacks() != null) {
1068 mWorkspace.getCustomContentCallbacks().onHide();
1069 }
Romain Guycbb89e42009-06-08 15:52:54 -07001070
Adam Cohen9211d422014-10-07 18:14:53 -07001071 if (mLauncherCallbacks != null) {
1072 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001073 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001074 }
1075
1076 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001077 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1078 // by a onResume or by scrolling otherwise.
1079 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001080
1081 // Custom content is completely hidden
1082 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001083
1084 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1085 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001086
1087 // Indicates whether the user is allowed to scroll away from the custom content.
1088 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001089 }
1090
Adam Cohenc2d6e892014-10-16 09:49:24 -07001091 public interface LauncherOverlay {
1092
1093 /**
1094 * Touch interaction leading to overscroll has begun
1095 */
1096 public void onScrollInteractionBegin();
1097
1098 /**
1099 * Touch interaction related to overscroll has ended
1100 */
1101 public void onScrollInteractionEnd();
1102
1103 /**
1104 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1105 * screen (or in the case of RTL, the rightmost screen).
1106 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001107 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001108
1109 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001110 * Called when the launcher is ready to use the overlay
1111 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001112 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001113 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001114 }
1115
Jun Mukai8af0cd82015-05-12 12:32:12 -07001116 public interface LauncherSearchCallbacks {
1117 /**
1118 * Called when the search overlay is shown.
1119 */
1120 public void onSearchOverlayOpened();
1121
1122 /**
1123 * Called when the search overlay is dismissed.
1124 */
1125 public void onSearchOverlayClosed();
1126 }
1127
Adam Cohenc2d6e892014-10-16 09:49:24 -07001128 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001129 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001130 }
1131
1132 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1133
Sunny Goyalc86df472016-02-25 09:19:38 -08001134 public void onScrollChanged(float progress) {
1135 if (mWorkspace != null) {
1136 mWorkspace.onOverlayScrollChanged(progress);
1137 }
1138 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001139 }
1140
Jorim Jaggid017f882014-01-14 17:08:48 -08001141 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001142 if (mLauncherCallbacks != null) {
1143 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001144 } else {
1145 // On devices with a locked orientation, we will at least have the allow rotation
1146 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001147 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001148 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001149 }
1150
Adam Cohen9211d422014-10-07 18:14:53 -07001151 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001152 CustomContentCallbacks callbacks, String description) {
1153 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001154 }
1155
Adam Cohenedb40762013-07-18 16:45:45 -07001156 // The custom content needs to offset its content to account for the QSB
1157 public int getTopOffsetForCustomContent() {
1158 return mWorkspace.getPaddingTop();
1159 }
1160
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001161 @Override
1162 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001163 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001164 if (mModel.isCurrentCallbacks(this)) {
1165 mModel.stopLoader();
1166 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001167 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1168
Romain Guy13c2e7b2010-03-10 19:45:00 -08001169 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001170 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001171
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001172 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001173 @Override
1174 public void onWindowFocusChanged(boolean hasFocus) {
1175 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001176 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001177
1178 if (mLauncherCallbacks != null) {
1179 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1180 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001181 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001182
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183 private boolean acceptFilter() {
1184 final InputMethodManager inputManager = (InputMethodManager)
1185 getSystemService(Context.INPUT_METHOD_SERVICE);
1186 return !inputManager.isFullscreenMode();
1187 }
1188
1189 @Override
1190 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001191 final int uniChar = event.getUnicodeChar();
1192 final boolean handled = super.onKeyDown(keyCode, event);
1193 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1194 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1196 keyCode, event);
1197 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001198 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001199 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001200 // showSearchDialog()
1201 // If there are multiple keystrokes before the search dialog takes focus,
1202 // onSearchRequested() will be called for every keystroke,
1203 // but it is idempotent, so it's fine.
1204 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 }
1206 }
1207
Joe Onorato8a9625e2010-01-28 15:55:35 -08001208 // Eat the long press event so the keyboard doesn't come up.
1209 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1210 return true;
1211 }
1212
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213 return handled;
1214 }
1215
Winson46163472015-09-22 17:31:56 -07001216 @Override
1217 public boolean onKeyUp(int keyCode, KeyEvent event) {
1218 if (keyCode == KeyEvent.KEYCODE_MENU) {
1219 // Ignore the menu key if we are currently dragging or are on the custom content screen
1220 if (!isOnCustomContent() && !mDragController.isDragging()) {
1221 // Close any open folders
1222 closeFolder();
1223
Tony Wickham49c8d292016-07-01 11:44:34 -07001224 // Close any shortcuts containers
1225 closeShortcutsContainer();
1226
Winson46163472015-09-22 17:31:56 -07001227 // Stop resizing any widgets
1228 mWorkspace.exitWidgetResizeMode();
1229
1230 // Show the overview mode if we are on the workspace
1231 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1232 !mWorkspace.isSwitchingState()) {
1233 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001234 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001235 }
1236 }
1237 return true;
1238 }
1239 return super.onKeyUp(keyCode, event);
1240 }
1241
Karl Rosaen138a0412009-04-23 19:00:21 -07001242 private String getTypedText() {
1243 return mDefaultKeySsb.toString();
1244 }
1245
Sunny Goyal6f28e712016-09-13 14:19:29 -07001246 @Override
1247 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001248 mDefaultKeySsb.clear();
1249 mDefaultKeySsb.clearSpans();
1250 Selection.setSelection(mDefaultKeySsb, 0);
1251 }
1252
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 /**
1254 * Restores the previous state, if it exists.
1255 *
1256 * @param savedState The previous state.
1257 */
1258 private void restoreState(Bundle savedState) {
1259 if (savedState == null) {
1260 return;
1261 }
1262
Sunny Goyalfe770c92016-09-27 14:38:58 -07001263 int stateOrdinal = savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal());
1264 State[] stateValues = State.values();
1265 State state = (stateOrdinal >= 0 && stateOrdinal < stateValues.length)
1266 ? stateValues[stateOrdinal] : State.WORKSPACE;
Winson Chungb745afb2015-03-02 11:51:23 -08001267 if (state == State.APPS || state == State.WIDGETS) {
1268 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001269 }
1270
Sunny Goyal2100c782016-08-22 16:00:03 -07001271 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1272 if (requestArgs != null) {
1273 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001275
1276 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 }
1278
1279 /**
1280 * Finds all the views we need and configure them properly.
1281 */
1282 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001283 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001284 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001285 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001286 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyal6178f132016-07-11 17:30:03 -07001287 mQsbContainer = mDragLayer.findViewById(mDeviceProfile.isVerticalBarLayout()
1288 ? R.id.workspace_blocked_row : R.id.qsb_container);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001289 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001290
Sunny Goyal784f9c32016-03-23 16:56:54 -07001291 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1292 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1293 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294
Winson Chung4c98d922011-05-31 16:50:48 -07001295 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001296 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001297
Winson Chung3d503fb2011-07-13 17:25:49 -07001298 // Setup the hotseat
1299 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1300 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001301 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001302 }
1303
Winsone9f27272015-10-13 10:47:51 -07001304 // Setup the overview panel
1305 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001306
Winson Chung4c98d922011-05-31 16:50:48 -07001307 // Setup the workspace
1308 mWorkspace.setHapticFeedbackEnabled(false);
1309 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001310 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001311 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1312 // default state, otherwise we will update to the wrong offsets in RTL
1313 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001314 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001315 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001316
Tony Wickham34d2c912015-09-11 09:27:58 -07001317 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001318 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001319
Winson Chungef7f8742015-06-04 17:18:17 -07001320 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001321 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001322 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001323 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1324 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1325 } else {
1326 mAppsView.setSearchBarController(new DefaultAppSearchController());
1327 }
Craig Mautner360310b2012-10-26 15:13:08 -07001328
Winson Chung3d503fb2011-07-13 17:25:49 -07001329 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001330 mDragController.setDragScoller(mWorkspace);
1331 mDragController.setScrollView(mDragLayer);
1332 mDragController.setMoveTarget(mWorkspace);
1333 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001334 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001335
Peter Schiller310a9882016-06-30 13:52:36 -07001336 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1337 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1338 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001339
Sunny Goyal322d5562015-06-25 19:35:49 -07001340 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1341 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001342 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 }
1344
Winsone9f27272015-10-13 10:47:51 -07001345 private void setupOverviewPanel() {
1346 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1347
1348 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1349 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1350 @Override
1351 public boolean onLongClick(View v) {
1352 return v.performClick();
1353 }
1354 };
1355
1356 // Bind wallpaper button actions
1357 View wallpaperButton = findViewById(R.id.wallpaper_button);
1358 wallpaperButton.setOnClickListener(new OnClickListener() {
1359 @Override
1360 public void onClick(View view) {
1361 if (!mWorkspace.isSwitchingState()) {
1362 onClickWallpaperPicker(view);
1363 }
1364 }
1365 });
1366 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1367 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1368
1369 // Bind widget button actions
1370 mWidgetsButton = findViewById(R.id.widget_button);
1371 mWidgetsButton.setOnClickListener(new OnClickListener() {
1372 @Override
1373 public void onClick(View view) {
1374 if (!mWorkspace.isSwitchingState()) {
1375 onClickAddWidgetButton(view);
1376 }
1377 }
1378 });
1379 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1380 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1381
1382 // Bind settings actions
1383 View settingsButton = findViewById(R.id.settings_button);
1384 boolean hasSettings = hasSettings();
1385 if (hasSettings) {
1386 settingsButton.setOnClickListener(new OnClickListener() {
1387 @Override
1388 public void onClick(View view) {
1389 if (!mWorkspace.isSwitchingState()) {
1390 onClickSettingsButton(view);
1391 }
1392 }
1393 });
1394 settingsButton.setOnLongClickListener(performClickOnLongClick);
1395 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1396 } else {
1397 settingsButton.setVisibility(View.GONE);
1398 }
1399
1400 mOverviewPanel.setAlpha(0f);
1401 }
1402
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001404 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001405 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001406 */
1407 public void setAllAppsButton(View allAppsButton) {
1408 mAllAppsButton = allAppsButton;
1409 }
1410
Sunny Goyalbb011da2016-06-15 15:42:29 -07001411 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001412 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001413 }
1414
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001415 public View getWidgetsButton() {
1416 return mWidgetsButton;
1417 }
1418
Anjali Koppal5ad44842014-03-10 20:34:39 -07001419 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001420 * Creates a view representing a shortcut.
1421 *
1422 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001424 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001425 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 }
1427
1428 /**
1429 * Creates a view representing a shortcut inflated from the specified resource.
1430 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001431 * @param parent The group the shortcut belongs to.
1432 * @param info The data structure describing the shortcut.
1433 *
1434 * @return A View inflated from layoutResId.
1435 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001436 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001437 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001438 parent, false);
1439 favorite.applyFromShortcutInfo(info, mIconCache);
1440 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001442 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 return favorite;
1444 }
1445
1446 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 * Add a shortcut to the workspace.
1448 *
1449 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001451 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001452 int cellY, PendingRequestArgs args) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001453 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001454 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001455
Sunny Goyal5c97f512015-05-19 16:03:28 -07001456 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001457 if (info == null || args.getRequestCode() != REQUEST_CREATE_SHORTCUT ||
1458 args.getPendingIntent().getComponent() == null) {
1459 return;
1460 }
1461 if (!PackageManagerHelper.hasPermissionForActivity(
1462 this, info.intent, args.getPendingIntent().getComponent().getPackageName())) {
1463 // The app is trying to add a shortcut without sufficient permissions
1464 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
Adam Cohend9198822011-11-22 16:42:47 -08001465 return;
1466 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001467 final View view = createShortcut(info);
1468
Sunny Goyal5c97f512015-05-19 16:03:28 -07001469 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001470 // First we check if we already know the exact location where we want to add this item.
1471 if (cellX >= 0 && cellY >= 0) {
1472 cellXY[0] = cellX;
1473 cellXY[1] = cellY;
1474 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001475
1476 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001477 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001478 true, null,null)) {
1479 return;
1480 }
1481 DragObject dragObject = new DragObject();
1482 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001483 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1484 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001485 return;
1486 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001487 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001488 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001489 }
1490
1491 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001492 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001493 return;
1494 }
1495
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001496 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497
Sunny Goyal2100c782016-08-22 16:00:03 -07001498 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
1499 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 }
1501
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001503 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001505 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001507 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001508 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1509
Adam Cohened66b2b2012-01-23 17:28:51 -08001510 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001511 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001512 }
Romain Guycbb89e42009-06-08 15:52:54 -07001513
Adam Cohen59400422014-03-05 18:07:04 -08001514 if (appWidgetInfo.isCustomWidget) {
1515 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001516 }
1517
Adam Cohen59400422014-03-05 18:07:04 -08001518 LauncherAppWidgetInfo launcherInfo;
1519 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001520 launcherInfo.spanX = itemInfo.spanX;
1521 launcherInfo.spanY = itemInfo.spanY;
1522 launcherInfo.minSpanX = itemInfo.minSpanX;
1523 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001524 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001525
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001527 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528
Sunny Goyal2100c782016-08-22 16:00:03 -07001529 if (hostView == null) {
1530 // Perform actual inflation because we're live
1531 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001533 hostView.setVisibility(View.VISIBLE);
1534 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 }
Romain Guycbb89e42009-06-08 15:52:54 -07001536
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001537 private void addAppWidgetToWorkspace(
1538 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001539 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001540 hostView.setTag(item);
1541 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001542
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001543 hostView.setFocusable(true);
1544 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001545
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001546 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001547 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1548
1549 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001550 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001551 }
1552 }
1553
Adam Cohended9f8d2010-11-03 13:25:16 -07001554 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1555 @Override
1556 public void onReceive(Context context, Intent intent) {
1557 final String action = intent.getAction();
1558 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1559 mUserPresent = false;
Sunny Goyal6ad72f02016-09-23 11:01:10 -07001560 mDragLayer.clearResizeFrame();
Winson Chungb745afb2015-03-02 11:51:23 -08001561 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001562
Winson Chungc100e8e2011-08-09 16:02:43 -07001563 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001564 // processing a multi-step drop
Sunny Goyal2100c782016-08-22 16:00:03 -07001565 if (mAppsView != null && mWidgetsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001566 if (!showWorkspace(false)) {
1567 // If we are already on the workspace, then manually reset all apps
1568 mAppsView.reset();
1569 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001570 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001571 mIsResumeFromActionScreenOff = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001572 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1573 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001574 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001575 }
1576 }
1577 };
1578
1579 @Override
1580 public void onAttachedToWindow() {
1581 super.onAttachedToWindow();
1582
1583 // Listen for broadcasts related to user-presence
1584 final IntentFilter filter = new IntentFilter();
1585 filter.addAction(Intent.ACTION_SCREEN_OFF);
1586 filter.addAction(Intent.ACTION_USER_PRESENT);
1587 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001588 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001589 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001590 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001591
1592 if (mLauncherCallbacks != null) {
1593 mLauncherCallbacks.onAttachedToWindow();
1594 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001595 }
1596
1597 @Override
1598 public void onDetachedFromWindow() {
1599 super.onDetachedFromWindow();
1600 mVisible = false;
1601
Adam Cohend113e0c2010-11-11 10:48:05 -08001602 if (mAttached) {
1603 unregisterReceiver(mReceiver);
1604 mAttached = false;
1605 }
Winson Chungb745afb2015-03-02 11:51:23 -08001606 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001607
1608 if (mLauncherCallbacks != null) {
1609 mLauncherCallbacks.onDetachedFromWindow();
1610 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001611 }
1612
1613 public void onWindowVisibilityChanged(int visibility) {
1614 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001615 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001616 // The following code used to be in onResume, but it turns out onResume is called when
1617 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1618 // is a more appropriate event to handle
1619 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001620 if (!mWorkspaceLoading) {
1621 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001622 // We want to let Launcher draw itself at least once before we force it to build
1623 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001624 // apps is nice and speedy.
1625 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001626 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001627 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001628 if (mStarted) return;
1629 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001630 // We delay the layer building a bit in order to give
1631 // other message processing a time to run. In particular
1632 // this avoids a delay in hiding the IME if it was
1633 // currently shown, because doing that may involve
1634 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001635 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001636 final ViewTreeObserver.OnDrawListener listener = this;
1637 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001638 public void run() {
1639 if (mWorkspace != null &&
1640 mWorkspace.getViewTreeObserver() != null) {
1641 mWorkspace.getViewTreeObserver().
1642 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001643 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001644 }
1645 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001646 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001647 }
1648 });
1649 }
1650 clearTypedText();
1651 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001652 }
1653
Adam Cohen091440a2015-03-18 14:16:05 -07001654 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001655 mHandler.removeMessages(ADVANCE_MSG);
1656 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1657 mHandler.sendMessageDelayed(msg, delay);
1658 mAutoAdvanceSentTime = System.currentTimeMillis();
1659 }
1660
Adam Cohen091440a2015-03-18 14:16:05 -07001661 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001662 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1663 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1664 mAutoAdvanceRunning = autoAdvanceRunning;
1665 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001666 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001667 sendAdvanceMessage(delay);
1668 } else {
1669 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001670 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001671 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1672 }
1673 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001674 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001675 }
1676 }
1677 }
1678
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001679 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1680
Adam Cohended9f8d2010-11-03 13:25:16 -07001681 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001682 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001683 if (msg.what == ADVANCE_MSG) {
1684 int i = 0;
1685 for (View key: mWidgetsToAdvance.keySet()) {
1686 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001687 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001688 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001689 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001690 public void run() {
1691 ((Advanceable) v).advance();
1692 }
1693 }, delay);
1694 }
1695 i++;
1696 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001697 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001698 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001699 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001700 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001701 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001702
Winsonc0b52fe2015-09-09 16:38:15 -07001703 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001704 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001705 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1706 if (v instanceof Advanceable) {
1707 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001708 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001709 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001710 }
1711 }
1712
Winsonc0b52fe2015-09-09 16:38:15 -07001713 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001714 if (mWidgetsToAdvance.containsKey(hostView)) {
1715 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001716 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001717 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001718 }
1719
Hyunyoung Song3f471442015-04-08 19:01:34 -07001720 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001721 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1722 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001723 }
1724
Winson Chunga6945242014-01-08 14:04:34 -08001725 public DragLayer getDragLayer() {
1726 return mDragLayer;
1727 }
1728
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001729 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001730 return mAppsView;
1731 }
1732
Hyunyoung Song3f471442015-04-08 19:01:34 -07001733 public WidgetsContainerView getWidgetsView() {
1734 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001735 }
1736
Winson Chunga6945242014-01-08 14:04:34 -08001737 public Workspace getWorkspace() {
1738 return mWorkspace;
1739 }
1740
Sunny Goyal6178f132016-07-11 17:30:03 -07001741 public View getQsbContainer() {
1742 return mQsbContainer;
1743 }
1744
Winson Chunga6945242014-01-08 14:04:34 -08001745 public Hotseat getHotseat() {
1746 return mHotseat;
1747 }
1748
Jorim Jaggid017f882014-01-14 17:08:48 -08001749 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001750 return mOverviewPanel;
1751 }
1752
Sunny Goyal47328fd2016-05-25 18:56:41 -07001753 public DropTargetBar getDropTargetBar() {
1754 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001755 }
1756
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001757 public LauncherAppWidgetHost getAppWidgetHost() {
1758 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001759 }
Romain Guycbb89e42009-06-08 15:52:54 -07001760
Winson Chunga9abd0e2010-10-27 17:18:37 -07001761 public LauncherModel getModel() {
1762 return mModel;
1763 }
1764
Adam Cohen79d90c52016-04-22 13:29:20 -07001765 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001766 return mSharedPrefs;
1767 }
1768
Adam Cohen2e6da152015-05-06 11:42:25 -07001769 public DeviceProfile getDeviceProfile() {
1770 return mDeviceProfile;
1771 }
1772
Bjorn Bringertc459e522013-06-07 19:36:01 +01001773 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001774 getWindow().closeAllPanels();
1775
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001776 // Whatever we were doing is hereby canceled.
Sunny Goyal2100c782016-08-22 16:00:03 -07001777 setWaitingForResult(null);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001778 }
1779
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001780 @Override
1781 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001782 long startTime = 0;
1783 if (DEBUG_RESUME_TIME) {
1784 startTime = System.currentTimeMillis();
1785 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 super.onNewIntent(intent);
1787
1788 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001789 Folder openFolder = mWorkspace.getOpenFolder();
1790 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1791 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1792 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1793 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1794 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001795 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001796 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001797
Adam Cohen6fecd412013-10-02 17:41:50 -07001798 if (mWorkspace == null) {
1799 // Can be cases where mWorkspace is null, this prevents a NPE
1800 return;
1801 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001802 // In all these cases, only animate if we're already on home
1803 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001804
Sunny Goyal935fca12015-10-13 10:19:01 -07001805 closeFolder(alreadyOnHome);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001806 closeShortcutsContainer(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001807 exitSpringLoadedDragMode();
1808
1809 // If we are already on home, then just animate back to the workspace,
1810 // otherwise, just wait until onResume to set the state back to Workspace
1811 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001812 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001813 } else {
1814 mOnResumeState = State.WORKSPACE;
1815 }
1816
1817 final View v = getWindow().peekDecorView();
1818 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001819 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001820 INPUT_METHOD_SERVICE);
1821 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1822 }
1823
Winson Chungb745afb2015-03-02 11:51:23 -08001824 // Reset the apps view
1825 if (!alreadyOnHome && mAppsView != null) {
1826 mAppsView.scrollToTop();
1827 }
1828
Hyunyoung Song3f471442015-04-08 19:01:34 -07001829 // Reset the widgets view
1830 if (!alreadyOnHome && mWidgetsView != null) {
1831 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001832 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001833
Adam Cohen9211d422014-10-07 18:14:53 -07001834 if (mLauncherCallbacks != null) {
1835 mLauncherCallbacks.onHomeIntent();
1836 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837 }
Adam Cohened307df2013-10-02 09:37:31 -07001838
Adam Cohen9211d422014-10-07 18:14:53 -07001839 if (mLauncherCallbacks != null) {
1840 mLauncherCallbacks.onNewIntent(intent);
1841 }
Winson15f8b172015-08-19 11:02:39 -07001842
1843 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1844 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1845 // animation.
1846 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001847 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1848 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001849 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1850 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001851
1852 // We use this flag to suppress noisy callbacks above custom content state
1853 // from onResume.
1854 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001855 mWorkspace.post(new Runnable() {
1856 @Override
1857 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001858 if (mWorkspace != null) {
1859 mWorkspace.moveToDefaultScreen(true);
1860 }
Winson15f8b172015-08-19 11:02:39 -07001861 }
1862 });
1863 }
1864 }
1865
1866 if (DEBUG_RESUME_TIME) {
1867 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1868 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001869 }
1870
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001872 public void onRestoreInstanceState(Bundle state) {
1873 super.onRestoreInstanceState(state);
1874 for (int page: mSynchronouslyBoundPages) {
1875 mWorkspace.restoreInstanceStateForChild(page);
1876 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001877 }
1878
1879 @Override
1880 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001881 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001882 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1883 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001884
Adam Cohen21cd0022013-10-09 18:57:02 -07001885 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001886 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887
Michael Jurka883f55b2010-10-21 15:47:14 -07001888 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001889 // We close any open folder since it will not be re-opened, and we need to make sure
1890 // this state is reflected.
1891 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1892 closeFolder(false);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001893 closeShortcutsContainer(false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001894
Sunny Goyal2100c782016-08-22 16:00:03 -07001895 if (mPendingRequestArgs != null) {
1896 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1897 }
1898 if (mPendingActivityResult != null) {
1899 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 }
1901
Adam Cohen9211d422014-10-07 18:14:53 -07001902 if (mLauncherCallbacks != null) {
1903 mLauncherCallbacks.onSaveInstanceState(outState);
1904 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905 }
1906
1907 @Override
1908 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001910
Winson Chunge7a03942011-08-05 15:05:12 -07001911 // Remove all pending runnables
1912 mHandler.removeMessages(ADVANCE_MSG);
1913 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001914 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001915 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001916
Winson Chungcd2b0142011-06-08 16:02:26 -07001917 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001918 // It's possible to receive onDestroy after a new Launcher activity has
1919 // been created. In this case, don't interfere with the new Launcher.
1920 if (mModel.isCurrentCallbacks(this)) {
1921 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001922 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001923 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001924
Sunny Goyal745bad92016-05-02 10:54:12 -07001925 if (mRotationPrefChangeHandler != null) {
1926 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1927 }
1928
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001930 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001932 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001934 mAppWidgetHost = null;
1935
1936 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937
1938 TextKeyListener.getInstance().release();
1939
Tony Wickhame2217252016-03-22 16:34:23 -07001940 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1941 .removeAccessibilityStateChangeListener(this);
1942
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001943 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001944
Michael Jurka2ecf9952012-06-18 12:52:28 -07001945 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001946
1947 if (mLauncherCallbacks != null) {
1948 mLauncherCallbacks.onDestroy();
1949 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 }
1951
Sunny Goyalae502842016-06-17 08:43:56 -07001952 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1953 return mAccessibilityDelegate;
1954 }
1955
Adam Cohena9cf38f2011-05-02 15:36:58 -07001956 public DragController getDragController() {
1957 return mDragController;
1958 }
1959
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001961 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001962 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001963 }
1964
1965 @Override
1966 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1967 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001968 try {
1969 super.startIntentSenderForResult(intent, requestCode,
1970 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1971 } catch (IntentSender.SendIntentException e) {
1972 throw new ActivityNotFoundException();
1973 }
1974 }
1975
Winson Chung70d72102011-08-12 11:24:35 -07001976 /**
1977 * Indicates that we want global search for this activity by setting the globalSearch
1978 * argument for {@link #startSearch} to true.
1979 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001981 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001983
Karl Rosaen138a0412009-04-23 19:00:21 -07001984 if (initialQuery == null) {
1985 // Use any text typed in the launcher as the initial query
1986 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001987 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 if (appSearchData == null) {
1989 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001990 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001992
Sunny Goyal6f28e712016-09-13 14:19:29 -07001993 if (mLauncherCallbacks == null ||
1994 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
1995 // Starting search from the callbacks failed. Start the default global search.
1996 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01001997 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001998
1999 // We need to show the workspace after starting the search
2000 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002001 }
2002
Ian Parkinson047036e2014-09-01 15:40:53 +01002003 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07002004 * Starts the global search activity. This code is a copied from SearchManager
2005 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07002006 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002007 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002008 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002009 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2010 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2011 if (globalSearchActivity == null) {
2012 Log.w(TAG, "No global search activity found.");
2013 return;
2014 }
2015 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2016 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2017 intent.setComponent(globalSearchActivity);
2018 // Make sure that we have a Bundle to put source in
2019 if (appSearchData == null) {
2020 appSearchData = new Bundle();
2021 } else {
2022 appSearchData = new Bundle(appSearchData);
2023 }
Adam Cohen9211d422014-10-07 18:14:53 -07002024 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002025 if (!appSearchData.containsKey("source")) {
2026 appSearchData.putString("source", getPackageName());
2027 }
2028 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2029 if (!TextUtils.isEmpty(initialQuery)) {
2030 intent.putExtra(SearchManager.QUERY, initialQuery);
2031 }
2032 if (selectInitialQuery) {
2033 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2034 }
2035 intent.setSourceBounds(sourceBounds);
2036 try {
2037 startActivity(intent);
2038 } catch (ActivityNotFoundException ex) {
2039 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2040 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 }
2042
Yura4f93ec62014-02-04 14:15:21 +00002043 public boolean isOnCustomContent() {
2044 return mWorkspace.isOnOrMovingToCustomContent();
2045 }
2046
Winson Chung70d72102011-08-12 11:24:35 -07002047 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002048 public boolean onPrepareOptionsMenu(Menu menu) {
2049 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002050 if (mLauncherCallbacks != null) {
2051 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2052 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002053 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002054 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002056 @Override
2057 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002058 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002059 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002060 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002061 }
2062
Joe Onorato9c1289c2009-08-17 11:03:03 -04002063 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07002064 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002065 }
2066
Adam Cohen517a7f52014-03-01 12:12:59 -08002067 public boolean isWorkspaceLoading() {
2068 return mWorkspaceLoading;
2069 }
2070
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002071 private void setWorkspaceLoading(boolean value) {
2072 boolean isLocked = isWorkspaceLocked();
2073 mWorkspaceLoading = value;
2074 if (isLocked != isWorkspaceLocked()) {
2075 onWorkspaceLockedChanged();
2076 }
2077 }
2078
Sunny Goyal2100c782016-08-22 16:00:03 -07002079 private void setWaitingForResult(PendingRequestArgs args) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002080 boolean isLocked = isWorkspaceLocked();
Sunny Goyal2100c782016-08-22 16:00:03 -07002081 mPendingRequestArgs = args;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002082 if (isLocked != isWorkspaceLocked()) {
2083 onWorkspaceLockedChanged();
2084 }
2085 }
2086
Adam Cohen9211d422014-10-07 18:14:53 -07002087 protected void onWorkspaceLockedChanged() {
2088 if (mLauncherCallbacks != null) {
2089 mLauncherCallbacks.onWorkspaceLockedChanged();
2090 }
2091 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002092
Sunny Goyal2100c782016-08-22 16:00:03 -07002093 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
2094 LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002095 if (LOGD) {
2096 Log.d(TAG, "Adding widget from drop");
2097 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002098 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2099 }
2100
Sunny Goyal2100c782016-08-22 16:00:03 -07002101 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
2102 AppWidgetHostView boundWidget, LauncherAppWidgetProviderInfo appWidgetInfo,
Adam Cohen59400422014-03-05 18:07:04 -08002103 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002104 if (appWidgetInfo.configure != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002105 setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, appWidgetInfo, info));
Michael Jurkaaf442092010-06-10 17:01:57 -07002106
Bjorn Bringert7984c942009-12-09 15:38:25 +00002107 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002108 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2109 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002111 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002112 Runnable onComplete = new Runnable() {
2113 @Override
2114 public void run() {
2115 // Exit spring loaded mode if necessary after adding the widget
2116 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2117 null);
2118 }
2119 };
Sunny Goyal2100c782016-08-22 16:00:03 -07002120 completeAddAppWidget(appWidgetId, info, boundWidget, appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002121 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002122 }
2123 }
Romain Guycbb89e42009-06-08 15:52:54 -07002124
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002125 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002126 // Close any folders that may be open.
2127 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002128 mWorkspace.moveToCustomContentScreen(animate);
2129 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002130
2131 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2132 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002133 info.container = container;
2134 info.screenId = screenId;
2135 if (cell != null) {
2136 info.cellX = cell[0];
2137 info.cellY = cell[1];
2138 }
2139 info.spanX = spanX;
2140 info.spanY = spanY;
2141
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002142 switch (info.itemType) {
2143 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2144 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07002145 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002146 break;
2147 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal2100c782016-08-22 16:00:03 -07002148 processShortcutFromDrop(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002149 break;
2150 default:
2151 throw new IllegalStateException("Unknown item type: " + info.itemType);
2152 }
2153 }
2154
Adam Cohenfbba09b2011-07-18 21:43:05 -07002155 /**
2156 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002157 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002158 private void processShortcutFromDrop(PendingAddItemInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07002159 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
2160 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
2161 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002162 }
2163
Adam Cohenfbba09b2011-07-18 21:43:05 -07002164 /**
2165 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002166 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002167 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002168 AppWidgetHostView hostView = info.boundWidget;
2169 int appWidgetId;
2170 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002171 // In the case where we've prebound the widget, we remove it from the DragLayer
2172 if (LOGD) {
2173 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2174 }
2175 getDragLayer().removeView(hostView);
2176
Adam Cohened66b2b2012-01-23 17:28:51 -08002177 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002178 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002179
2180 // Clear the boundWidget so that it doesn't get destroyed.
2181 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002182 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002183 // In this case, we either need to start an activity to get permission to bind
2184 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002185 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002186 Bundle options = info.bindOptions;
2187
Sunny Goyalffe83f12014-08-14 17:39:34 -07002188 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2189 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002190 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002191 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002192 } else {
Sunny Goyal2100c782016-08-22 16:00:03 -07002193 setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, info.info, info));
Michael Jurka8b805b12012-04-18 14:23:14 -07002194 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2195 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2196 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyal2100c782016-08-22 16:00:03 -07002197 mAppWidgetManager.getUser(info.info)
Sunny Goyalffe83f12014-08-14 17:39:34 -07002198 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002199 // TODO: we need to make sure that this accounts for the options bundle.
2200 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002201 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2202 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002203 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002204 }
2205
Adam Cohendcd297f2013-06-18 13:13:40 -07002206 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002207 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002208 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002209 folderInfo.title = getText(R.string.folder_name);
2210
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002211 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002212 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2213 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214
2215 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002216 FolderIcon newFolder =
2217 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002218 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002219 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002220 // Force measure the new folder icon
2221 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2222 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002223 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002224 }
Romain Guycbb89e42009-06-08 15:52:54 -07002225
Winsonc0b52fe2015-09-09 16:38:15 -07002226 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002227 * Unbinds the view for the specified item, and removes the item and all its children.
2228 *
2229 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002230 * @param itemInfo the {@link ItemInfo} for this view.
2231 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002232 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002233 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002234 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002235 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002236 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2237 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002238 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002239 } else {
2240 mWorkspace.removeWorkspaceItem(v);
2241 }
Winsonc0b52fe2015-09-09 16:38:15 -07002242 if (deleteFromDb) {
2243 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2244 }
2245 } else if (itemInfo instanceof FolderInfo) {
2246 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002247 if (v instanceof FolderIcon) {
2248 ((FolderIcon) v).removeListeners();
2249 }
Winsonc0b52fe2015-09-09 16:38:15 -07002250 mWorkspace.removeWorkspaceItem(v);
2251 if (deleteFromDb) {
2252 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2253 }
2254 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2255 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002256 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002257 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002258 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002259 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002260 }
2261 } else {
2262 return false;
2263 }
2264 return true;
2265 }
2266
2267 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002268 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002269 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002270 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002271 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002272 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002273 // Deleting an app widget ID is a void call but writes to disk before returning
2274 // to the caller...
2275 new AsyncTask<Void, Void, Void>() {
2276 public Void doInBackground(Void ... args) {
2277 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2278 return null;
2279 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002280 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002281 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002282 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002283 }
2284
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002285 @Override
2286 public boolean dispatchKeyEvent(KeyEvent event) {
2287 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2288 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002289 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002290 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002291 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002292 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002293 dumpState();
2294 return true;
2295 }
2296 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002297 }
2298 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2299 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002300 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002301 return true;
2302 }
2303 }
2304
2305 return super.dispatchKeyEvent(event);
2306 }
2307
Joe Onorato88ec0992009-11-19 13:16:06 -08002308 @Override
2309 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002310 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2311 return;
2312 }
2313
Sunny Goyal45478022015-06-08 16:52:41 -07002314 if (mDragController.isDragging()) {
2315 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002316 return;
2317 }
2318
Tony Wickham49c8d292016-07-01 11:44:34 -07002319 if (getOpenShortcutsContainer() != null) {
2320 closeShortcutsContainer();
2321 } else if (isAppsViewVisible()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002322 showWorkspace(true);
2323 } else if (isWidgetsViewVisible()) {
2324 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002325 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002326 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002327 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002328 Folder openFolder = mWorkspace.getOpenFolder();
2329 if (openFolder.isEditingName()) {
2330 openFolder.dismissEditingName();
2331 } else {
2332 closeFolder();
2333 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002334 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002335 mWorkspace.exitWidgetResizeMode();
2336
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002337 // Back button is a no-op here, but give at least some feedback for the button press
2338 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002339 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002340 }
2341
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002342 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002343 * Launches the intent referred by the clicked shortcut.
2344 *
2345 * @param v The view representing the clicked shortcut.
2346 */
2347 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002348 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2349 // view has detached (it's possible for this to happen if the view is removed mid touch).
2350 if (v.getWindowToken() == null) {
2351 return;
2352 }
2353
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002354 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002355 return;
2356 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002357
Adam Cohen1697b792013-09-17 19:08:21 -07002358 if (v instanceof Workspace) {
2359 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002360 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002361 }
2362 return;
2363 }
2364
2365 if (v instanceof CellLayout) {
2366 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002367 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2368 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002369 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002370 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002371 }
2372
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002373 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002374 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002375 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002376 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002377 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002378 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002379 }
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002380 } else if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2381 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002382 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002383 } else if (tag instanceof AppInfo) {
2384 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002385 } else if (tag instanceof LauncherAppWidgetInfo) {
2386 if (v instanceof PendingAppWidgetHostView) {
2387 onClickPendingWidget((PendingAppWidgetHostView) v);
2388 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002389 }
2390 }
2391
Sunny Goyal70660032015-05-14 00:07:08 -07002392 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002393 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002394 return false;
2395 }
2396
Michael Jurkaaf442092010-06-10 17:01:57 -07002397 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002398 * Event handler for the app widget view which has not fully restored.
2399 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002400 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002401 if (mIsSafeModeEnabled) {
2402 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2403 return;
2404 }
2405
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002406 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002407 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002408 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2409 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2410 // This should not happen, as we make sure that an Id is allocated during bind.
2411 return;
2412 }
2413 LauncherAppWidgetProviderInfo appWidgetInfo =
2414 mAppWidgetManager.findProvider(info.providerName, info.user);
2415 if (appWidgetInfo != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002416 setWaitingForResult(PendingRequestArgs
2417 .forWidgetInfo(info.appWidgetId, appWidgetInfo, info));
Sunny Goyalff572272014-07-23 13:58:07 -07002418
Sunny Goyald478c832016-04-01 12:04:16 -07002419 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
Sunny Goyal2100c782016-08-22 16:00:03 -07002420 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, info.appWidgetId);
Sunny Goyald478c832016-04-01 12:04:16 -07002421 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07002422 mAppWidgetManager.getUser(appWidgetInfo)
Sunny Goyald478c832016-04-01 12:04:16 -07002423 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2424 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2425 }
2426 } else {
2427 LauncherAppWidgetProviderInfo appWidgetInfo =
2428 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2429 if (appWidgetInfo != null) {
2430 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2431 }
Sunny Goyalff572272014-07-23 13:58:07 -07002432 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002433 } else if (info.installProgress < 0) {
2434 // The install has not been queued
2435 final String packageName = info.providerName.getPackageName();
2436 showBrokenAppInstallDialog(packageName,
2437 new DialogInterface.OnClickListener() {
2438 public void onClick(DialogInterface dialog, int id) {
2439 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2440 }
2441 });
2442 } else {
2443 // Download has started.
2444 final String packageName = info.providerName.getPackageName();
2445 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002446 }
2447 }
2448
Sunny Goyald478c832016-04-01 12:04:16 -07002449 private void startRestoredWidgetReconfigActivity(
2450 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002451 setWaitingForResult(PendingRequestArgs.forWidgetInfo(info.appWidgetId, provider, info));
Sunny Goyald478c832016-04-01 12:04:16 -07002452 mAppWidgetManager.startConfigActivity(provider,
2453 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2454 }
2455
Sunny Goyalff572272014-07-23 13:58:07 -07002456 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002457 * Event handler for the "grid" button that appears on the home screen, which
2458 * enters all apps mode.
2459 *
2460 * @param v The view that was clicked.
2461 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002462 protected void onClickAllAppsButton(View v) {
2463 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002464 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002465 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.TAP,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002466 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002467 showAppsView(true /* animated */, true /* updatePredictedApps */,
2468 false /* focusSearchBar */);
Winson Chung76648c52015-07-10 14:33:23 -07002469 }
2470 }
2471
2472 protected void onLongClickAllAppsButton(View v) {
2473 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2474 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002475 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.LONGPRESS,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002476 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002477 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07002478 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002479 }
2480 }
2481
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002482 private void showBrokenAppInstallDialog(final String packageName,
2483 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002484 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002485 .setTitle(R.string.abandoned_promises_title)
2486 .setMessage(R.string.abandoned_promise_explanation)
2487 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2488 .setNeutralButton(R.string.abandoned_clean_this,
2489 new DialogInterface.OnClickListener() {
2490 public void onClick(DialogInterface dialog, int id) {
2491 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2492 mWorkspace.removeAbandonedPromise(packageName, user);
2493 }
2494 })
2495 .create().show();
2496 return;
2497 }
2498
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002499 /**
2500 * Event handler for an app shortcut click.
2501 *
2502 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2503 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002504 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002505 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2506 Object tag = v.getTag();
2507 if (!(tag instanceof ShortcutInfo)) {
2508 throw new IllegalArgumentException("Input must be a Shortcut");
2509 }
2510
2511 // Open shortcut
2512 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002513
2514 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002515 if ((shortcut.isDisabled &
2516 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2517 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2518 // If the app is only disabled because of the above flags, launch activity anyway.
2519 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002520 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002521 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2522 // Use a message specific to this shortcut, if it has one.
2523 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2524 return;
2525 }
2526 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002527 int error = R.string.activity_not_available;
2528 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2529 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002530 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2531 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002532 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002533 }
2534 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2535 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002536 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002537 }
2538
Chris Wren40c5ed32014-06-24 18:24:23 -04002539 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002540 if ((v instanceof BubbleTextView)
2541 && shortcut.isPromise()
2542 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002543 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002544 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002545 new DialogInterface.OnClickListener() {
2546 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002547 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002548 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002549 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002550 return;
2551 }
2552
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002553 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002554 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002555 }
2556
Sunny Goyala7ce1662016-05-31 15:01:35 -07002557 private void startAppShortcutOrInfoActivity(View v) {
2558 ItemInfo item = (ItemInfo) v.getTag();
2559 Intent intent = item.getIntent();
2560 if (intent == null) {
2561 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002562 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002563 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002564 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002565
2566 if (success && v instanceof BubbleTextView) {
2567 mWaitingForResume = (BubbleTextView) v;
2568 mWaitingForResume.setStayPressed(true);
2569 }
2570 }
2571
2572 /**
2573 * Event handler for a folder icon click.
2574 *
2575 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2576 */
2577 protected void onClickFolderIcon(View v) {
2578 if (LOGD) Log.d(TAG, "onClickFolder");
2579 if (!(v instanceof FolderIcon)){
2580 throw new IllegalArgumentException("Input must be a FolderIcon");
2581 }
2582
2583 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002584 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002585 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002586 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002587 }
Michael Jurka5130e402011-10-13 04:55:35 -07002588 }
2589
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002590 /**
2591 * Event handler for the (Add) Widgets button that appears after a long press
2592 * on the home screen.
2593 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002594 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002595 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002596 if (mIsSafeModeEnabled) {
2597 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2598 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002599 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002600 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002601 }
2602
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002603 /**
2604 * Event handler for the wallpaper picker button that appears after a long press
2605 * on the home screen.
2606 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002607 public void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002608 if (!Utilities.isWallapaperAllowed(this)) {
2609 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2610 return;
2611 }
2612
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002613 String pickerPackage = getString(R.string.wallpaper_picker_package);
2614 if (TextUtils.isEmpty(pickerPackage)) {
2615 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2616 }
2617
Tony Wickham785f7a52015-08-31 17:28:32 -07002618 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2619 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002620
Sunny Goyal2100c782016-08-22 16:00:03 -07002621 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002622 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002623 .setPackage(pickerPackage)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002624 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
2625 intent.setSourceBounds(getViewBounds(v));
2626 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002627 }
2628
2629 /**
2630 * Event handler for a click on the settings button that appears after a long press
2631 * on the home screen.
2632 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002633 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002634 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002635 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2636 .setPackage(getPackageName());
2637 intent.setSourceBounds(getViewBounds(v));
2638 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002639 }
2640
Adam Cohen61f560d2013-09-30 15:58:20 -07002641 public View.OnTouchListener getHapticFeedbackTouchListener() {
2642 if (mHapticFeedbackTouchListener == null) {
2643 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002644 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002645 @Override
2646 public boolean onTouch(View v, MotionEvent event) {
2647 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2648 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2649 }
2650 return false;
2651 }
2652 };
2653 }
2654 return mHapticFeedbackTouchListener;
2655 }
2656
Tony Wickhame2217252016-03-22 16:34:23 -07002657 @Override
2658 public void onAccessibilityStateChanged(boolean enabled) {
2659 mDragLayer.onAccessibilityStateChanged(enabled);
2660 }
2661
Sunny Goyal06e21a22016-08-11 16:02:02 -07002662 public void onDragStarted() {
Adam Cohen9211d422014-10-07 18:14:53 -07002663 if (isOnCustomContent()) {
2664 // Custom content screen doesn't participate in drag and drop. If on custom
2665 // content screen, move to default.
2666 moveWorkspaceToDefaultScreen();
2667 }
Adam Cohen9211d422014-10-07 18:14:53 -07002668 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002669
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002670 /**
2671 * Called when the user stops interacting with the launcher.
2672 * This implies that the user is now on the homescreen and is not doing housekeeping.
2673 */
Adam Cohen9211d422014-10-07 18:14:53 -07002674 protected void onInteractionEnd() {
2675 if (mLauncherCallbacks != null) {
2676 mLauncherCallbacks.onInteractionEnd();
2677 }
2678 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002679
2680 /**
2681 * Called when the user starts interacting with the launcher.
2682 * The possible interactions are:
2683 * - open all apps
2684 * - reorder an app shortcut, or a widget
2685 * - open the overview mode.
2686 * This is a good time to stop doing things that only make sense
2687 * when the user is on the homescreen and not doing housekeeping.
2688 */
Adam Cohen9211d422014-10-07 18:14:53 -07002689 protected void onInteractionBegin() {
2690 if (mLauncherCallbacks != null) {
2691 mLauncherCallbacks.onInteractionBegin();
2692 }
2693 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002694
Winson Chungcd99cd32015-04-29 11:03:24 -07002695 /** Updates the interaction state. */
2696 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002697 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002698 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002699 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2700 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002701 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002702 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002703 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002704 onInteractionEnd();
2705 }
2706 }
2707
Sunny Goyala7ce1662016-05-31 15:01:35 -07002708 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002709 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002710 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2711 try {
2712 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2713 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2714 // is enabled by default on NYC.
2715 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2716 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002717
2718 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2719 String id = ((ShortcutInfo) info).getDeepShortcutId();
2720 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002721 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2722 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002723 } else {
2724 // Could be launching some bookkeeping activity
2725 startActivity(intent, optsBundle);
2726 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002727 } finally {
2728 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002729 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002730 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002731 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2732 // corresponding permission. Show the appropriate permission prompt if that
2733 // is the case.
2734 if (intent.getComponent() == null
2735 && Intent.ACTION_CALL.equals(intent.getAction())
2736 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2737 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002738
2739 setWaitingForResult(PendingRequestArgs
2740 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002741 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2742 REQUEST_PERMISSION_CALL_PHONE);
2743 } else {
2744 // No idea why this was thrown.
2745 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002746 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002747 }
2748 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002749
Sunny Goyalfe770c92016-09-27 14:38:58 -07002750 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyala7ce1662016-05-31 15:01:35 -07002751 private Bundle getActivityLaunchOptions(View v) {
2752 if (Utilities.ATLEAST_MARSHMALLOW) {
2753 int left = 0, top = 0;
2754 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2755 if (v instanceof TextView) {
2756 // Launch from center of icon, not entire view
2757 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2758 if (icon != null) {
2759 Rect bounds = icon.getBounds();
2760 left = (width - bounds.width()) / 2;
2761 top = v.getPaddingTop();
2762 width = bounds.width();
2763 height = bounds.height();
2764 }
2765 }
2766 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2767 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2768 // On L devices, we use the device default slide-up transition.
2769 // On L MR1 devices, we use a custom version of the slide-up transition which
2770 // doesn't have the delay present in the device default.
2771 return ActivityOptions.makeCustomAnimation(
2772 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2773 }
2774 return null;
2775 }
2776
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002777 private Rect getViewBounds(View v) {
2778 int[] pos = new int[2];
2779 v.getLocationOnScreen(pos);
2780 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2781 }
2782
Sunny Goyala7ce1662016-05-31 15:01:35 -07002783 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002784 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2785 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2786 return false;
2787 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002788 // Only launch using the new animation if the shortcut has not opted out (this is a
2789 // private contract between launcher and may be ignored in the future).
2790 boolean useLaunchAnimation = (v != null) &&
2791 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2792 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2793
2794 UserHandleCompat user = null;
2795 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2796 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2797 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002798 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002799
2800 // Prepare intent
2801 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2802 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002803 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002804 }
2805 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002806 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2807 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07002808 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
2809 && ((ShortcutInfo) item).promisedIntent == null) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002810 // Shortcuts need some special checks due to legacy reasons.
2811 startShortcutIntentSafely(intent, optsBundle, item);
2812 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2813 // Could be launching some bookkeeping activity
2814 startActivity(intent, optsBundle);
2815 } else {
2816 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2817 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2818 }
2819 return true;
2820 } catch (ActivityNotFoundException|SecurityException e) {
2821 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2822 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2823 }
2824 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002825 }
2826
Adam Cohen268c4752012-06-06 17:47:33 -07002827 /**
2828 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2829 * in the DragLayer in the exact absolute location of the original FolderIcon.
2830 */
2831 private void copyFolderIconToImage(FolderIcon fi) {
2832 final int width = fi.getMeasuredWidth();
2833 final int height = fi.getMeasuredHeight();
2834
2835 // Lazy load ImageView, Bitmap and Canvas
2836 if (mFolderIconImageView == null) {
2837 mFolderIconImageView = new ImageView(this);
2838 }
2839 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2840 mFolderIconBitmap.getHeight() != height) {
2841 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2842 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2843 }
2844
2845 DragLayer.LayoutParams lp;
2846 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2847 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2848 } else {
2849 lp = new DragLayer.LayoutParams(width, height);
2850 }
2851
Adam Cohen307fe232012-08-16 17:55:58 -07002852 // The layout from which the folder is being opened may be scaled, adjust the starting
2853 // view size by this scale factor.
2854 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002855 lp.customPosition = true;
2856 lp.x = mRectForFolderAnimation.left;
2857 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002858 lp.width = (int) (scale * width);
2859 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002860
2861 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2862 fi.draw(mFolderIconCanvas);
2863 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002864 if (fi.getFolder() != null) {
2865 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2866 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002867 }
Adam Cohen268c4752012-06-06 17:47:33 -07002868 // Just in case this image view is still in the drag layer from a previous animation,
2869 // we remove it and re-add it.
2870 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2871 mDragLayer.removeView(mFolderIconImageView);
2872 }
2873 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002874 if (fi.getFolder() != null) {
2875 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002876 }
Adam Cohen268c4752012-06-06 17:47:33 -07002877 }
2878
Adam Cohen37b2a492016-04-06 18:36:06 -07002879 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002880 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002881 FolderInfo info = (FolderInfo) fi.getTag();
2882 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2883 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002884 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2885 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002886 }
2887
Adam Cohen268c4752012-06-06 17:47:33 -07002888 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2889 copyFolderIconToImage(fi);
2890 fi.setVisibility(View.INVISIBLE);
2891
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002892 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2893 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002894 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002895 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2896 }
2897 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002898 oa.start();
2899 }
2900
Sunny Goyal935fca12015-10-13 10:19:01 -07002901 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002902 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002903 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002904
Adam Cohen268c4752012-06-06 17:47:33 -07002905 // We remove and re-draw the FolderIcon in-case it has changed
2906 mDragLayer.removeView(mFolderIconImageView);
2907 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08002908
2909 if (cl != null) {
2910 cl.clearFolderLeaveBehind();
2911 }
2912
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002913 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002914 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002915 oa.addListener(new AnimatorListenerAdapter() {
2916 @Override
2917 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002918 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07002919 // Remove the ImageView copy of the FolderIcon and make the original visible.
2920 mDragLayer.removeView(mFolderIconImageView);
2921 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002922 }
2923 }
2924 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002925 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07002926 if (!animate) {
2927 oa.end();
2928 }
Adam Cohen2801caf2011-05-13 20:57:39 -07002929 }
2930
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002931 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002932 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002933 * is animated relative to the specified View. If the View is null, no animation
2934 * is played.
2935 *
Sunny Goyal08442b82015-10-21 17:15:08 -07002936 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002937 */
Adam Cohen37b2a492016-04-06 18:36:06 -07002938 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07002939
Adam Cohenfb91f302012-06-11 15:45:18 -07002940 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07002941 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
2942 if (openFolder != null && openFolder != folder) {
2943 // Close any open folder before opening a folder.
2944 closeFolder();
2945 }
2946
Adam Cohena9cf38f2011-05-02 15:36:58 -07002947 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002948
Adam Cohena9cf38f2011-05-02 15:36:58 -07002949 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002950
Sunny Goyalf4066152015-04-15 09:42:19 -07002951 // While the folder is open, the position of the icon cannot change.
2952 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
2953
Adam Cohen4554ee12011-08-03 16:13:21 -07002954 // Just verify that the folder hasn't already been added to the DragLayer.
2955 // There was a one-off crash where the folder had a parent already.
2956 if (folder.getParent() == null) {
2957 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07002958 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07002959 } else {
2960 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2961 folder.getParent() + ").");
2962 }
Adam Cohen37b2a492016-04-06 18:36:06 -07002963 folder.animateOpen();
2964
2965 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002966
2967 // Notify the accessibility manager that this folder "window" has appeared and occluded
2968 // the workspace items
2969 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2970 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002971 }
2972
2973 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07002974 closeFolder(true);
2975 }
2976
2977 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08002978 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002979 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002980 if (folder.isEditingName()) {
2981 folder.dismissEditingName();
2982 }
Sunny Goyal935fca12015-10-13 10:19:01 -07002983 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07002984 }
2985 }
2986
Sunny Goyal935fca12015-10-13 10:19:01 -07002987 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07002988 animate &= !Utilities.isPowerSaverOn(this);
2989
Adam Cohen4554ee12011-08-03 16:13:21 -07002990 folder.getInfo().opened = false;
2991
2992 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2993 if (parent != null) {
2994 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07002995 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07002996 if (fi != null) {
2997 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
2998 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002999 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003000 if (animate) {
3001 folder.animateClosed();
3002 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003003 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003004 }
Winson Chung83ca4802013-04-12 15:10:52 -07003005
Sunny Goyal935fca12015-10-13 10:19:01 -07003006 // Notify the accessibility manager that this folder "window" has disappeared and no
3007 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003008 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003009 }
3010
Tony Wickham49c8d292016-07-01 11:44:34 -07003011 public void closeShortcutsContainer() {
Sunny Goyala2454ad2016-07-22 10:50:11 -07003012 closeShortcutsContainer(true);
3013 }
3014
3015 public void closeShortcutsContainer(boolean animate) {
Tony Wickham49c8d292016-07-01 11:44:34 -07003016 DeepShortcutsContainer deepShortcutsContainer = getOpenShortcutsContainer();
3017 if (deepShortcutsContainer != null) {
Sunny Goyala2454ad2016-07-22 10:50:11 -07003018 if (animate) {
3019 deepShortcutsContainer.animateClose();
3020 } else {
3021 deepShortcutsContainer.close();
3022 }
Tony Wickham49c8d292016-07-01 11:44:34 -07003023 }
3024 }
3025
Sunny Goyal52851aa2016-09-02 10:41:43 -07003026 public View getTopFloatingView() {
3027 View topView = getOpenShortcutsContainer();
3028 if (topView == null) {
3029 topView = getWorkspace().getOpenFolder();
3030 }
3031 return topView;
3032 }
3033
Tony Wickham49c8d292016-07-01 11:44:34 -07003034 /**
3035 * @return The open shortcuts container, or null if there is none
3036 */
3037 public DeepShortcutsContainer getOpenShortcutsContainer() {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07003038 // Iterate in reverse order. Shortcuts container is added later to the dragLayer,
3039 // and will be one of the last views.
3040 for (int i = mDragLayer.getChildCount() - 1; i >= 0; i--) {
3041 View child = mDragLayer.getChildAt(i);
Sunny Goyala2454ad2016-07-22 10:50:11 -07003042 if (child instanceof DeepShortcutsContainer
3043 && ((DeepShortcutsContainer) child).isOpen()) {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07003044 return (DeepShortcutsContainer) child;
3045 }
3046 }
3047 return null;
Tony Wickham49c8d292016-07-01 11:44:34 -07003048 }
3049
Tony Wickhamdadb3042016-02-24 11:07:00 -08003050 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07003051 public boolean dispatchTouchEvent(MotionEvent ev) {
3052 mLastDispatchTouchEventX = ev.getX();
3053 return super.dispatchTouchEvent(ev);
3054 }
3055
3056 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003057 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003058 if (!isDraggingEnabled()) return false;
3059 if (isWorkspaceLocked()) return false;
3060 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003061
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07003062 if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
3063 (v == mAllAppsButton && mAllAppsButton != null)) {
Winson Chung76648c52015-07-10 14:33:23 -07003064 onLongClickAllAppsButton(v);
3065 return true;
3066 }
3067
Jon Miranda379198e2016-09-23 08:54:40 -07003068 boolean fromEdgeOfScreen = mLastDispatchTouchEventX < mEdgeOfScreenThresholdPx
3069 || mLastDispatchTouchEventX > (mDeviceProfile.widthPx - mEdgeOfScreenThresholdPx);
3070
Adam Cohen1697b792013-09-17 19:08:21 -07003071 if (v instanceof Workspace) {
3072 if (!mWorkspace.isInOverviewMode()) {
Jon Miranda379198e2016-09-23 08:54:40 -07003073 if (!mWorkspace.isTouchActive() && !fromEdgeOfScreen) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003074 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003075 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3076 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3077 return true;
3078 } else {
3079 return false;
3080 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003081 } else {
3082 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003083 }
Adam Cohen1697b792013-09-17 19:08:21 -07003084 }
3085
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003086 CellLayout.CellInfo longClickCellInfo = null;
3087 View itemUnderLongClick = null;
3088 if (v.getTag() instanceof ItemInfo) {
3089 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003090 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003091 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07003092 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003093 }
3094
Winson Chung3d503fb2011-07-13 17:25:49 -07003095 // The hotseat touch handling does not go through Workspace, and we always allow long press
3096 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003097 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003098 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003099 // User long pressed on empty space
Adam Cohend3ecce92013-09-16 14:58:00 -07003100 if (mWorkspace.isInOverviewMode()) {
3101 mWorkspace.startReordering(v);
3102 } else {
Jon Miranda379198e2016-09-23 08:54:40 -07003103 if (fromEdgeOfScreen) {
3104 return false;
3105 }
Winson Chungdc61c4d2015-04-20 18:26:57 -07003106 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003107 }
Jon Miranda379198e2016-09-23 08:54:40 -07003108 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3109 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003110 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003111 final boolean isAllAppsButton =
3112 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3113 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3114 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003115 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003116 // User long pressed on an item
Tony Wickham6e74e892016-09-19 14:06:31 -07003117 DragOptions dragOptions = new DragOptions();
3118 if (itemUnderLongClick instanceof BubbleTextView) {
3119 BubbleTextView icon = (BubbleTextView) itemUnderLongClick;
3120 if (icon.hasDeepShortcuts()) {
3121 DeepShortcutsContainer dsc = DeepShortcutsContainer.showForIcon(icon);
3122 if (dsc != null) {
3123 dragOptions.deferDragCondition = dsc.createDeferDragCondition(null);
3124 }
3125 }
3126 }
3127 mWorkspace.startDrag(longClickCellInfo, dragOptions);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003128 }
3129 }
3130 }
3131 return true;
3132 }
3133
Winson Chung3d503fb2011-07-13 17:25:49 -07003134 boolean isHotseatLayout(View layout) {
3135 return mHotseat != null && layout != null &&
3136 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3137 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003138
Winson Chung3d503fb2011-07-13 17:25:49 -07003139 /**
3140 * Returns the CellLayout of the specified container at the specified screen.
3141 */
Sunny Goyal92820592015-03-02 11:31:35 -08003142 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003143 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3144 if (mHotseat != null) {
3145 return mHotseat.getLayout();
3146 } else {
3147 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003148 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003149 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003150 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003151 }
3152 }
3153
Winson Chungb745afb2015-03-02 11:51:23 -08003154 /**
3155 * For overridden classes.
3156 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003157 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003158 return isAppsViewVisible();
3159 }
3160
3161 public boolean isAppsViewVisible() {
3162 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3163 }
3164
3165 public boolean isWidgetsViewVisible() {
3166 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003167 }
3168
Michael Jurkae326f182011-11-21 14:05:46 -08003169 @Override
3170 public void onTrimMemory(int level) {
3171 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003172 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3173 // The widget preview db can result in holding onto over
3174 // 3MB of memory for caching which isn't necessary.
3175 SQLiteDatabase.releaseMemory();
3176
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003177 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003178 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003179 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003180 if (mLauncherCallbacks != null) {
3181 mLauncherCallbacks.onTrimMemory(level);
3182 }
Michael Jurkae326f182011-11-21 14:05:46 -08003183 }
3184
Winson5c6bdbb2015-09-03 11:36:19 -07003185 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003186 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003187 }
3188
Winson5c6bdbb2015-09-03 11:36:19 -07003189 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003190 boolean changed = mState != State.WORKSPACE ||
3191 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003192 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003193 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003194 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003195 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003196
Michael Jurkab3e22d92011-10-31 15:58:33 -07003197 // Set focus to the AppsCustomize button
3198 if (mAllAppsButton != null) {
3199 mAllAppsButton.requestFocus();
3200 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003201 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003202
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003203 // Change the state *after* we've called all the transition code
3204 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003205
Winson Chung5fb63472011-02-02 17:03:37 -08003206 // Resume the auto-advance of widgets
3207 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003208 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003209
Winson Chung0f785722015-04-08 10:27:49 -07003210 if (changed) {
3211 // Send an accessibility event to announce the context change
3212 getWindow().getDecorView()
3213 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003214 }
Winson5c6bdbb2015-09-03 11:36:19 -07003215 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003216 }
3217
Winsone9f27272015-10-13 10:47:51 -07003218 /**
3219 * Shows the overview button.
3220 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07003221 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003222 showOverviewMode(animated, false);
3223 }
3224
3225 /**
3226 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3227 * onto one of the overview panel buttons.
3228 */
3229 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3230 Runnable postAnimRunnable = null;
3231 if (requestButtonFocus) {
3232 postAnimRunnable = new Runnable() {
3233 @Override
3234 public void run() {
3235 // Hitting the menu button when in touch mode does not trigger touch mode to
3236 // be disabled, so if requested, force focus on one of the overview panel
3237 // buttons.
3238 mOverviewPanel.requestFocusFromTouch();
3239 }
3240 };
3241 }
Adam Cohened307df2013-10-02 09:37:31 -07003242 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003243 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003244 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003245 mState = State.WORKSPACE;
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07003246 // If animated from long press, then don't allow any of the controller in the drag
3247 // layer to intercept any remaining touch.
3248 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04003249 }
3250
Winson Chungb745afb2015-03-02 11:51:23 -08003251 /**
3252 * Shows the apps view.
3253 */
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003254 public void showAppsView(boolean animated, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003255 boolean focusSearchBar) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003256 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07003257 if (updatePredictedApps) {
3258 tryAndUpdatePredictedApps();
3259 }
Winson Chung76648c52015-07-10 14:33:23 -07003260 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003261 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003262
Winson Chungb745afb2015-03-02 11:51:23 -08003263 /**
3264 * Shows the widgets view.
3265 */
3266 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003267 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003268 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003269 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003270 }
Winson Chung76648c52015-07-10 14:33:23 -07003271 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003272
3273 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003274 @Override
3275 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003276 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003277 }
3278 });
Winson Chungb745afb2015-03-02 11:51:23 -08003279 }
3280
3281 /**
3282 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003283 *
3284 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003285 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003286 // TODO: calling method should use the return value so that when {@code false} is returned
3287 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003288 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003289 if (!(mState == State.WORKSPACE ||
3290 mState == State.APPS_SPRING_LOADED ||
3291 mState == State.WIDGETS_SPRING_LOADED ||
3292 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003293 return false;
3294 }
3295 if (toState != State.APPS && toState != State.WIDGETS) {
3296 return false;
3297 }
Winson Chungb745afb2015-03-02 11:51:23 -08003298
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003299 // This is a safe and supported transition to bypass spring_loaded mode.
3300 if (mExitSpringLoadedModeRunnable != null) {
3301 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3302 mExitSpringLoadedModeRunnable = null;
3303 }
3304
Winson Chungb745afb2015-03-02 11:51:23 -08003305 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003306 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3307 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003308 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003309 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003310 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003311
Michael Jurkab3e22d92011-10-31 15:58:33 -07003312 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003313 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003314
3315 // Pause the auto-advance of widgets until we are out of AllApps
3316 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003317 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003318 closeFolder();
Tony Wickham49c8d292016-07-01 11:44:34 -07003319 closeShortcutsContainer();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003320
3321 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003322 getWindow().getDecorView()
3323 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003324 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003325 }
3326
Winson Chungcd99cd32015-04-29 11:03:24 -07003327 /**
3328 * Updates the workspace and interaction state on state change, and return the animation to this
3329 * new state.
3330 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003331 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003332 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003333 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003334 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003335 updateInteraction(fromState, toState);
3336 return anim;
3337 }
3338
Hyunyoung Song3f471442015-04-08 19:01:34 -07003339 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003340 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003341 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003342 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003343 }
Winson Chungb745afb2015-03-02 11:51:23 -08003344
Winson Chung006ee262015-08-03 14:40:11 -07003345 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003346 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003347 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003348
3349 if (isAppsViewVisible()) {
3350 mState = State.APPS_SPRING_LOADED;
3351 } else if (isWidgetsViewVisible()) {
3352 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003353 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003354 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003355 } else {
3356 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003357 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003358 }
Adam Cohen7777d962011-08-18 18:58:38 -07003359
Hyunyoung Song3f471442015-04-08 19:01:34 -07003360 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003361 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003362 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003363
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003364 if (mExitSpringLoadedModeRunnable != null) {
3365 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3366 }
3367 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003368 @Override
3369 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003370 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003371 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3372 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003373 // Before we show workspace, hide all apps again because
3374 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3375 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003376 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003377 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003378 } else {
3379 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003380 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003381 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07003382 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003383 };
3384 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003385 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003386
Tony Wickhame0c33232016-02-08 11:37:04 -08003387 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003388 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3389 || mState == State.WIDGETS_SPRING_LOADED;
3390 }
3391
Michael Jurkad3ef3062010-11-23 16:23:58 -08003392 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003393 if (mState == State.APPS_SPRING_LOADED) {
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003394 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07003395 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003396 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003397 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003398 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3399 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003400 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003401 }
3402
Winson Chung4ac30062015-05-08 17:34:17 -07003403 /**
3404 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3405 * resumed.
3406 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003407 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003408 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003409 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003410 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003411 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003412 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003413 }
3414 }
3415 }
3416
Michael Jurkab3e22d92011-10-31 15:58:33 -07003417 void lockAllApps() {
3418 // TODO
3419 }
3420
3421 void unlockAllApps() {
3422 // TODO
3423 }
3424
Michael Jurkad7c28052012-04-27 15:43:36 -07003425 @Override
3426 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003427 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003428 final List<CharSequence> text = event.getText();
3429 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003430 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003431 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003432 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003433 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003434 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003435 } else if (mWorkspace != null) {
3436 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003437 } else {
3438 text.add(getString(R.string.all_apps_home_button_label));
3439 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003440 return result;
3441 }
3442
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003443 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003444 * If the activity is currently paused, signal that we need to run the passed Runnable
3445 * in onResume.
3446 *
3447 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003448 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3449 * wrong when we're not running, and if the activity comes back to what the configuration was
3450 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003451 *
3452 * Implementation of the method from LauncherModel.Callbacks.
3453 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003454 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003455 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003456 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003457 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003458 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003459 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003460 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003461 }
3462 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003463 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003464 return true;
3465 } else {
3466 return false;
3467 }
3468 }
3469
Michael Jurkac402cd92013-05-20 15:49:32 +02003470 private boolean waitUntilResume(Runnable run) {
3471 return waitUntilResume(run, false);
3472 }
3473
Michael Jurka1e2f4652013-07-08 18:03:46 -07003474 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003475 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003476 }
3477
Michael Jurka7607c2f2013-04-03 14:33:19 -07003478 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003479 * If the activity is currently paused, signal that we need to re-run the loader
3480 * in onResume.
3481 *
3482 * This needs to be called from incoming places where resources might have been loaded
3483 * while we are paused. That is becaues the Configuration might be wrong
3484 * when we're not running, and if it comes back to what it was when we
3485 * were paused, we are not restarted.
3486 *
3487 * Implementation of the method from LauncherModel.Callbacks.
3488 *
3489 * @return true if we are currently paused. The caller might be able to
3490 * skip some work in that case since we will come back again.
3491 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003492 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003493 public boolean setLoadOnResume() {
3494 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003495 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003496 mOnResumeNeedsLoad = true;
3497 return true;
3498 } else {
3499 return false;
3500 }
3501 }
3502
3503 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003504 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003505 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003506 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003507 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003508 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003509 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003510 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003511 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003512 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003513 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003514
Joe Onorato9c1289c2009-08-17 11:03:03 -04003515 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003516 * Clear any pending bind callbacks. This is called when is loader is planning to
3517 * perform a full rebind from scratch.
3518 */
3519 @Override
3520 public void clearPendingBinds() {
3521 mBindOnResumeCallbacks.clear();
3522 if (mPendingExecutor != null) {
3523 mPendingExecutor.markCompleted();
3524 mPendingExecutor = null;
3525 }
3526 }
3527
3528 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003529 * Refreshes the shortcuts shown on the workspace.
3530 *
3531 * Implementation of the method from LauncherModel.Callbacks.
3532 */
3533 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003534 if (LauncherAppState.PROFILE_STARTUP) {
3535 Trace.beginSection("Starting page bind");
3536 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003537 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003538
Winson Chungd64d1762013-08-20 14:37:16 -07003539 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003540 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003541 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003542
Michael Jurka05bf644e2011-11-30 20:28:53 -08003543 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003544 if (mHotseat != null) {
3545 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003546 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003547 if (LauncherAppState.PROFILE_STARTUP) {
3548 Trace.endSection();
3549 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003550 }
3551
Adam Cohendcd297f2013-06-18 13:13:40 -07003552 @Override
3553 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003554 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003555 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3556 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003557 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3558 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3559 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003560 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3561 // If there are no screens, we need to have an empty screen
3562 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003563 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003564 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003565
3566 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003567 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003568 if (hasCustomContentToLeft()) {
3569 mWorkspace.createCustomContentContainer();
3570 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003571 }
Winsonc7d2e832016-07-28 12:24:55 -07003572
3573 // After we have added all the screens, if the wallpaper was locked to the default state,
3574 // then notify to indicate that it can be released and a proper wallpaper offset can be
3575 // computed before the next layout
3576 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07003577 }
3578
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003579 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003580 int count = orderedScreenIds.size();
3581 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003582 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003583 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003584 // No need to bind the first screen, as its always bound.
3585 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3586 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003587 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003588 }
3589
Winson Chungd64d1762013-08-20 14:37:16 -07003590 public void bindAppsAdded(final ArrayList<Long> newScreens,
3591 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003592 final ArrayList<ItemInfo> addAnimated,
3593 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003594 Runnable r = new Runnable() {
3595 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003596 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003597 }
3598 };
3599 if (waitUntilResume(r)) {
3600 return;
3601 }
3602
Winson Chungd64d1762013-08-20 14:37:16 -07003603 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003604 if (newScreens != null) {
3605 bindAddScreens(newScreens);
3606 }
Winson Chungd64d1762013-08-20 14:37:16 -07003607
3608 // We add the items without animation on non-visible pages, and with
3609 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003610 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003611 bindItems(addNotAnimated, 0,
3612 addNotAnimated.size(), false);
3613 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003614 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003615 bindItems(addAnimated, 0,
3616 addAnimated.size(), true);
3617 }
Winson Chungc58497e2013-09-03 17:48:37 -07003618
Winson Chung87412982013-10-03 18:34:14 -07003619 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003620 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003621
Winson Chungb745afb2015-03-02 11:51:23 -08003622 if (addedApps != null && mAppsView != null) {
3623 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003624 }
Winson Chungd64d1762013-08-20 14:37:16 -07003625 }
3626
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003627 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003628 * Bind the items start-end from the list.
3629 *
3630 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003631 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003632 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003633 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3634 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003635 Runnable r = new Runnable() {
3636 public void run() {
3637 bindItems(shortcuts, start, end, forceAnimateIcons);
3638 }
3639 };
3640 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003641 return;
3642 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003643
Winson Chungf0c6ae02012-03-21 16:10:31 -07003644 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003645 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3646 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003647 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003648 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003649 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003650 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003651 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003652
3653 // Short circuit if we are loading dock items for a configuration which has no dock
3654 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3655 mHotseat == null) {
3656 continue;
3657 }
3658
Sunny Goyal639e9062015-08-19 19:17:06 -07003659 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003660 switch (item.itemType) {
3661 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3662 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003663 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003664 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003665 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003666 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003667 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003668 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003669 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003670 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003671 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003672 default:
3673 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003674 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003675
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003676 /*
3677 * Remove colliding items.
3678 */
3679 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3680 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3681 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3682 View v = cl.getChildAt(item.cellX, item.cellY);
3683 Object tag = v.getTag();
3684 String desc = "Collision while binding workspace item: " + item
3685 + ". Collides with " + tag;
3686 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3687 throw (new RuntimeException(desc));
3688 } else {
3689 Log.d(TAG, desc);
3690 LauncherModel.deleteItemFromDatabase(this, item);
3691 continue;
3692 }
3693 }
3694 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003695 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3696 item.cellY, 1, 1);
3697 if (animateIcons) {
3698 // Animate all the applications up now
3699 view.setAlpha(0f);
3700 view.setScaleX(0f);
3701 view.setScaleY(0f);
3702 bounceAnims.add(createNewAppBounceAnimation(view, i));
3703 newShortcutsScreenId = item.screenId;
3704 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003705 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003706
Winson Chung997a9232013-07-24 15:33:46 -07003707 if (animateIcons) {
3708 // Animate to the correct page
3709 if (newShortcutsScreenId > -1) {
3710 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003711 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003712 final Runnable startBounceAnimRunnable = new Runnable() {
3713 public void run() {
3714 anim.playTogether(bounceAnims);
3715 anim.start();
3716 }
3717 };
Winson Chung997a9232013-07-24 15:33:46 -07003718 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003719 // We post the animation slightly delayed to prevent slowdowns
3720 // when we are loading right after we return to launcher.
3721 mWorkspace.postDelayed(new Runnable() {
3722 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003723 if (mWorkspace != null) {
3724 mWorkspace.snapToPage(newScreenIndex);
3725 mWorkspace.postDelayed(startBounceAnimRunnable,
3726 NEW_APPS_ANIMATION_DELAY);
3727 }
Winson Chung94d67682013-09-25 16:29:40 -07003728 }
3729 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003730 } else {
3731 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003732 }
3733 }
Winson Chung64359a52013-07-08 17:17:08 -07003734 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003735 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003736 }
3737
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003738 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3739 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3740 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003741 view.updateAppWidget(null);
3742 view.setOnClickListener(this);
3743 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003744 mWorkspace.requestLayout();
3745 }
3746
Joe Onorato9c1289c2009-08-17 11:03:03 -04003747 /**
3748 * Add the views for a widget to the workspace.
3749 *
3750 * Implementation of the method from LauncherModel.Callbacks.
3751 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003752 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003753 Runnable r = new Runnable() {
3754 public void run() {
3755 bindAppWidget(item);
3756 }
3757 };
3758 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003759 return;
3760 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003761
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003762 if (mIsSafeModeEnabled) {
3763 bindSafeModeWidget(item);
3764 return;
3765 }
3766
Daniel Sandler843e8602010-06-07 14:59:01 -04003767 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3768 if (DEBUG_WIDGETS) {
3769 Log.d(TAG, "bindAppWidget: " + item);
3770 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003771
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003772 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003773
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003774 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3775 // If the provider is not ready, bind as a pending widget.
3776 appWidgetInfo = null;
3777 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3778 // The widget id is not valid. Try to find the widget based on the provider info.
3779 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3780 } else {
3781 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3782 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003783
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003784 // If the provider is ready, but the width is not yet restored, try to restore it.
3785 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3786 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003787 if (appWidgetInfo == null) {
3788 if (DEBUG_WIDGETS) {
3789 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3790 + " belongs to component " + item.providerName
3791 + ", as the povider is null");
3792 }
3793 LauncherModel.deleteItemFromDatabase(this, item);
3794 return;
3795 }
Sunny Goyalff572272014-07-23 13:58:07 -07003796
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003797 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003798 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003799 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3800 // Id has not been allocated yet. Allocate a new id.
3801 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3802 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003803
Sunny Goyald478c832016-04-01 12:04:16 -07003804 // Also try to bind the widget. If the bind fails, the user will be shown
3805 // a click to setup UI, which will ask for the bind permission.
3806 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3807 pendingInfo.spanX = item.spanX;
3808 pendingInfo.spanY = item.spanY;
3809 pendingInfo.minSpanX = item.minSpanX;
3810 pendingInfo.minSpanY = item.minSpanY;
3811 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003812
3813 boolean isDirectConfig =
3814 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3815 if (isDirectConfig && item.bindOptions != null) {
3816 Bundle newOptions = item.bindOptions.getExtras();
3817 if (options != null) {
3818 newOptions.putAll(options);
3819 }
3820 options = newOptions;
3821 }
Sunny Goyald478c832016-04-01 12:04:16 -07003822 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3823 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003824
Sunny Goyal86df1382016-08-10 15:03:22 -07003825 // We tried to bind once. If we were not able to bind, we would need to
3826 // go through the permission dialog, which means we cannot skip the config
3827 // activity.
3828 item.bindOptions = null;
3829 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3830
Sunny Goyald478c832016-04-01 12:04:16 -07003831 // Bind succeeded
3832 if (success) {
3833 // If the widget has a configure activity, it is still needs to set it up,
3834 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003835 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003836 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3837 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003838 }
Sunny Goyald478c832016-04-01 12:04:16 -07003839
3840 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003841 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003842 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003843 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003844 // The widget was marked as UI not ready, but there is no configure activity to
3845 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003846 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3847 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003848 }
Sunny Goyalff572272014-07-23 13:58:07 -07003849 }
3850
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003851 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003852 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003853 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3854 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003855 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003856
Sunny Goyal56c73602015-09-25 12:55:01 -07003857 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003858 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003859 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003860 deleteWidgetInfo(item);
3861 return;
3862 }
3863
Sunny Goyal233ee962015-08-03 13:05:01 -07003864 item.minSpanX = appWidgetInfo.minSpanX;
3865 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003866 addAppWidgetToWorkspace(
3867 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3868 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003869 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003870 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003871 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003872 view.updateAppWidget(null);
3873 view.setOnClickListener(this);
3874 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003875 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003876 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003877
Daniel Sandler843e8602010-06-07 14:59:01 -04003878 if (DEBUG_WIDGETS) {
3879 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3880 + (SystemClock.uptimeMillis()-start) + "ms");
3881 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003882 }
3883
Sunny Goyalff572272014-07-23 13:58:07 -07003884 /**
3885 * Restores a pending widget.
3886 *
3887 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003888 */
Sunny Goyald478c832016-04-01 12:04:16 -07003889 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003890 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3891 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3892 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003893 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003894 }
3895
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003896 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003897 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003898
3899 mWorkspace.reinflateWidgetsIfNecessary();
3900 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003901 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003902 }
3903
Adam Cohen1462de32012-07-24 22:34:36 -07003904 public void onPageBoundSynchronously(int page) {
3905 mSynchronouslyBoundPages.add(page);
3906 }
3907
Sunny Goyal527c7d32015-08-28 15:19:36 -07003908 @Override
3909 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3910 if (mPendingExecutor != null) {
3911 mPendingExecutor.markCompleted();
3912 }
3913 mPendingExecutor = executor;
3914 executor.attachTo(this);
3915 }
3916
3917 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3918 if (mPendingExecutor == executor) {
3919 mPendingExecutor = null;
3920 }
3921 }
3922
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003923 @Override
3924 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3925 Runnable r = new Runnable() {
3926 public void run() {
3927 finishFirstPageBind(executor);
3928 }
3929 };
3930 if (waitUntilResume(r)) {
3931 return;
3932 }
3933
3934 Runnable onComplete = new Runnable() {
3935 @Override
3936 public void run() {
3937 if (executor != null) {
3938 executor.onLoadAnimationCompleted();
3939 }
3940 }
3941 };
3942 if (mDragLayer.getAlpha() < 1) {
3943 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3944 } else {
3945 onComplete.run();
3946 }
3947 }
3948
Joe Onorato9c1289c2009-08-17 11:03:03 -04003949 /**
3950 * Callback saying that there aren't any more items to bind.
3951 *
3952 * Implementation of the method from LauncherModel.Callbacks.
3953 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003954 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003955 Runnable r = new Runnable() {
3956 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003957 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003958 }
3959 };
3960 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003961 return;
3962 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003963 if (LauncherAppState.PROFILE_STARTUP) {
3964 Trace.beginSection("Page bind completed");
3965 }
Adam Cohen1462de32012-07-24 22:34:36 -07003966 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003967
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003968 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003969
Sunny Goyal2100c782016-08-22 16:00:03 -07003970 if (mPendingActivityResult != null) {
3971 handleActivityResult(mPendingActivityResult.requestCode,
3972 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3973 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003974 }
3975
Sunny Goyal756adbc2015-04-16 15:20:51 -07003976 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003977
3978 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003979 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003980 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003981 if (LauncherAppState.PROFILE_STARTUP) {
3982 Trace.endSection();
3983 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003984 }
3985
Winson Chunga2413752012-04-03 14:22:34 -07003986 private boolean canRunNewAppsAnimation() {
3987 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003988 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003989 }
3990
Winson Chungc9168342013-06-26 14:54:55 -07003991 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003992 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003993 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3994 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003995 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003996 return bounceAnim;
3997 }
3998
Adam Cohen27772732013-11-11 14:00:56 +00003999 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004000 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004001 }
4002
Tony Wickham55616cd2015-09-23 14:55:17 -07004003 public int getSearchBarHeight() {
4004 if (mLauncherCallbacks != null) {
4005 return mLauncherCallbacks.getSearchBarHeight();
4006 }
4007 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4008 }
4009
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004010 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004011 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4012 * multiple calls to bind the same list.)
4013 */
4014 @Thunk ArrayList<AppInfo> mTmpAppsList;
4015 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4016 public void run() {
4017 bindAllApplications(mTmpAppsList);
4018 mTmpAppsList = null;
4019 }
4020 };
4021
4022 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004023 * Add the icons for all apps.
4024 *
4025 * Implementation of the method from LauncherModel.Callbacks.
4026 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004027 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004028 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4029 mTmpAppsList = apps;
4030 return;
4031 }
4032
Winson Chungb745afb2015-03-02 11:51:23 -08004033 if (mAppsView != null) {
4034 mAppsView.setApps(apps);
4035 }
Adam Cohen9211d422014-10-07 18:14:53 -07004036 if (mLauncherCallbacks != null) {
4037 mLauncherCallbacks.bindAllApplications(apps);
4038 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004039 }
4040
4041 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004042 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
4043 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
4044 */
4045 @Override
4046 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
4047 mDeepShortcutMap = deepShortcutMapCopy;
4048 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
4049 }
4050
Tony Wickham1bce7fd2016-04-28 17:39:03 -07004051 public List<String> getShortcutIdsForItem(ItemInfo info) {
4052 if (!DeepShortcutManager.supportsShortcuts(info)) {
4053 return Collections.EMPTY_LIST;
4054 }
4055 ComponentName component = info.getTargetComponent();
Hyunyoung Songd3bf9802016-08-29 14:43:53 -07004056 if (component == null) {
4057 return Collections.EMPTY_LIST;
4058 }
4059
Tony Wickham1bce7fd2016-04-28 17:39:03 -07004060 List<String> ids = mDeepShortcutMap.get(new ComponentKey(component, info.user));
4061 return ids == null ? Collections.EMPTY_LIST : ids;
4062 }
4063
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004064 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004065 * A package was updated.
4066 *
4067 * Implementation of the method from LauncherModel.Callbacks.
4068 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004069 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004070 Runnable r = new Runnable() {
4071 public void run() {
4072 bindAppsUpdated(apps);
4073 }
4074 };
4075 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004076 return;
4077 }
4078
Winson Chungb745afb2015-03-02 11:51:23 -08004079 if (mAppsView != null) {
4080 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004081 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004082 }
4083
Sunny Goyal4390ace2014-10-13 11:33:11 -07004084 @Override
4085 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4086 Runnable r = new Runnable() {
4087 public void run() {
4088 bindWidgetsRestored(widgets);
4089 }
4090 };
4091 if (waitUntilResume(r)) {
4092 return;
4093 }
4094 mWorkspace.widgetsRestored(widgets);
4095 }
4096
Joe Onorato9c1289c2009-08-17 11:03:03 -04004097 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004098 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004099 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004100 *
4101 * @param updated list of shortcuts which have changed.
4102 * @param removed list of shortcuts which were deleted in the background. This can happen when
4103 * an app gets removed from the system or some of its components are no longer
4104 * available.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004105 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004106 @Override
4107 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4108 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004109 Runnable r = new Runnable() {
4110 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004111 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004112 }
4113 };
4114 if (waitUntilResume(r)) {
4115 return;
4116 }
4117
Sunny Goyal4390ace2014-10-13 11:33:11 -07004118 if (!updated.isEmpty()) {
4119 mWorkspace.updateShortcuts(updated);
4120 }
4121
4122 if (!removed.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004123 HashSet<ComponentName> removedComponents = new HashSet<>();
4124 HashSet<ShortcutKey> removedDeepShortcuts = new HashSet<>();
4125
Sunny Goyal4390ace2014-10-13 11:33:11 -07004126 for (ShortcutInfo si : removed) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004127 if (si.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07004128 removedDeepShortcuts.add(ShortcutKey.fromShortcutInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004129 } else {
4130 removedComponents.add(si.getTargetComponent());
4131 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004132 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004133
4134 if (!removedComponents.isEmpty()) {
4135 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(removedComponents, user);
4136 mWorkspace.removeItemsByMatcher(matcher);
4137 mDragController.onAppsRemoved(matcher);
4138 }
4139
4140 if (!removedDeepShortcuts.isEmpty()) {
4141 ItemInfoMatcher matcher = ItemInfoMatcher.ofShortcutKeys(removedDeepShortcuts);
4142 mWorkspace.removeItemsByMatcher(matcher);
4143 mDragController.onAppsRemoved(matcher);
4144 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004145 }
4146 }
4147
4148 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004149 * Update the state of a package, typically related to install state.
4150 *
4151 * Implementation of the method from LauncherModel.Callbacks.
4152 */
Sunny Goyale755d462014-07-22 13:48:29 -07004153 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004154 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4155 Runnable r = new Runnable() {
4156 public void run() {
4157 bindRestoreItemsChange(updates);
4158 }
4159 };
4160 if (waitUntilResume(r)) {
4161 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004162 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004163
Sunny Goyal756adbc2015-04-16 15:20:51 -07004164 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004165 }
4166
4167 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004168 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004169 * in addition to specific applications to remove, the reason being that
4170 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004171 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004172 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004173 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004174 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004175 public void bindWorkspaceComponentsRemoved(
4176 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4177 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004178 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004179 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004180 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004181 }
Winson Chungd64d1762013-08-20 14:37:16 -07004182 };
4183 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004184 return;
4185 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004186 if (!packageNames.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004187 ItemInfoMatcher matcher = ItemInfoMatcher.ofPackages(packageNames, user);
4188 mWorkspace.removeItemsByMatcher(matcher);
4189 mDragController.onAppsRemoved(matcher);
4190
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004191 }
4192 if (!components.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004193 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(components, user);
4194 mWorkspace.removeItemsByMatcher(matcher);
4195 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004196 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004197 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004198
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004199 @Override
4200 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4201 Runnable r = new Runnable() {
4202 public void run() {
4203 bindAppInfosRemoved(appInfos);
4204 }
4205 };
4206 if (waitUntilResume(r)) {
4207 return;
Joe Onorato36115782010-06-17 13:28:48 -04004208 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004209
Winson Chungdf95eb12013-10-16 14:57:07 -07004210 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004211 if (mAppsView != null) {
4212 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004213 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004214 }
Joe Onoratobe386092009-11-17 17:32:16 -08004215
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004216 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004217 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004218 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004219 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004220 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004221
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004222 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004223 public void bindWidgetsModel(WidgetsModel model) {
4224 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004225 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004226 return;
4227 }
4228
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004229 if (mWidgetsView != null && model != null) {
4230 mWidgetsView.addWidgets(model);
4231 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004232 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004233 }
4234
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004235 @Override
4236 public void notifyWidgetProvidersChanged() {
4237 if (mWorkspace.getState().shouldUpdateWidget) {
4238 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4239 }
4240 }
4241
Winson Chung400438b2011-01-16 17:53:48 -08004242 private int mapConfigurationOriActivityInfoOri(int configOri) {
4243 final Display d = getWindowManager().getDefaultDisplay();
4244 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4245 switch (d.getRotation()) {
4246 case Surface.ROTATION_0:
4247 case Surface.ROTATION_180:
4248 // We are currently in the same basic orientation as the natural orientation
4249 naturalOri = configOri;
4250 break;
4251 case Surface.ROTATION_90:
4252 case Surface.ROTATION_270:
4253 // We are currently in the other basic orientation to the natural orientation
4254 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4255 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4256 break;
4257 }
4258
4259 int[] oriMap = {
4260 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4261 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4262 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4263 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4264 };
4265 // Since the map starts at portrait, we need to offset if this device's natural orientation
4266 // is landscape.
4267 int indexOffset = 0;
4268 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4269 indexOffset = 1;
4270 }
4271 return oriMap[(d.getRotation() + indexOffset) % 4];
4272 }
Adam Cohen446e9402011-09-15 18:21:21 -07004273
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004274 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004275 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004276 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004277 if (Utilities.ATLEAST_JB_MR2) {
4278 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4279 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004280 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4281 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004282 }
Winson Chung4b919f82012-05-01 10:44:08 -07004283 }
4284 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004285
Winson Chung4b919f82012-05-01 10:44:08 -07004286 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004287 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004288 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004289 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004290 } else {
4291 mHandler.postDelayed(new Runnable() {
4292 public void run() {
4293 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4294 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004295 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004296 }
Winson Chung4b919f82012-05-01 10:44:08 -07004297 }
Winson Chung400438b2011-01-16 17:53:48 -08004298 }
4299
Hyunyoung Songc001cf52016-07-21 17:32:43 -07004300 private void markAppsViewShown() {
4301 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4302 return;
4303 }
4304 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
4305 }
4306
4307 private boolean shouldShowDiscoveryBounce() {
4308 if (mState != mState.WORKSPACE) {
4309 return false;
4310 }
4311 if (mLauncherCallbacks != null && mLauncherCallbacks.shouldShowDiscoveryBounce()) {
4312 return true;
4313 }
4314 if (!mIsResumeFromActionScreenOff) {
4315 return false;
4316 }
4317 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4318 return false;
4319 }
4320 return true;
4321 }
4322
Winson Chung5ffd51d2015-06-25 11:36:50 -07004323 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004324 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004325 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004326 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004327 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004328 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004329 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4330 if (userHandle != null) {
4331 user = UserHandleCompat.fromUser(userHandle);
4332 }
4333 }
4334 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004335 }
4336
4337 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004338 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004339 if (user == null) {
4340 user = UserHandleCompat.myUserHandle();
4341 }
4342
4343 // Called from search suggestion, add the profile extra to the intent to ensure that we
4344 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004345 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004346 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004347 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004348 return null;
4349 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004350 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004351 }
4352
Winson Chung5ffd51d2015-06-25 11:36:50 -07004353 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004354 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4355 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004356 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004357 UserHandleCompat.myUserHandle());
4358 }
4359
Winson Chung5ffd51d2015-06-25 11:36:50 -07004360 protected void moveWorkspaceToDefaultScreen() {
4361 mWorkspace.moveToDefaultScreen(false);
4362 }
4363
Winson Chung80baf5a2010-08-09 16:03:15 -07004364 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004365 * Returns a FastBitmapDrawable with the icon, accurately sized.
4366 */
4367 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4368 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4369 d.setFilterBitmap(true);
4370 resizeIconDrawable(d);
4371 return d;
4372 }
4373
4374 /**
4375 * Resizes an icon drawable to the correct icon size.
4376 */
Winson5fbe0742015-09-30 15:33:00 -07004377 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004378 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004379 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004380 }
4381
4382 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004383 * Prints out out state for debugging.
4384 */
4385 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004386 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004387 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
Sunny Goyal2100c782016-08-22 16:00:03 -07004388 Log.d(TAG, "mPendingRequestArgs=" + mPendingRequestArgs);
4389 Log.d(TAG, "mPendingActivityResult=" + mPendingActivityResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004390 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004391 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004392
Daniel Sandler325dc232013-06-05 22:57:57 -04004393 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004394 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004395
4396 @Override
4397 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4398 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004399 // Dump workspace
4400 writer.println(prefix + "Workspace Items");
4401 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4402 writer.println(prefix + " Homescreen " + i);
4403
4404 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4405 for (int j = 0; j < layout.getChildCount(); j++) {
4406 Object tag = layout.getChildAt(j).getTag();
4407 if (tag != null) {
4408 writer.println(prefix + " " + tag.toString());
4409 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004410 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004411 }
Sunny Goyala1365452015-10-01 15:46:24 -07004412
4413 writer.println(prefix + " Hotseat");
4414 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4415 for (int j = 0; j < layout.getChildCount(); j++) {
4416 Object tag = layout.getChildAt(j).getTag();
4417 if (tag != null) {
4418 writer.println(prefix + " " + tag.toString());
4419 }
4420 }
4421
Sunny Goyal713edfc2016-05-06 09:58:34 -07004422 try {
4423 FileLog.flushAll(writer);
4424 } catch (Exception e) {
4425 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004426 }
4427
Adam Cohen9211d422014-10-07 18:14:53 -07004428 if (mLauncherCallbacks != null) {
4429 mLauncherCallbacks.dump(prefix, fd, writer, args);
4430 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004431 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004432
Adam Cohen59400422014-03-05 18:07:04 -08004433 public static CustomAppWidget getCustomAppWidget(String name) {
4434 return sCustomAppWidgets.get(name);
4435 }
4436
4437 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4438 return sCustomAppWidgets;
4439 }
4440
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004441 public static Launcher getLauncher(Context context) {
4442 if (context instanceof Launcher) {
4443 return (Launcher) context;
4444 }
4445 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4446 }
4447
Sunny Goyal745bad92016-05-02 10:54:12 -07004448 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4449
4450 @Override
4451 public void onSharedPreferenceChanged(
4452 SharedPreferences sharedPreferences, String key) {
4453 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4454 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4455 if (!waitUntilResume(this, true)) {
4456 run();
4457 }
4458 }
4459 }
4460
4461 @Override
4462 public void run() {
4463 setOrientation();
4464 }
4465 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004466}