blob: 245cd7159fba04e3b01e2662dd46b3084ca9a489 [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;
Tony Wickham93113872016-09-14 16:46:32 -0700280 private boolean mVisible;
281 private boolean mHasFocus;
282 private boolean mAttached;
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.
Tony Wickham345bff32016-09-28 15:34:51 -0700487 if (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 Wickham345bff32016-09-28 15:34:51 -0700491 // It's possible that All Apps is visible when this is run,
492 // so always use light status bar in that case.
493 activateLightStatusBar(isAllAppsVisible());
Tony Wickham827cef22016-03-17 15:39:39 -0700494 }
495 }
496
Tony Wickham345bff32016-09-28 15:34:51 -0700497 /**
498 * Sets the status bar to be light or not. Light status bar means dark icons.
499 * @param activate if true, make sure the status bar is light, otherwise base on wallpaper.
500 */
501 public void activateLightStatusBar(boolean activate) {
502 boolean lightStatusBar = activate
503 || mExtractedColors.getColor(ExtractedColors.STATUS_BAR_INDEX,
504 ExtractedColors.DEFAULT_DARK) == ExtractedColors.DEFAULT_LIGHT;
505 int oldSystemUiFlags = getWindow().getDecorView().getSystemUiVisibility();
506 int newSystemUiFlags = oldSystemUiFlags;
Tony Wickham93113872016-09-14 16:46:32 -0700507 if (lightStatusBar) {
Tony Wickham345bff32016-09-28 15:34:51 -0700508 newSystemUiFlags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
Tony Wickham93113872016-09-14 16:46:32 -0700509 } else {
Tony Wickham345bff32016-09-28 15:34:51 -0700510 newSystemUiFlags &= ~(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Tony Wickham93113872016-09-14 16:46:32 -0700511 }
Tony Wickham345bff32016-09-28 15:34:51 -0700512 if (newSystemUiFlags != oldSystemUiFlags) {
513 getWindow().getDecorView().setSystemUiVisibility(newSystemUiFlags);
514 }
Tony Wickham93113872016-09-14 16:46:32 -0700515 }
516
Adam Cohen9211d422014-10-07 18:14:53 -0700517 private LauncherCallbacks mLauncherCallbacks;
518
519 public void onPostCreate(Bundle savedInstanceState) {
520 super.onPostCreate(savedInstanceState);
521 if (mLauncherCallbacks != null) {
522 mLauncherCallbacks.onPostCreate(savedInstanceState);
523 }
524 }
525
Winson1f064272016-07-18 17:18:02 -0700526 public void onInsetsChanged(Rect insets) {
527 mDeviceProfile.updateInsets(insets);
528 mDeviceProfile.layout(this, true /* notifyListeners */);
529 }
530
Sunny Goyal32554d12015-12-03 15:31:25 -0800531 /**
532 * Call this after onCreate to set or clear overlay.
533 */
534 public void setLauncherOverlay(LauncherOverlay overlay) {
535 if (overlay != null) {
536 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
537 }
538 mWorkspace.setLauncherOverlay(overlay);
539 }
540
Adam Cohen9211d422014-10-07 18:14:53 -0700541 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
542 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700543 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700544 private boolean mWorkspaceImportanceStored = false;
545 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700546 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800547 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700548 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
549
550 @Override
551 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700552 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700553 return;
554 }
555 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700556 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700557 if (mWorkspace != null) {
558 mWorkspaceImportanceForAccessibility =
559 mWorkspace.getImportantForAccessibility();
560 mWorkspace.setImportantForAccessibility(
561 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
562 mWorkspaceImportanceStored = true;
563 }
564 if (mHotseat != null) {
565 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
566 mHotseat.setImportantForAccessibility(
567 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
568 mHotseatImportanceStored = true;
569 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700570 }
571
572 @Override
573 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700574 if (mWorkspaceImportanceStored && mWorkspace != null) {
575 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
576 }
577 if (mHotseatImportanceStored && mHotseat != null) {
578 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
579 }
580 mWorkspaceImportanceStored = false;
581 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700582 }
583 });
Adam Cohen9211d422014-10-07 18:14:53 -0700584 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700585 }
586
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700587 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700588 public void onLauncherProviderChange() {
589 if (mLauncherCallbacks != null) {
590 mLauncherCallbacks.onLauncherProviderChange();
591 }
592 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700593
Adam Cohen9211d422014-10-07 18:14:53 -0700594 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700595 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700596 if (mLauncherCallbacks != null) {
597 return mLauncherCallbacks.hasCustomContentToLeft();
598 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700599 return false;
600 }
601
Dave Hawkeya8881582013-09-17 15:55:33 -0600602 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500603 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600604 * {@link #addToCustomContentPage}. This will only be invoked if
605 * {@link #hasCustomContentToLeft()} is {@code true}.
606 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500607 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700608 if (mLauncherCallbacks != null) {
609 mLauncherCallbacks.populateCustomContentContainer();
610 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600611 }
612
613 /**
614 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
615 * ensure the custom content page is added or removed if necessary.
616 */
617 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600618 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600619 // Not bound yet, wait for bindScreens to be called.
620 return;
621 }
622
623 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
624 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500625 mWorkspace.createCustomContentContainer();
626 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600627 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
628 mWorkspace.removeCustomContentPage();
629 }
630 }
631
Hyunyoung Songaa953652016-04-19 18:30:24 -0700632 public UserEventDispatcher getUserEventDispatcher() {
633 if (mLauncherCallbacks != null) {
634 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
635 if (dispatcher != null) {
636 return dispatcher;
637 }
638 }
639
Sunny Goyal64976d52016-06-20 14:56:28 -0700640 // Logger object is a singleton and does not have to be coupled with the foreground
641 // activity. Since most user event logging is done on the UI, the object is retrieved
642 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700643 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700644 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700645 }
646 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800647 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100648
Hyunyoung Song3f471442015-04-08 19:01:34 -0700649 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700650 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
651 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700652 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700653 }
654
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000655 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700656 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
657 // This cast is safe as long as the id < 0x00FFFFFF
658 // Since we jail all the dynamically generated views, there should be no clashes
659 // with any other views.
660 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000661 }
662
663 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700664 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
665 * a configuration step, this allows the proper animations to run after other transitions.
666 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700667 private long completeAdd(
668 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
669 long screenId = info.screenId;
670 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700671 // When the screen id represents an actual screen (as opposed to a rank) we make sure
672 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700673 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700674 }
Adam Cohendb364c32014-05-20 14:23:40 -0700675
Sunny Goyal2100c782016-08-22 16:00:03 -0700676 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800677 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700678 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700679 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800680 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700681 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700682 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700683 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700684 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700685 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700686 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700687 int widgetId = appWidgetId;
688 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700689 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700690 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700691 // Since the view was just bound, also launch the configure activity if needed
692 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
693 .getLauncherAppWidgetInfo(widgetId);
694 if (provider != null && provider.configure != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700695 startRestoredWidgetReconfigActivity(provider, widgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -0700696 }
697 }
698 break;
699 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800700 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700701
Adam Cohendb364c32014-05-20 14:23:40 -0700702 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800703 }
704
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800705 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700706 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700707 if (isWorkspaceLoading()) {
708 // process the result once the workspace has loaded.
709 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
710 return;
711 }
712 mPendingActivityResult = null;
713
Winson Chunge341d302013-08-16 14:31:00 -0700714 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700715 final PendingRequestArgs requestArgs = mPendingRequestArgs;
716 setWaitingForResult(null);
717 if (requestArgs == null) {
718 return;
719 }
720
721 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700722
Adam Cohenad4e15c2013-10-17 16:21:35 -0700723 Runnable exitSpringLoaded = new Runnable() {
724 @Override
725 public void run() {
726 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
727 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
728 }
729 };
730
Michael Jurka8b805b12012-04-18 14:23:14 -0700731 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700732 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700733 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700734 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
735 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700736 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700737 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700738 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700739 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700740 addAppWidgetImpl(
741 appWidgetId, requestArgs, null,
742 requestArgs.getWidgetProvider(),
743 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700744 }
745 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200746 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
747 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700748 // User could have free-scrolled between pages before picking a wallpaper; make sure
749 // we move to the closest one now.
750 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700751 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200752 }
753 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700754 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200755
Adam Cohened66b2b2012-01-23 17:28:51 -0800756 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700757 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700758
Adam Cohened66b2b2012-01-23 17:28:51 -0800759 // We have special handling for widgets
760 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800761 final int appWidgetId;
762 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
763 : -1;
764 if (widgetId < 0) {
765 appWidgetId = pendingAddWidgetId;
766 } else {
767 appWidgetId = widgetId;
768 }
769
Adam Cohenad4e15c2013-10-17 16:21:35 -0700770 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800771 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700772 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
773 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700774 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700775 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700776 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700777 @Override
778 public void run() {
779 exitSpringLoadedDragModeDelayed(false, 0, null);
780 }
781 };
Adam Cohendb364c32014-05-20 14:23:40 -0700782
Sunny Goyal2100c782016-08-22 16:00:03 -0700783 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
784 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
785 } else {
786 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
787 // When the screen id represents an actual screen (as opposed to a rank)
788 // we make sure that the drop page actually exists.
789 requestArgs.screenId =
790 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700791 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700792 final CellLayout dropLayout =
793 mWorkspace.getScreenWithId(requestArgs.screenId);
794
795 dropLayout.setDropPending(true);
796 final Runnable onComplete = new Runnable() {
797 @Override
798 public void run() {
799 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
800 dropLayout.setDropPending(false);
801 }
802 };
803 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
804 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700805 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800806 return;
807 }
808
Sunny Goyald478c832016-04-01 12:04:16 -0700809 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
810 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700811 if (resultCode == RESULT_OK) {
812 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700813 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700814 }
815 // Leave the widget in the pending state if the user canceled the configure.
816 return;
817 }
818
Sunny Goyalaad90582015-07-09 14:22:50 -0700819 if (requestCode == REQUEST_CREATE_SHORTCUT) {
820 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700821 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
822 completeAdd(requestCode, data, -1, requestArgs);
823 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
824 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
825
Sunny Goyalaad90582015-07-09 14:22:50 -0700826 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700827 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
828 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800831 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800832 }
833
834 @Override
835 protected void onActivityResult(
836 final int requestCode, final int resultCode, final Intent data) {
837 handleActivityResult(requestCode, resultCode, data);
838 if (mLauncherCallbacks != null) {
839 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
840 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800841 }
842
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600843 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700844 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600845 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700846 PendingRequestArgs pendingArgs = mPendingRequestArgs;
847 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
848 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
849 setWaitingForResult(null);
850
Sunny Goyal28c6b962015-10-12 11:42:05 -0700851 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700852 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700853 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700854 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700855 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700856 Intent intent = pendingArgs.getPendingIntent();
857
Sunny Goyal28c6b962015-10-12 11:42:05 -0700858 if (grantResults.length > 0
859 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700860 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700861 } else {
862 // TODO: Show a snack bar with link to settings
863 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700864 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700865 }
866 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600867 if (mLauncherCallbacks != null) {
868 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
869 grantResults);
870 }
871 }
872
Adam Cohendb364c32014-05-20 14:23:40 -0700873 /**
874 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
875 *
876 * @param screenId the screen id to check
877 * @return the new screen, or screenId if it exists
878 */
879 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800880 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700881 if (dropLayout == null) {
882 // it's possible that the add screen was removed because it was
883 // empty and a re-bind occurred
884 mWorkspace.addExtraEmptyScreen();
885 return mWorkspace.commitExtraEmptyScreen();
886 } else {
887 return screenId;
888 }
889 }
890
Sunny Goyal2100c782016-08-22 16:00:03 -0700891 @Thunk void completeTwoStageWidgetDrop(
892 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
893 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800894 Runnable onCompleteRunnable = null;
895 int animationType = 0;
896
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700897 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800898 if (resultCode == RESULT_OK) {
899 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
900 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal2100c782016-08-22 16:00:03 -0700901 requestArgs.getWidgetProvider());
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700902 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800903 onCompleteRunnable = new Runnable() {
904 @Override
905 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700906 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700907 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
908 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800909 }
910 };
911 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800912 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800913 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800914 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700915 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700916 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700917 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
918 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700919 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700920 // The animated view may be null in the case of a rotation during widget configuration
921 onCompleteRunnable.run();
922 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 }
924
925 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700926 protected void onStop() {
927 super.onStop();
928 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700929
930 if (mLauncherCallbacks != null) {
931 mLauncherCallbacks.onStop();
932 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700933
934 if (Utilities.isNycMR1OrAbove()) {
935 mAppWidgetHost.stopListening();
936 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700937 }
938
939 @Override
940 protected void onStart() {
941 super.onStart();
942 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700943
944 if (mLauncherCallbacks != null) {
945 mLauncherCallbacks.onStart();
946 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700947
948 if (Utilities.isNycMR1OrAbove()) {
949 mAppWidgetHost.startListening();
950 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700951 }
952
953 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200955 long startTime = 0;
956 if (DEBUG_RESUME_TIME) {
957 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400958 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200959 }
Adam Cohen9211d422014-10-07 18:14:53 -0700960
961 if (mLauncherCallbacks != null) {
962 mLauncherCallbacks.preOnResume();
963 }
964
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700966 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700967
Winson Chung4a2afa32012-07-19 14:53:05 -0700968 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700969 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700970 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800971 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700972 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700973 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700974 // view after launching an app, as they may be depending on the UI to be static to
975 // switch to another app, otherwise, if it was
Hyunyoung Songdd60ce42016-07-27 17:08:38 -0700976 showAppsView(false /* animated */, !launchedFromApp /* updatePredictedApps */,
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700977 mAppsView.shouldRestoreImeState() /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800978 } else if (mOnResumeState == State.WIDGETS) {
979 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700980 }
981 mOnResumeState = State.NONE;
982
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800983 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700984 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700985 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700986 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700987 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700989 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200990 // We might have postponed some bind calls until onResume (see waitUntilResume) --
991 // execute them here
992 long startTimeCallbacks = 0;
993 if (DEBUG_RESUME_TIME) {
994 startTimeCallbacks = System.currentTimeMillis();
995 }
996
Michael Jurka1e2f4652013-07-08 18:03:46 -0700997 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
998 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200999 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001000 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001001 if (DEBUG_RESUME_TIME) {
1002 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1003 (System.currentTimeMillis() - startTimeCallbacks));
1004 }
Michael Jurka447bf842013-05-15 14:52:15 +02001005 }
Michael Jurka54554252013-08-01 12:52:23 +02001006 if (mOnResumeCallbacks.size() > 0) {
1007 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1008 mOnResumeCallbacks.get(i).run();
1009 }
1010 mOnResumeCallbacks.clear();
1011 }
Winson Chunge4e50662012-01-23 14:45:13 -08001012
1013 // Reset the pressed state of icons that were locked in the press state while activities
1014 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001015 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001016 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001017 mWaitingForResume.setStayPressed(false);
1018 }
Winson Chung82963d52013-10-09 11:20:57 -07001019
Adam Cohen06dff352012-06-01 17:17:08 -07001020 // It is possible that widgets can receive updates while launcher is not in the foreground.
1021 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1022 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1023 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001024 if (!isWorkspaceLoading()) {
1025 getWorkspace().reinflateWidgetsIfNecessary();
1026 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001027
Michael Jurka447bf842013-05-15 14:52:15 +02001028 if (DEBUG_RESUME_TIME) {
1029 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1030 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001031
Adam Cohen4b66bf32015-09-18 12:15:19 -07001032 // We want to suppress callbacks about CustomContent being shown if we have just received
1033 // onNewIntent while the user was present within launcher. In that case, we post a call
1034 // to move the user to the main screen (which will occur after onResume). We don't want to
1035 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1036 // suppress here.
1037 if (mWorkspace.getCustomContentCallbacks() != null
1038 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001039 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001040 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001041 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1042 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001043 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001044 }
1045 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001046 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001047 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001048 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001049
Sunny Goyal756adbc2015-04-16 15:20:51 -07001050 if (!isWorkspaceLoading()) {
1051 // Process any items that were added while Launcher was away.
1052 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001053
1054 // Refresh shortcuts if the permission changed.
1055 mModel.refreshShortcutsIfRequired();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001056 }
Adam Cohen9211d422014-10-07 18:14:53 -07001057
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001058 if (shouldShowDiscoveryBounce()) {
1059 mAllAppsController.showDiscoveryBounce();
1060 }
1061 mIsResumeFromActionScreenOff = false;
Adam Cohen9211d422014-10-07 18:14:53 -07001062 if (mLauncherCallbacks != null) {
1063 mLauncherCallbacks.onResume();
1064 }
Adam Cohen06dff352012-06-01 17:17:08 -07001065 }
1066
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001068 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001069 // Ensure that items added to Launcher are queued until Launcher returns
1070 InstallShortcutReceiver.enableInstallQueue();
1071
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001072 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001073 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001074 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001075 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001076
1077 // We call onHide() aggressively. The custom content callbacks should be able to
1078 // debounce excess onHide calls.
1079 if (mWorkspace.getCustomContentCallbacks() != null) {
1080 mWorkspace.getCustomContentCallbacks().onHide();
1081 }
Romain Guycbb89e42009-06-08 15:52:54 -07001082
Adam Cohen9211d422014-10-07 18:14:53 -07001083 if (mLauncherCallbacks != null) {
1084 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001085 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001086 }
1087
1088 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001089 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1090 // by a onResume or by scrolling otherwise.
1091 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001092
1093 // Custom content is completely hidden
1094 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001095
1096 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1097 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001098
1099 // Indicates whether the user is allowed to scroll away from the custom content.
1100 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001101 }
1102
Adam Cohenc2d6e892014-10-16 09:49:24 -07001103 public interface LauncherOverlay {
1104
1105 /**
1106 * Touch interaction leading to overscroll has begun
1107 */
1108 public void onScrollInteractionBegin();
1109
1110 /**
1111 * Touch interaction related to overscroll has ended
1112 */
1113 public void onScrollInteractionEnd();
1114
1115 /**
1116 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1117 * screen (or in the case of RTL, the rightmost screen).
1118 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001119 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001120
1121 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001122 * Called when the launcher is ready to use the overlay
1123 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001124 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001125 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001126 }
1127
Jun Mukai8af0cd82015-05-12 12:32:12 -07001128 public interface LauncherSearchCallbacks {
1129 /**
1130 * Called when the search overlay is shown.
1131 */
1132 public void onSearchOverlayOpened();
1133
1134 /**
1135 * Called when the search overlay is dismissed.
1136 */
1137 public void onSearchOverlayClosed();
1138 }
1139
Adam Cohenc2d6e892014-10-16 09:49:24 -07001140 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001141 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001142 }
1143
1144 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1145
Sunny Goyalc86df472016-02-25 09:19:38 -08001146 public void onScrollChanged(float progress) {
1147 if (mWorkspace != null) {
1148 mWorkspace.onOverlayScrollChanged(progress);
1149 }
1150 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001151 }
1152
Jorim Jaggid017f882014-01-14 17:08:48 -08001153 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001154 if (mLauncherCallbacks != null) {
1155 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001156 } else {
1157 // On devices with a locked orientation, we will at least have the allow rotation
1158 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001159 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001160 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001161 }
1162
Adam Cohen9211d422014-10-07 18:14:53 -07001163 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001164 CustomContentCallbacks callbacks, String description) {
1165 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001166 }
1167
Adam Cohenedb40762013-07-18 16:45:45 -07001168 // The custom content needs to offset its content to account for the QSB
1169 public int getTopOffsetForCustomContent() {
1170 return mWorkspace.getPaddingTop();
1171 }
1172
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001173 @Override
1174 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001175 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001176 if (mModel.isCurrentCallbacks(this)) {
1177 mModel.stopLoader();
1178 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001179 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1180
Romain Guy13c2e7b2010-03-10 19:45:00 -08001181 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001182 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001183
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001184 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001185 @Override
1186 public void onWindowFocusChanged(boolean hasFocus) {
1187 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001188 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001189
1190 if (mLauncherCallbacks != null) {
1191 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1192 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001193 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001194
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195 private boolean acceptFilter() {
1196 final InputMethodManager inputManager = (InputMethodManager)
1197 getSystemService(Context.INPUT_METHOD_SERVICE);
1198 return !inputManager.isFullscreenMode();
1199 }
1200
1201 @Override
1202 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001203 final int uniChar = event.getUnicodeChar();
1204 final boolean handled = super.onKeyDown(keyCode, event);
1205 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1206 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1208 keyCode, event);
1209 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001210 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001211 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001212 // showSearchDialog()
1213 // If there are multiple keystrokes before the search dialog takes focus,
1214 // onSearchRequested() will be called for every keystroke,
1215 // but it is idempotent, so it's fine.
1216 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217 }
1218 }
1219
Joe Onorato8a9625e2010-01-28 15:55:35 -08001220 // Eat the long press event so the keyboard doesn't come up.
1221 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1222 return true;
1223 }
1224
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 return handled;
1226 }
1227
Winson46163472015-09-22 17:31:56 -07001228 @Override
1229 public boolean onKeyUp(int keyCode, KeyEvent event) {
1230 if (keyCode == KeyEvent.KEYCODE_MENU) {
1231 // Ignore the menu key if we are currently dragging or are on the custom content screen
1232 if (!isOnCustomContent() && !mDragController.isDragging()) {
1233 // Close any open folders
1234 closeFolder();
1235
Tony Wickham49c8d292016-07-01 11:44:34 -07001236 // Close any shortcuts containers
1237 closeShortcutsContainer();
1238
Winson46163472015-09-22 17:31:56 -07001239 // Stop resizing any widgets
1240 mWorkspace.exitWidgetResizeMode();
1241
1242 // Show the overview mode if we are on the workspace
1243 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1244 !mWorkspace.isSwitchingState()) {
1245 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001246 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001247 }
1248 }
1249 return true;
1250 }
1251 return super.onKeyUp(keyCode, event);
1252 }
1253
Karl Rosaen138a0412009-04-23 19:00:21 -07001254 private String getTypedText() {
1255 return mDefaultKeySsb.toString();
1256 }
1257
Sunny Goyal6f28e712016-09-13 14:19:29 -07001258 @Override
1259 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001260 mDefaultKeySsb.clear();
1261 mDefaultKeySsb.clearSpans();
1262 Selection.setSelection(mDefaultKeySsb, 0);
1263 }
1264
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001266 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1267 * State
1268 */
1269 private static State intToState(int stateOrdinal) {
1270 State state = State.WORKSPACE;
1271 final State[] stateValues = State.values();
1272 for (int i = 0; i < stateValues.length; i++) {
1273 if (stateValues[i].ordinal() == stateOrdinal) {
1274 state = stateValues[i];
1275 break;
1276 }
1277 }
1278 return state;
1279 }
1280
1281 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 * Restores the previous state, if it exists.
1283 *
1284 * @param savedState The previous state.
1285 */
1286 private void restoreState(Bundle savedState) {
1287 if (savedState == null) {
1288 return;
1289 }
1290
Michael Jurka883f55b2010-10-21 15:47:14 -07001291 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001292 if (state == State.APPS || state == State.WIDGETS) {
1293 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001294 }
1295
Adam Cohen21cd0022013-10-09 18:57:02 -07001296 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1297 PagedView.INVALID_RESTORE_PAGE);
1298 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001299 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 }
1301
Sunny Goyal2100c782016-08-22 16:00:03 -07001302 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1303 if (requestArgs != null) {
1304 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001306
1307 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 }
1309
1310 /**
1311 * Finds all the views we need and configure them properly.
1312 */
1313 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001314 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001315 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001316 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001317 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyal6178f132016-07-11 17:30:03 -07001318 mQsbContainer = mDragLayer.findViewById(mDeviceProfile.isVerticalBarLayout()
1319 ? R.id.workspace_blocked_row : R.id.qsb_container);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001320 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001321
Sunny Goyal784f9c32016-03-23 16:56:54 -07001322 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1323 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1324 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325
Winson Chung4c98d922011-05-31 16:50:48 -07001326 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001327 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001328
Winson Chung3d503fb2011-07-13 17:25:49 -07001329 // Setup the hotseat
1330 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1331 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001332 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001333 }
1334
Winsone9f27272015-10-13 10:47:51 -07001335 // Setup the overview panel
1336 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001337
Winson Chung4c98d922011-05-31 16:50:48 -07001338 // Setup the workspace
1339 mWorkspace.setHapticFeedbackEnabled(false);
1340 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001341 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001342 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1343 // default state, otherwise we will update to the wrong offsets in RTL
1344 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001345 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001346 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001347
Tony Wickham34d2c912015-09-11 09:27:58 -07001348 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001349 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001350
Winson Chungef7f8742015-06-04 17:18:17 -07001351 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001352 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001353 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001354 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1355 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1356 } else {
1357 mAppsView.setSearchBarController(new DefaultAppSearchController());
1358 }
Craig Mautner360310b2012-10-26 15:13:08 -07001359
Winson Chung3d503fb2011-07-13 17:25:49 -07001360 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001361 mDragController.setDragScoller(mWorkspace);
1362 mDragController.setScrollView(mDragLayer);
1363 mDragController.setMoveTarget(mWorkspace);
1364 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001365 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001366
Peter Schiller310a9882016-06-30 13:52:36 -07001367 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1368 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1369 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001370
Sunny Goyal322d5562015-06-25 19:35:49 -07001371 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1372 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001373 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 }
1375
Winsone9f27272015-10-13 10:47:51 -07001376 private void setupOverviewPanel() {
1377 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1378
1379 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1380 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1381 @Override
1382 public boolean onLongClick(View v) {
1383 return v.performClick();
1384 }
1385 };
1386
1387 // Bind wallpaper button actions
1388 View wallpaperButton = findViewById(R.id.wallpaper_button);
1389 wallpaperButton.setOnClickListener(new OnClickListener() {
1390 @Override
1391 public void onClick(View view) {
1392 if (!mWorkspace.isSwitchingState()) {
1393 onClickWallpaperPicker(view);
1394 }
1395 }
1396 });
1397 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1398 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1399
1400 // Bind widget button actions
1401 mWidgetsButton = findViewById(R.id.widget_button);
1402 mWidgetsButton.setOnClickListener(new OnClickListener() {
1403 @Override
1404 public void onClick(View view) {
1405 if (!mWorkspace.isSwitchingState()) {
1406 onClickAddWidgetButton(view);
1407 }
1408 }
1409 });
1410 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1411 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1412
1413 // Bind settings actions
1414 View settingsButton = findViewById(R.id.settings_button);
1415 boolean hasSettings = hasSettings();
1416 if (hasSettings) {
1417 settingsButton.setOnClickListener(new OnClickListener() {
1418 @Override
1419 public void onClick(View view) {
1420 if (!mWorkspace.isSwitchingState()) {
1421 onClickSettingsButton(view);
1422 }
1423 }
1424 });
1425 settingsButton.setOnLongClickListener(performClickOnLongClick);
1426 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1427 } else {
1428 settingsButton.setVisibility(View.GONE);
1429 }
1430
1431 mOverviewPanel.setAlpha(0f);
1432 }
1433
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001435 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001436 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001437 */
1438 public void setAllAppsButton(View allAppsButton) {
1439 mAllAppsButton = allAppsButton;
1440 }
1441
Sunny Goyalbb011da2016-06-15 15:42:29 -07001442 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001443 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001444 }
1445
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001446 public View getWidgetsButton() {
1447 return mWidgetsButton;
1448 }
1449
Anjali Koppal5ad44842014-03-10 20:34:39 -07001450 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 * Creates a view representing a shortcut.
1452 *
1453 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001455 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001456 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 }
1458
1459 /**
1460 * Creates a view representing a shortcut inflated from the specified resource.
1461 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 * @param parent The group the shortcut belongs to.
1463 * @param info The data structure describing the shortcut.
1464 *
1465 * @return A View inflated from layoutResId.
1466 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001467 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001468 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001469 parent, false);
1470 favorite.applyFromShortcutInfo(info, mIconCache);
1471 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001473 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 return favorite;
1475 }
1476
1477 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 * Add a shortcut to the workspace.
1479 *
1480 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001482 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001483 int cellY, PendingRequestArgs args) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001484 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001485 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001486
Sunny Goyal5c97f512015-05-19 16:03:28 -07001487 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001488 if (info == null || args.getRequestCode() != REQUEST_CREATE_SHORTCUT ||
1489 args.getPendingIntent().getComponent() == null) {
1490 return;
1491 }
1492 if (!PackageManagerHelper.hasPermissionForActivity(
1493 this, info.intent, args.getPendingIntent().getComponent().getPackageName())) {
1494 // The app is trying to add a shortcut without sufficient permissions
1495 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
Adam Cohend9198822011-11-22 16:42:47 -08001496 return;
1497 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001498 final View view = createShortcut(info);
1499
Sunny Goyal5c97f512015-05-19 16:03:28 -07001500 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001501 // First we check if we already know the exact location where we want to add this item.
1502 if (cellX >= 0 && cellY >= 0) {
1503 cellXY[0] = cellX;
1504 cellXY[1] = cellY;
1505 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001506
1507 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001508 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001509 true, null,null)) {
1510 return;
1511 }
1512 DragObject dragObject = new DragObject();
1513 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001514 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1515 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001516 return;
1517 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001518 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001519 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001520 }
1521
1522 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001523 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001524 return;
1525 }
1526
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001527 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528
Sunny Goyal2100c782016-08-22 16:00:03 -07001529 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
1530 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 }
1532
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001534 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001536 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001538 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001539 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1540
Adam Cohened66b2b2012-01-23 17:28:51 -08001541 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001542 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001543 }
Romain Guycbb89e42009-06-08 15:52:54 -07001544
Adam Cohen59400422014-03-05 18:07:04 -08001545 if (appWidgetInfo.isCustomWidget) {
1546 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001547 }
1548
Adam Cohen59400422014-03-05 18:07:04 -08001549 LauncherAppWidgetInfo launcherInfo;
1550 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001551 launcherInfo.spanX = itemInfo.spanX;
1552 launcherInfo.spanY = itemInfo.spanY;
1553 launcherInfo.minSpanX = itemInfo.minSpanX;
1554 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001555 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001556
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001558 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559
Sunny Goyal2100c782016-08-22 16:00:03 -07001560 if (hostView == null) {
1561 // Perform actual inflation because we're live
1562 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001564 hostView.setVisibility(View.VISIBLE);
1565 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566 }
Romain Guycbb89e42009-06-08 15:52:54 -07001567
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001568 private void addAppWidgetToWorkspace(
1569 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001570 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001571 hostView.setTag(item);
1572 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001573
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001574 hostView.setFocusable(true);
1575 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001576
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001577 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001578 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1579
1580 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001581 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001582 }
1583 }
1584
Adam Cohended9f8d2010-11-03 13:25:16 -07001585 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1586 @Override
1587 public void onReceive(Context context, Intent intent) {
1588 final String action = intent.getAction();
1589 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1590 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001591 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001592 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001593
Winson Chungc100e8e2011-08-09 16:02:43 -07001594 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001595 // processing a multi-step drop
Sunny Goyal2100c782016-08-22 16:00:03 -07001596 if (mAppsView != null && mWidgetsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001597 if (!showWorkspace(false)) {
1598 // If we are already on the workspace, then manually reset all apps
1599 mAppsView.reset();
1600 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001601 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001602 mIsResumeFromActionScreenOff = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001603 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1604 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001605 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001606 }
1607 }
1608 };
1609
1610 @Override
1611 public void onAttachedToWindow() {
1612 super.onAttachedToWindow();
1613
1614 // Listen for broadcasts related to user-presence
1615 final IntentFilter filter = new IntentFilter();
1616 filter.addAction(Intent.ACTION_SCREEN_OFF);
1617 filter.addAction(Intent.ACTION_USER_PRESENT);
1618 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001619 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001620 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001621 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001622
1623 if (mLauncherCallbacks != null) {
1624 mLauncherCallbacks.onAttachedToWindow();
1625 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001626 }
1627
1628 @Override
1629 public void onDetachedFromWindow() {
1630 super.onDetachedFromWindow();
1631 mVisible = false;
1632
Adam Cohend113e0c2010-11-11 10:48:05 -08001633 if (mAttached) {
1634 unregisterReceiver(mReceiver);
1635 mAttached = false;
1636 }
Winson Chungb745afb2015-03-02 11:51:23 -08001637 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001638
1639 if (mLauncherCallbacks != null) {
1640 mLauncherCallbacks.onDetachedFromWindow();
1641 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001642 }
1643
1644 public void onWindowVisibilityChanged(int visibility) {
1645 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001646 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001647 // The following code used to be in onResume, but it turns out onResume is called when
1648 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1649 // is a more appropriate event to handle
1650 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001651 if (!mWorkspaceLoading) {
1652 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001653 // We want to let Launcher draw itself at least once before we force it to build
1654 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001655 // apps is nice and speedy.
1656 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001657 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001658 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001659 if (mStarted) return;
1660 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001661 // We delay the layer building a bit in order to give
1662 // other message processing a time to run. In particular
1663 // this avoids a delay in hiding the IME if it was
1664 // currently shown, because doing that may involve
1665 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001666 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001667 final ViewTreeObserver.OnDrawListener listener = this;
1668 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001669 public void run() {
1670 if (mWorkspace != null &&
1671 mWorkspace.getViewTreeObserver() != null) {
1672 mWorkspace.getViewTreeObserver().
1673 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001674 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001675 }
1676 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001677 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001678 }
1679 });
1680 }
1681 clearTypedText();
1682 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001683 }
1684
Adam Cohen091440a2015-03-18 14:16:05 -07001685 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001686 mHandler.removeMessages(ADVANCE_MSG);
1687 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1688 mHandler.sendMessageDelayed(msg, delay);
1689 mAutoAdvanceSentTime = System.currentTimeMillis();
1690 }
1691
Adam Cohen091440a2015-03-18 14:16:05 -07001692 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001693 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1694 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1695 mAutoAdvanceRunning = autoAdvanceRunning;
1696 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001697 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001698 sendAdvanceMessage(delay);
1699 } else {
1700 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001701 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001702 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1703 }
1704 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001705 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001706 }
1707 }
1708 }
1709
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001710 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1711
Adam Cohended9f8d2010-11-03 13:25:16 -07001712 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001713 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001714 if (msg.what == ADVANCE_MSG) {
1715 int i = 0;
1716 for (View key: mWidgetsToAdvance.keySet()) {
1717 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001718 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001719 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001720 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001721 public void run() {
1722 ((Advanceable) v).advance();
1723 }
1724 }, delay);
1725 }
1726 i++;
1727 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001728 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001729 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001730 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001731 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001732 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001733
Winsonc0b52fe2015-09-09 16:38:15 -07001734 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001735 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001736 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1737 if (v instanceof Advanceable) {
1738 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001739 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001740 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001741 }
1742 }
1743
Winsonc0b52fe2015-09-09 16:38:15 -07001744 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001745 if (mWidgetsToAdvance.containsKey(hostView)) {
1746 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001747 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001748 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001749 }
1750
Hyunyoung Song3f471442015-04-08 19:01:34 -07001751 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001752 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1753 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001754 }
1755
Winson Chunga6945242014-01-08 14:04:34 -08001756 public DragLayer getDragLayer() {
1757 return mDragLayer;
1758 }
1759
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001760 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001761 return mAppsView;
1762 }
1763
Hyunyoung Song3f471442015-04-08 19:01:34 -07001764 public WidgetsContainerView getWidgetsView() {
1765 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001766 }
1767
Winson Chunga6945242014-01-08 14:04:34 -08001768 public Workspace getWorkspace() {
1769 return mWorkspace;
1770 }
1771
Sunny Goyal6178f132016-07-11 17:30:03 -07001772 public View getQsbContainer() {
1773 return mQsbContainer;
1774 }
1775
Winson Chunga6945242014-01-08 14:04:34 -08001776 public Hotseat getHotseat() {
1777 return mHotseat;
1778 }
1779
Jorim Jaggid017f882014-01-14 17:08:48 -08001780 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001781 return mOverviewPanel;
1782 }
1783
Sunny Goyal47328fd2016-05-25 18:56:41 -07001784 public DropTargetBar getDropTargetBar() {
1785 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001786 }
1787
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001788 public LauncherAppWidgetHost getAppWidgetHost() {
1789 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 }
Romain Guycbb89e42009-06-08 15:52:54 -07001791
Winson Chunga9abd0e2010-10-27 17:18:37 -07001792 public LauncherModel getModel() {
1793 return mModel;
1794 }
1795
Adam Cohen79d90c52016-04-22 13:29:20 -07001796 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001797 return mSharedPrefs;
1798 }
1799
Adam Cohen2e6da152015-05-06 11:42:25 -07001800 public DeviceProfile getDeviceProfile() {
1801 return mDeviceProfile;
1802 }
1803
Bjorn Bringertc459e522013-06-07 19:36:01 +01001804 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001805 getWindow().closeAllPanels();
1806
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001807 // Whatever we were doing is hereby canceled.
Sunny Goyal2100c782016-08-22 16:00:03 -07001808 setWaitingForResult(null);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001809 }
1810
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811 @Override
1812 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001813 long startTime = 0;
1814 if (DEBUG_RESUME_TIME) {
1815 startTime = System.currentTimeMillis();
1816 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 super.onNewIntent(intent);
1818
1819 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001820 Folder openFolder = mWorkspace.getOpenFolder();
1821 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1822 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1823 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1824 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1825 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001826 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001827 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001828
Adam Cohen6fecd412013-10-02 17:41:50 -07001829 if (mWorkspace == null) {
1830 // Can be cases where mWorkspace is null, this prevents a NPE
1831 return;
1832 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001833 // In all these cases, only animate if we're already on home
1834 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001835
Sunny Goyal935fca12015-10-13 10:19:01 -07001836 closeFolder(alreadyOnHome);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001837 closeShortcutsContainer(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001838 exitSpringLoadedDragMode();
1839
1840 // If we are already on home, then just animate back to the workspace,
1841 // otherwise, just wait until onResume to set the state back to Workspace
1842 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001843 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001844 } else {
1845 mOnResumeState = State.WORKSPACE;
1846 }
1847
1848 final View v = getWindow().peekDecorView();
1849 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001850 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001851 INPUT_METHOD_SERVICE);
1852 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1853 }
1854
Winson Chungb745afb2015-03-02 11:51:23 -08001855 // Reset the apps view
1856 if (!alreadyOnHome && mAppsView != null) {
1857 mAppsView.scrollToTop();
1858 }
1859
Hyunyoung Song3f471442015-04-08 19:01:34 -07001860 // Reset the widgets view
1861 if (!alreadyOnHome && mWidgetsView != null) {
1862 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001863 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001864
Adam Cohen9211d422014-10-07 18:14:53 -07001865 if (mLauncherCallbacks != null) {
1866 mLauncherCallbacks.onHomeIntent();
1867 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001868 }
Adam Cohened307df2013-10-02 09:37:31 -07001869
Adam Cohen9211d422014-10-07 18:14:53 -07001870 if (mLauncherCallbacks != null) {
1871 mLauncherCallbacks.onNewIntent(intent);
1872 }
Winson15f8b172015-08-19 11:02:39 -07001873
1874 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1875 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1876 // animation.
1877 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001878 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1879 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001880 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1881 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001882
1883 // We use this flag to suppress noisy callbacks above custom content state
1884 // from onResume.
1885 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001886 mWorkspace.post(new Runnable() {
1887 @Override
1888 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001889 if (mWorkspace != null) {
1890 mWorkspace.moveToDefaultScreen(true);
1891 }
Winson15f8b172015-08-19 11:02:39 -07001892 }
1893 });
1894 }
1895 }
1896
1897 if (DEBUG_RESUME_TIME) {
1898 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1899 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001900 }
1901
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001903 public void onRestoreInstanceState(Bundle state) {
1904 super.onRestoreInstanceState(state);
1905 for (int page: mSynchronouslyBoundPages) {
1906 mWorkspace.restoreInstanceStateForChild(page);
1907 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 }
1909
1910 @Override
1911 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001912 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001913 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1914 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001915
Adam Cohen21cd0022013-10-09 18:57:02 -07001916 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001917 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918
Michael Jurka883f55b2010-10-21 15:47:14 -07001919 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001920 // We close any open folder since it will not be re-opened, and we need to make sure
1921 // this state is reflected.
1922 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1923 closeFolder(false);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001924 closeShortcutsContainer(false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001925
Sunny Goyal2100c782016-08-22 16:00:03 -07001926 if (mPendingRequestArgs != null) {
1927 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1928 }
1929 if (mPendingActivityResult != null) {
1930 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 }
1932
Adam Cohen9211d422014-10-07 18:14:53 -07001933 if (mLauncherCallbacks != null) {
1934 mLauncherCallbacks.onSaveInstanceState(outState);
1935 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 }
1937
1938 @Override
1939 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001941
Winson Chunge7a03942011-08-05 15:05:12 -07001942 // Remove all pending runnables
1943 mHandler.removeMessages(ADVANCE_MSG);
1944 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001945 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001946 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001947
Winson Chungcd2b0142011-06-08 16:02:26 -07001948 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001949 // It's possible to receive onDestroy after a new Launcher activity has
1950 // been created. In this case, don't interfere with the new Launcher.
1951 if (mModel.isCurrentCallbacks(this)) {
1952 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001953 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001954 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001955
Sunny Goyal745bad92016-05-02 10:54:12 -07001956 if (mRotationPrefChangeHandler != null) {
1957 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1958 }
1959
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001961 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001963 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001965 mAppWidgetHost = null;
1966
1967 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968
1969 TextKeyListener.getInstance().release();
1970
Tony Wickhame2217252016-03-22 16:34:23 -07001971 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1972 .removeAccessibilityStateChangeListener(this);
1973
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001974 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001975
Michael Jurka2ecf9952012-06-18 12:52:28 -07001976 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001977
1978 if (mLauncherCallbacks != null) {
1979 mLauncherCallbacks.onDestroy();
1980 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 }
1982
Sunny Goyalae502842016-06-17 08:43:56 -07001983 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1984 return mAccessibilityDelegate;
1985 }
1986
Adam Cohena9cf38f2011-05-02 15:36:58 -07001987 public DragController getDragController() {
1988 return mDragController;
1989 }
1990
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001992 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001993 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001994 }
1995
1996 @Override
1997 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1998 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001999 try {
2000 super.startIntentSenderForResult(intent, requestCode,
2001 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2002 } catch (IntentSender.SendIntentException e) {
2003 throw new ActivityNotFoundException();
2004 }
2005 }
2006
Winson Chung70d72102011-08-12 11:24:35 -07002007 /**
2008 * Indicates that we want global search for this activity by setting the globalSearch
2009 * argument for {@link #startSearch} to true.
2010 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002012 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002014
Karl Rosaen138a0412009-04-23 19:00:21 -07002015 if (initialQuery == null) {
2016 // Use any text typed in the launcher as the initial query
2017 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002018 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002019 if (appSearchData == null) {
2020 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002021 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002023
Sunny Goyal6f28e712016-09-13 14:19:29 -07002024 if (mLauncherCallbacks == null ||
2025 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
2026 // Starting search from the callbacks failed. Start the default global search.
2027 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01002028 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002029
2030 // We need to show the workspace after starting the search
2031 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002032 }
2033
Ian Parkinson047036e2014-09-01 15:40:53 +01002034 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07002035 * Starts the global search activity. This code is a copied from SearchManager
2036 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07002037 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002038 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002039 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002040 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2041 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2042 if (globalSearchActivity == null) {
2043 Log.w(TAG, "No global search activity found.");
2044 return;
2045 }
2046 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2047 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2048 intent.setComponent(globalSearchActivity);
2049 // Make sure that we have a Bundle to put source in
2050 if (appSearchData == null) {
2051 appSearchData = new Bundle();
2052 } else {
2053 appSearchData = new Bundle(appSearchData);
2054 }
Adam Cohen9211d422014-10-07 18:14:53 -07002055 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002056 if (!appSearchData.containsKey("source")) {
2057 appSearchData.putString("source", getPackageName());
2058 }
2059 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2060 if (!TextUtils.isEmpty(initialQuery)) {
2061 intent.putExtra(SearchManager.QUERY, initialQuery);
2062 }
2063 if (selectInitialQuery) {
2064 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2065 }
2066 intent.setSourceBounds(sourceBounds);
2067 try {
2068 startActivity(intent);
2069 } catch (ActivityNotFoundException ex) {
2070 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2071 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002072 }
2073
Yura4f93ec62014-02-04 14:15:21 +00002074 public boolean isOnCustomContent() {
2075 return mWorkspace.isOnOrMovingToCustomContent();
2076 }
2077
Winson Chung70d72102011-08-12 11:24:35 -07002078 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002079 public boolean onPrepareOptionsMenu(Menu menu) {
2080 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002081 if (mLauncherCallbacks != null) {
2082 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2083 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002084 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002085 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002086
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002087 @Override
2088 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002089 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002090 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002091 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002092 }
2093
Joe Onorato9c1289c2009-08-17 11:03:03 -04002094 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07002095 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002096 }
2097
Adam Cohen517a7f52014-03-01 12:12:59 -08002098 public boolean isWorkspaceLoading() {
2099 return mWorkspaceLoading;
2100 }
2101
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002102 private void setWorkspaceLoading(boolean value) {
2103 boolean isLocked = isWorkspaceLocked();
2104 mWorkspaceLoading = value;
2105 if (isLocked != isWorkspaceLocked()) {
2106 onWorkspaceLockedChanged();
2107 }
2108 }
2109
Sunny Goyal2100c782016-08-22 16:00:03 -07002110 private void setWaitingForResult(PendingRequestArgs args) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002111 boolean isLocked = isWorkspaceLocked();
Sunny Goyal2100c782016-08-22 16:00:03 -07002112 mPendingRequestArgs = args;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002113 if (isLocked != isWorkspaceLocked()) {
2114 onWorkspaceLockedChanged();
2115 }
2116 }
2117
Adam Cohen9211d422014-10-07 18:14:53 -07002118 protected void onWorkspaceLockedChanged() {
2119 if (mLauncherCallbacks != null) {
2120 mLauncherCallbacks.onWorkspaceLockedChanged();
2121 }
2122 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002123
Sunny Goyal2100c782016-08-22 16:00:03 -07002124 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
2125 LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002126 if (LOGD) {
2127 Log.d(TAG, "Adding widget from drop");
2128 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002129 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2130 }
2131
Sunny Goyal2100c782016-08-22 16:00:03 -07002132 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
2133 AppWidgetHostView boundWidget, LauncherAppWidgetProviderInfo appWidgetInfo,
Adam Cohen59400422014-03-05 18:07:04 -08002134 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002135 if (appWidgetInfo.configure != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002136 setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, appWidgetInfo, info));
Michael Jurkaaf442092010-06-10 17:01:57 -07002137
Bjorn Bringert7984c942009-12-09 15:38:25 +00002138 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002139 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2140 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002141 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002142 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002143 Runnable onComplete = new Runnable() {
2144 @Override
2145 public void run() {
2146 // Exit spring loaded mode if necessary after adding the widget
2147 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2148 null);
2149 }
2150 };
Sunny Goyal2100c782016-08-22 16:00:03 -07002151 completeAddAppWidget(appWidgetId, info, boundWidget, appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002152 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002153 }
2154 }
Romain Guycbb89e42009-06-08 15:52:54 -07002155
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002156 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002157 // Close any folders that may be open.
2158 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002159 mWorkspace.moveToCustomContentScreen(animate);
2160 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002161
2162 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2163 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002164 info.container = container;
2165 info.screenId = screenId;
2166 if (cell != null) {
2167 info.cellX = cell[0];
2168 info.cellY = cell[1];
2169 }
2170 info.spanX = spanX;
2171 info.spanY = spanY;
2172
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002173 switch (info.itemType) {
2174 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2175 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07002176 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002177 break;
2178 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal2100c782016-08-22 16:00:03 -07002179 processShortcutFromDrop(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002180 break;
2181 default:
2182 throw new IllegalStateException("Unknown item type: " + info.itemType);
2183 }
2184 }
2185
Adam Cohenfbba09b2011-07-18 21:43:05 -07002186 /**
2187 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002188 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002189 private void processShortcutFromDrop(PendingAddItemInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07002190 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
2191 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
2192 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002193 }
2194
Adam Cohenfbba09b2011-07-18 21:43:05 -07002195 /**
2196 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002197 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002198 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002199 AppWidgetHostView hostView = info.boundWidget;
2200 int appWidgetId;
2201 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002202 // In the case where we've prebound the widget, we remove it from the DragLayer
2203 if (LOGD) {
2204 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2205 }
2206 getDragLayer().removeView(hostView);
2207
Adam Cohened66b2b2012-01-23 17:28:51 -08002208 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002209 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002210
2211 // Clear the boundWidget so that it doesn't get destroyed.
2212 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002213 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002214 // In this case, we either need to start an activity to get permission to bind
2215 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002216 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002217 Bundle options = info.bindOptions;
2218
Sunny Goyalffe83f12014-08-14 17:39:34 -07002219 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2220 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002221 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002222 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002223 } else {
Sunny Goyal2100c782016-08-22 16:00:03 -07002224 setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, info.info, info));
Michael Jurka8b805b12012-04-18 14:23:14 -07002225 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2226 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2227 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyal2100c782016-08-22 16:00:03 -07002228 mAppWidgetManager.getUser(info.info)
Sunny Goyalffe83f12014-08-14 17:39:34 -07002229 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002230 // TODO: we need to make sure that this accounts for the options bundle.
2231 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002232 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2233 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002234 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002235 }
2236
Adam Cohendcd297f2013-06-18 13:13:40 -07002237 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002238 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002239 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002240 folderInfo.title = getText(R.string.folder_name);
2241
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002242 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002243 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2244 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002245
2246 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002247 FolderIcon newFolder =
2248 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002249 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002250 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002251 // Force measure the new folder icon
2252 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2253 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002254 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002255 }
Romain Guycbb89e42009-06-08 15:52:54 -07002256
Winsonc0b52fe2015-09-09 16:38:15 -07002257 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002258 * Unbinds the view for the specified item, and removes the item and all its children.
2259 *
2260 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002261 * @param itemInfo the {@link ItemInfo} for this view.
2262 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002263 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002264 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002265 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002266 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002267 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2268 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002269 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002270 } else {
2271 mWorkspace.removeWorkspaceItem(v);
2272 }
Winsonc0b52fe2015-09-09 16:38:15 -07002273 if (deleteFromDb) {
2274 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2275 }
2276 } else if (itemInfo instanceof FolderInfo) {
2277 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002278 if (v instanceof FolderIcon) {
2279 ((FolderIcon) v).removeListeners();
2280 }
Winsonc0b52fe2015-09-09 16:38:15 -07002281 mWorkspace.removeWorkspaceItem(v);
2282 if (deleteFromDb) {
2283 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2284 }
2285 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2286 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002287 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002288 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002289 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002290 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002291 }
2292 } else {
2293 return false;
2294 }
2295 return true;
2296 }
2297
2298 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002299 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002300 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002301 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002302 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002303 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002304 // Deleting an app widget ID is a void call but writes to disk before returning
2305 // to the caller...
2306 new AsyncTask<Void, Void, Void>() {
2307 public Void doInBackground(Void ... args) {
2308 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2309 return null;
2310 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002311 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002312 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002313 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002314 }
2315
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002316 @Override
2317 public boolean dispatchKeyEvent(KeyEvent event) {
2318 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2319 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002320 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002321 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002322 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002323 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002324 dumpState();
2325 return true;
2326 }
2327 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002328 }
2329 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2330 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002331 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002332 return true;
2333 }
2334 }
2335
2336 return super.dispatchKeyEvent(event);
2337 }
2338
Joe Onorato88ec0992009-11-19 13:16:06 -08002339 @Override
2340 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002341 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2342 return;
2343 }
2344
Sunny Goyal45478022015-06-08 16:52:41 -07002345 if (mDragController.isDragging()) {
2346 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002347 return;
2348 }
2349
Tony Wickham49c8d292016-07-01 11:44:34 -07002350 if (getOpenShortcutsContainer() != null) {
2351 closeShortcutsContainer();
2352 } else if (isAppsViewVisible()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002353 showWorkspace(true);
2354 } else if (isWidgetsViewVisible()) {
2355 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002356 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002357 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002358 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002359 Folder openFolder = mWorkspace.getOpenFolder();
2360 if (openFolder.isEditingName()) {
2361 openFolder.dismissEditingName();
2362 } else {
2363 closeFolder();
2364 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002365 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002366 mWorkspace.exitWidgetResizeMode();
2367
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002368 // Back button is a no-op here, but give at least some feedback for the button press
2369 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002370 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002371 }
2372
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002373 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002374 * Launches the intent referred by the clicked shortcut.
2375 *
2376 * @param v The view representing the clicked shortcut.
2377 */
2378 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002379 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2380 // view has detached (it's possible for this to happen if the view is removed mid touch).
2381 if (v.getWindowToken() == null) {
2382 return;
2383 }
2384
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002385 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002386 return;
2387 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002388
Adam Cohen1697b792013-09-17 19:08:21 -07002389 if (v instanceof Workspace) {
2390 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002391 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002392 }
2393 return;
2394 }
2395
2396 if (v instanceof CellLayout) {
2397 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002398 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2399 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002400 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002401 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002402 }
2403
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002404 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002405 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002406 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002407 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002408 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002409 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002410 }
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002411 } else if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2412 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002413 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002414 } else if (tag instanceof AppInfo) {
2415 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002416 } else if (tag instanceof LauncherAppWidgetInfo) {
2417 if (v instanceof PendingAppWidgetHostView) {
2418 onClickPendingWidget((PendingAppWidgetHostView) v);
2419 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002420 }
2421 }
2422
Sunny Goyal70660032015-05-14 00:07:08 -07002423 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002424 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002425 return false;
2426 }
2427
Michael Jurkaaf442092010-06-10 17:01:57 -07002428 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002429 * Event handler for the app widget view which has not fully restored.
2430 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002431 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002432 if (mIsSafeModeEnabled) {
2433 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2434 return;
2435 }
2436
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002437 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002438 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002439 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2440 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2441 // This should not happen, as we make sure that an Id is allocated during bind.
2442 return;
2443 }
2444 LauncherAppWidgetProviderInfo appWidgetInfo =
2445 mAppWidgetManager.findProvider(info.providerName, info.user);
2446 if (appWidgetInfo != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002447 setWaitingForResult(PendingRequestArgs
2448 .forWidgetInfo(info.appWidgetId, appWidgetInfo, info));
Sunny Goyalff572272014-07-23 13:58:07 -07002449
Sunny Goyald478c832016-04-01 12:04:16 -07002450 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
Sunny Goyal2100c782016-08-22 16:00:03 -07002451 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, info.appWidgetId);
Sunny Goyald478c832016-04-01 12:04:16 -07002452 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07002453 mAppWidgetManager.getUser(appWidgetInfo)
Sunny Goyald478c832016-04-01 12:04:16 -07002454 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2455 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2456 }
2457 } else {
2458 LauncherAppWidgetProviderInfo appWidgetInfo =
2459 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2460 if (appWidgetInfo != null) {
2461 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2462 }
Sunny Goyalff572272014-07-23 13:58:07 -07002463 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002464 } else if (info.installProgress < 0) {
2465 // The install has not been queued
2466 final String packageName = info.providerName.getPackageName();
2467 showBrokenAppInstallDialog(packageName,
2468 new DialogInterface.OnClickListener() {
2469 public void onClick(DialogInterface dialog, int id) {
2470 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2471 }
2472 });
2473 } else {
2474 // Download has started.
2475 final String packageName = info.providerName.getPackageName();
2476 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002477 }
2478 }
2479
Sunny Goyald478c832016-04-01 12:04:16 -07002480 private void startRestoredWidgetReconfigActivity(
2481 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002482 setWaitingForResult(PendingRequestArgs.forWidgetInfo(info.appWidgetId, provider, info));
Sunny Goyald478c832016-04-01 12:04:16 -07002483 mAppWidgetManager.startConfigActivity(provider,
2484 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2485 }
2486
Sunny Goyalff572272014-07-23 13:58:07 -07002487 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002488 * Event handler for the "grid" button that appears on the home screen, which
2489 * enters all apps mode.
2490 *
2491 * @param v The view that was clicked.
2492 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002493 protected void onClickAllAppsButton(View v) {
2494 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002495 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002496 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.TAP,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002497 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002498 showAppsView(true /* animated */, true /* updatePredictedApps */,
2499 false /* focusSearchBar */);
Winson Chung76648c52015-07-10 14:33:23 -07002500 }
2501 }
2502
2503 protected void onLongClickAllAppsButton(View v) {
2504 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2505 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002506 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.LONGPRESS,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002507 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002508 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07002509 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002510 }
2511 }
2512
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002513 private void showBrokenAppInstallDialog(final String packageName,
2514 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002515 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002516 .setTitle(R.string.abandoned_promises_title)
2517 .setMessage(R.string.abandoned_promise_explanation)
2518 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2519 .setNeutralButton(R.string.abandoned_clean_this,
2520 new DialogInterface.OnClickListener() {
2521 public void onClick(DialogInterface dialog, int id) {
2522 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2523 mWorkspace.removeAbandonedPromise(packageName, user);
2524 }
2525 })
2526 .create().show();
2527 return;
2528 }
2529
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002530 /**
2531 * Event handler for an app shortcut click.
2532 *
2533 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2534 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002535 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002536 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2537 Object tag = v.getTag();
2538 if (!(tag instanceof ShortcutInfo)) {
2539 throw new IllegalArgumentException("Input must be a Shortcut");
2540 }
2541
2542 // Open shortcut
2543 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002544
2545 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002546 if ((shortcut.isDisabled &
2547 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2548 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2549 // If the app is only disabled because of the above flags, launch activity anyway.
2550 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002551 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002552 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2553 // Use a message specific to this shortcut, if it has one.
2554 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2555 return;
2556 }
2557 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002558 int error = R.string.activity_not_available;
2559 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2560 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002561 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2562 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002563 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002564 }
2565 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2566 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002567 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002568 }
2569
Chris Wren40c5ed32014-06-24 18:24:23 -04002570 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002571 if ((v instanceof BubbleTextView)
2572 && shortcut.isPromise()
2573 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002574 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002575 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002576 new DialogInterface.OnClickListener() {
2577 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002578 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002579 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002580 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002581 return;
2582 }
2583
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002584 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002585 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002586 }
2587
Sunny Goyala7ce1662016-05-31 15:01:35 -07002588 private void startAppShortcutOrInfoActivity(View v) {
2589 ItemInfo item = (ItemInfo) v.getTag();
2590 Intent intent = item.getIntent();
2591 if (intent == null) {
2592 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002593 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002594 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002595 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002596
2597 if (success && v instanceof BubbleTextView) {
2598 mWaitingForResume = (BubbleTextView) v;
2599 mWaitingForResume.setStayPressed(true);
2600 }
2601 }
2602
2603 /**
2604 * Event handler for a folder icon click.
2605 *
2606 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2607 */
2608 protected void onClickFolderIcon(View v) {
2609 if (LOGD) Log.d(TAG, "onClickFolder");
2610 if (!(v instanceof FolderIcon)){
2611 throw new IllegalArgumentException("Input must be a FolderIcon");
2612 }
2613
2614 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002615 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002616 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002617 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002618 }
Michael Jurka5130e402011-10-13 04:55:35 -07002619 }
2620
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002621 /**
2622 * Event handler for the (Add) Widgets button that appears after a long press
2623 * on the home screen.
2624 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002625 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002626 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002627 if (mIsSafeModeEnabled) {
2628 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2629 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002630 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002631 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002632 }
2633
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002634 /**
2635 * Event handler for the wallpaper picker button that appears after a long press
2636 * on the home screen.
2637 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002638 public void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002639 if (!Utilities.isWallapaperAllowed(this)) {
2640 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2641 return;
2642 }
2643
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002644 String pickerPackage = getString(R.string.wallpaper_picker_package);
2645 if (TextUtils.isEmpty(pickerPackage)) {
2646 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2647 }
2648
Tony Wickham785f7a52015-08-31 17:28:32 -07002649 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2650 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002651
Sunny Goyal2100c782016-08-22 16:00:03 -07002652 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002653 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002654 .setPackage(pickerPackage)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002655 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
2656 intent.setSourceBounds(getViewBounds(v));
2657 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002658 }
2659
2660 /**
2661 * Event handler for a click on the settings button that appears after a long press
2662 * on the home screen.
2663 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002664 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002665 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002666 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2667 .setPackage(getPackageName());
2668 intent.setSourceBounds(getViewBounds(v));
2669 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002670 }
2671
Adam Cohen61f560d2013-09-30 15:58:20 -07002672 public View.OnTouchListener getHapticFeedbackTouchListener() {
2673 if (mHapticFeedbackTouchListener == null) {
2674 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002675 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002676 @Override
2677 public boolean onTouch(View v, MotionEvent event) {
2678 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2679 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2680 }
2681 return false;
2682 }
2683 };
2684 }
2685 return mHapticFeedbackTouchListener;
2686 }
2687
Tony Wickhame2217252016-03-22 16:34:23 -07002688 @Override
2689 public void onAccessibilityStateChanged(boolean enabled) {
2690 mDragLayer.onAccessibilityStateChanged(enabled);
2691 }
2692
Sunny Goyal06e21a22016-08-11 16:02:02 -07002693 public void onDragStarted() {
Adam Cohen9211d422014-10-07 18:14:53 -07002694 if (isOnCustomContent()) {
2695 // Custom content screen doesn't participate in drag and drop. If on custom
2696 // content screen, move to default.
2697 moveWorkspaceToDefaultScreen();
2698 }
Adam Cohen9211d422014-10-07 18:14:53 -07002699 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002700
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002701 /**
2702 * Called when the user stops interacting with the launcher.
2703 * This implies that the user is now on the homescreen and is not doing housekeeping.
2704 */
Adam Cohen9211d422014-10-07 18:14:53 -07002705 protected void onInteractionEnd() {
2706 if (mLauncherCallbacks != null) {
2707 mLauncherCallbacks.onInteractionEnd();
2708 }
2709 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002710
2711 /**
2712 * Called when the user starts interacting with the launcher.
2713 * The possible interactions are:
2714 * - open all apps
2715 * - reorder an app shortcut, or a widget
2716 * - open the overview mode.
2717 * This is a good time to stop doing things that only make sense
2718 * when the user is on the homescreen and not doing housekeeping.
2719 */
Adam Cohen9211d422014-10-07 18:14:53 -07002720 protected void onInteractionBegin() {
2721 if (mLauncherCallbacks != null) {
2722 mLauncherCallbacks.onInteractionBegin();
2723 }
2724 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002725
Winson Chungcd99cd32015-04-29 11:03:24 -07002726 /** Updates the interaction state. */
2727 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002728 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002729 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002730 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2731 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002732 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002733 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002734 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002735 onInteractionEnd();
2736 }
2737 }
2738
Sunny Goyala7ce1662016-05-31 15:01:35 -07002739 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002740 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002741 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2742 try {
2743 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2744 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2745 // is enabled by default on NYC.
2746 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2747 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002748
2749 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2750 String id = ((ShortcutInfo) info).getDeepShortcutId();
2751 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002752 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2753 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002754 } else {
2755 // Could be launching some bookkeeping activity
2756 startActivity(intent, optsBundle);
2757 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002758 } finally {
2759 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002760 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002761 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002762 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2763 // corresponding permission. Show the appropriate permission prompt if that
2764 // is the case.
2765 if (intent.getComponent() == null
2766 && Intent.ACTION_CALL.equals(intent.getAction())
2767 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2768 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002769
2770 setWaitingForResult(PendingRequestArgs
2771 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002772 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2773 REQUEST_PERMISSION_CALL_PHONE);
2774 } else {
2775 // No idea why this was thrown.
2776 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002777 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002778 }
2779 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002780
Sunny Goyala7ce1662016-05-31 15:01:35 -07002781 private Bundle getActivityLaunchOptions(View v) {
2782 if (Utilities.ATLEAST_MARSHMALLOW) {
2783 int left = 0, top = 0;
2784 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2785 if (v instanceof TextView) {
2786 // Launch from center of icon, not entire view
2787 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2788 if (icon != null) {
2789 Rect bounds = icon.getBounds();
2790 left = (width - bounds.width()) / 2;
2791 top = v.getPaddingTop();
2792 width = bounds.width();
2793 height = bounds.height();
2794 }
2795 }
2796 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2797 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2798 // On L devices, we use the device default slide-up transition.
2799 // On L MR1 devices, we use a custom version of the slide-up transition which
2800 // doesn't have the delay present in the device default.
2801 return ActivityOptions.makeCustomAnimation(
2802 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2803 }
2804 return null;
2805 }
2806
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002807 private Rect getViewBounds(View v) {
2808 int[] pos = new int[2];
2809 v.getLocationOnScreen(pos);
2810 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2811 }
2812
Sunny Goyala7ce1662016-05-31 15:01:35 -07002813 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002814 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2815 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2816 return false;
2817 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002818 // Only launch using the new animation if the shortcut has not opted out (this is a
2819 // private contract between launcher and may be ignored in the future).
2820 boolean useLaunchAnimation = (v != null) &&
2821 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2822 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2823
2824 UserHandleCompat user = null;
2825 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2826 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2827 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002828 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002829
2830 // Prepare intent
2831 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2832 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002833 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002834 }
2835 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002836 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2837 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07002838 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
2839 && ((ShortcutInfo) item).promisedIntent == null) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002840 // Shortcuts need some special checks due to legacy reasons.
2841 startShortcutIntentSafely(intent, optsBundle, item);
2842 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2843 // Could be launching some bookkeeping activity
2844 startActivity(intent, optsBundle);
2845 } else {
2846 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2847 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2848 }
2849 return true;
2850 } catch (ActivityNotFoundException|SecurityException e) {
2851 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2852 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2853 }
2854 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002855 }
2856
Adam Cohen268c4752012-06-06 17:47:33 -07002857 /**
2858 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2859 * in the DragLayer in the exact absolute location of the original FolderIcon.
2860 */
2861 private void copyFolderIconToImage(FolderIcon fi) {
2862 final int width = fi.getMeasuredWidth();
2863 final int height = fi.getMeasuredHeight();
2864
2865 // Lazy load ImageView, Bitmap and Canvas
2866 if (mFolderIconImageView == null) {
2867 mFolderIconImageView = new ImageView(this);
2868 }
2869 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2870 mFolderIconBitmap.getHeight() != height) {
2871 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2872 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2873 }
2874
2875 DragLayer.LayoutParams lp;
2876 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2877 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2878 } else {
2879 lp = new DragLayer.LayoutParams(width, height);
2880 }
2881
Adam Cohen307fe232012-08-16 17:55:58 -07002882 // The layout from which the folder is being opened may be scaled, adjust the starting
2883 // view size by this scale factor.
2884 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002885 lp.customPosition = true;
2886 lp.x = mRectForFolderAnimation.left;
2887 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002888 lp.width = (int) (scale * width);
2889 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002890
2891 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2892 fi.draw(mFolderIconCanvas);
2893 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002894 if (fi.getFolder() != null) {
2895 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2896 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002897 }
Adam Cohen268c4752012-06-06 17:47:33 -07002898 // Just in case this image view is still in the drag layer from a previous animation,
2899 // we remove it and re-add it.
2900 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2901 mDragLayer.removeView(mFolderIconImageView);
2902 }
2903 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002904 if (fi.getFolder() != null) {
2905 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002906 }
Adam Cohen268c4752012-06-06 17:47:33 -07002907 }
2908
Adam Cohen37b2a492016-04-06 18:36:06 -07002909 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002910 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002911 FolderInfo info = (FolderInfo) fi.getTag();
2912 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2913 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002914 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2915 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002916 }
2917
Adam Cohen268c4752012-06-06 17:47:33 -07002918 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2919 copyFolderIconToImage(fi);
2920 fi.setVisibility(View.INVISIBLE);
2921
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002922 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2923 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002924 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002925 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2926 }
2927 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002928 oa.start();
2929 }
2930
Sunny Goyal935fca12015-10-13 10:19:01 -07002931 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002932 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002933 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002934
Adam Cohen268c4752012-06-06 17:47:33 -07002935 // We remove and re-draw the FolderIcon in-case it has changed
2936 mDragLayer.removeView(mFolderIconImageView);
2937 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08002938
2939 if (cl != null) {
2940 cl.clearFolderLeaveBehind();
2941 }
2942
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002943 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002944 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002945 oa.addListener(new AnimatorListenerAdapter() {
2946 @Override
2947 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002948 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07002949 // Remove the ImageView copy of the FolderIcon and make the original visible.
2950 mDragLayer.removeView(mFolderIconImageView);
2951 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002952 }
2953 }
2954 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002955 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07002956 if (!animate) {
2957 oa.end();
2958 }
Adam Cohen2801caf2011-05-13 20:57:39 -07002959 }
2960
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002961 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002962 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002963 * is animated relative to the specified View. If the View is null, no animation
2964 * is played.
2965 *
Sunny Goyal08442b82015-10-21 17:15:08 -07002966 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002967 */
Adam Cohen37b2a492016-04-06 18:36:06 -07002968 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07002969
Adam Cohenfb91f302012-06-11 15:45:18 -07002970 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07002971 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
2972 if (openFolder != null && openFolder != folder) {
2973 // Close any open folder before opening a folder.
2974 closeFolder();
2975 }
2976
Adam Cohena9cf38f2011-05-02 15:36:58 -07002977 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002978
Adam Cohena9cf38f2011-05-02 15:36:58 -07002979 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002980
Sunny Goyalf4066152015-04-15 09:42:19 -07002981 // While the folder is open, the position of the icon cannot change.
2982 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
2983
Adam Cohen4554ee12011-08-03 16:13:21 -07002984 // Just verify that the folder hasn't already been added to the DragLayer.
2985 // There was a one-off crash where the folder had a parent already.
2986 if (folder.getParent() == null) {
2987 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07002988 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07002989 } else {
2990 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2991 folder.getParent() + ").");
2992 }
Adam Cohen37b2a492016-04-06 18:36:06 -07002993 folder.animateOpen();
2994
2995 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002996
2997 // Notify the accessibility manager that this folder "window" has appeared and occluded
2998 // the workspace items
2999 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3000 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003001 }
3002
3003 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003004 closeFolder(true);
3005 }
3006
3007 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003008 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003009 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003010 if (folder.isEditingName()) {
3011 folder.dismissEditingName();
3012 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003013 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003014 }
3015 }
3016
Sunny Goyal935fca12015-10-13 10:19:01 -07003017 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003018 animate &= !Utilities.isPowerSaverOn(this);
3019
Adam Cohen4554ee12011-08-03 16:13:21 -07003020 folder.getInfo().opened = false;
3021
3022 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3023 if (parent != null) {
3024 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003025 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003026 if (fi != null) {
3027 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3028 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003029 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003030 if (animate) {
3031 folder.animateClosed();
3032 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003033 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003034 }
Winson Chung83ca4802013-04-12 15:10:52 -07003035
Sunny Goyal935fca12015-10-13 10:19:01 -07003036 // Notify the accessibility manager that this folder "window" has disappeared and no
3037 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003038 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003039 }
3040
Tony Wickham49c8d292016-07-01 11:44:34 -07003041 public void closeShortcutsContainer() {
Sunny Goyala2454ad2016-07-22 10:50:11 -07003042 closeShortcutsContainer(true);
3043 }
3044
3045 public void closeShortcutsContainer(boolean animate) {
Tony Wickham49c8d292016-07-01 11:44:34 -07003046 DeepShortcutsContainer deepShortcutsContainer = getOpenShortcutsContainer();
3047 if (deepShortcutsContainer != null) {
Sunny Goyala2454ad2016-07-22 10:50:11 -07003048 if (animate) {
3049 deepShortcutsContainer.animateClose();
3050 } else {
3051 deepShortcutsContainer.close();
3052 }
Tony Wickham49c8d292016-07-01 11:44:34 -07003053 }
3054 }
3055
Sunny Goyal52851aa2016-09-02 10:41:43 -07003056 public View getTopFloatingView() {
3057 View topView = getOpenShortcutsContainer();
3058 if (topView == null) {
3059 topView = getWorkspace().getOpenFolder();
3060 }
3061 return topView;
3062 }
3063
Tony Wickham49c8d292016-07-01 11:44:34 -07003064 /**
3065 * @return The open shortcuts container, or null if there is none
3066 */
3067 public DeepShortcutsContainer getOpenShortcutsContainer() {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07003068 // Iterate in reverse order. Shortcuts container is added later to the dragLayer,
3069 // and will be one of the last views.
3070 for (int i = mDragLayer.getChildCount() - 1; i >= 0; i--) {
3071 View child = mDragLayer.getChildAt(i);
Sunny Goyala2454ad2016-07-22 10:50:11 -07003072 if (child instanceof DeepShortcutsContainer
3073 && ((DeepShortcutsContainer) child).isOpen()) {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07003074 return (DeepShortcutsContainer) child;
3075 }
3076 }
3077 return null;
Tony Wickham49c8d292016-07-01 11:44:34 -07003078 }
3079
Tony Wickhamdadb3042016-02-24 11:07:00 -08003080 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003081 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003082 if (!isDraggingEnabled()) return false;
3083 if (isWorkspaceLocked()) return false;
3084 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003085
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07003086 if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
3087 (v == mAllAppsButton && mAllAppsButton != null)) {
Winson Chung76648c52015-07-10 14:33:23 -07003088 onLongClickAllAppsButton(v);
3089 return true;
3090 }
3091
Adam Cohen1697b792013-09-17 19:08:21 -07003092 if (v instanceof Workspace) {
3093 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003094 if (!mWorkspace.isTouchActive()) {
3095 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003096 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3097 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3098 return true;
3099 } else {
3100 return false;
3101 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003102 } else {
3103 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003104 }
Adam Cohen1697b792013-09-17 19:08:21 -07003105 }
3106
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003107 CellLayout.CellInfo longClickCellInfo = null;
3108 View itemUnderLongClick = null;
3109 if (v.getTag() instanceof ItemInfo) {
3110 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003111 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003112 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07003113 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003114 }
3115
Winson Chung3d503fb2011-07-13 17:25:49 -07003116 // The hotseat touch handling does not go through Workspace, and we always allow long press
3117 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003118 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003119 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003120 // User long pressed on empty space
3121 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3122 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003123 if (mWorkspace.isInOverviewMode()) {
3124 mWorkspace.startReordering(v);
3125 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003126 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003127 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003128 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003129 final boolean isAllAppsButton =
3130 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3131 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3132 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003133 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003134 // User long pressed on an item
Tony Wickham6e74e892016-09-19 14:06:31 -07003135 DragOptions dragOptions = new DragOptions();
3136 if (itemUnderLongClick instanceof BubbleTextView) {
3137 BubbleTextView icon = (BubbleTextView) itemUnderLongClick;
3138 if (icon.hasDeepShortcuts()) {
3139 DeepShortcutsContainer dsc = DeepShortcutsContainer.showForIcon(icon);
3140 if (dsc != null) {
3141 dragOptions.deferDragCondition = dsc.createDeferDragCondition(null);
3142 }
3143 }
3144 }
3145 mWorkspace.startDrag(longClickCellInfo, dragOptions);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003146 }
3147 }
3148 }
3149 return true;
3150 }
3151
Winson Chung3d503fb2011-07-13 17:25:49 -07003152 boolean isHotseatLayout(View layout) {
3153 return mHotseat != null && layout != null &&
3154 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3155 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003156
Winson Chung3d503fb2011-07-13 17:25:49 -07003157 /**
3158 * Returns the CellLayout of the specified container at the specified screen.
3159 */
Sunny Goyal92820592015-03-02 11:31:35 -08003160 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003161 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3162 if (mHotseat != null) {
3163 return mHotseat.getLayout();
3164 } else {
3165 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003166 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003167 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003168 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003169 }
3170 }
3171
Winson Chungb745afb2015-03-02 11:51:23 -08003172 /**
3173 * For overridden classes.
3174 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003175 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003176 return isAppsViewVisible();
3177 }
3178
3179 public boolean isAppsViewVisible() {
3180 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3181 }
3182
3183 public boolean isWidgetsViewVisible() {
3184 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003185 }
3186
Michael Jurkae326f182011-11-21 14:05:46 -08003187 @Override
3188 public void onTrimMemory(int level) {
3189 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003190 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3191 // The widget preview db can result in holding onto over
3192 // 3MB of memory for caching which isn't necessary.
3193 SQLiteDatabase.releaseMemory();
3194
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003195 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003196 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003197 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003198 if (mLauncherCallbacks != null) {
3199 mLauncherCallbacks.onTrimMemory(level);
3200 }
Michael Jurkae326f182011-11-21 14:05:46 -08003201 }
3202
Winson5c6bdbb2015-09-03 11:36:19 -07003203 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003204 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003205 }
3206
Winson5c6bdbb2015-09-03 11:36:19 -07003207 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003208 boolean changed = mState != State.WORKSPACE ||
3209 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003210 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003211 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003212 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003213 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003214
Michael Jurkab3e22d92011-10-31 15:58:33 -07003215 // Set focus to the AppsCustomize button
3216 if (mAllAppsButton != null) {
3217 mAllAppsButton.requestFocus();
3218 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003219 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003220
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003221 // Change the state *after* we've called all the transition code
3222 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003223
Winson Chung5fb63472011-02-02 17:03:37 -08003224 // Resume the auto-advance of widgets
3225 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003226 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003227
Winson Chung0f785722015-04-08 10:27:49 -07003228 if (changed) {
3229 // Send an accessibility event to announce the context change
3230 getWindow().getDecorView()
3231 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003232 }
Winson5c6bdbb2015-09-03 11:36:19 -07003233 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003234 }
3235
Winsone9f27272015-10-13 10:47:51 -07003236 /**
3237 * Shows the overview button.
3238 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07003239 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003240 showOverviewMode(animated, false);
3241 }
3242
3243 /**
3244 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3245 * onto one of the overview panel buttons.
3246 */
3247 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3248 Runnable postAnimRunnable = null;
3249 if (requestButtonFocus) {
3250 postAnimRunnable = new Runnable() {
3251 @Override
3252 public void run() {
3253 // Hitting the menu button when in touch mode does not trigger touch mode to
3254 // be disabled, so if requested, force focus on one of the overview panel
3255 // buttons.
3256 mOverviewPanel.requestFocusFromTouch();
3257 }
3258 };
3259 }
Adam Cohened307df2013-10-02 09:37:31 -07003260 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003261 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003262 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003263 mState = State.WORKSPACE;
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07003264 // If animated from long press, then don't allow any of the controller in the drag
3265 // layer to intercept any remaining touch.
3266 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04003267 }
3268
Winson Chungb745afb2015-03-02 11:51:23 -08003269 /**
3270 * Shows the apps view.
3271 */
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003272 public void showAppsView(boolean animated, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003273 boolean focusSearchBar) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003274 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07003275 if (updatePredictedApps) {
3276 tryAndUpdatePredictedApps();
3277 }
Winson Chung76648c52015-07-10 14:33:23 -07003278 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003279 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003280
Winson Chungb745afb2015-03-02 11:51:23 -08003281 /**
3282 * Shows the widgets view.
3283 */
3284 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003285 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003286 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003287 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003288 }
Winson Chung76648c52015-07-10 14:33:23 -07003289 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003290
3291 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003292 @Override
3293 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003294 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003295 }
3296 });
Winson Chungb745afb2015-03-02 11:51:23 -08003297 }
3298
3299 /**
3300 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003301 *
3302 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003303 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003304 // TODO: calling method should use the return value so that when {@code false} is returned
3305 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003306 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003307 if (!(mState == State.WORKSPACE ||
3308 mState == State.APPS_SPRING_LOADED ||
3309 mState == State.WIDGETS_SPRING_LOADED ||
3310 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003311 return false;
3312 }
3313 if (toState != State.APPS && toState != State.WIDGETS) {
3314 return false;
3315 }
Winson Chungb745afb2015-03-02 11:51:23 -08003316
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003317 // This is a safe and supported transition to bypass spring_loaded mode.
3318 if (mExitSpringLoadedModeRunnable != null) {
3319 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3320 mExitSpringLoadedModeRunnable = null;
3321 }
3322
Winson Chungb745afb2015-03-02 11:51:23 -08003323 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003324 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3325 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003326 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003327 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003328 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003329
Michael Jurkab3e22d92011-10-31 15:58:33 -07003330 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003331 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003332
3333 // Pause the auto-advance of widgets until we are out of AllApps
3334 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003335 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003336 closeFolder();
Tony Wickham49c8d292016-07-01 11:44:34 -07003337 closeShortcutsContainer();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003338
3339 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003340 getWindow().getDecorView()
3341 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003342 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003343 }
3344
Winson Chungcd99cd32015-04-29 11:03:24 -07003345 /**
3346 * Updates the workspace and interaction state on state change, and return the animation to this
3347 * new state.
3348 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003349 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003350 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003351 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003352 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003353 updateInteraction(fromState, toState);
3354 return anim;
3355 }
3356
Hyunyoung Song3f471442015-04-08 19:01:34 -07003357 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003358 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003359 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003360 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003361 }
Winson Chungb745afb2015-03-02 11:51:23 -08003362
Winson Chung006ee262015-08-03 14:40:11 -07003363 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003364 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003365 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003366
3367 if (isAppsViewVisible()) {
3368 mState = State.APPS_SPRING_LOADED;
3369 } else if (isWidgetsViewVisible()) {
3370 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003371 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003372 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003373 } else {
3374 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003375 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003376 }
Adam Cohen7777d962011-08-18 18:58:38 -07003377
Hyunyoung Song3f471442015-04-08 19:01:34 -07003378 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003379 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003380 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003381
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003382 if (mExitSpringLoadedModeRunnable != null) {
3383 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3384 }
3385 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003386 @Override
3387 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003388 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003389 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3390 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003391 // Before we show workspace, hide all apps again because
3392 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3393 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003394 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003395 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003396 } else {
3397 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003398 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003399 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07003400 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003401 };
3402 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003403 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003404
Tony Wickhame0c33232016-02-08 11:37:04 -08003405 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003406 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3407 || mState == State.WIDGETS_SPRING_LOADED;
3408 }
3409
Michael Jurkad3ef3062010-11-23 16:23:58 -08003410 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003411 if (mState == State.APPS_SPRING_LOADED) {
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003412 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07003413 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003414 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003415 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003416 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3417 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003418 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003419 }
3420
Winson Chung4ac30062015-05-08 17:34:17 -07003421 /**
3422 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3423 * resumed.
3424 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003425 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003426 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003427 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003428 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003429 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003430 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003431 }
3432 }
3433 }
3434
Michael Jurkab3e22d92011-10-31 15:58:33 -07003435 void lockAllApps() {
3436 // TODO
3437 }
3438
3439 void unlockAllApps() {
3440 // TODO
3441 }
3442
Michael Jurkad7c28052012-04-27 15:43:36 -07003443 @Override
3444 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003445 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003446 final List<CharSequence> text = event.getText();
3447 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003448 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003449 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003450 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003451 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003452 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003453 } else if (mWorkspace != null) {
3454 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003455 } else {
3456 text.add(getString(R.string.all_apps_home_button_label));
3457 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003458 return result;
3459 }
3460
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003461 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003462 * If the activity is currently paused, signal that we need to run the passed Runnable
3463 * in onResume.
3464 *
3465 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003466 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3467 * wrong when we're not running, and if the activity comes back to what the configuration was
3468 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003469 *
3470 * Implementation of the method from LauncherModel.Callbacks.
3471 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003472 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003473 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003474 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003475 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003476 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003477 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003478 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003479 }
3480 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003481 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003482 return true;
3483 } else {
3484 return false;
3485 }
3486 }
3487
Michael Jurkac402cd92013-05-20 15:49:32 +02003488 private boolean waitUntilResume(Runnable run) {
3489 return waitUntilResume(run, false);
3490 }
3491
Michael Jurka1e2f4652013-07-08 18:03:46 -07003492 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003493 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003494 }
3495
Michael Jurka7607c2f2013-04-03 14:33:19 -07003496 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003497 * If the activity is currently paused, signal that we need to re-run the loader
3498 * in onResume.
3499 *
3500 * This needs to be called from incoming places where resources might have been loaded
3501 * while we are paused. That is becaues the Configuration might be wrong
3502 * when we're not running, and if it comes back to what it was when we
3503 * were paused, we are not restarted.
3504 *
3505 * Implementation of the method from LauncherModel.Callbacks.
3506 *
3507 * @return true if we are currently paused. The caller might be able to
3508 * skip some work in that case since we will come back again.
3509 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003510 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003511 public boolean setLoadOnResume() {
3512 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003513 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003514 mOnResumeNeedsLoad = true;
3515 return true;
3516 } else {
3517 return false;
3518 }
3519 }
3520
3521 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003522 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003523 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003524 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003525 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003526 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003527 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003528 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003529 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003530 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003531 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003532
Joe Onorato9c1289c2009-08-17 11:03:03 -04003533 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003534 * Clear any pending bind callbacks. This is called when is loader is planning to
3535 * perform a full rebind from scratch.
3536 */
3537 @Override
3538 public void clearPendingBinds() {
3539 mBindOnResumeCallbacks.clear();
3540 if (mPendingExecutor != null) {
3541 mPendingExecutor.markCompleted();
3542 mPendingExecutor = null;
3543 }
3544 }
3545
3546 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003547 * Refreshes the shortcuts shown on the workspace.
3548 *
3549 * Implementation of the method from LauncherModel.Callbacks.
3550 */
3551 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003552 if (LauncherAppState.PROFILE_STARTUP) {
3553 Trace.beginSection("Starting page bind");
3554 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003555 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003556
Winson Chungd64d1762013-08-20 14:37:16 -07003557 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003558 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003559 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003560
Michael Jurka05bf644e2011-11-30 20:28:53 -08003561 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003562 if (mHotseat != null) {
3563 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003564 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003565 if (LauncherAppState.PROFILE_STARTUP) {
3566 Trace.endSection();
3567 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003568 }
3569
Adam Cohendcd297f2013-06-18 13:13:40 -07003570 @Override
3571 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003572 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003573 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3574 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003575 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3576 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3577 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003578 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3579 // If there are no screens, we need to have an empty screen
3580 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003581 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003582 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003583
3584 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003585 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003586 if (hasCustomContentToLeft()) {
3587 mWorkspace.createCustomContentContainer();
3588 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003589 }
Winsonc7d2e832016-07-28 12:24:55 -07003590
3591 // After we have added all the screens, if the wallpaper was locked to the default state,
3592 // then notify to indicate that it can be released and a proper wallpaper offset can be
3593 // computed before the next layout
3594 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07003595 }
3596
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003597 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003598 int count = orderedScreenIds.size();
3599 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003600 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003601 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003602 // No need to bind the first screen, as its always bound.
3603 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3604 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003605 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003606 }
3607
Winson Chungd64d1762013-08-20 14:37:16 -07003608 public void bindAppsAdded(final ArrayList<Long> newScreens,
3609 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003610 final ArrayList<ItemInfo> addAnimated,
3611 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003612 Runnable r = new Runnable() {
3613 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003614 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003615 }
3616 };
3617 if (waitUntilResume(r)) {
3618 return;
3619 }
3620
Winson Chungd64d1762013-08-20 14:37:16 -07003621 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003622 if (newScreens != null) {
3623 bindAddScreens(newScreens);
3624 }
Winson Chungd64d1762013-08-20 14:37:16 -07003625
3626 // We add the items without animation on non-visible pages, and with
3627 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003628 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003629 bindItems(addNotAnimated, 0,
3630 addNotAnimated.size(), false);
3631 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003632 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003633 bindItems(addAnimated, 0,
3634 addAnimated.size(), true);
3635 }
Winson Chungc58497e2013-09-03 17:48:37 -07003636
Winson Chung87412982013-10-03 18:34:14 -07003637 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003638 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003639
Winson Chungb745afb2015-03-02 11:51:23 -08003640 if (addedApps != null && mAppsView != null) {
3641 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003642 }
Winson Chungd64d1762013-08-20 14:37:16 -07003643 }
3644
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003645 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003646 * Bind the items start-end from the list.
3647 *
3648 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003649 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003650 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003651 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3652 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003653 Runnable r = new Runnable() {
3654 public void run() {
3655 bindItems(shortcuts, start, end, forceAnimateIcons);
3656 }
3657 };
3658 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003659 return;
3660 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003661
Winson Chungf0c6ae02012-03-21 16:10:31 -07003662 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003663 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3664 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003665 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003666 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003667 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003668 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003669 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003670
3671 // Short circuit if we are loading dock items for a configuration which has no dock
3672 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3673 mHotseat == null) {
3674 continue;
3675 }
3676
Sunny Goyal639e9062015-08-19 19:17:06 -07003677 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003678 switch (item.itemType) {
3679 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3680 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003681 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003682 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003683 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003684 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003685 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003686 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003687 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003688 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003689 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003690 default:
3691 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003692 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003693
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003694 /*
3695 * Remove colliding items.
3696 */
3697 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3698 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3699 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3700 View v = cl.getChildAt(item.cellX, item.cellY);
3701 Object tag = v.getTag();
3702 String desc = "Collision while binding workspace item: " + item
3703 + ". Collides with " + tag;
3704 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3705 throw (new RuntimeException(desc));
3706 } else {
3707 Log.d(TAG, desc);
3708 LauncherModel.deleteItemFromDatabase(this, item);
3709 continue;
3710 }
3711 }
3712 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003713 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3714 item.cellY, 1, 1);
3715 if (animateIcons) {
3716 // Animate all the applications up now
3717 view.setAlpha(0f);
3718 view.setScaleX(0f);
3719 view.setScaleY(0f);
3720 bounceAnims.add(createNewAppBounceAnimation(view, i));
3721 newShortcutsScreenId = item.screenId;
3722 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003723 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003724
Winson Chung997a9232013-07-24 15:33:46 -07003725 if (animateIcons) {
3726 // Animate to the correct page
3727 if (newShortcutsScreenId > -1) {
3728 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003729 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003730 final Runnable startBounceAnimRunnable = new Runnable() {
3731 public void run() {
3732 anim.playTogether(bounceAnims);
3733 anim.start();
3734 }
3735 };
Winson Chung997a9232013-07-24 15:33:46 -07003736 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003737 // We post the animation slightly delayed to prevent slowdowns
3738 // when we are loading right after we return to launcher.
3739 mWorkspace.postDelayed(new Runnable() {
3740 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003741 if (mWorkspace != null) {
3742 mWorkspace.snapToPage(newScreenIndex);
3743 mWorkspace.postDelayed(startBounceAnimRunnable,
3744 NEW_APPS_ANIMATION_DELAY);
3745 }
Winson Chung94d67682013-09-25 16:29:40 -07003746 }
3747 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003748 } else {
3749 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003750 }
3751 }
Winson Chung64359a52013-07-08 17:17:08 -07003752 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003753 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003754 }
3755
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003756 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3757 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3758 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003759 view.updateAppWidget(null);
3760 view.setOnClickListener(this);
3761 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003762 mWorkspace.requestLayout();
3763 }
3764
Joe Onorato9c1289c2009-08-17 11:03:03 -04003765 /**
3766 * Add the views for a widget to the workspace.
3767 *
3768 * Implementation of the method from LauncherModel.Callbacks.
3769 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003770 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003771 Runnable r = new Runnable() {
3772 public void run() {
3773 bindAppWidget(item);
3774 }
3775 };
3776 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003777 return;
3778 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003779
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003780 if (mIsSafeModeEnabled) {
3781 bindSafeModeWidget(item);
3782 return;
3783 }
3784
Daniel Sandler843e8602010-06-07 14:59:01 -04003785 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3786 if (DEBUG_WIDGETS) {
3787 Log.d(TAG, "bindAppWidget: " + item);
3788 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003789
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003790 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003791
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003792 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3793 // If the provider is not ready, bind as a pending widget.
3794 appWidgetInfo = null;
3795 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3796 // The widget id is not valid. Try to find the widget based on the provider info.
3797 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3798 } else {
3799 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3800 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003801
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003802 // If the provider is ready, but the width is not yet restored, try to restore it.
3803 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3804 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003805 if (appWidgetInfo == null) {
3806 if (DEBUG_WIDGETS) {
3807 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3808 + " belongs to component " + item.providerName
3809 + ", as the povider is null");
3810 }
3811 LauncherModel.deleteItemFromDatabase(this, item);
3812 return;
3813 }
Sunny Goyalff572272014-07-23 13:58:07 -07003814
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003815 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003816 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003817 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3818 // Id has not been allocated yet. Allocate a new id.
3819 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3820 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003821
Sunny Goyald478c832016-04-01 12:04:16 -07003822 // Also try to bind the widget. If the bind fails, the user will be shown
3823 // a click to setup UI, which will ask for the bind permission.
3824 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3825 pendingInfo.spanX = item.spanX;
3826 pendingInfo.spanY = item.spanY;
3827 pendingInfo.minSpanX = item.minSpanX;
3828 pendingInfo.minSpanY = item.minSpanY;
3829 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003830
3831 boolean isDirectConfig =
3832 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3833 if (isDirectConfig && item.bindOptions != null) {
3834 Bundle newOptions = item.bindOptions.getExtras();
3835 if (options != null) {
3836 newOptions.putAll(options);
3837 }
3838 options = newOptions;
3839 }
Sunny Goyald478c832016-04-01 12:04:16 -07003840 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3841 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003842
Sunny Goyal86df1382016-08-10 15:03:22 -07003843 // We tried to bind once. If we were not able to bind, we would need to
3844 // go through the permission dialog, which means we cannot skip the config
3845 // activity.
3846 item.bindOptions = null;
3847 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3848
Sunny Goyald478c832016-04-01 12:04:16 -07003849 // Bind succeeded
3850 if (success) {
3851 // If the widget has a configure activity, it is still needs to set it up,
3852 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003853 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003854 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3855 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003856 }
Sunny Goyald478c832016-04-01 12:04:16 -07003857
3858 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003859 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003860 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003861 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003862 // The widget was marked as UI not ready, but there is no configure activity to
3863 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003864 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3865 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003866 }
Sunny Goyalff572272014-07-23 13:58:07 -07003867 }
3868
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003869 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003870 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003871 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3872 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003873 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003874
Sunny Goyal56c73602015-09-25 12:55:01 -07003875 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003876 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003877 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003878 deleteWidgetInfo(item);
3879 return;
3880 }
3881
Sunny Goyal233ee962015-08-03 13:05:01 -07003882 item.minSpanX = appWidgetInfo.minSpanX;
3883 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003884 addAppWidgetToWorkspace(
3885 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3886 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003887 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003888 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003889 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003890 view.updateAppWidget(null);
3891 view.setOnClickListener(this);
3892 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003893 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003894 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003895
Daniel Sandler843e8602010-06-07 14:59:01 -04003896 if (DEBUG_WIDGETS) {
3897 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3898 + (SystemClock.uptimeMillis()-start) + "ms");
3899 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003900 }
3901
Sunny Goyalff572272014-07-23 13:58:07 -07003902 /**
3903 * Restores a pending widget.
3904 *
3905 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003906 */
Sunny Goyald478c832016-04-01 12:04:16 -07003907 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003908 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3909 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3910 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003911 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003912 }
3913
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003914 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003915 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003916
3917 mWorkspace.reinflateWidgetsIfNecessary();
3918 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003919 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003920 }
3921
Adam Cohen1462de32012-07-24 22:34:36 -07003922 public void onPageBoundSynchronously(int page) {
3923 mSynchronouslyBoundPages.add(page);
3924 }
3925
Sunny Goyal527c7d32015-08-28 15:19:36 -07003926 @Override
3927 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3928 if (mPendingExecutor != null) {
3929 mPendingExecutor.markCompleted();
3930 }
3931 mPendingExecutor = executor;
3932 executor.attachTo(this);
3933 }
3934
3935 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3936 if (mPendingExecutor == executor) {
3937 mPendingExecutor = null;
3938 }
3939 }
3940
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003941 @Override
3942 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3943 Runnable r = new Runnable() {
3944 public void run() {
3945 finishFirstPageBind(executor);
3946 }
3947 };
3948 if (waitUntilResume(r)) {
3949 return;
3950 }
3951
3952 Runnable onComplete = new Runnable() {
3953 @Override
3954 public void run() {
3955 if (executor != null) {
3956 executor.onLoadAnimationCompleted();
3957 }
3958 }
3959 };
3960 if (mDragLayer.getAlpha() < 1) {
3961 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3962 } else {
3963 onComplete.run();
3964 }
3965 }
3966
Joe Onorato9c1289c2009-08-17 11:03:03 -04003967 /**
3968 * Callback saying that there aren't any more items to bind.
3969 *
3970 * Implementation of the method from LauncherModel.Callbacks.
3971 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003972 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003973 Runnable r = new Runnable() {
3974 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003975 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003976 }
3977 };
3978 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003979 return;
3980 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003981 if (LauncherAppState.PROFILE_STARTUP) {
3982 Trace.beginSection("Page bind completed");
3983 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003984 if (mSavedState != null) {
3985 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003986 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003987 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003988
Joe Onorato9c1289c2009-08-17 11:03:03 -04003989 mSavedState = null;
3990 }
3991
Adam Cohen1462de32012-07-24 22:34:36 -07003992 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003993
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003994 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003995
Sunny Goyal2100c782016-08-22 16:00:03 -07003996 if (mPendingActivityResult != null) {
3997 handleActivityResult(mPendingActivityResult.requestCode,
3998 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3999 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07004000 }
4001
Sunny Goyal756adbc2015-04-16 15:20:51 -07004002 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004003
4004 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004005 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004006 }
Sunny Goyale26d1002016-06-20 14:52:14 -07004007 if (LauncherAppState.PROFILE_STARTUP) {
4008 Trace.endSection();
4009 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004010 }
4011
Winson Chunga2413752012-04-03 14:22:34 -07004012 private boolean canRunNewAppsAnimation() {
4013 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07004014 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07004015 }
4016
Winson Chungc9168342013-06-26 14:54:55 -07004017 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004018 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004019 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4020 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004021 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004022 return bounceAnim;
4023 }
4024
Adam Cohen27772732013-11-11 14:00:56 +00004025 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004026 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004027 }
4028
Tony Wickham55616cd2015-09-23 14:55:17 -07004029 public int getSearchBarHeight() {
4030 if (mLauncherCallbacks != null) {
4031 return mLauncherCallbacks.getSearchBarHeight();
4032 }
4033 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4034 }
4035
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004036 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004037 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4038 * multiple calls to bind the same list.)
4039 */
4040 @Thunk ArrayList<AppInfo> mTmpAppsList;
4041 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4042 public void run() {
4043 bindAllApplications(mTmpAppsList);
4044 mTmpAppsList = null;
4045 }
4046 };
4047
4048 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004049 * Add the icons for all apps.
4050 *
4051 * Implementation of the method from LauncherModel.Callbacks.
4052 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004053 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004054 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4055 mTmpAppsList = apps;
4056 return;
4057 }
4058
Winson Chungb745afb2015-03-02 11:51:23 -08004059 if (mAppsView != null) {
4060 mAppsView.setApps(apps);
4061 }
Adam Cohen9211d422014-10-07 18:14:53 -07004062 if (mLauncherCallbacks != null) {
4063 mLauncherCallbacks.bindAllApplications(apps);
4064 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004065 }
4066
4067 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004068 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
4069 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
4070 */
4071 @Override
4072 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
4073 mDeepShortcutMap = deepShortcutMapCopy;
4074 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
4075 }
4076
Tony Wickham1bce7fd2016-04-28 17:39:03 -07004077 public List<String> getShortcutIdsForItem(ItemInfo info) {
4078 if (!DeepShortcutManager.supportsShortcuts(info)) {
4079 return Collections.EMPTY_LIST;
4080 }
4081 ComponentName component = info.getTargetComponent();
Hyunyoung Songd3bf9802016-08-29 14:43:53 -07004082 if (component == null) {
4083 return Collections.EMPTY_LIST;
4084 }
4085
Tony Wickham1bce7fd2016-04-28 17:39:03 -07004086 List<String> ids = mDeepShortcutMap.get(new ComponentKey(component, info.user));
4087 return ids == null ? Collections.EMPTY_LIST : ids;
4088 }
4089
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004090 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004091 * A package was updated.
4092 *
4093 * Implementation of the method from LauncherModel.Callbacks.
4094 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004095 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004096 Runnable r = new Runnable() {
4097 public void run() {
4098 bindAppsUpdated(apps);
4099 }
4100 };
4101 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004102 return;
4103 }
4104
Winson Chungb745afb2015-03-02 11:51:23 -08004105 if (mAppsView != null) {
4106 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004107 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004108 }
4109
Sunny Goyal4390ace2014-10-13 11:33:11 -07004110 @Override
4111 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4112 Runnable r = new Runnable() {
4113 public void run() {
4114 bindWidgetsRestored(widgets);
4115 }
4116 };
4117 if (waitUntilResume(r)) {
4118 return;
4119 }
4120 mWorkspace.widgetsRestored(widgets);
4121 }
4122
Joe Onorato9c1289c2009-08-17 11:03:03 -04004123 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004124 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004125 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004126 *
4127 * @param updated list of shortcuts which have changed.
4128 * @param removed list of shortcuts which were deleted in the background. This can happen when
4129 * an app gets removed from the system or some of its components are no longer
4130 * available.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004131 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004132 @Override
4133 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4134 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004135 Runnable r = new Runnable() {
4136 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004137 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004138 }
4139 };
4140 if (waitUntilResume(r)) {
4141 return;
4142 }
4143
Sunny Goyal4390ace2014-10-13 11:33:11 -07004144 if (!updated.isEmpty()) {
4145 mWorkspace.updateShortcuts(updated);
4146 }
4147
4148 if (!removed.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004149 HashSet<ComponentName> removedComponents = new HashSet<>();
4150 HashSet<ShortcutKey> removedDeepShortcuts = new HashSet<>();
4151
Sunny Goyal4390ace2014-10-13 11:33:11 -07004152 for (ShortcutInfo si : removed) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004153 if (si.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07004154 removedDeepShortcuts.add(ShortcutKey.fromShortcutInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004155 } else {
4156 removedComponents.add(si.getTargetComponent());
4157 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004158 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004159
4160 if (!removedComponents.isEmpty()) {
4161 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(removedComponents, user);
4162 mWorkspace.removeItemsByMatcher(matcher);
4163 mDragController.onAppsRemoved(matcher);
4164 }
4165
4166 if (!removedDeepShortcuts.isEmpty()) {
4167 ItemInfoMatcher matcher = ItemInfoMatcher.ofShortcutKeys(removedDeepShortcuts);
4168 mWorkspace.removeItemsByMatcher(matcher);
4169 mDragController.onAppsRemoved(matcher);
4170 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004171 }
4172 }
4173
4174 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004175 * Update the state of a package, typically related to install state.
4176 *
4177 * Implementation of the method from LauncherModel.Callbacks.
4178 */
Sunny Goyale755d462014-07-22 13:48:29 -07004179 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004180 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4181 Runnable r = new Runnable() {
4182 public void run() {
4183 bindRestoreItemsChange(updates);
4184 }
4185 };
4186 if (waitUntilResume(r)) {
4187 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004188 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004189
Sunny Goyal756adbc2015-04-16 15:20:51 -07004190 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004191 }
4192
4193 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004194 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004195 * in addition to specific applications to remove, the reason being that
4196 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004197 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004198 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004199 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004200 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004201 public void bindWorkspaceComponentsRemoved(
4202 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4203 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004204 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004205 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004206 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004207 }
Winson Chungd64d1762013-08-20 14:37:16 -07004208 };
4209 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004210 return;
4211 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004212 if (!packageNames.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004213 ItemInfoMatcher matcher = ItemInfoMatcher.ofPackages(packageNames, user);
4214 mWorkspace.removeItemsByMatcher(matcher);
4215 mDragController.onAppsRemoved(matcher);
4216
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004217 }
4218 if (!components.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004219 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(components, user);
4220 mWorkspace.removeItemsByMatcher(matcher);
4221 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004222 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004223 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004224
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004225 @Override
4226 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4227 Runnable r = new Runnable() {
4228 public void run() {
4229 bindAppInfosRemoved(appInfos);
4230 }
4231 };
4232 if (waitUntilResume(r)) {
4233 return;
Joe Onorato36115782010-06-17 13:28:48 -04004234 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004235
Winson Chungdf95eb12013-10-16 14:57:07 -07004236 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004237 if (mAppsView != null) {
4238 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004239 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004240 }
Joe Onoratobe386092009-11-17 17:32:16 -08004241
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004242 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004243 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004244 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004245 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004246 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004247
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004248 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004249 public void bindWidgetsModel(WidgetsModel model) {
4250 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004251 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004252 return;
4253 }
4254
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004255 if (mWidgetsView != null && model != null) {
4256 mWidgetsView.addWidgets(model);
4257 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004258 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004259 }
4260
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004261 @Override
4262 public void notifyWidgetProvidersChanged() {
4263 if (mWorkspace.getState().shouldUpdateWidget) {
4264 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4265 }
4266 }
4267
Winson Chung400438b2011-01-16 17:53:48 -08004268 private int mapConfigurationOriActivityInfoOri(int configOri) {
4269 final Display d = getWindowManager().getDefaultDisplay();
4270 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4271 switch (d.getRotation()) {
4272 case Surface.ROTATION_0:
4273 case Surface.ROTATION_180:
4274 // We are currently in the same basic orientation as the natural orientation
4275 naturalOri = configOri;
4276 break;
4277 case Surface.ROTATION_90:
4278 case Surface.ROTATION_270:
4279 // We are currently in the other basic orientation to the natural orientation
4280 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4281 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4282 break;
4283 }
4284
4285 int[] oriMap = {
4286 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4287 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4288 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4289 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4290 };
4291 // Since the map starts at portrait, we need to offset if this device's natural orientation
4292 // is landscape.
4293 int indexOffset = 0;
4294 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4295 indexOffset = 1;
4296 }
4297 return oriMap[(d.getRotation() + indexOffset) % 4];
4298 }
Adam Cohen446e9402011-09-15 18:21:21 -07004299
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004300 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004301 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004302 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004303 if (Utilities.ATLEAST_JB_MR2) {
4304 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4305 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004306 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4307 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004308 }
Winson Chung4b919f82012-05-01 10:44:08 -07004309 }
4310 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004311
Winson Chung4b919f82012-05-01 10:44:08 -07004312 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004313 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004314 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004315 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004316 } else {
4317 mHandler.postDelayed(new Runnable() {
4318 public void run() {
4319 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4320 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004321 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004322 }
Winson Chung4b919f82012-05-01 10:44:08 -07004323 }
Winson Chung400438b2011-01-16 17:53:48 -08004324 }
4325
Hyunyoung Songc001cf52016-07-21 17:32:43 -07004326 private void markAppsViewShown() {
4327 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4328 return;
4329 }
4330 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
4331 }
4332
4333 private boolean shouldShowDiscoveryBounce() {
4334 if (mState != mState.WORKSPACE) {
4335 return false;
4336 }
4337 if (mLauncherCallbacks != null && mLauncherCallbacks.shouldShowDiscoveryBounce()) {
4338 return true;
4339 }
4340 if (!mIsResumeFromActionScreenOff) {
4341 return false;
4342 }
4343 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4344 return false;
4345 }
4346 return true;
4347 }
4348
Winson Chung5ffd51d2015-06-25 11:36:50 -07004349 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004350 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004351 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004352 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004353 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004354 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004355 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4356 if (userHandle != null) {
4357 user = UserHandleCompat.fromUser(userHandle);
4358 }
4359 }
4360 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004361 }
4362
4363 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004364 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004365 if (user == null) {
4366 user = UserHandleCompat.myUserHandle();
4367 }
4368
4369 // Called from search suggestion, add the profile extra to the intent to ensure that we
4370 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004371 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004372 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004373 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004374 return null;
4375 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004376 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004377 }
4378
Winson Chung5ffd51d2015-06-25 11:36:50 -07004379 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004380 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4381 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004382 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004383 UserHandleCompat.myUserHandle());
4384 }
4385
Winson Chung5ffd51d2015-06-25 11:36:50 -07004386 protected void moveWorkspaceToDefaultScreen() {
4387 mWorkspace.moveToDefaultScreen(false);
4388 }
4389
Winson Chung80baf5a2010-08-09 16:03:15 -07004390 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004391 * Returns a FastBitmapDrawable with the icon, accurately sized.
4392 */
4393 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4394 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4395 d.setFilterBitmap(true);
4396 resizeIconDrawable(d);
4397 return d;
4398 }
4399
4400 /**
4401 * Resizes an icon drawable to the correct icon size.
4402 */
Winson5fbe0742015-09-30 15:33:00 -07004403 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004404 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004405 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004406 }
4407
4408 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004409 * Prints out out state for debugging.
4410 */
4411 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004412 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004413 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004414 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
Sunny Goyal2100c782016-08-22 16:00:03 -07004415 Log.d(TAG, "mPendingRequestArgs=" + mPendingRequestArgs);
4416 Log.d(TAG, "mPendingActivityResult=" + mPendingActivityResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004417 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004418 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004419
Daniel Sandler325dc232013-06-05 22:57:57 -04004420 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004421 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004422
4423 @Override
4424 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4425 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004426 // Dump workspace
4427 writer.println(prefix + "Workspace Items");
4428 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4429 writer.println(prefix + " Homescreen " + i);
4430
4431 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4432 for (int j = 0; j < layout.getChildCount(); j++) {
4433 Object tag = layout.getChildAt(j).getTag();
4434 if (tag != null) {
4435 writer.println(prefix + " " + tag.toString());
4436 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004437 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004438 }
Sunny Goyala1365452015-10-01 15:46:24 -07004439
4440 writer.println(prefix + " Hotseat");
4441 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4442 for (int j = 0; j < layout.getChildCount(); j++) {
4443 Object tag = layout.getChildAt(j).getTag();
4444 if (tag != null) {
4445 writer.println(prefix + " " + tag.toString());
4446 }
4447 }
4448
Sunny Goyal713edfc2016-05-06 09:58:34 -07004449 try {
4450 FileLog.flushAll(writer);
4451 } catch (Exception e) {
4452 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004453 }
4454
Adam Cohen9211d422014-10-07 18:14:53 -07004455 if (mLauncherCallbacks != null) {
4456 mLauncherCallbacks.dump(prefix, fd, writer, args);
4457 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004458 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004459
Adam Cohen59400422014-03-05 18:07:04 -08004460 public static CustomAppWidget getCustomAppWidget(String name) {
4461 return sCustomAppWidgets.get(name);
4462 }
4463
4464 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4465 return sCustomAppWidgets;
4466 }
4467
Sunny Goyal29538332016-02-18 09:10:19 -08004468 public static List<View> getFolderContents(View icon) {
4469 if (icon instanceof FolderIcon) {
4470 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4471 } else {
4472 return Collections.EMPTY_LIST;
4473 }
4474 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004475
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004476 public static Launcher getLauncher(Context context) {
4477 if (context instanceof Launcher) {
4478 return (Launcher) context;
4479 }
4480 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4481 }
4482
Sunny Goyal745bad92016-05-02 10:54:12 -07004483 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4484
4485 @Override
4486 public void onSharedPreferenceChanged(
4487 SharedPreferences sharedPreferences, String key) {
4488 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4489 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4490 if (!waitUntilResume(this, true)) {
4491 run();
4492 }
4493 }
4494 }
4495
4496 @Override
4497 public void run() {
4498 setOrientation();
4499 }
4500 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004501}