blob: 1c549e82a9fc1f8edb562f1ab0aa29099b2d86ea [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
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700258 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
259 // scroll issues (because the workspace may not have been measured yet) and extra work.
260 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
261 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262
263 private SpannableStringBuilder mDefaultKeySsb = null;
264
Adam Cohen091440a2015-03-18 14:16:05 -0700265 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400266
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800267 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700268 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269
Michael Jurka1e2f4652013-07-08 18:03:46 -0700270 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700271 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700272 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700273
Joe Onorato9c1289c2009-08-17 11:03:03 -0400274 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800275 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700276 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700277 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700278 private boolean mIsResumeFromActionScreenOff;
Adam Cohen091440a2015-03-18 14:16:05 -0700279 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800280 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700281 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800282 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400283
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700284 /** Maps launcher activity components to their list of shortcut ids. */
285 private MultiHashMap<ComponentKey, String> mDeepShortcutMap = new MultiHashMap<>();
286
Adam Cohen61f560d2013-09-30 15:58:20 -0700287 private View.OnTouchListener mHapticFeedbackTouchListener;
288
Adam Cohended9f8d2010-11-03 13:25:16 -0700289 // Related to the auto-advancing of widgets
290 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700291 private static final int ADVANCE_INTERVAL = 20000;
292 private static final int ADVANCE_STAGGER = 250;
293
294 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700295 private long mAutoAdvanceSentTime;
296 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700297 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700298
Winson Chung400438b2011-01-16 17:53:48 -0800299 // Determines how long to wait after a rotation before restoring the screen orientation to
300 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700301 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800302
Adam Cohen1462de32012-07-24 22:34:36 -0700303 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
304
Winson Chung46353de2012-02-16 14:05:10 -0800305 // We only want to get the SharedPreferences once since it does an FS stat each time we get
306 // it from the context.
307 private SharedPreferences mSharedPrefs;
308
Winson Chungf0c6ae02012-03-21 16:10:31 -0700309 // Holds the page that we need to animate to, and the icon views that we need to animate up
310 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700311 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700312 private Bitmap mFolderIconBitmap;
313 private Canvas mFolderIconCanvas;
314 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700315
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700316 private DeviceProfile mDeviceProfile;
317
Adam Cohen4b66bf32015-09-18 12:15:19 -0700318 private boolean mMoveToDefaultScreenFromNewIntent;
319
Winson Chung13eb5272015-05-11 16:30:13 -0700320 // This is set to the view that launched the activity that navigated the user away from
321 // launcher. Since there is no callback for when the activity has finished launching, enable
322 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800323 private BubbleTextView mWaitingForResume;
324
Adam Cohen59400422014-03-05 18:07:04 -0800325 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
326 new HashMap<String, CustomAppWidget>();
327
Adam Cohen59400422014-03-05 18:07:04 -0800328 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700329 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
330 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800331 }
332 }
333
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700334 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
335 // state transition. If another state transition happened during this delay,
336 // simply unregister this runnable.
337 private Runnable mExitSpringLoadedModeRunnable;
338
Adam Cohen091440a2015-03-18 14:16:05 -0700339 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700340 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700341 if (mWorkspace != null) {
342 mWorkspace.buildPageHardwareLayers();
343 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700344 }
345 };
346
Sunny Goyal2100c782016-08-22 16:00:03 -0700347 // Activity result which needs to be processed after workspace has loaded.
348 private ActivityResultInfo mPendingActivityResult;
349 /**
350 * Holds extra information required to handle a result from an external call, like
351 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
352 */
353 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800354
Hyunyoung Songaa953652016-04-19 18:30:24 -0700355 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800356
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700357 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700358 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400359
360 @Thunk void setOrientation() {
361 if (mRotationEnabled) {
362 unlockScreenOrientation(true);
363 } else {
364 setRequestedOrientation(
365 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700366 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400367 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700368
Sunny Goyal745bad92016-05-02 10:54:12 -0700369 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700370
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371 @Override
372 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700373 if (DEBUG_STRICT_MODE) {
374 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
375 .detectDiskReads()
376 .detectDiskWrites()
377 .detectNetwork() // or .detectAll() for all detectable problems
378 .penaltyLog()
379 .build());
380 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
381 .detectLeakedSqlLiteObjects()
382 .detectLeakedClosableObjects()
383 .penaltyLog()
384 .penaltyDeath()
385 .build());
386 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700387 if (LauncherAppState.PROFILE_STARTUP) {
388 Trace.beginSection("Launcher-onCreate");
389 }
Winson Chunga2413752012-04-03 14:22:34 -0700390
Adam Cohen9211d422014-10-07 18:14:53 -0700391 if (mLauncherCallbacks != null) {
392 mLauncherCallbacks.preOnCreate();
393 }
394
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400396
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400397 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700398
Adam Cohen2e6da152015-05-06 11:42:25 -0700399 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700400 mDeviceProfile = getResources().getConfiguration().orientation
401 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700402 app.getInvariantDeviceProfile().landscapeProfile
403 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700404
Sunny Goyalf7258242015-10-19 16:59:07 -0700405 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700406 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800407 mModel = app.setLauncher(this);
408 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700409 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700410
Joe Onorato41a12d22009-10-31 18:30:00 -0400411 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700412 mAllAppsController = new AllAppsTransitionController(this);
413 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700414
Sunny Goyalffe83f12014-08-14 17:39:34 -0700415 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700416
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700417 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
418 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700419
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700420 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
421 // this also ensures that any synchronous binding below doesn't re-trigger another
422 // LauncherModel load.
423 mPaused = false;
424
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700426
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800427 setupViews();
Winson1f064272016-07-18 17:18:02 -0700428 mDeviceProfile.layout(this, false /* notifyListeners */);
Tony Wickham827cef22016-03-17 15:39:39 -0700429 mExtractedColors = new ExtractedColors();
430 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431
Tony Wickhame2217252016-03-22 16:34:23 -0700432 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
433 .addAccessibilityStateChangeListener(this);
434
Joe Onorato7c312c12009-08-13 21:36:53 -0700435 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700436
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 mSavedState = savedInstanceState;
438 restoreState(mSavedState);
439
Sunny Goyale26d1002016-06-20 14:52:14 -0700440 if (LauncherAppState.PROFILE_STARTUP) {
441 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442 }
443
Sunny Goyal2100c782016-08-22 16:00:03 -0700444 // We only load the page synchronously if the user rotates (or triggers a
445 // configuration change) while launcher is in the foreground
446 if (!mModel.startLoader(mWorkspace.getRestorePage())) {
447 // If we are not binding synchronously, show a fade in animation when
448 // the first page bind completes.
449 mDragLayer.setAlpha(0);
450 } else {
451 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 }
453
454 // For handling default keys
455 mDefaultKeySsb = new SpannableStringBuilder();
456 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800457
458 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700459 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
460 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800461
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800462 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700463 // In case we are on a device with locked rotation, we should look at preferences to check
464 // if the user has specifically allowed rotation.
465 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700466 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700467 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
468 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700469 }
470
471 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
472 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400473 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700474
Adam Cohen9211d422014-10-07 18:14:53 -0700475 if (mLauncherCallbacks != null) {
476 mLauncherCallbacks.onCreate(savedInstanceState);
477 }
478 }
479
Sunny Goyal7779d622015-06-11 16:18:39 -0700480 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700481 public void onExtractedColorsChanged() {
482 loadExtractedColorsAndColorItems();
483 }
484
485 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700486 // TODO: do this in pre-N as well, once the extraction part is complete.
487 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700488 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700489 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700490 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham827cef22016-03-17 15:39:39 -0700491 }
492 }
493
Adam Cohen9211d422014-10-07 18:14:53 -0700494 private LauncherCallbacks mLauncherCallbacks;
495
496 public void onPostCreate(Bundle savedInstanceState) {
497 super.onPostCreate(savedInstanceState);
498 if (mLauncherCallbacks != null) {
499 mLauncherCallbacks.onPostCreate(savedInstanceState);
500 }
501 }
502
Winson1f064272016-07-18 17:18:02 -0700503 public void onInsetsChanged(Rect insets) {
504 mDeviceProfile.updateInsets(insets);
505 mDeviceProfile.layout(this, true /* notifyListeners */);
506 }
507
Sunny Goyal32554d12015-12-03 15:31:25 -0800508 /**
509 * Call this after onCreate to set or clear overlay.
510 */
511 public void setLauncherOverlay(LauncherOverlay overlay) {
512 if (overlay != null) {
513 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
514 }
515 mWorkspace.setLauncherOverlay(overlay);
516 }
517
Adam Cohen9211d422014-10-07 18:14:53 -0700518 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
519 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700520 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700521 private boolean mWorkspaceImportanceStored = false;
522 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700523 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800524 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700525 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
526
527 @Override
528 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700529 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700530 return;
531 }
532 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700533 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700534 if (mWorkspace != null) {
535 mWorkspaceImportanceForAccessibility =
536 mWorkspace.getImportantForAccessibility();
537 mWorkspace.setImportantForAccessibility(
538 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
539 mWorkspaceImportanceStored = true;
540 }
541 if (mHotseat != null) {
542 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
543 mHotseat.setImportantForAccessibility(
544 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
545 mHotseatImportanceStored = true;
546 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700547 }
548
549 @Override
550 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700551 if (mWorkspaceImportanceStored && mWorkspace != null) {
552 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
553 }
554 if (mHotseatImportanceStored && mHotseat != null) {
555 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
556 }
557 mWorkspaceImportanceStored = false;
558 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700559 }
560 });
Adam Cohen9211d422014-10-07 18:14:53 -0700561 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700562 }
563
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700564 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700565 public void onLauncherProviderChange() {
566 if (mLauncherCallbacks != null) {
567 mLauncherCallbacks.onLauncherProviderChange();
568 }
569 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700570
Adam Cohen9211d422014-10-07 18:14:53 -0700571 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700572 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700573 if (mLauncherCallbacks != null) {
574 return mLauncherCallbacks.hasCustomContentToLeft();
575 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700576 return false;
577 }
578
Dave Hawkeya8881582013-09-17 15:55:33 -0600579 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500580 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600581 * {@link #addToCustomContentPage}. This will only be invoked if
582 * {@link #hasCustomContentToLeft()} is {@code true}.
583 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500584 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700585 if (mLauncherCallbacks != null) {
586 mLauncherCallbacks.populateCustomContentContainer();
587 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600588 }
589
590 /**
591 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
592 * ensure the custom content page is added or removed if necessary.
593 */
594 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600595 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600596 // Not bound yet, wait for bindScreens to be called.
597 return;
598 }
599
600 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
601 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500602 mWorkspace.createCustomContentContainer();
603 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600604 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
605 mWorkspace.removeCustomContentPage();
606 }
607 }
608
Hyunyoung Songaa953652016-04-19 18:30:24 -0700609 public UserEventDispatcher getUserEventDispatcher() {
610 if (mLauncherCallbacks != null) {
611 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
612 if (dispatcher != null) {
613 return dispatcher;
614 }
615 }
616
Sunny Goyal64976d52016-06-20 14:56:28 -0700617 // Logger object is a singleton and does not have to be coupled with the foreground
618 // activity. Since most user event logging is done on the UI, the object is retrieved
619 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700620 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700621 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700622 }
623 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800624 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100625
Hyunyoung Song3f471442015-04-08 19:01:34 -0700626 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700627 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
628 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700629 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700630 }
631
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000632 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700633 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
634 // This cast is safe as long as the id < 0x00FFFFFF
635 // Since we jail all the dynamically generated views, there should be no clashes
636 // with any other views.
637 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000638 }
639
640 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700641 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
642 * a configuration step, this allows the proper animations to run after other transitions.
643 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700644 private long completeAdd(
645 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
646 long screenId = info.screenId;
647 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700648 // When the screen id represents an actual screen (as opposed to a rank) we make sure
649 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700650 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700651 }
Adam Cohendb364c32014-05-20 14:23:40 -0700652
Sunny Goyal2100c782016-08-22 16:00:03 -0700653 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800654 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700655 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700656 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800657 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700658 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700659 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700660 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700661 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700662 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700663 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700664 int widgetId = appWidgetId;
665 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700666 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700667 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700668 // Since the view was just bound, also launch the configure activity if needed
669 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
670 .getLauncherAppWidgetInfo(widgetId);
671 if (provider != null && provider.configure != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700672 startRestoredWidgetReconfigActivity(provider, widgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -0700673 }
674 }
675 break;
676 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800677 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700678
Adam Cohendb364c32014-05-20 14:23:40 -0700679 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800680 }
681
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800682 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700683 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700684 if (isWorkspaceLoading()) {
685 // process the result once the workspace has loaded.
686 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
687 return;
688 }
689 mPendingActivityResult = null;
690
Winson Chunge341d302013-08-16 14:31:00 -0700691 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700692 final PendingRequestArgs requestArgs = mPendingRequestArgs;
693 setWaitingForResult(null);
694 if (requestArgs == null) {
695 return;
696 }
697
698 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700699
Adam Cohenad4e15c2013-10-17 16:21:35 -0700700 Runnable exitSpringLoaded = new Runnable() {
701 @Override
702 public void run() {
703 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
704 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
705 }
706 };
707
Michael Jurka8b805b12012-04-18 14:23:14 -0700708 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700709 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700710 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700711 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
712 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700713 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700714 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700715 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700716 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700717 addAppWidgetImpl(
718 appWidgetId, requestArgs, null,
719 requestArgs.getWidgetProvider(),
720 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700721 }
722 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200723 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
724 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700725 // User could have free-scrolled between pages before picking a wallpaper; make sure
726 // we move to the closest one now.
727 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700728 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200729 }
730 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700731 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200732
Adam Cohened66b2b2012-01-23 17:28:51 -0800733 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700734 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700735
Adam Cohened66b2b2012-01-23 17:28:51 -0800736 // We have special handling for widgets
737 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800738 final int appWidgetId;
739 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
740 : -1;
741 if (widgetId < 0) {
742 appWidgetId = pendingAddWidgetId;
743 } else {
744 appWidgetId = widgetId;
745 }
746
Adam Cohenad4e15c2013-10-17 16:21:35 -0700747 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800748 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700749 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
750 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700751 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700752 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700753 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700754 @Override
755 public void run() {
756 exitSpringLoadedDragModeDelayed(false, 0, null);
757 }
758 };
Adam Cohendb364c32014-05-20 14:23:40 -0700759
Sunny Goyal2100c782016-08-22 16:00:03 -0700760 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
761 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
762 } else {
763 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
764 // When the screen id represents an actual screen (as opposed to a rank)
765 // we make sure that the drop page actually exists.
766 requestArgs.screenId =
767 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700768 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700769 final CellLayout dropLayout =
770 mWorkspace.getScreenWithId(requestArgs.screenId);
771
772 dropLayout.setDropPending(true);
773 final Runnable onComplete = new Runnable() {
774 @Override
775 public void run() {
776 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
777 dropLayout.setDropPending(false);
778 }
779 };
780 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
781 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700782 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800783 return;
784 }
785
Sunny Goyald478c832016-04-01 12:04:16 -0700786 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
787 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700788 if (resultCode == RESULT_OK) {
789 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700790 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700791 }
792 // Leave the widget in the pending state if the user canceled the configure.
793 return;
794 }
795
Sunny Goyalaad90582015-07-09 14:22:50 -0700796 if (requestCode == REQUEST_CREATE_SHORTCUT) {
797 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700798 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
799 completeAdd(requestCode, data, -1, requestArgs);
800 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
801 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
802
Sunny Goyalaad90582015-07-09 14:22:50 -0700803 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700804 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
805 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800808 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800809 }
810
811 @Override
812 protected void onActivityResult(
813 final int requestCode, final int resultCode, final Intent data) {
814 handleActivityResult(requestCode, resultCode, data);
815 if (mLauncherCallbacks != null) {
816 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
817 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800818 }
819
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600820 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700821 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600822 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700823 PendingRequestArgs pendingArgs = mPendingRequestArgs;
824 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
825 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
826 setWaitingForResult(null);
827
Sunny Goyal28c6b962015-10-12 11:42:05 -0700828 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700829 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700830 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700831 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700832 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700833 Intent intent = pendingArgs.getPendingIntent();
834
Sunny Goyal28c6b962015-10-12 11:42:05 -0700835 if (grantResults.length > 0
836 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700837 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700838 } else {
839 // TODO: Show a snack bar with link to settings
840 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700841 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700842 }
843 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600844 if (mLauncherCallbacks != null) {
845 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
846 grantResults);
847 }
848 }
849
Adam Cohendb364c32014-05-20 14:23:40 -0700850 /**
851 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
852 *
853 * @param screenId the screen id to check
854 * @return the new screen, or screenId if it exists
855 */
856 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800857 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700858 if (dropLayout == null) {
859 // it's possible that the add screen was removed because it was
860 // empty and a re-bind occurred
861 mWorkspace.addExtraEmptyScreen();
862 return mWorkspace.commitExtraEmptyScreen();
863 } else {
864 return screenId;
865 }
866 }
867
Sunny Goyal2100c782016-08-22 16:00:03 -0700868 @Thunk void completeTwoStageWidgetDrop(
869 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
870 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800871 Runnable onCompleteRunnable = null;
872 int animationType = 0;
873
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700874 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800875 if (resultCode == RESULT_OK) {
876 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
877 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal2100c782016-08-22 16:00:03 -0700878 requestArgs.getWidgetProvider());
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700879 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800880 onCompleteRunnable = new Runnable() {
881 @Override
882 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700883 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700884 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
885 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800886 }
887 };
888 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800889 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800890 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800891 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700892 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700893 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700894 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
895 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700896 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700897 // The animated view may be null in the case of a rotation during widget configuration
898 onCompleteRunnable.run();
899 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900 }
901
902 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700903 protected void onStop() {
904 super.onStop();
905 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700906
907 if (mLauncherCallbacks != null) {
908 mLauncherCallbacks.onStop();
909 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700910
911 if (Utilities.isNycMR1OrAbove()) {
912 mAppWidgetHost.stopListening();
913 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700914 }
915
916 @Override
917 protected void onStart() {
918 super.onStart();
919 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700920
921 if (mLauncherCallbacks != null) {
922 mLauncherCallbacks.onStart();
923 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700924
925 if (Utilities.isNycMR1OrAbove()) {
926 mAppWidgetHost.startListening();
927 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700928 }
929
930 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200932 long startTime = 0;
933 if (DEBUG_RESUME_TIME) {
934 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400935 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200936 }
Adam Cohen9211d422014-10-07 18:14:53 -0700937
938 if (mLauncherCallbacks != null) {
939 mLauncherCallbacks.preOnResume();
940 }
941
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700943 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700944
Winson Chung4a2afa32012-07-19 14:53:05 -0700945 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700946 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700947 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800948 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700949 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700950 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700951 // view after launching an app, as they may be depending on the UI to be static to
952 // switch to another app, otherwise, if it was
Hyunyoung Songdd60ce42016-07-27 17:08:38 -0700953 showAppsView(false /* animated */, !launchedFromApp /* updatePredictedApps */,
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700954 mAppsView.shouldRestoreImeState() /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800955 } else if (mOnResumeState == State.WIDGETS) {
956 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700957 }
958 mOnResumeState = State.NONE;
959
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800960 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700961 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700962 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700963 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700964 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700966 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200967 // We might have postponed some bind calls until onResume (see waitUntilResume) --
968 // execute them here
969 long startTimeCallbacks = 0;
970 if (DEBUG_RESUME_TIME) {
971 startTimeCallbacks = System.currentTimeMillis();
972 }
973
Michael Jurka1e2f4652013-07-08 18:03:46 -0700974 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
975 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200976 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700977 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200978 if (DEBUG_RESUME_TIME) {
979 Log.d(TAG, "Time spent processing callbacks in onResume: " +
980 (System.currentTimeMillis() - startTimeCallbacks));
981 }
Michael Jurka447bf842013-05-15 14:52:15 +0200982 }
Michael Jurka54554252013-08-01 12:52:23 +0200983 if (mOnResumeCallbacks.size() > 0) {
984 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
985 mOnResumeCallbacks.get(i).run();
986 }
987 mOnResumeCallbacks.clear();
988 }
Winson Chunge4e50662012-01-23 14:45:13 -0800989
990 // Reset the pressed state of icons that were locked in the press state while activities
991 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800992 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800993 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800994 mWaitingForResume.setStayPressed(false);
995 }
Winson Chung82963d52013-10-09 11:20:57 -0700996
Adam Cohen06dff352012-06-01 17:17:08 -0700997 // It is possible that widgets can receive updates while launcher is not in the foreground.
998 // Consequently, the widgets will be inflated in the orientation of the foreground activity
999 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1000 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001001 if (!isWorkspaceLoading()) {
1002 getWorkspace().reinflateWidgetsIfNecessary();
1003 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001004
Michael Jurka447bf842013-05-15 14:52:15 +02001005 if (DEBUG_RESUME_TIME) {
1006 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1007 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001008
Adam Cohen4b66bf32015-09-18 12:15:19 -07001009 // We want to suppress callbacks about CustomContent being shown if we have just received
1010 // onNewIntent while the user was present within launcher. In that case, we post a call
1011 // to move the user to the main screen (which will occur after onResume). We don't want to
1012 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1013 // suppress here.
1014 if (mWorkspace.getCustomContentCallbacks() != null
1015 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001016 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001017 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001018 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1019 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001020 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001021 }
1022 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001023 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001024 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001025 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001026
Sunny Goyal756adbc2015-04-16 15:20:51 -07001027 if (!isWorkspaceLoading()) {
1028 // Process any items that were added while Launcher was away.
1029 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001030
1031 // Refresh shortcuts if the permission changed.
1032 mModel.refreshShortcutsIfRequired();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001033 }
Adam Cohen9211d422014-10-07 18:14:53 -07001034
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001035 if (shouldShowDiscoveryBounce()) {
1036 mAllAppsController.showDiscoveryBounce();
1037 }
1038 mIsResumeFromActionScreenOff = false;
Adam Cohen9211d422014-10-07 18:14:53 -07001039 if (mLauncherCallbacks != null) {
1040 mLauncherCallbacks.onResume();
1041 }
Adam Cohen06dff352012-06-01 17:17:08 -07001042 }
1043
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001045 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001046 // Ensure that items added to Launcher are queued until Launcher returns
1047 InstallShortcutReceiver.enableInstallQueue();
1048
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001049 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001050 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001051 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001052 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001053
1054 // We call onHide() aggressively. The custom content callbacks should be able to
1055 // debounce excess onHide calls.
1056 if (mWorkspace.getCustomContentCallbacks() != null) {
1057 mWorkspace.getCustomContentCallbacks().onHide();
1058 }
Romain Guycbb89e42009-06-08 15:52:54 -07001059
Adam Cohen9211d422014-10-07 18:14:53 -07001060 if (mLauncherCallbacks != null) {
1061 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001062 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001063 }
1064
1065 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001066 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1067 // by a onResume or by scrolling otherwise.
1068 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001069
1070 // Custom content is completely hidden
1071 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001072
1073 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1074 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001075
1076 // Indicates whether the user is allowed to scroll away from the custom content.
1077 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001078 }
1079
Adam Cohenc2d6e892014-10-16 09:49:24 -07001080 public interface LauncherOverlay {
1081
1082 /**
1083 * Touch interaction leading to overscroll has begun
1084 */
1085 public void onScrollInteractionBegin();
1086
1087 /**
1088 * Touch interaction related to overscroll has ended
1089 */
1090 public void onScrollInteractionEnd();
1091
1092 /**
1093 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1094 * screen (or in the case of RTL, the rightmost screen).
1095 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001096 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001097
1098 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001099 * Called when the launcher is ready to use the overlay
1100 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001101 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001102 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001103 }
1104
Jun Mukai8af0cd82015-05-12 12:32:12 -07001105 public interface LauncherSearchCallbacks {
1106 /**
1107 * Called when the search overlay is shown.
1108 */
1109 public void onSearchOverlayOpened();
1110
1111 /**
1112 * Called when the search overlay is dismissed.
1113 */
1114 public void onSearchOverlayClosed();
1115 }
1116
Adam Cohenc2d6e892014-10-16 09:49:24 -07001117 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001118 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001119 }
1120
1121 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1122
Sunny Goyalc86df472016-02-25 09:19:38 -08001123 public void onScrollChanged(float progress) {
1124 if (mWorkspace != null) {
1125 mWorkspace.onOverlayScrollChanged(progress);
1126 }
1127 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001128 }
1129
Jorim Jaggid017f882014-01-14 17:08:48 -08001130 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001131 if (mLauncherCallbacks != null) {
1132 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001133 } else {
1134 // On devices with a locked orientation, we will at least have the allow rotation
1135 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001136 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001137 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001138 }
1139
Adam Cohen9211d422014-10-07 18:14:53 -07001140 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001141 CustomContentCallbacks callbacks, String description) {
1142 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001143 }
1144
Adam Cohenedb40762013-07-18 16:45:45 -07001145 // The custom content needs to offset its content to account for the QSB
1146 public int getTopOffsetForCustomContent() {
1147 return mWorkspace.getPaddingTop();
1148 }
1149
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001150 @Override
1151 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001152 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001153 if (mModel.isCurrentCallbacks(this)) {
1154 mModel.stopLoader();
1155 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001156 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1157
Romain Guy13c2e7b2010-03-10 19:45:00 -08001158 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001159 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001160
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001161 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001162 @Override
1163 public void onWindowFocusChanged(boolean hasFocus) {
1164 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001165 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001166
1167 if (mLauncherCallbacks != null) {
1168 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1169 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001170 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001171
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172 private boolean acceptFilter() {
1173 final InputMethodManager inputManager = (InputMethodManager)
1174 getSystemService(Context.INPUT_METHOD_SERVICE);
1175 return !inputManager.isFullscreenMode();
1176 }
1177
1178 @Override
1179 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001180 final int uniChar = event.getUnicodeChar();
1181 final boolean handled = super.onKeyDown(keyCode, event);
1182 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1183 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1185 keyCode, event);
1186 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001187 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001188 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001189 // showSearchDialog()
1190 // If there are multiple keystrokes before the search dialog takes focus,
1191 // onSearchRequested() will be called for every keystroke,
1192 // but it is idempotent, so it's fine.
1193 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 }
1195 }
1196
Joe Onorato8a9625e2010-01-28 15:55:35 -08001197 // Eat the long press event so the keyboard doesn't come up.
1198 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1199 return true;
1200 }
1201
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 return handled;
1203 }
1204
Winson46163472015-09-22 17:31:56 -07001205 @Override
1206 public boolean onKeyUp(int keyCode, KeyEvent event) {
1207 if (keyCode == KeyEvent.KEYCODE_MENU) {
1208 // Ignore the menu key if we are currently dragging or are on the custom content screen
1209 if (!isOnCustomContent() && !mDragController.isDragging()) {
1210 // Close any open folders
1211 closeFolder();
1212
Tony Wickham49c8d292016-07-01 11:44:34 -07001213 // Close any shortcuts containers
1214 closeShortcutsContainer();
1215
Winson46163472015-09-22 17:31:56 -07001216 // Stop resizing any widgets
1217 mWorkspace.exitWidgetResizeMode();
1218
1219 // Show the overview mode if we are on the workspace
1220 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1221 !mWorkspace.isSwitchingState()) {
1222 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001223 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001224 }
1225 }
1226 return true;
1227 }
1228 return super.onKeyUp(keyCode, event);
1229 }
1230
Karl Rosaen138a0412009-04-23 19:00:21 -07001231 private String getTypedText() {
1232 return mDefaultKeySsb.toString();
1233 }
1234
Sunny Goyal6f28e712016-09-13 14:19:29 -07001235 @Override
1236 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001237 mDefaultKeySsb.clear();
1238 mDefaultKeySsb.clearSpans();
1239 Selection.setSelection(mDefaultKeySsb, 0);
1240 }
1241
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001243 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1244 * State
1245 */
1246 private static State intToState(int stateOrdinal) {
1247 State state = State.WORKSPACE;
1248 final State[] stateValues = State.values();
1249 for (int i = 0; i < stateValues.length; i++) {
1250 if (stateValues[i].ordinal() == stateOrdinal) {
1251 state = stateValues[i];
1252 break;
1253 }
1254 }
1255 return state;
1256 }
1257
1258 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 * Restores the previous state, if it exists.
1260 *
1261 * @param savedState The previous state.
1262 */
1263 private void restoreState(Bundle savedState) {
1264 if (savedState == null) {
1265 return;
1266 }
1267
Michael Jurka883f55b2010-10-21 15:47:14 -07001268 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001269 if (state == State.APPS || state == State.WIDGETS) {
1270 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001271 }
1272
Adam Cohen21cd0022013-10-09 18:57:02 -07001273 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1274 PagedView.INVALID_RESTORE_PAGE);
1275 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001276 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 }
1278
Sunny Goyal2100c782016-08-22 16:00:03 -07001279 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1280 if (requestArgs != null) {
1281 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001283
1284 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 }
1286
1287 /**
1288 * Finds all the views we need and configure them properly.
1289 */
1290 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001291 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001292 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001293 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001294 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyal6178f132016-07-11 17:30:03 -07001295 mQsbContainer = mDragLayer.findViewById(mDeviceProfile.isVerticalBarLayout()
1296 ? R.id.workspace_blocked_row : R.id.qsb_container);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001297 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001298
Sunny Goyal784f9c32016-03-23 16:56:54 -07001299 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1300 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1301 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302
Winson Chung4c98d922011-05-31 16:50:48 -07001303 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001304 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001305
Winson Chung3d503fb2011-07-13 17:25:49 -07001306 // Setup the hotseat
1307 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1308 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001309 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001310 }
1311
Winsone9f27272015-10-13 10:47:51 -07001312 // Setup the overview panel
1313 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001314
Winson Chung4c98d922011-05-31 16:50:48 -07001315 // Setup the workspace
1316 mWorkspace.setHapticFeedbackEnabled(false);
1317 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001318 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001319 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1320 // default state, otherwise we will update to the wrong offsets in RTL
1321 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001322 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001323 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001324
Tony Wickham34d2c912015-09-11 09:27:58 -07001325 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001326 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001327
Winson Chungef7f8742015-06-04 17:18:17 -07001328 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001329 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001330 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001331 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1332 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1333 } else {
1334 mAppsView.setSearchBarController(new DefaultAppSearchController());
1335 }
Craig Mautner360310b2012-10-26 15:13:08 -07001336
Winson Chung3d503fb2011-07-13 17:25:49 -07001337 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001338 mDragController.setDragScoller(mWorkspace);
1339 mDragController.setScrollView(mDragLayer);
1340 mDragController.setMoveTarget(mWorkspace);
1341 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001342 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001343
Peter Schiller310a9882016-06-30 13:52:36 -07001344 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1345 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1346 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001347
Sunny Goyal322d5562015-06-25 19:35:49 -07001348 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1349 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001350 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 }
1352
Winsone9f27272015-10-13 10:47:51 -07001353 private void setupOverviewPanel() {
1354 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1355
1356 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1357 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1358 @Override
1359 public boolean onLongClick(View v) {
1360 return v.performClick();
1361 }
1362 };
1363
1364 // Bind wallpaper button actions
1365 View wallpaperButton = findViewById(R.id.wallpaper_button);
1366 wallpaperButton.setOnClickListener(new OnClickListener() {
1367 @Override
1368 public void onClick(View view) {
1369 if (!mWorkspace.isSwitchingState()) {
1370 onClickWallpaperPicker(view);
1371 }
1372 }
1373 });
1374 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1375 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1376
1377 // Bind widget button actions
1378 mWidgetsButton = findViewById(R.id.widget_button);
1379 mWidgetsButton.setOnClickListener(new OnClickListener() {
1380 @Override
1381 public void onClick(View view) {
1382 if (!mWorkspace.isSwitchingState()) {
1383 onClickAddWidgetButton(view);
1384 }
1385 }
1386 });
1387 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1388 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1389
1390 // Bind settings actions
1391 View settingsButton = findViewById(R.id.settings_button);
1392 boolean hasSettings = hasSettings();
1393 if (hasSettings) {
1394 settingsButton.setOnClickListener(new OnClickListener() {
1395 @Override
1396 public void onClick(View view) {
1397 if (!mWorkspace.isSwitchingState()) {
1398 onClickSettingsButton(view);
1399 }
1400 }
1401 });
1402 settingsButton.setOnLongClickListener(performClickOnLongClick);
1403 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1404 } else {
1405 settingsButton.setVisibility(View.GONE);
1406 }
1407
1408 mOverviewPanel.setAlpha(0f);
1409 }
1410
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001412 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001413 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001414 */
1415 public void setAllAppsButton(View allAppsButton) {
1416 mAllAppsButton = allAppsButton;
1417 }
1418
Sunny Goyalbb011da2016-06-15 15:42:29 -07001419 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001420 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001421 }
1422
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001423 public View getWidgetsButton() {
1424 return mWidgetsButton;
1425 }
1426
Anjali Koppal5ad44842014-03-10 20:34:39 -07001427 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 * Creates a view representing a shortcut.
1429 *
1430 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001431 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001432 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001433 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434 }
1435
1436 /**
1437 * Creates a view representing a shortcut inflated from the specified resource.
1438 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001439 * @param parent The group the shortcut belongs to.
1440 * @param info The data structure describing the shortcut.
1441 *
1442 * @return A View inflated from layoutResId.
1443 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001444 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001445 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001446 parent, false);
1447 favorite.applyFromShortcutInfo(info, mIconCache);
1448 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001450 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 return favorite;
1452 }
1453
1454 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 * Add a shortcut to the workspace.
1456 *
1457 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001459 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001460 int cellY, PendingRequestArgs args) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001461 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001462 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001463
Sunny Goyal5c97f512015-05-19 16:03:28 -07001464 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001465 if (info == null || args.getRequestCode() != REQUEST_CREATE_SHORTCUT ||
1466 args.getPendingIntent().getComponent() == null) {
1467 return;
1468 }
1469 if (!PackageManagerHelper.hasPermissionForActivity(
1470 this, info.intent, args.getPendingIntent().getComponent().getPackageName())) {
1471 // The app is trying to add a shortcut without sufficient permissions
1472 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
Adam Cohend9198822011-11-22 16:42:47 -08001473 return;
1474 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001475 final View view = createShortcut(info);
1476
Sunny Goyal5c97f512015-05-19 16:03:28 -07001477 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001478 // First we check if we already know the exact location where we want to add this item.
1479 if (cellX >= 0 && cellY >= 0) {
1480 cellXY[0] = cellX;
1481 cellXY[1] = cellY;
1482 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001483
1484 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001485 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001486 true, null,null)) {
1487 return;
1488 }
1489 DragObject dragObject = new DragObject();
1490 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001491 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1492 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001493 return;
1494 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001495 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001496 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001497 }
1498
1499 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001500 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001501 return;
1502 }
1503
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001504 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505
Sunny Goyal2100c782016-08-22 16:00:03 -07001506 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
1507 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 }
1509
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001511 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001513 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001515 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001516 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1517
Adam Cohened66b2b2012-01-23 17:28:51 -08001518 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001519 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001520 }
Romain Guycbb89e42009-06-08 15:52:54 -07001521
Adam Cohen59400422014-03-05 18:07:04 -08001522 if (appWidgetInfo.isCustomWidget) {
1523 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001524 }
1525
Adam Cohen59400422014-03-05 18:07:04 -08001526 LauncherAppWidgetInfo launcherInfo;
1527 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001528 launcherInfo.spanX = itemInfo.spanX;
1529 launcherInfo.spanY = itemInfo.spanY;
1530 launcherInfo.minSpanX = itemInfo.minSpanX;
1531 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001532 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001533
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001535 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536
Sunny Goyal2100c782016-08-22 16:00:03 -07001537 if (hostView == null) {
1538 // Perform actual inflation because we're live
1539 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001541 hostView.setVisibility(View.VISIBLE);
1542 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 }
Romain Guycbb89e42009-06-08 15:52:54 -07001544
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001545 private void addAppWidgetToWorkspace(
1546 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001547 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001548 hostView.setTag(item);
1549 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001550
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001551 hostView.setFocusable(true);
1552 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001553
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001554 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001555 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1556
1557 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001558 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001559 }
1560 }
1561
Adam Cohended9f8d2010-11-03 13:25:16 -07001562 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1563 @Override
1564 public void onReceive(Context context, Intent intent) {
1565 final String action = intent.getAction();
1566 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1567 mUserPresent = false;
Sunny Goyal6ad72f02016-09-23 11:01:10 -07001568 mDragLayer.clearResizeFrame();
Winson Chungb745afb2015-03-02 11:51:23 -08001569 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001570
Winson Chungc100e8e2011-08-09 16:02:43 -07001571 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001572 // processing a multi-step drop
Sunny Goyal2100c782016-08-22 16:00:03 -07001573 if (mAppsView != null && mWidgetsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001574 if (!showWorkspace(false)) {
1575 // If we are already on the workspace, then manually reset all apps
1576 mAppsView.reset();
1577 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001578 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001579 mIsResumeFromActionScreenOff = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001580 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1581 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001582 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001583 }
1584 }
1585 };
1586
1587 @Override
1588 public void onAttachedToWindow() {
1589 super.onAttachedToWindow();
1590
1591 // Listen for broadcasts related to user-presence
1592 final IntentFilter filter = new IntentFilter();
1593 filter.addAction(Intent.ACTION_SCREEN_OFF);
1594 filter.addAction(Intent.ACTION_USER_PRESENT);
1595 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001596 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001597 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001598 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001599
1600 if (mLauncherCallbacks != null) {
1601 mLauncherCallbacks.onAttachedToWindow();
1602 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001603 }
1604
1605 @Override
1606 public void onDetachedFromWindow() {
1607 super.onDetachedFromWindow();
1608 mVisible = false;
1609
Adam Cohend113e0c2010-11-11 10:48:05 -08001610 if (mAttached) {
1611 unregisterReceiver(mReceiver);
1612 mAttached = false;
1613 }
Winson Chungb745afb2015-03-02 11:51:23 -08001614 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001615
1616 if (mLauncherCallbacks != null) {
1617 mLauncherCallbacks.onDetachedFromWindow();
1618 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001619 }
1620
1621 public void onWindowVisibilityChanged(int visibility) {
1622 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001623 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001624 // The following code used to be in onResume, but it turns out onResume is called when
1625 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1626 // is a more appropriate event to handle
1627 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001628 if (!mWorkspaceLoading) {
1629 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001630 // We want to let Launcher draw itself at least once before we force it to build
1631 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001632 // apps is nice and speedy.
1633 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001634 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001635 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001636 if (mStarted) return;
1637 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001638 // We delay the layer building a bit in order to give
1639 // other message processing a time to run. In particular
1640 // this avoids a delay in hiding the IME if it was
1641 // currently shown, because doing that may involve
1642 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001643 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001644 final ViewTreeObserver.OnDrawListener listener = this;
1645 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001646 public void run() {
1647 if (mWorkspace != null &&
1648 mWorkspace.getViewTreeObserver() != null) {
1649 mWorkspace.getViewTreeObserver().
1650 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001651 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001652 }
1653 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001654 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001655 }
1656 });
1657 }
1658 clearTypedText();
1659 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001660 }
1661
Adam Cohen091440a2015-03-18 14:16:05 -07001662 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001663 mHandler.removeMessages(ADVANCE_MSG);
1664 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1665 mHandler.sendMessageDelayed(msg, delay);
1666 mAutoAdvanceSentTime = System.currentTimeMillis();
1667 }
1668
Adam Cohen091440a2015-03-18 14:16:05 -07001669 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001670 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1671 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1672 mAutoAdvanceRunning = autoAdvanceRunning;
1673 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001674 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001675 sendAdvanceMessage(delay);
1676 } else {
1677 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001678 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001679 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1680 }
1681 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001682 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001683 }
1684 }
1685 }
1686
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001687 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1688
Adam Cohended9f8d2010-11-03 13:25:16 -07001689 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001690 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001691 if (msg.what == ADVANCE_MSG) {
1692 int i = 0;
1693 for (View key: mWidgetsToAdvance.keySet()) {
1694 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001695 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001696 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001697 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001698 public void run() {
1699 ((Advanceable) v).advance();
1700 }
1701 }, delay);
1702 }
1703 i++;
1704 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001705 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001706 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001707 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001708 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001709 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001710
Winsonc0b52fe2015-09-09 16:38:15 -07001711 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001712 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001713 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1714 if (v instanceof Advanceable) {
1715 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001716 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001717 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001718 }
1719 }
1720
Winsonc0b52fe2015-09-09 16:38:15 -07001721 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001722 if (mWidgetsToAdvance.containsKey(hostView)) {
1723 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001724 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001725 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001726 }
1727
Hyunyoung Song3f471442015-04-08 19:01:34 -07001728 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001729 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1730 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001731 }
1732
Winson Chunga6945242014-01-08 14:04:34 -08001733 public DragLayer getDragLayer() {
1734 return mDragLayer;
1735 }
1736
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001737 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001738 return mAppsView;
1739 }
1740
Hyunyoung Song3f471442015-04-08 19:01:34 -07001741 public WidgetsContainerView getWidgetsView() {
1742 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001743 }
1744
Winson Chunga6945242014-01-08 14:04:34 -08001745 public Workspace getWorkspace() {
1746 return mWorkspace;
1747 }
1748
Sunny Goyal6178f132016-07-11 17:30:03 -07001749 public View getQsbContainer() {
1750 return mQsbContainer;
1751 }
1752
Winson Chunga6945242014-01-08 14:04:34 -08001753 public Hotseat getHotseat() {
1754 return mHotseat;
1755 }
1756
Jorim Jaggid017f882014-01-14 17:08:48 -08001757 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001758 return mOverviewPanel;
1759 }
1760
Sunny Goyal47328fd2016-05-25 18:56:41 -07001761 public DropTargetBar getDropTargetBar() {
1762 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001763 }
1764
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001765 public LauncherAppWidgetHost getAppWidgetHost() {
1766 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001767 }
Romain Guycbb89e42009-06-08 15:52:54 -07001768
Winson Chunga9abd0e2010-10-27 17:18:37 -07001769 public LauncherModel getModel() {
1770 return mModel;
1771 }
1772
Adam Cohen79d90c52016-04-22 13:29:20 -07001773 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001774 return mSharedPrefs;
1775 }
1776
Adam Cohen2e6da152015-05-06 11:42:25 -07001777 public DeviceProfile getDeviceProfile() {
1778 return mDeviceProfile;
1779 }
1780
Bjorn Bringertc459e522013-06-07 19:36:01 +01001781 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001782 getWindow().closeAllPanels();
1783
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001784 // Whatever we were doing is hereby canceled.
Sunny Goyal2100c782016-08-22 16:00:03 -07001785 setWaitingForResult(null);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001786 }
1787
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001788 @Override
1789 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001790 long startTime = 0;
1791 if (DEBUG_RESUME_TIME) {
1792 startTime = System.currentTimeMillis();
1793 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 super.onNewIntent(intent);
1795
1796 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001797 Folder openFolder = mWorkspace.getOpenFolder();
1798 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1799 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1800 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1801 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1802 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001803 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001804 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001805
Adam Cohen6fecd412013-10-02 17:41:50 -07001806 if (mWorkspace == null) {
1807 // Can be cases where mWorkspace is null, this prevents a NPE
1808 return;
1809 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001810 // In all these cases, only animate if we're already on home
1811 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001812
Sunny Goyal935fca12015-10-13 10:19:01 -07001813 closeFolder(alreadyOnHome);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001814 closeShortcutsContainer(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001815 exitSpringLoadedDragMode();
1816
1817 // If we are already on home, then just animate back to the workspace,
1818 // otherwise, just wait until onResume to set the state back to Workspace
1819 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001820 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001821 } else {
1822 mOnResumeState = State.WORKSPACE;
1823 }
1824
1825 final View v = getWindow().peekDecorView();
1826 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001827 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001828 INPUT_METHOD_SERVICE);
1829 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1830 }
1831
Winson Chungb745afb2015-03-02 11:51:23 -08001832 // Reset the apps view
1833 if (!alreadyOnHome && mAppsView != null) {
1834 mAppsView.scrollToTop();
1835 }
1836
Hyunyoung Song3f471442015-04-08 19:01:34 -07001837 // Reset the widgets view
1838 if (!alreadyOnHome && mWidgetsView != null) {
1839 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001840 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001841
Adam Cohen9211d422014-10-07 18:14:53 -07001842 if (mLauncherCallbacks != null) {
1843 mLauncherCallbacks.onHomeIntent();
1844 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 }
Adam Cohened307df2013-10-02 09:37:31 -07001846
Adam Cohen9211d422014-10-07 18:14:53 -07001847 if (mLauncherCallbacks != null) {
1848 mLauncherCallbacks.onNewIntent(intent);
1849 }
Winson15f8b172015-08-19 11:02:39 -07001850
1851 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1852 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1853 // animation.
1854 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001855 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1856 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001857 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1858 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001859
1860 // We use this flag to suppress noisy callbacks above custom content state
1861 // from onResume.
1862 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001863 mWorkspace.post(new Runnable() {
1864 @Override
1865 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001866 if (mWorkspace != null) {
1867 mWorkspace.moveToDefaultScreen(true);
1868 }
Winson15f8b172015-08-19 11:02:39 -07001869 }
1870 });
1871 }
1872 }
1873
1874 if (DEBUG_RESUME_TIME) {
1875 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1876 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001877 }
1878
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001879 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001880 public void onRestoreInstanceState(Bundle state) {
1881 super.onRestoreInstanceState(state);
1882 for (int page: mSynchronouslyBoundPages) {
1883 mWorkspace.restoreInstanceStateForChild(page);
1884 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 }
1886
1887 @Override
1888 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001889 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001890 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1891 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001892
Adam Cohen21cd0022013-10-09 18:57:02 -07001893 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001894 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895
Michael Jurka883f55b2010-10-21 15:47:14 -07001896 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001897 // We close any open folder since it will not be re-opened, and we need to make sure
1898 // this state is reflected.
1899 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1900 closeFolder(false);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001901 closeShortcutsContainer(false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001902
Sunny Goyal2100c782016-08-22 16:00:03 -07001903 if (mPendingRequestArgs != null) {
1904 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1905 }
1906 if (mPendingActivityResult != null) {
1907 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 }
1909
Adam Cohen9211d422014-10-07 18:14:53 -07001910 if (mLauncherCallbacks != null) {
1911 mLauncherCallbacks.onSaveInstanceState(outState);
1912 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913 }
1914
1915 @Override
1916 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001918
Winson Chunge7a03942011-08-05 15:05:12 -07001919 // Remove all pending runnables
1920 mHandler.removeMessages(ADVANCE_MSG);
1921 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001922 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001923 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001924
Winson Chungcd2b0142011-06-08 16:02:26 -07001925 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001926 // It's possible to receive onDestroy after a new Launcher activity has
1927 // been created. In this case, don't interfere with the new Launcher.
1928 if (mModel.isCurrentCallbacks(this)) {
1929 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001930 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001931 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001932
Sunny Goyal745bad92016-05-02 10:54:12 -07001933 if (mRotationPrefChangeHandler != null) {
1934 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1935 }
1936
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001938 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001939 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001940 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001942 mAppWidgetHost = null;
1943
1944 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945
1946 TextKeyListener.getInstance().release();
1947
Tony Wickhame2217252016-03-22 16:34:23 -07001948 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1949 .removeAccessibilityStateChangeListener(this);
1950
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001951 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001952
Michael Jurka2ecf9952012-06-18 12:52:28 -07001953 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001954
1955 if (mLauncherCallbacks != null) {
1956 mLauncherCallbacks.onDestroy();
1957 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 }
1959
Sunny Goyalae502842016-06-17 08:43:56 -07001960 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1961 return mAccessibilityDelegate;
1962 }
1963
Adam Cohena9cf38f2011-05-02 15:36:58 -07001964 public DragController getDragController() {
1965 return mDragController;
1966 }
1967
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001969 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001970 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001971 }
1972
1973 @Override
1974 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1975 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001976 try {
1977 super.startIntentSenderForResult(intent, requestCode,
1978 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1979 } catch (IntentSender.SendIntentException e) {
1980 throw new ActivityNotFoundException();
1981 }
1982 }
1983
Winson Chung70d72102011-08-12 11:24:35 -07001984 /**
1985 * Indicates that we want global search for this activity by setting the globalSearch
1986 * argument for {@link #startSearch} to true.
1987 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001989 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001991
Karl Rosaen138a0412009-04-23 19:00:21 -07001992 if (initialQuery == null) {
1993 // Use any text typed in the launcher as the initial query
1994 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001995 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 if (appSearchData == null) {
1997 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001998 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002000
Sunny Goyal6f28e712016-09-13 14:19:29 -07002001 if (mLauncherCallbacks == null ||
2002 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
2003 // Starting search from the callbacks failed. Start the default global search.
2004 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01002005 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002006
2007 // We need to show the workspace after starting the search
2008 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002009 }
2010
Ian Parkinson047036e2014-09-01 15:40:53 +01002011 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07002012 * Starts the global search activity. This code is a copied from SearchManager
2013 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07002014 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002015 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002016 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002017 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2018 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2019 if (globalSearchActivity == null) {
2020 Log.w(TAG, "No global search activity found.");
2021 return;
2022 }
2023 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2024 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2025 intent.setComponent(globalSearchActivity);
2026 // Make sure that we have a Bundle to put source in
2027 if (appSearchData == null) {
2028 appSearchData = new Bundle();
2029 } else {
2030 appSearchData = new Bundle(appSearchData);
2031 }
Adam Cohen9211d422014-10-07 18:14:53 -07002032 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002033 if (!appSearchData.containsKey("source")) {
2034 appSearchData.putString("source", getPackageName());
2035 }
2036 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2037 if (!TextUtils.isEmpty(initialQuery)) {
2038 intent.putExtra(SearchManager.QUERY, initialQuery);
2039 }
2040 if (selectInitialQuery) {
2041 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2042 }
2043 intent.setSourceBounds(sourceBounds);
2044 try {
2045 startActivity(intent);
2046 } catch (ActivityNotFoundException ex) {
2047 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2048 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049 }
2050
Yura4f93ec62014-02-04 14:15:21 +00002051 public boolean isOnCustomContent() {
2052 return mWorkspace.isOnOrMovingToCustomContent();
2053 }
2054
Winson Chung70d72102011-08-12 11:24:35 -07002055 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002056 public boolean onPrepareOptionsMenu(Menu menu) {
2057 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002058 if (mLauncherCallbacks != null) {
2059 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2060 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002061 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002062 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002064 @Override
2065 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002066 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002067 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002068 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002069 }
2070
Joe Onorato9c1289c2009-08-17 11:03:03 -04002071 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07002072 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002073 }
2074
Adam Cohen517a7f52014-03-01 12:12:59 -08002075 public boolean isWorkspaceLoading() {
2076 return mWorkspaceLoading;
2077 }
2078
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002079 private void setWorkspaceLoading(boolean value) {
2080 boolean isLocked = isWorkspaceLocked();
2081 mWorkspaceLoading = value;
2082 if (isLocked != isWorkspaceLocked()) {
2083 onWorkspaceLockedChanged();
2084 }
2085 }
2086
Sunny Goyal2100c782016-08-22 16:00:03 -07002087 private void setWaitingForResult(PendingRequestArgs args) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002088 boolean isLocked = isWorkspaceLocked();
Sunny Goyal2100c782016-08-22 16:00:03 -07002089 mPendingRequestArgs = args;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002090 if (isLocked != isWorkspaceLocked()) {
2091 onWorkspaceLockedChanged();
2092 }
2093 }
2094
Adam Cohen9211d422014-10-07 18:14:53 -07002095 protected void onWorkspaceLockedChanged() {
2096 if (mLauncherCallbacks != null) {
2097 mLauncherCallbacks.onWorkspaceLockedChanged();
2098 }
2099 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002100
Sunny Goyal2100c782016-08-22 16:00:03 -07002101 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
2102 LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002103 if (LOGD) {
2104 Log.d(TAG, "Adding widget from drop");
2105 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002106 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2107 }
2108
Sunny Goyal2100c782016-08-22 16:00:03 -07002109 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
2110 AppWidgetHostView boundWidget, LauncherAppWidgetProviderInfo appWidgetInfo,
Adam Cohen59400422014-03-05 18:07:04 -08002111 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002112 if (appWidgetInfo.configure != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002113 setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, appWidgetInfo, info));
Michael Jurkaaf442092010-06-10 17:01:57 -07002114
Bjorn Bringert7984c942009-12-09 15:38:25 +00002115 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002116 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2117 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002118 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002119 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002120 Runnable onComplete = new Runnable() {
2121 @Override
2122 public void run() {
2123 // Exit spring loaded mode if necessary after adding the widget
2124 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2125 null);
2126 }
2127 };
Sunny Goyal2100c782016-08-22 16:00:03 -07002128 completeAddAppWidget(appWidgetId, info, boundWidget, appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002129 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002130 }
2131 }
Romain Guycbb89e42009-06-08 15:52:54 -07002132
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002133 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002134 // Close any folders that may be open.
2135 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002136 mWorkspace.moveToCustomContentScreen(animate);
2137 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002138
2139 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2140 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002141 info.container = container;
2142 info.screenId = screenId;
2143 if (cell != null) {
2144 info.cellX = cell[0];
2145 info.cellY = cell[1];
2146 }
2147 info.spanX = spanX;
2148 info.spanY = spanY;
2149
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002150 switch (info.itemType) {
2151 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2152 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07002153 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002154 break;
2155 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal2100c782016-08-22 16:00:03 -07002156 processShortcutFromDrop(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002157 break;
2158 default:
2159 throw new IllegalStateException("Unknown item type: " + info.itemType);
2160 }
2161 }
2162
Adam Cohenfbba09b2011-07-18 21:43:05 -07002163 /**
2164 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002165 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002166 private void processShortcutFromDrop(PendingAddItemInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07002167 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
2168 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
2169 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002170 }
2171
Adam Cohenfbba09b2011-07-18 21:43:05 -07002172 /**
2173 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002174 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002175 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002176 AppWidgetHostView hostView = info.boundWidget;
2177 int appWidgetId;
2178 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002179 // In the case where we've prebound the widget, we remove it from the DragLayer
2180 if (LOGD) {
2181 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2182 }
2183 getDragLayer().removeView(hostView);
2184
Adam Cohened66b2b2012-01-23 17:28:51 -08002185 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002186 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002187
2188 // Clear the boundWidget so that it doesn't get destroyed.
2189 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002190 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002191 // In this case, we either need to start an activity to get permission to bind
2192 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002193 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002194 Bundle options = info.bindOptions;
2195
Sunny Goyalffe83f12014-08-14 17:39:34 -07002196 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2197 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002198 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002199 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002200 } else {
Sunny Goyal2100c782016-08-22 16:00:03 -07002201 setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, info.info, info));
Michael Jurka8b805b12012-04-18 14:23:14 -07002202 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2203 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2204 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyal2100c782016-08-22 16:00:03 -07002205 mAppWidgetManager.getUser(info.info)
Sunny Goyalffe83f12014-08-14 17:39:34 -07002206 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002207 // TODO: we need to make sure that this accounts for the options bundle.
2208 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002209 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2210 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002211 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002212 }
2213
Adam Cohendcd297f2013-06-18 13:13:40 -07002214 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002215 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002216 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002217 folderInfo.title = getText(R.string.folder_name);
2218
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002219 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002220 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2221 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002222
2223 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002224 FolderIcon newFolder =
2225 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002226 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002227 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002228 // Force measure the new folder icon
2229 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2230 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002231 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002232 }
Romain Guycbb89e42009-06-08 15:52:54 -07002233
Winsonc0b52fe2015-09-09 16:38:15 -07002234 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002235 * Unbinds the view for the specified item, and removes the item and all its children.
2236 *
2237 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002238 * @param itemInfo the {@link ItemInfo} for this view.
2239 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002240 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002241 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002242 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002243 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002244 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2245 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002246 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002247 } else {
2248 mWorkspace.removeWorkspaceItem(v);
2249 }
Winsonc0b52fe2015-09-09 16:38:15 -07002250 if (deleteFromDb) {
2251 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2252 }
2253 } else if (itemInfo instanceof FolderInfo) {
2254 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002255 if (v instanceof FolderIcon) {
2256 ((FolderIcon) v).removeListeners();
2257 }
Winsonc0b52fe2015-09-09 16:38:15 -07002258 mWorkspace.removeWorkspaceItem(v);
2259 if (deleteFromDb) {
2260 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2261 }
2262 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2263 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002264 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002265 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002266 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002267 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002268 }
2269 } else {
2270 return false;
2271 }
2272 return true;
2273 }
2274
2275 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002276 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002277 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002278 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002279 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002280 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002281 // Deleting an app widget ID is a void call but writes to disk before returning
2282 // to the caller...
2283 new AsyncTask<Void, Void, Void>() {
2284 public Void doInBackground(Void ... args) {
2285 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2286 return null;
2287 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002288 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002289 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002290 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002291 }
2292
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002293 @Override
2294 public boolean dispatchKeyEvent(KeyEvent event) {
2295 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2296 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002297 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002298 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002299 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002300 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002301 dumpState();
2302 return true;
2303 }
2304 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002305 }
2306 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2307 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002308 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002309 return true;
2310 }
2311 }
2312
2313 return super.dispatchKeyEvent(event);
2314 }
2315
Joe Onorato88ec0992009-11-19 13:16:06 -08002316 @Override
2317 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002318 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2319 return;
2320 }
2321
Sunny Goyal45478022015-06-08 16:52:41 -07002322 if (mDragController.isDragging()) {
2323 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002324 return;
2325 }
2326
Tony Wickham49c8d292016-07-01 11:44:34 -07002327 if (getOpenShortcutsContainer() != null) {
2328 closeShortcutsContainer();
2329 } else if (isAppsViewVisible()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002330 showWorkspace(true);
2331 } else if (isWidgetsViewVisible()) {
2332 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002333 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002334 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002335 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002336 Folder openFolder = mWorkspace.getOpenFolder();
2337 if (openFolder.isEditingName()) {
2338 openFolder.dismissEditingName();
2339 } else {
2340 closeFolder();
2341 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002342 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002343 mWorkspace.exitWidgetResizeMode();
2344
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002345 // Back button is a no-op here, but give at least some feedback for the button press
2346 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002347 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002348 }
2349
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002350 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002351 * Launches the intent referred by the clicked shortcut.
2352 *
2353 * @param v The view representing the clicked shortcut.
2354 */
2355 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002356 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2357 // view has detached (it's possible for this to happen if the view is removed mid touch).
2358 if (v.getWindowToken() == null) {
2359 return;
2360 }
2361
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002362 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002363 return;
2364 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002365
Adam Cohen1697b792013-09-17 19:08:21 -07002366 if (v instanceof Workspace) {
2367 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002368 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002369 }
2370 return;
2371 }
2372
2373 if (v instanceof CellLayout) {
2374 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002375 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2376 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002377 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002378 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002379 }
2380
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002381 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002382 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002383 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002384 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002385 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002386 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002387 }
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002388 } else if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2389 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002390 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002391 } else if (tag instanceof AppInfo) {
2392 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002393 } else if (tag instanceof LauncherAppWidgetInfo) {
2394 if (v instanceof PendingAppWidgetHostView) {
2395 onClickPendingWidget((PendingAppWidgetHostView) v);
2396 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002397 }
2398 }
2399
Sunny Goyal70660032015-05-14 00:07:08 -07002400 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002401 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002402 return false;
2403 }
2404
Michael Jurkaaf442092010-06-10 17:01:57 -07002405 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002406 * Event handler for the app widget view which has not fully restored.
2407 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002408 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002409 if (mIsSafeModeEnabled) {
2410 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2411 return;
2412 }
2413
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002414 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002415 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002416 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2417 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2418 // This should not happen, as we make sure that an Id is allocated during bind.
2419 return;
2420 }
2421 LauncherAppWidgetProviderInfo appWidgetInfo =
2422 mAppWidgetManager.findProvider(info.providerName, info.user);
2423 if (appWidgetInfo != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002424 setWaitingForResult(PendingRequestArgs
2425 .forWidgetInfo(info.appWidgetId, appWidgetInfo, info));
Sunny Goyalff572272014-07-23 13:58:07 -07002426
Sunny Goyald478c832016-04-01 12:04:16 -07002427 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
Sunny Goyal2100c782016-08-22 16:00:03 -07002428 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, info.appWidgetId);
Sunny Goyald478c832016-04-01 12:04:16 -07002429 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07002430 mAppWidgetManager.getUser(appWidgetInfo)
Sunny Goyald478c832016-04-01 12:04:16 -07002431 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2432 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2433 }
2434 } else {
2435 LauncherAppWidgetProviderInfo appWidgetInfo =
2436 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2437 if (appWidgetInfo != null) {
2438 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2439 }
Sunny Goyalff572272014-07-23 13:58:07 -07002440 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002441 } else if (info.installProgress < 0) {
2442 // The install has not been queued
2443 final String packageName = info.providerName.getPackageName();
2444 showBrokenAppInstallDialog(packageName,
2445 new DialogInterface.OnClickListener() {
2446 public void onClick(DialogInterface dialog, int id) {
2447 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2448 }
2449 });
2450 } else {
2451 // Download has started.
2452 final String packageName = info.providerName.getPackageName();
2453 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002454 }
2455 }
2456
Sunny Goyald478c832016-04-01 12:04:16 -07002457 private void startRestoredWidgetReconfigActivity(
2458 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002459 setWaitingForResult(PendingRequestArgs.forWidgetInfo(info.appWidgetId, provider, info));
Sunny Goyald478c832016-04-01 12:04:16 -07002460 mAppWidgetManager.startConfigActivity(provider,
2461 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2462 }
2463
Sunny Goyalff572272014-07-23 13:58:07 -07002464 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002465 * Event handler for the "grid" button that appears on the home screen, which
2466 * enters all apps mode.
2467 *
2468 * @param v The view that was clicked.
2469 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002470 protected void onClickAllAppsButton(View v) {
2471 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002472 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002473 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.TAP,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002474 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002475 showAppsView(true /* animated */, true /* updatePredictedApps */,
2476 false /* focusSearchBar */);
Winson Chung76648c52015-07-10 14:33:23 -07002477 }
2478 }
2479
2480 protected void onLongClickAllAppsButton(View v) {
2481 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2482 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002483 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.LONGPRESS,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002484 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002485 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07002486 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002487 }
2488 }
2489
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002490 private void showBrokenAppInstallDialog(final String packageName,
2491 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002492 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002493 .setTitle(R.string.abandoned_promises_title)
2494 .setMessage(R.string.abandoned_promise_explanation)
2495 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2496 .setNeutralButton(R.string.abandoned_clean_this,
2497 new DialogInterface.OnClickListener() {
2498 public void onClick(DialogInterface dialog, int id) {
2499 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2500 mWorkspace.removeAbandonedPromise(packageName, user);
2501 }
2502 })
2503 .create().show();
2504 return;
2505 }
2506
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002507 /**
2508 * Event handler for an app shortcut click.
2509 *
2510 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2511 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002512 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002513 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2514 Object tag = v.getTag();
2515 if (!(tag instanceof ShortcutInfo)) {
2516 throw new IllegalArgumentException("Input must be a Shortcut");
2517 }
2518
2519 // Open shortcut
2520 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002521
2522 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002523 if ((shortcut.isDisabled &
2524 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2525 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2526 // If the app is only disabled because of the above flags, launch activity anyway.
2527 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002528 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002529 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2530 // Use a message specific to this shortcut, if it has one.
2531 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2532 return;
2533 }
2534 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002535 int error = R.string.activity_not_available;
2536 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2537 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002538 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2539 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002540 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002541 }
2542 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2543 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002544 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002545 }
2546
Chris Wren40c5ed32014-06-24 18:24:23 -04002547 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002548 if ((v instanceof BubbleTextView)
2549 && shortcut.isPromise()
2550 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002551 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002552 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002553 new DialogInterface.OnClickListener() {
2554 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002555 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002556 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002557 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002558 return;
2559 }
2560
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002561 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002562 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002563 }
2564
Sunny Goyala7ce1662016-05-31 15:01:35 -07002565 private void startAppShortcutOrInfoActivity(View v) {
2566 ItemInfo item = (ItemInfo) v.getTag();
2567 Intent intent = item.getIntent();
2568 if (intent == null) {
2569 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002570 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002571 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002572 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002573
2574 if (success && v instanceof BubbleTextView) {
2575 mWaitingForResume = (BubbleTextView) v;
2576 mWaitingForResume.setStayPressed(true);
2577 }
2578 }
2579
2580 /**
2581 * Event handler for a folder icon click.
2582 *
2583 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2584 */
2585 protected void onClickFolderIcon(View v) {
2586 if (LOGD) Log.d(TAG, "onClickFolder");
2587 if (!(v instanceof FolderIcon)){
2588 throw new IllegalArgumentException("Input must be a FolderIcon");
2589 }
2590
2591 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002592 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002593 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002594 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002595 }
Michael Jurka5130e402011-10-13 04:55:35 -07002596 }
2597
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002598 /**
2599 * Event handler for the (Add) Widgets button that appears after a long press
2600 * on the home screen.
2601 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002602 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002603 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002604 if (mIsSafeModeEnabled) {
2605 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2606 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002607 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002608 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002609 }
2610
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002611 /**
2612 * Event handler for the wallpaper picker button that appears after a long press
2613 * on the home screen.
2614 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002615 public void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002616 if (!Utilities.isWallapaperAllowed(this)) {
2617 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2618 return;
2619 }
2620
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002621 String pickerPackage = getString(R.string.wallpaper_picker_package);
2622 if (TextUtils.isEmpty(pickerPackage)) {
2623 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2624 }
2625
Tony Wickham785f7a52015-08-31 17:28:32 -07002626 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2627 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002628
Sunny Goyal2100c782016-08-22 16:00:03 -07002629 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002630 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002631 .setPackage(pickerPackage)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002632 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
2633 intent.setSourceBounds(getViewBounds(v));
2634 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002635 }
2636
2637 /**
2638 * Event handler for a click on the settings button that appears after a long press
2639 * on the home screen.
2640 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002641 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002642 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002643 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2644 .setPackage(getPackageName());
2645 intent.setSourceBounds(getViewBounds(v));
2646 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002647 }
2648
Adam Cohen61f560d2013-09-30 15:58:20 -07002649 public View.OnTouchListener getHapticFeedbackTouchListener() {
2650 if (mHapticFeedbackTouchListener == null) {
2651 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002652 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002653 @Override
2654 public boolean onTouch(View v, MotionEvent event) {
2655 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2656 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2657 }
2658 return false;
2659 }
2660 };
2661 }
2662 return mHapticFeedbackTouchListener;
2663 }
2664
Tony Wickhame2217252016-03-22 16:34:23 -07002665 @Override
2666 public void onAccessibilityStateChanged(boolean enabled) {
2667 mDragLayer.onAccessibilityStateChanged(enabled);
2668 }
2669
Sunny Goyal06e21a22016-08-11 16:02:02 -07002670 public void onDragStarted() {
Adam Cohen9211d422014-10-07 18:14:53 -07002671 if (isOnCustomContent()) {
2672 // Custom content screen doesn't participate in drag and drop. If on custom
2673 // content screen, move to default.
2674 moveWorkspaceToDefaultScreen();
2675 }
Adam Cohen9211d422014-10-07 18:14:53 -07002676 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002677
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002678 /**
2679 * Called when the user stops interacting with the launcher.
2680 * This implies that the user is now on the homescreen and is not doing housekeeping.
2681 */
Adam Cohen9211d422014-10-07 18:14:53 -07002682 protected void onInteractionEnd() {
2683 if (mLauncherCallbacks != null) {
2684 mLauncherCallbacks.onInteractionEnd();
2685 }
2686 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002687
2688 /**
2689 * Called when the user starts interacting with the launcher.
2690 * The possible interactions are:
2691 * - open all apps
2692 * - reorder an app shortcut, or a widget
2693 * - open the overview mode.
2694 * This is a good time to stop doing things that only make sense
2695 * when the user is on the homescreen and not doing housekeeping.
2696 */
Adam Cohen9211d422014-10-07 18:14:53 -07002697 protected void onInteractionBegin() {
2698 if (mLauncherCallbacks != null) {
2699 mLauncherCallbacks.onInteractionBegin();
2700 }
2701 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002702
Winson Chungcd99cd32015-04-29 11:03:24 -07002703 /** Updates the interaction state. */
2704 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002705 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002706 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002707 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2708 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002709 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002710 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002711 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002712 onInteractionEnd();
2713 }
2714 }
2715
Sunny Goyala7ce1662016-05-31 15:01:35 -07002716 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002717 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002718 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2719 try {
2720 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2721 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2722 // is enabled by default on NYC.
2723 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2724 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002725
2726 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2727 String id = ((ShortcutInfo) info).getDeepShortcutId();
2728 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002729 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2730 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002731 } else {
2732 // Could be launching some bookkeeping activity
2733 startActivity(intent, optsBundle);
2734 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002735 } finally {
2736 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002737 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002738 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002739 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2740 // corresponding permission. Show the appropriate permission prompt if that
2741 // is the case.
2742 if (intent.getComponent() == null
2743 && Intent.ACTION_CALL.equals(intent.getAction())
2744 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2745 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002746
2747 setWaitingForResult(PendingRequestArgs
2748 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002749 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2750 REQUEST_PERMISSION_CALL_PHONE);
2751 } else {
2752 // No idea why this was thrown.
2753 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002754 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002755 }
2756 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002757
Sunny Goyala7ce1662016-05-31 15:01:35 -07002758 private Bundle getActivityLaunchOptions(View v) {
2759 if (Utilities.ATLEAST_MARSHMALLOW) {
2760 int left = 0, top = 0;
2761 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2762 if (v instanceof TextView) {
2763 // Launch from center of icon, not entire view
2764 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2765 if (icon != null) {
2766 Rect bounds = icon.getBounds();
2767 left = (width - bounds.width()) / 2;
2768 top = v.getPaddingTop();
2769 width = bounds.width();
2770 height = bounds.height();
2771 }
2772 }
2773 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2774 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2775 // On L devices, we use the device default slide-up transition.
2776 // On L MR1 devices, we use a custom version of the slide-up transition which
2777 // doesn't have the delay present in the device default.
2778 return ActivityOptions.makeCustomAnimation(
2779 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2780 }
2781 return null;
2782 }
2783
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002784 private Rect getViewBounds(View v) {
2785 int[] pos = new int[2];
2786 v.getLocationOnScreen(pos);
2787 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2788 }
2789
Sunny Goyala7ce1662016-05-31 15:01:35 -07002790 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002791 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2792 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2793 return false;
2794 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002795 // Only launch using the new animation if the shortcut has not opted out (this is a
2796 // private contract between launcher and may be ignored in the future).
2797 boolean useLaunchAnimation = (v != null) &&
2798 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2799 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2800
2801 UserHandleCompat user = null;
2802 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2803 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2804 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002805 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002806
2807 // Prepare intent
2808 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2809 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002810 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002811 }
2812 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002813 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2814 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07002815 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
2816 && ((ShortcutInfo) item).promisedIntent == null) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002817 // Shortcuts need some special checks due to legacy reasons.
2818 startShortcutIntentSafely(intent, optsBundle, item);
2819 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2820 // Could be launching some bookkeeping activity
2821 startActivity(intent, optsBundle);
2822 } else {
2823 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2824 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2825 }
2826 return true;
2827 } catch (ActivityNotFoundException|SecurityException e) {
2828 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2829 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2830 }
2831 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002832 }
2833
Adam Cohen268c4752012-06-06 17:47:33 -07002834 /**
2835 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2836 * in the DragLayer in the exact absolute location of the original FolderIcon.
2837 */
2838 private void copyFolderIconToImage(FolderIcon fi) {
2839 final int width = fi.getMeasuredWidth();
2840 final int height = fi.getMeasuredHeight();
2841
2842 // Lazy load ImageView, Bitmap and Canvas
2843 if (mFolderIconImageView == null) {
2844 mFolderIconImageView = new ImageView(this);
2845 }
2846 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2847 mFolderIconBitmap.getHeight() != height) {
2848 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2849 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2850 }
2851
2852 DragLayer.LayoutParams lp;
2853 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2854 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2855 } else {
2856 lp = new DragLayer.LayoutParams(width, height);
2857 }
2858
Adam Cohen307fe232012-08-16 17:55:58 -07002859 // The layout from which the folder is being opened may be scaled, adjust the starting
2860 // view size by this scale factor.
2861 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002862 lp.customPosition = true;
2863 lp.x = mRectForFolderAnimation.left;
2864 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002865 lp.width = (int) (scale * width);
2866 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002867
2868 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2869 fi.draw(mFolderIconCanvas);
2870 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002871 if (fi.getFolder() != null) {
2872 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2873 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002874 }
Adam Cohen268c4752012-06-06 17:47:33 -07002875 // Just in case this image view is still in the drag layer from a previous animation,
2876 // we remove it and re-add it.
2877 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2878 mDragLayer.removeView(mFolderIconImageView);
2879 }
2880 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002881 if (fi.getFolder() != null) {
2882 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002883 }
Adam Cohen268c4752012-06-06 17:47:33 -07002884 }
2885
Adam Cohen37b2a492016-04-06 18:36:06 -07002886 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002887 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002888 FolderInfo info = (FolderInfo) fi.getTag();
2889 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2890 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002891 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2892 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002893 }
2894
Adam Cohen268c4752012-06-06 17:47:33 -07002895 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2896 copyFolderIconToImage(fi);
2897 fi.setVisibility(View.INVISIBLE);
2898
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002899 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2900 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002901 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002902 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2903 }
2904 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002905 oa.start();
2906 }
2907
Sunny Goyal935fca12015-10-13 10:19:01 -07002908 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002909 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002910 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002911
Adam Cohen268c4752012-06-06 17:47:33 -07002912 // We remove and re-draw the FolderIcon in-case it has changed
2913 mDragLayer.removeView(mFolderIconImageView);
2914 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08002915
2916 if (cl != null) {
2917 cl.clearFolderLeaveBehind();
2918 }
2919
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002920 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002921 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002922 oa.addListener(new AnimatorListenerAdapter() {
2923 @Override
2924 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002925 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07002926 // Remove the ImageView copy of the FolderIcon and make the original visible.
2927 mDragLayer.removeView(mFolderIconImageView);
2928 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002929 }
2930 }
2931 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002932 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07002933 if (!animate) {
2934 oa.end();
2935 }
Adam Cohen2801caf2011-05-13 20:57:39 -07002936 }
2937
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002938 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002939 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002940 * is animated relative to the specified View. If the View is null, no animation
2941 * is played.
2942 *
Sunny Goyal08442b82015-10-21 17:15:08 -07002943 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002944 */
Adam Cohen37b2a492016-04-06 18:36:06 -07002945 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07002946
Adam Cohenfb91f302012-06-11 15:45:18 -07002947 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07002948 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
2949 if (openFolder != null && openFolder != folder) {
2950 // Close any open folder before opening a folder.
2951 closeFolder();
2952 }
2953
Adam Cohena9cf38f2011-05-02 15:36:58 -07002954 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002955
Adam Cohena9cf38f2011-05-02 15:36:58 -07002956 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002957
Sunny Goyalf4066152015-04-15 09:42:19 -07002958 // While the folder is open, the position of the icon cannot change.
2959 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
2960
Adam Cohen4554ee12011-08-03 16:13:21 -07002961 // Just verify that the folder hasn't already been added to the DragLayer.
2962 // There was a one-off crash where the folder had a parent already.
2963 if (folder.getParent() == null) {
2964 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07002965 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07002966 } else {
2967 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2968 folder.getParent() + ").");
2969 }
Adam Cohen37b2a492016-04-06 18:36:06 -07002970 folder.animateOpen();
2971
2972 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002973
2974 // Notify the accessibility manager that this folder "window" has appeared and occluded
2975 // the workspace items
2976 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2977 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002978 }
2979
2980 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07002981 closeFolder(true);
2982 }
2983
2984 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08002985 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002986 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002987 if (folder.isEditingName()) {
2988 folder.dismissEditingName();
2989 }
Sunny Goyal935fca12015-10-13 10:19:01 -07002990 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07002991 }
2992 }
2993
Sunny Goyal935fca12015-10-13 10:19:01 -07002994 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07002995 animate &= !Utilities.isPowerSaverOn(this);
2996
Adam Cohen4554ee12011-08-03 16:13:21 -07002997 folder.getInfo().opened = false;
2998
2999 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3000 if (parent != null) {
3001 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003002 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003003 if (fi != null) {
3004 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3005 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003006 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003007 if (animate) {
3008 folder.animateClosed();
3009 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003010 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003011 }
Winson Chung83ca4802013-04-12 15:10:52 -07003012
Sunny Goyal935fca12015-10-13 10:19:01 -07003013 // Notify the accessibility manager that this folder "window" has disappeared and no
3014 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003015 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003016 }
3017
Tony Wickham49c8d292016-07-01 11:44:34 -07003018 public void closeShortcutsContainer() {
Sunny Goyala2454ad2016-07-22 10:50:11 -07003019 closeShortcutsContainer(true);
3020 }
3021
3022 public void closeShortcutsContainer(boolean animate) {
Tony Wickham49c8d292016-07-01 11:44:34 -07003023 DeepShortcutsContainer deepShortcutsContainer = getOpenShortcutsContainer();
3024 if (deepShortcutsContainer != null) {
Sunny Goyala2454ad2016-07-22 10:50:11 -07003025 if (animate) {
3026 deepShortcutsContainer.animateClose();
3027 } else {
3028 deepShortcutsContainer.close();
3029 }
Tony Wickham49c8d292016-07-01 11:44:34 -07003030 }
3031 }
3032
Sunny Goyal52851aa2016-09-02 10:41:43 -07003033 public View getTopFloatingView() {
3034 View topView = getOpenShortcutsContainer();
3035 if (topView == null) {
3036 topView = getWorkspace().getOpenFolder();
3037 }
3038 return topView;
3039 }
3040
Tony Wickham49c8d292016-07-01 11:44:34 -07003041 /**
3042 * @return The open shortcuts container, or null if there is none
3043 */
3044 public DeepShortcutsContainer getOpenShortcutsContainer() {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07003045 // Iterate in reverse order. Shortcuts container is added later to the dragLayer,
3046 // and will be one of the last views.
3047 for (int i = mDragLayer.getChildCount() - 1; i >= 0; i--) {
3048 View child = mDragLayer.getChildAt(i);
Sunny Goyala2454ad2016-07-22 10:50:11 -07003049 if (child instanceof DeepShortcutsContainer
3050 && ((DeepShortcutsContainer) child).isOpen()) {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07003051 return (DeepShortcutsContainer) child;
3052 }
3053 }
3054 return null;
Tony Wickham49c8d292016-07-01 11:44:34 -07003055 }
3056
Tony Wickhamdadb3042016-02-24 11:07:00 -08003057 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003058 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003059 if (!isDraggingEnabled()) return false;
3060 if (isWorkspaceLocked()) return false;
3061 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003062
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07003063 if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
3064 (v == mAllAppsButton && mAllAppsButton != null)) {
Winson Chung76648c52015-07-10 14:33:23 -07003065 onLongClickAllAppsButton(v);
3066 return true;
3067 }
3068
Adam Cohen1697b792013-09-17 19:08:21 -07003069 if (v instanceof Workspace) {
3070 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003071 if (!mWorkspace.isTouchActive()) {
3072 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003073 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3074 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3075 return true;
3076 } else {
3077 return false;
3078 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003079 } else {
3080 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003081 }
Adam Cohen1697b792013-09-17 19:08:21 -07003082 }
3083
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003084 CellLayout.CellInfo longClickCellInfo = null;
3085 View itemUnderLongClick = null;
3086 if (v.getTag() instanceof ItemInfo) {
3087 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003088 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003089 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07003090 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003091 }
3092
Winson Chung3d503fb2011-07-13 17:25:49 -07003093 // The hotseat touch handling does not go through Workspace, and we always allow long press
3094 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003095 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003096 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003097 // User long pressed on empty space
3098 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3099 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003100 if (mWorkspace.isInOverviewMode()) {
3101 mWorkspace.startReordering(v);
3102 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003103 showOverviewMode(true);
Hyunyoung Song645764e2016-06-06 14:19:02 -07003104 mHotseat.requestDisallowInterceptTouchEvent(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003105 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003106 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003107 final boolean isAllAppsButton =
3108 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3109 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3110 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003111 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003112 // User long pressed on an item
Sunny Goyal94b510c2016-08-16 15:36:48 -07003113 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003114 }
3115 }
3116 }
3117 return true;
3118 }
3119
Winson Chung3d503fb2011-07-13 17:25:49 -07003120 boolean isHotseatLayout(View layout) {
3121 return mHotseat != null && layout != null &&
3122 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3123 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003124
Winson Chung3d503fb2011-07-13 17:25:49 -07003125 /**
3126 * Returns the CellLayout of the specified container at the specified screen.
3127 */
Sunny Goyal92820592015-03-02 11:31:35 -08003128 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003129 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3130 if (mHotseat != null) {
3131 return mHotseat.getLayout();
3132 } else {
3133 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003134 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003135 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003136 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003137 }
3138 }
3139
Winson Chungb745afb2015-03-02 11:51:23 -08003140 /**
3141 * For overridden classes.
3142 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003143 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003144 return isAppsViewVisible();
3145 }
3146
3147 public boolean isAppsViewVisible() {
3148 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3149 }
3150
3151 public boolean isWidgetsViewVisible() {
3152 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003153 }
3154
Michael Jurkae326f182011-11-21 14:05:46 -08003155 @Override
3156 public void onTrimMemory(int level) {
3157 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003158 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3159 // The widget preview db can result in holding onto over
3160 // 3MB of memory for caching which isn't necessary.
3161 SQLiteDatabase.releaseMemory();
3162
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003163 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003164 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003165 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003166 if (mLauncherCallbacks != null) {
3167 mLauncherCallbacks.onTrimMemory(level);
3168 }
Michael Jurkae326f182011-11-21 14:05:46 -08003169 }
3170
Winson5c6bdbb2015-09-03 11:36:19 -07003171 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003172 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003173 }
3174
Winson5c6bdbb2015-09-03 11:36:19 -07003175 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003176 boolean changed = mState != State.WORKSPACE ||
3177 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003178 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003179 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003180 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003181 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003182
Michael Jurkab3e22d92011-10-31 15:58:33 -07003183 // Set focus to the AppsCustomize button
3184 if (mAllAppsButton != null) {
3185 mAllAppsButton.requestFocus();
3186 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003187 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003188
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003189 // Change the state *after* we've called all the transition code
3190 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003191
Winson Chung5fb63472011-02-02 17:03:37 -08003192 // Resume the auto-advance of widgets
3193 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003194 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003195
Winson Chung0f785722015-04-08 10:27:49 -07003196 if (changed) {
3197 // Send an accessibility event to announce the context change
3198 getWindow().getDecorView()
3199 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003200 }
Winson5c6bdbb2015-09-03 11:36:19 -07003201 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003202 }
3203
Winsone9f27272015-10-13 10:47:51 -07003204 /**
3205 * Shows the overview button.
3206 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07003207 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003208 showOverviewMode(animated, false);
3209 }
3210
3211 /**
3212 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3213 * onto one of the overview panel buttons.
3214 */
3215 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3216 Runnable postAnimRunnable = null;
3217 if (requestButtonFocus) {
3218 postAnimRunnable = new Runnable() {
3219 @Override
3220 public void run() {
3221 // Hitting the menu button when in touch mode does not trigger touch mode to
3222 // be disabled, so if requested, force focus on one of the overview panel
3223 // buttons.
3224 mOverviewPanel.requestFocusFromTouch();
3225 }
3226 };
3227 }
Adam Cohened307df2013-10-02 09:37:31 -07003228 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003229 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003230 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003231 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003232 }
3233
Winson Chungb745afb2015-03-02 11:51:23 -08003234 /**
3235 * Shows the apps view.
3236 */
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003237 public void showAppsView(boolean animated, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003238 boolean focusSearchBar) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003239 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07003240 if (updatePredictedApps) {
3241 tryAndUpdatePredictedApps();
3242 }
Winson Chung76648c52015-07-10 14:33:23 -07003243 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003244 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003245
Winson Chungb745afb2015-03-02 11:51:23 -08003246 /**
3247 * Shows the widgets view.
3248 */
3249 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003250 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003251 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003252 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003253 }
Winson Chung76648c52015-07-10 14:33:23 -07003254 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003255
3256 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003257 @Override
3258 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003259 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003260 }
3261 });
Winson Chungb745afb2015-03-02 11:51:23 -08003262 }
3263
3264 /**
3265 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003266 *
3267 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003268 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003269 // TODO: calling method should use the return value so that when {@code false} is returned
3270 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003271 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003272 if (!(mState == State.WORKSPACE ||
3273 mState == State.APPS_SPRING_LOADED ||
3274 mState == State.WIDGETS_SPRING_LOADED ||
3275 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003276 return false;
3277 }
3278 if (toState != State.APPS && toState != State.WIDGETS) {
3279 return false;
3280 }
Winson Chungb745afb2015-03-02 11:51:23 -08003281
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003282 // This is a safe and supported transition to bypass spring_loaded mode.
3283 if (mExitSpringLoadedModeRunnable != null) {
3284 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3285 mExitSpringLoadedModeRunnable = null;
3286 }
3287
Winson Chungb745afb2015-03-02 11:51:23 -08003288 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003289 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3290 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003291 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003292 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003293 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003294
Michael Jurkab3e22d92011-10-31 15:58:33 -07003295 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003296 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003297
3298 // Pause the auto-advance of widgets until we are out of AllApps
3299 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003300 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003301 closeFolder();
Tony Wickham49c8d292016-07-01 11:44:34 -07003302 closeShortcutsContainer();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003303
3304 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003305 getWindow().getDecorView()
3306 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003307 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003308 }
3309
Winson Chungcd99cd32015-04-29 11:03:24 -07003310 /**
3311 * Updates the workspace and interaction state on state change, and return the animation to this
3312 * new state.
3313 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003314 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003315 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003316 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003317 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003318 updateInteraction(fromState, toState);
3319 return anim;
3320 }
3321
Hyunyoung Song3f471442015-04-08 19:01:34 -07003322 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003323 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003324 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003325 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003326 }
Winson Chungb745afb2015-03-02 11:51:23 -08003327
Winson Chung006ee262015-08-03 14:40:11 -07003328 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003329 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003330 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003331
3332 if (isAppsViewVisible()) {
3333 mState = State.APPS_SPRING_LOADED;
3334 } else if (isWidgetsViewVisible()) {
3335 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003336 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003337 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003338 } else {
3339 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003340 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003341 }
Adam Cohen7777d962011-08-18 18:58:38 -07003342
Hyunyoung Song3f471442015-04-08 19:01:34 -07003343 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003344 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003345 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003346
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003347 if (mExitSpringLoadedModeRunnable != null) {
3348 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3349 }
3350 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003351 @Override
3352 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003353 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003354 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3355 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003356 // Before we show workspace, hide all apps again because
3357 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3358 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003359 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003360 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003361 } else {
3362 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003363 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003364 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07003365 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003366 };
3367 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003368 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003369
Tony Wickhame0c33232016-02-08 11:37:04 -08003370 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003371 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3372 || mState == State.WIDGETS_SPRING_LOADED;
3373 }
3374
Michael Jurkad3ef3062010-11-23 16:23:58 -08003375 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003376 if (mState == State.APPS_SPRING_LOADED) {
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003377 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07003378 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003379 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003380 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003381 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3382 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003383 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003384 }
3385
Winson Chung4ac30062015-05-08 17:34:17 -07003386 /**
3387 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3388 * resumed.
3389 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003390 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003391 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003392 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003393 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003394 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003395 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003396 }
3397 }
3398 }
3399
Michael Jurkab3e22d92011-10-31 15:58:33 -07003400 void lockAllApps() {
3401 // TODO
3402 }
3403
3404 void unlockAllApps() {
3405 // TODO
3406 }
3407
Michael Jurkad7c28052012-04-27 15:43:36 -07003408 @Override
3409 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003410 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003411 final List<CharSequence> text = event.getText();
3412 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003413 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003414 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003415 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003416 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003417 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003418 } else if (mWorkspace != null) {
3419 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003420 } else {
3421 text.add(getString(R.string.all_apps_home_button_label));
3422 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003423 return result;
3424 }
3425
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003426 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003427 * If the activity is currently paused, signal that we need to run the passed Runnable
3428 * in onResume.
3429 *
3430 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003431 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3432 * wrong when we're not running, and if the activity comes back to what the configuration was
3433 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003434 *
3435 * Implementation of the method from LauncherModel.Callbacks.
3436 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003437 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003438 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003439 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003440 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003441 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003442 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003443 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003444 }
3445 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003446 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003447 return true;
3448 } else {
3449 return false;
3450 }
3451 }
3452
Michael Jurkac402cd92013-05-20 15:49:32 +02003453 private boolean waitUntilResume(Runnable run) {
3454 return waitUntilResume(run, false);
3455 }
3456
Michael Jurka1e2f4652013-07-08 18:03:46 -07003457 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003458 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003459 }
3460
Michael Jurka7607c2f2013-04-03 14:33:19 -07003461 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003462 * If the activity is currently paused, signal that we need to re-run the loader
3463 * in onResume.
3464 *
3465 * This needs to be called from incoming places where resources might have been loaded
3466 * while we are paused. That is becaues the Configuration might be wrong
3467 * when we're not running, and if it comes back to what it was when we
3468 * were paused, we are not restarted.
3469 *
3470 * Implementation of the method from LauncherModel.Callbacks.
3471 *
3472 * @return true if we are currently paused. The caller might be able to
3473 * skip some work in that case since we will come back again.
3474 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003475 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003476 public boolean setLoadOnResume() {
3477 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003478 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003479 mOnResumeNeedsLoad = true;
3480 return true;
3481 } else {
3482 return false;
3483 }
3484 }
3485
3486 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003487 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003488 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003489 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003490 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003491 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003492 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003493 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003494 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003495 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003496 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003497
Joe Onorato9c1289c2009-08-17 11:03:03 -04003498 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003499 * Clear any pending bind callbacks. This is called when is loader is planning to
3500 * perform a full rebind from scratch.
3501 */
3502 @Override
3503 public void clearPendingBinds() {
3504 mBindOnResumeCallbacks.clear();
3505 if (mPendingExecutor != null) {
3506 mPendingExecutor.markCompleted();
3507 mPendingExecutor = null;
3508 }
3509 }
3510
3511 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003512 * Refreshes the shortcuts shown on the workspace.
3513 *
3514 * Implementation of the method from LauncherModel.Callbacks.
3515 */
3516 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003517 if (LauncherAppState.PROFILE_STARTUP) {
3518 Trace.beginSection("Starting page bind");
3519 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003520 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003521
Winson Chungd64d1762013-08-20 14:37:16 -07003522 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003523 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003524 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003525
Michael Jurka05bf644e2011-11-30 20:28:53 -08003526 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003527 if (mHotseat != null) {
3528 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003529 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003530 if (LauncherAppState.PROFILE_STARTUP) {
3531 Trace.endSection();
3532 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003533 }
3534
Adam Cohendcd297f2013-06-18 13:13:40 -07003535 @Override
3536 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003537 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003538 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3539 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003540 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3541 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3542 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003543 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3544 // If there are no screens, we need to have an empty screen
3545 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003546 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003547 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003548
3549 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003550 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003551 if (hasCustomContentToLeft()) {
3552 mWorkspace.createCustomContentContainer();
3553 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003554 }
Winsonc7d2e832016-07-28 12:24:55 -07003555
3556 // After we have added all the screens, if the wallpaper was locked to the default state,
3557 // then notify to indicate that it can be released and a proper wallpaper offset can be
3558 // computed before the next layout
3559 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07003560 }
3561
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003562 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003563 int count = orderedScreenIds.size();
3564 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003565 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003566 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003567 // No need to bind the first screen, as its always bound.
3568 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3569 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003570 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003571 }
3572
Winson Chungd64d1762013-08-20 14:37:16 -07003573 public void bindAppsAdded(final ArrayList<Long> newScreens,
3574 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003575 final ArrayList<ItemInfo> addAnimated,
3576 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003577 Runnable r = new Runnable() {
3578 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003579 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003580 }
3581 };
3582 if (waitUntilResume(r)) {
3583 return;
3584 }
3585
Winson Chungd64d1762013-08-20 14:37:16 -07003586 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003587 if (newScreens != null) {
3588 bindAddScreens(newScreens);
3589 }
Winson Chungd64d1762013-08-20 14:37:16 -07003590
3591 // We add the items without animation on non-visible pages, and with
3592 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003593 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003594 bindItems(addNotAnimated, 0,
3595 addNotAnimated.size(), false);
3596 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003597 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003598 bindItems(addAnimated, 0,
3599 addAnimated.size(), true);
3600 }
Winson Chungc58497e2013-09-03 17:48:37 -07003601
Winson Chung87412982013-10-03 18:34:14 -07003602 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003603 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003604
Winson Chungb745afb2015-03-02 11:51:23 -08003605 if (addedApps != null && mAppsView != null) {
3606 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003607 }
Winson Chungd64d1762013-08-20 14:37:16 -07003608 }
3609
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003610 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003611 * Bind the items start-end from the list.
3612 *
3613 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003614 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003615 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003616 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3617 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003618 Runnable r = new Runnable() {
3619 public void run() {
3620 bindItems(shortcuts, start, end, forceAnimateIcons);
3621 }
3622 };
3623 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003624 return;
3625 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003626
Winson Chungf0c6ae02012-03-21 16:10:31 -07003627 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003628 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3629 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003630 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003631 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003632 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003633 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003634 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003635
3636 // Short circuit if we are loading dock items for a configuration which has no dock
3637 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3638 mHotseat == null) {
3639 continue;
3640 }
3641
Sunny Goyal639e9062015-08-19 19:17:06 -07003642 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003643 switch (item.itemType) {
3644 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3645 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003646 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003647 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003648 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003649 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003650 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003651 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003652 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003653 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003654 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003655 default:
3656 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003657 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003658
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003659 /*
3660 * Remove colliding items.
3661 */
3662 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3663 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3664 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3665 View v = cl.getChildAt(item.cellX, item.cellY);
3666 Object tag = v.getTag();
3667 String desc = "Collision while binding workspace item: " + item
3668 + ". Collides with " + tag;
3669 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3670 throw (new RuntimeException(desc));
3671 } else {
3672 Log.d(TAG, desc);
3673 LauncherModel.deleteItemFromDatabase(this, item);
3674 continue;
3675 }
3676 }
3677 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003678 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3679 item.cellY, 1, 1);
3680 if (animateIcons) {
3681 // Animate all the applications up now
3682 view.setAlpha(0f);
3683 view.setScaleX(0f);
3684 view.setScaleY(0f);
3685 bounceAnims.add(createNewAppBounceAnimation(view, i));
3686 newShortcutsScreenId = item.screenId;
3687 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003688 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003689
Winson Chung997a9232013-07-24 15:33:46 -07003690 if (animateIcons) {
3691 // Animate to the correct page
3692 if (newShortcutsScreenId > -1) {
3693 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003694 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003695 final Runnable startBounceAnimRunnable = new Runnable() {
3696 public void run() {
3697 anim.playTogether(bounceAnims);
3698 anim.start();
3699 }
3700 };
Winson Chung997a9232013-07-24 15:33:46 -07003701 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003702 // We post the animation slightly delayed to prevent slowdowns
3703 // when we are loading right after we return to launcher.
3704 mWorkspace.postDelayed(new Runnable() {
3705 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003706 if (mWorkspace != null) {
3707 mWorkspace.snapToPage(newScreenIndex);
3708 mWorkspace.postDelayed(startBounceAnimRunnable,
3709 NEW_APPS_ANIMATION_DELAY);
3710 }
Winson Chung94d67682013-09-25 16:29:40 -07003711 }
3712 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003713 } else {
3714 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003715 }
3716 }
Winson Chung64359a52013-07-08 17:17:08 -07003717 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003718 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003719 }
3720
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003721 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3722 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3723 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003724 view.updateAppWidget(null);
3725 view.setOnClickListener(this);
3726 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003727 mWorkspace.requestLayout();
3728 }
3729
Joe Onorato9c1289c2009-08-17 11:03:03 -04003730 /**
3731 * Add the views for a widget to the workspace.
3732 *
3733 * Implementation of the method from LauncherModel.Callbacks.
3734 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003735 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003736 Runnable r = new Runnable() {
3737 public void run() {
3738 bindAppWidget(item);
3739 }
3740 };
3741 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003742 return;
3743 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003744
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003745 if (mIsSafeModeEnabled) {
3746 bindSafeModeWidget(item);
3747 return;
3748 }
3749
Daniel Sandler843e8602010-06-07 14:59:01 -04003750 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3751 if (DEBUG_WIDGETS) {
3752 Log.d(TAG, "bindAppWidget: " + item);
3753 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003754
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003755 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003756
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003757 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3758 // If the provider is not ready, bind as a pending widget.
3759 appWidgetInfo = null;
3760 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3761 // The widget id is not valid. Try to find the widget based on the provider info.
3762 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3763 } else {
3764 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3765 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003766
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003767 // If the provider is ready, but the width is not yet restored, try to restore it.
3768 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3769 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003770 if (appWidgetInfo == null) {
3771 if (DEBUG_WIDGETS) {
3772 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3773 + " belongs to component " + item.providerName
3774 + ", as the povider is null");
3775 }
3776 LauncherModel.deleteItemFromDatabase(this, item);
3777 return;
3778 }
Sunny Goyalff572272014-07-23 13:58:07 -07003779
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003780 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003781 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003782 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3783 // Id has not been allocated yet. Allocate a new id.
3784 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3785 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003786
Sunny Goyald478c832016-04-01 12:04:16 -07003787 // Also try to bind the widget. If the bind fails, the user will be shown
3788 // a click to setup UI, which will ask for the bind permission.
3789 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3790 pendingInfo.spanX = item.spanX;
3791 pendingInfo.spanY = item.spanY;
3792 pendingInfo.minSpanX = item.minSpanX;
3793 pendingInfo.minSpanY = item.minSpanY;
3794 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003795
3796 boolean isDirectConfig =
3797 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3798 if (isDirectConfig && item.bindOptions != null) {
3799 Bundle newOptions = item.bindOptions.getExtras();
3800 if (options != null) {
3801 newOptions.putAll(options);
3802 }
3803 options = newOptions;
3804 }
Sunny Goyald478c832016-04-01 12:04:16 -07003805 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3806 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003807
Sunny Goyal86df1382016-08-10 15:03:22 -07003808 // We tried to bind once. If we were not able to bind, we would need to
3809 // go through the permission dialog, which means we cannot skip the config
3810 // activity.
3811 item.bindOptions = null;
3812 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3813
Sunny Goyald478c832016-04-01 12:04:16 -07003814 // Bind succeeded
3815 if (success) {
3816 // If the widget has a configure activity, it is still needs to set it up,
3817 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003818 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003819 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3820 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003821 }
Sunny Goyald478c832016-04-01 12:04:16 -07003822
3823 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003824 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003825 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003826 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003827 // The widget was marked as UI not ready, but there is no configure activity to
3828 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003829 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3830 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003831 }
Sunny Goyalff572272014-07-23 13:58:07 -07003832 }
3833
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003834 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003835 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003836 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3837 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003838 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003839
Sunny Goyal56c73602015-09-25 12:55:01 -07003840 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003841 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003842 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003843 deleteWidgetInfo(item);
3844 return;
3845 }
3846
Sunny Goyal233ee962015-08-03 13:05:01 -07003847 item.minSpanX = appWidgetInfo.minSpanX;
3848 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003849 addAppWidgetToWorkspace(
3850 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3851 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003852 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003853 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003854 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003855 view.updateAppWidget(null);
3856 view.setOnClickListener(this);
3857 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003858 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003859 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003860
Daniel Sandler843e8602010-06-07 14:59:01 -04003861 if (DEBUG_WIDGETS) {
3862 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3863 + (SystemClock.uptimeMillis()-start) + "ms");
3864 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003865 }
3866
Sunny Goyalff572272014-07-23 13:58:07 -07003867 /**
3868 * Restores a pending widget.
3869 *
3870 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003871 */
Sunny Goyald478c832016-04-01 12:04:16 -07003872 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003873 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3874 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3875 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003876 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003877 }
3878
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003879 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003880 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003881
3882 mWorkspace.reinflateWidgetsIfNecessary();
3883 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003884 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003885 }
3886
Adam Cohen1462de32012-07-24 22:34:36 -07003887 public void onPageBoundSynchronously(int page) {
3888 mSynchronouslyBoundPages.add(page);
3889 }
3890
Sunny Goyal527c7d32015-08-28 15:19:36 -07003891 @Override
3892 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3893 if (mPendingExecutor != null) {
3894 mPendingExecutor.markCompleted();
3895 }
3896 mPendingExecutor = executor;
3897 executor.attachTo(this);
3898 }
3899
3900 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3901 if (mPendingExecutor == executor) {
3902 mPendingExecutor = null;
3903 }
3904 }
3905
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003906 @Override
3907 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3908 Runnable r = new Runnable() {
3909 public void run() {
3910 finishFirstPageBind(executor);
3911 }
3912 };
3913 if (waitUntilResume(r)) {
3914 return;
3915 }
3916
3917 Runnable onComplete = new Runnable() {
3918 @Override
3919 public void run() {
3920 if (executor != null) {
3921 executor.onLoadAnimationCompleted();
3922 }
3923 }
3924 };
3925 if (mDragLayer.getAlpha() < 1) {
3926 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3927 } else {
3928 onComplete.run();
3929 }
3930 }
3931
Joe Onorato9c1289c2009-08-17 11:03:03 -04003932 /**
3933 * Callback saying that there aren't any more items to bind.
3934 *
3935 * Implementation of the method from LauncherModel.Callbacks.
3936 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003937 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003938 Runnable r = new Runnable() {
3939 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003940 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003941 }
3942 };
3943 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003944 return;
3945 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003946 if (LauncherAppState.PROFILE_STARTUP) {
3947 Trace.beginSection("Page bind completed");
3948 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003949 if (mSavedState != null) {
3950 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003951 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003952 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003953
Joe Onorato9c1289c2009-08-17 11:03:03 -04003954 mSavedState = null;
3955 }
3956
Adam Cohen1462de32012-07-24 22:34:36 -07003957 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003958
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003959 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003960
Sunny Goyal2100c782016-08-22 16:00:03 -07003961 if (mPendingActivityResult != null) {
3962 handleActivityResult(mPendingActivityResult.requestCode,
3963 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3964 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003965 }
3966
Sunny Goyal756adbc2015-04-16 15:20:51 -07003967 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003968
3969 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003970 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003971 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003972 if (LauncherAppState.PROFILE_STARTUP) {
3973 Trace.endSection();
3974 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003975 }
3976
Winson Chunga2413752012-04-03 14:22:34 -07003977 private boolean canRunNewAppsAnimation() {
3978 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003979 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003980 }
3981
Winson Chungc9168342013-06-26 14:54:55 -07003982 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003983 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003984 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3985 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003986 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003987 return bounceAnim;
3988 }
3989
Adam Cohen27772732013-11-11 14:00:56 +00003990 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07003991 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00003992 }
3993
Tony Wickham55616cd2015-09-23 14:55:17 -07003994 public int getSearchBarHeight() {
3995 if (mLauncherCallbacks != null) {
3996 return mLauncherCallbacks.getSearchBarHeight();
3997 }
3998 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
3999 }
4000
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004001 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004002 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4003 * multiple calls to bind the same list.)
4004 */
4005 @Thunk ArrayList<AppInfo> mTmpAppsList;
4006 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4007 public void run() {
4008 bindAllApplications(mTmpAppsList);
4009 mTmpAppsList = null;
4010 }
4011 };
4012
4013 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004014 * Add the icons for all apps.
4015 *
4016 * Implementation of the method from LauncherModel.Callbacks.
4017 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004018 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004019 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4020 mTmpAppsList = apps;
4021 return;
4022 }
4023
Winson Chungb745afb2015-03-02 11:51:23 -08004024 if (mAppsView != null) {
4025 mAppsView.setApps(apps);
4026 }
Adam Cohen9211d422014-10-07 18:14:53 -07004027 if (mLauncherCallbacks != null) {
4028 mLauncherCallbacks.bindAllApplications(apps);
4029 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004030 }
4031
4032 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004033 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
4034 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
4035 */
4036 @Override
4037 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
4038 mDeepShortcutMap = deepShortcutMapCopy;
4039 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
4040 }
4041
Tony Wickham1bce7fd2016-04-28 17:39:03 -07004042 public List<String> getShortcutIdsForItem(ItemInfo info) {
4043 if (!DeepShortcutManager.supportsShortcuts(info)) {
4044 return Collections.EMPTY_LIST;
4045 }
4046 ComponentName component = info.getTargetComponent();
Hyunyoung Songd3bf9802016-08-29 14:43:53 -07004047 if (component == null) {
4048 return Collections.EMPTY_LIST;
4049 }
4050
Tony Wickham1bce7fd2016-04-28 17:39:03 -07004051 List<String> ids = mDeepShortcutMap.get(new ComponentKey(component, info.user));
4052 return ids == null ? Collections.EMPTY_LIST : ids;
4053 }
4054
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004055 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004056 * A package was updated.
4057 *
4058 * Implementation of the method from LauncherModel.Callbacks.
4059 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004060 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004061 Runnable r = new Runnable() {
4062 public void run() {
4063 bindAppsUpdated(apps);
4064 }
4065 };
4066 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004067 return;
4068 }
4069
Winson Chungb745afb2015-03-02 11:51:23 -08004070 if (mAppsView != null) {
4071 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004072 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004073 }
4074
Sunny Goyal4390ace2014-10-13 11:33:11 -07004075 @Override
4076 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4077 Runnable r = new Runnable() {
4078 public void run() {
4079 bindWidgetsRestored(widgets);
4080 }
4081 };
4082 if (waitUntilResume(r)) {
4083 return;
4084 }
4085 mWorkspace.widgetsRestored(widgets);
4086 }
4087
Joe Onorato9c1289c2009-08-17 11:03:03 -04004088 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004089 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004090 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004091 *
4092 * @param updated list of shortcuts which have changed.
4093 * @param removed list of shortcuts which were deleted in the background. This can happen when
4094 * an app gets removed from the system or some of its components are no longer
4095 * available.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004096 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004097 @Override
4098 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4099 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004100 Runnable r = new Runnable() {
4101 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004102 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004103 }
4104 };
4105 if (waitUntilResume(r)) {
4106 return;
4107 }
4108
Sunny Goyal4390ace2014-10-13 11:33:11 -07004109 if (!updated.isEmpty()) {
4110 mWorkspace.updateShortcuts(updated);
4111 }
4112
4113 if (!removed.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004114 HashSet<ComponentName> removedComponents = new HashSet<>();
4115 HashSet<ShortcutKey> removedDeepShortcuts = new HashSet<>();
4116
Sunny Goyal4390ace2014-10-13 11:33:11 -07004117 for (ShortcutInfo si : removed) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004118 if (si.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07004119 removedDeepShortcuts.add(ShortcutKey.fromShortcutInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004120 } else {
4121 removedComponents.add(si.getTargetComponent());
4122 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004123 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004124
4125 if (!removedComponents.isEmpty()) {
4126 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(removedComponents, user);
4127 mWorkspace.removeItemsByMatcher(matcher);
4128 mDragController.onAppsRemoved(matcher);
4129 }
4130
4131 if (!removedDeepShortcuts.isEmpty()) {
4132 ItemInfoMatcher matcher = ItemInfoMatcher.ofShortcutKeys(removedDeepShortcuts);
4133 mWorkspace.removeItemsByMatcher(matcher);
4134 mDragController.onAppsRemoved(matcher);
4135 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004136 }
4137 }
4138
4139 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004140 * Update the state of a package, typically related to install state.
4141 *
4142 * Implementation of the method from LauncherModel.Callbacks.
4143 */
Sunny Goyale755d462014-07-22 13:48:29 -07004144 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004145 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4146 Runnable r = new Runnable() {
4147 public void run() {
4148 bindRestoreItemsChange(updates);
4149 }
4150 };
4151 if (waitUntilResume(r)) {
4152 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004153 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004154
Sunny Goyal756adbc2015-04-16 15:20:51 -07004155 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004156 }
4157
4158 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004159 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004160 * in addition to specific applications to remove, the reason being that
4161 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004162 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004163 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004164 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004165 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004166 public void bindWorkspaceComponentsRemoved(
4167 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4168 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004169 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004170 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004171 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004172 }
Winson Chungd64d1762013-08-20 14:37:16 -07004173 };
4174 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004175 return;
4176 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004177 if (!packageNames.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004178 ItemInfoMatcher matcher = ItemInfoMatcher.ofPackages(packageNames, user);
4179 mWorkspace.removeItemsByMatcher(matcher);
4180 mDragController.onAppsRemoved(matcher);
4181
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004182 }
4183 if (!components.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004184 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(components, user);
4185 mWorkspace.removeItemsByMatcher(matcher);
4186 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004187 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004188 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004189
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004190 @Override
4191 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4192 Runnable r = new Runnable() {
4193 public void run() {
4194 bindAppInfosRemoved(appInfos);
4195 }
4196 };
4197 if (waitUntilResume(r)) {
4198 return;
Joe Onorato36115782010-06-17 13:28:48 -04004199 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004200
Winson Chungdf95eb12013-10-16 14:57:07 -07004201 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004202 if (mAppsView != null) {
4203 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004204 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004205 }
Joe Onoratobe386092009-11-17 17:32:16 -08004206
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004207 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004208 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004209 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004210 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004211 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004212
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004213 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004214 public void bindWidgetsModel(WidgetsModel model) {
4215 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004216 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004217 return;
4218 }
4219
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004220 if (mWidgetsView != null && model != null) {
4221 mWidgetsView.addWidgets(model);
4222 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004223 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004224 }
4225
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004226 @Override
4227 public void notifyWidgetProvidersChanged() {
4228 if (mWorkspace.getState().shouldUpdateWidget) {
4229 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4230 }
4231 }
4232
Winson Chung400438b2011-01-16 17:53:48 -08004233 private int mapConfigurationOriActivityInfoOri(int configOri) {
4234 final Display d = getWindowManager().getDefaultDisplay();
4235 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4236 switch (d.getRotation()) {
4237 case Surface.ROTATION_0:
4238 case Surface.ROTATION_180:
4239 // We are currently in the same basic orientation as the natural orientation
4240 naturalOri = configOri;
4241 break;
4242 case Surface.ROTATION_90:
4243 case Surface.ROTATION_270:
4244 // We are currently in the other basic orientation to the natural orientation
4245 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4246 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4247 break;
4248 }
4249
4250 int[] oriMap = {
4251 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4252 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4253 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4254 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4255 };
4256 // Since the map starts at portrait, we need to offset if this device's natural orientation
4257 // is landscape.
4258 int indexOffset = 0;
4259 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4260 indexOffset = 1;
4261 }
4262 return oriMap[(d.getRotation() + indexOffset) % 4];
4263 }
Adam Cohen446e9402011-09-15 18:21:21 -07004264
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004265 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004266 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004267 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004268 if (Utilities.ATLEAST_JB_MR2) {
4269 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4270 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004271 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4272 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004273 }
Winson Chung4b919f82012-05-01 10:44:08 -07004274 }
4275 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004276
Winson Chung4b919f82012-05-01 10:44:08 -07004277 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004278 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004279 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004280 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004281 } else {
4282 mHandler.postDelayed(new Runnable() {
4283 public void run() {
4284 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4285 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004286 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004287 }
Winson Chung4b919f82012-05-01 10:44:08 -07004288 }
Winson Chung400438b2011-01-16 17:53:48 -08004289 }
4290
Hyunyoung Songc001cf52016-07-21 17:32:43 -07004291 private void markAppsViewShown() {
4292 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4293 return;
4294 }
4295 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
4296 }
4297
4298 private boolean shouldShowDiscoveryBounce() {
4299 if (mState != mState.WORKSPACE) {
4300 return false;
4301 }
4302 if (mLauncherCallbacks != null && mLauncherCallbacks.shouldShowDiscoveryBounce()) {
4303 return true;
4304 }
4305 if (!mIsResumeFromActionScreenOff) {
4306 return false;
4307 }
4308 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4309 return false;
4310 }
4311 return true;
4312 }
4313
Winson Chung5ffd51d2015-06-25 11:36:50 -07004314 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004315 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004316 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004317 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004318 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004319 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004320 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4321 if (userHandle != null) {
4322 user = UserHandleCompat.fromUser(userHandle);
4323 }
4324 }
4325 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004326 }
4327
4328 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004329 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004330 if (user == null) {
4331 user = UserHandleCompat.myUserHandle();
4332 }
4333
4334 // Called from search suggestion, add the profile extra to the intent to ensure that we
4335 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004336 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004337 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004338 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004339 return null;
4340 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004341 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004342 }
4343
Winson Chung5ffd51d2015-06-25 11:36:50 -07004344 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004345 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4346 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004347 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004348 UserHandleCompat.myUserHandle());
4349 }
4350
Winson Chung5ffd51d2015-06-25 11:36:50 -07004351 protected void moveWorkspaceToDefaultScreen() {
4352 mWorkspace.moveToDefaultScreen(false);
4353 }
4354
Winson Chung80baf5a2010-08-09 16:03:15 -07004355 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004356 * Returns a FastBitmapDrawable with the icon, accurately sized.
4357 */
4358 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4359 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4360 d.setFilterBitmap(true);
4361 resizeIconDrawable(d);
4362 return d;
4363 }
4364
4365 /**
4366 * Resizes an icon drawable to the correct icon size.
4367 */
Winson5fbe0742015-09-30 15:33:00 -07004368 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004369 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004370 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004371 }
4372
4373 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004374 * Prints out out state for debugging.
4375 */
4376 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004377 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004378 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004379 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
Sunny Goyal2100c782016-08-22 16:00:03 -07004380 Log.d(TAG, "mPendingRequestArgs=" + mPendingRequestArgs);
4381 Log.d(TAG, "mPendingActivityResult=" + mPendingActivityResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004382 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004383 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004384
Daniel Sandler325dc232013-06-05 22:57:57 -04004385 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004386 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004387
4388 @Override
4389 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4390 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004391 // Dump workspace
4392 writer.println(prefix + "Workspace Items");
4393 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4394 writer.println(prefix + " Homescreen " + i);
4395
4396 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4397 for (int j = 0; j < layout.getChildCount(); j++) {
4398 Object tag = layout.getChildAt(j).getTag();
4399 if (tag != null) {
4400 writer.println(prefix + " " + tag.toString());
4401 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004402 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004403 }
Sunny Goyala1365452015-10-01 15:46:24 -07004404
4405 writer.println(prefix + " Hotseat");
4406 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4407 for (int j = 0; j < layout.getChildCount(); j++) {
4408 Object tag = layout.getChildAt(j).getTag();
4409 if (tag != null) {
4410 writer.println(prefix + " " + tag.toString());
4411 }
4412 }
4413
Sunny Goyal713edfc2016-05-06 09:58:34 -07004414 try {
4415 FileLog.flushAll(writer);
4416 } catch (Exception e) {
4417 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004418 }
4419
Adam Cohen9211d422014-10-07 18:14:53 -07004420 if (mLauncherCallbacks != null) {
4421 mLauncherCallbacks.dump(prefix, fd, writer, args);
4422 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004423 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004424
Adam Cohen59400422014-03-05 18:07:04 -08004425 public static CustomAppWidget getCustomAppWidget(String name) {
4426 return sCustomAppWidgets.get(name);
4427 }
4428
4429 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4430 return sCustomAppWidgets;
4431 }
4432
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004433 public static Launcher getLauncher(Context context) {
4434 if (context instanceof Launcher) {
4435 return (Launcher) context;
4436 }
4437 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4438 }
4439
Sunny Goyal745bad92016-05-02 10:54:12 -07004440 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4441
4442 @Override
4443 public void onSharedPreferenceChanged(
4444 SharedPreferences sharedPreferences, String key) {
4445 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4446 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4447 if (!waitUntilResume(this, true)) {
4448 run();
4449 }
4450 }
4451 }
4452
4453 @Override
4454 public void run() {
4455 setOrientation();
4456 }
4457 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004458}