blob: e1c64a72d04aac966c995bf8548a710a484cdefb [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;
Sunny Goyal756cd262015-08-20 12:33:21 -070038import android.content.ContentValues;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070040import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040041import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070044import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070046import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070050import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
53import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070054import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070056import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020057import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080059import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070060import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070061import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040062import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070063import android.os.Trace;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070064import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.text.Selection;
66import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070067import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070069import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070070import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070071import android.view.HapticFeedbackConstants;
72import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.MotionEvent;
75import android.view.Surface;
76import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070077import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080078import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.ViewGroup;
80import android.view.ViewTreeObserver;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080081import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070082import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070083import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080087import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070089
Sunny Goyal651077b2014-06-30 14:15:31 -070090import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070091import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyalae502842016-06-17 08:43:56 -070092import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070093import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070094import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080095import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -070096import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010097import com.android.launcher3.compat.LauncherActivityInfoCompat;
98import com.android.launcher3.compat.LauncherAppsCompat;
99import com.android.launcher3.compat.UserHandleCompat;
100import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800101import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700102import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700103import com.android.launcher3.dragndrop.DragController;
104import com.android.launcher3.dragndrop.DragLayer;
105import 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;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700118import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700119import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700120import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700121import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal322d5562015-06-25 19:35:49 -0700122import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700123import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700124import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700125import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700126import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700127import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700128
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700129import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700130import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700131import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700132import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800133import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700134import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700135import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700136import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700137
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138/**
139 * Default launcher application.
140 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100141public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700142 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
143 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700144 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700145 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700146 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700148 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700149 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400150 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700151
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700153 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700154 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700155 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
Michael Jurka8b805b12012-04-18 14:23:14 -0700157 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700158 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700159 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700160
Sunny Goyal28c6b962015-10-12 11:42:05 -0700161 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
162
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700163 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
164
Mathew Inwood876a8462013-06-14 14:12:41 +0100165 /**
166 * IntentStarter uses request codes starting with this. This must be greater than all activity
167 * request codes used internally.
168 */
169 protected static final int REQUEST_LAST = 100;
170
Michael Jurka0a457bf2012-11-19 14:05:05 -0800171 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700172 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800173 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174
Winson Chung2672ff92012-05-04 16:22:30 -0700175 // The Intent extra that defines whether to ignore the launch animation
176 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400177 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700178
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700179 public static final String ACTION_APPWIDGET_HOST_RESET =
180 "com.android.launcher3.intent.ACTION_APPWIDGET_HOST_RESET";
181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 // Type: int
183 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700184 // Type: int
185 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700186 // Type: Content Values / parcelable
187 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700188 // Type: parcelable
189 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000190 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800191 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
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
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700239 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800240 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800241 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700242
Michael Jurka0280c3b2010-09-17 15:00:07 -0700243 private int[] mTmpAddItemCellCoordinates = new int[2];
244
Sunny Goyal316490e2015-06-02 09:38:28 -0700245 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800246 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700247
Michael Jurka838a4ca2011-02-07 13:33:06 -0800248 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700249 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700250
Sunny Goyal47328fd2016-05-25 18:56:41 -0700251 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700252
253 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700254 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700255 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700256
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700257 // Main container view and the model for the widget tray screen.
258 @Thunk WidgetsContainerView mWidgetsView;
259 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700260
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700262 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
263 // scroll issues (because the workspace may not have been measured yet) and extra work.
264 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
265 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266
267 private SpannableStringBuilder mDefaultKeySsb = null;
268
Adam Cohen091440a2015-03-18 14:16:05 -0700269 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400270
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800271 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 private boolean mRestoring;
273 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700274 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275
Michael Jurka1e2f4652013-07-08 18:03:46 -0700276 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700277 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700278 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700279
Joe Onorato9c1289c2009-08-17 11:03:03 -0400280 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800281 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700282 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700283 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700284 private boolean mIsResumeFromActionScreenOff;
Adam Cohen091440a2015-03-18 14:16:05 -0700285 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800286 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700287 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800288 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400289
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700290 /** Maps launcher activity components to their list of shortcut ids. */
291 private MultiHashMap<ComponentKey, String> mDeepShortcutMap = new MultiHashMap<>();
292
Adam Cohen61f560d2013-09-30 15:58:20 -0700293 private View.OnTouchListener mHapticFeedbackTouchListener;
294
Adam Cohended9f8d2010-11-03 13:25:16 -0700295 // Related to the auto-advancing of widgets
296 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700297 private static final int ADVANCE_INTERVAL = 20000;
298 private static final int ADVANCE_STAGGER = 250;
299
300 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700301 private long mAutoAdvanceSentTime;
302 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700303 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700304
Winson Chung400438b2011-01-16 17:53:48 -0800305 // Determines how long to wait after a rotation before restoring the screen orientation to
306 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700307 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800308
Adam Cohen1462de32012-07-24 22:34:36 -0700309 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700310 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700311
Winson Chung46353de2012-02-16 14:05:10 -0800312 // We only want to get the SharedPreferences once since it does an FS stat each time we get
313 // it from the context.
314 private SharedPreferences mSharedPrefs;
315
Winson Chungf0c6ae02012-03-21 16:10:31 -0700316 // Holds the page that we need to animate to, and the icon views that we need to animate up
317 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700318 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700319 private Bitmap mFolderIconBitmap;
320 private Canvas mFolderIconCanvas;
321 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700322
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700323 private DeviceProfile mDeviceProfile;
324
Adam Cohen4b66bf32015-09-18 12:15:19 -0700325 private boolean mMoveToDefaultScreenFromNewIntent;
326
Winson Chung13eb5272015-05-11 16:30:13 -0700327 // This is set to the view that launched the activity that navigated the user away from
328 // launcher. Since there is no callback for when the activity has finished launching, enable
329 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800330 private BubbleTextView mWaitingForResume;
331
Adam Cohen59400422014-03-05 18:07:04 -0800332 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
333 new HashMap<String, CustomAppWidget>();
334
Adam Cohen59400422014-03-05 18:07:04 -0800335 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700336 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
337 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800338 }
339 }
340
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700341 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
342 // state transition. If another state transition happened during this delay,
343 // simply unregister this runnable.
344 private Runnable mExitSpringLoadedModeRunnable;
345
Adam Cohen091440a2015-03-18 14:16:05 -0700346 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700347 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700348 if (mWorkspace != null) {
349 mWorkspace.buildPageHardwareLayers();
350 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700351 }
352 };
353
Adam Cohendb364c32014-05-20 14:23:40 -0700354 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800355
Adam Cohen091440a2015-03-18 14:16:05 -0700356 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800357 int requestCode;
358 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700359 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700360 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800361 int cellX;
362 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700363 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800364 }
365
Hyunyoung Songaa953652016-04-19 18:30:24 -0700366 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800367
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700368 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700369 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400370
371 @Thunk void setOrientation() {
372 if (mRotationEnabled) {
373 unlockScreenOrientation(true);
374 } else {
375 setRequestedOrientation(
376 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700377 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400378 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700379
Sunny Goyal745bad92016-05-02 10:54:12 -0700380 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700381
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 @Override
383 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700384 if (DEBUG_STRICT_MODE) {
385 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
386 .detectDiskReads()
387 .detectDiskWrites()
388 .detectNetwork() // or .detectAll() for all detectable problems
389 .penaltyLog()
390 .build());
391 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
392 .detectLeakedSqlLiteObjects()
393 .detectLeakedClosableObjects()
394 .penaltyLog()
395 .penaltyDeath()
396 .build());
397 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700398 if (LauncherAppState.PROFILE_STARTUP) {
399 Trace.beginSection("Launcher-onCreate");
400 }
Winson Chunga2413752012-04-03 14:22:34 -0700401
Adam Cohen9211d422014-10-07 18:14:53 -0700402 if (mLauncherCallbacks != null) {
403 mLauncherCallbacks.preOnCreate();
404 }
405
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400407
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400408 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700409
Adam Cohen2e6da152015-05-06 11:42:25 -0700410 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700411 mDeviceProfile = getResources().getConfiguration().orientation
412 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700413 app.getInvariantDeviceProfile().landscapeProfile
414 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700415
Sunny Goyalf7258242015-10-19 16:59:07 -0700416 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700417 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800418 mModel = app.setLauncher(this);
419 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700420 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700421
Joe Onorato41a12d22009-10-31 18:30:00 -0400422 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700423 mAllAppsController = new AllAppsTransitionController(this);
424 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700425
Sunny Goyalffe83f12014-08-14 17:39:34 -0700426 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700427
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700428 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
429 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700430
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700431 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
432 // this also ensures that any synchronous binding below doesn't re-trigger another
433 // LauncherModel load.
434 mPaused = false;
435
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700437
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 setupViews();
Winson1f064272016-07-18 17:18:02 -0700439 mDeviceProfile.layout(this, false /* notifyListeners */);
Tony Wickham827cef22016-03-17 15:39:39 -0700440 mExtractedColors = new ExtractedColors();
441 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442
Tony Wickhame2217252016-03-22 16:34:23 -0700443 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
444 .addAccessibilityStateChangeListener(this);
445
Joe Onorato7c312c12009-08-13 21:36:53 -0700446 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700447
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 mSavedState = savedInstanceState;
449 restoreState(mSavedState);
450
Sunny Goyale26d1002016-06-20 14:52:14 -0700451 if (LauncherAppState.PROFILE_STARTUP) {
452 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 }
454
455 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700456 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700457 // If the user leaves launcher, then we should just load items asynchronously when
458 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700459 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700460 } else {
461 // We only load the page synchronously if the user rotates (or triggers a
462 // configuration change) while launcher is in the foreground
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700463 if (!mModel.startLoader(mWorkspace.getRestorePage())) {
464 // If we are not binding synchronously, show a fade in animation when
465 // the first page bind completes.
466 mDragLayer.setAlpha(0);
467 }
Winson Chung4a2afa32012-07-19 14:53:05 -0700468 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 }
470
471 // For handling default keys
472 mDefaultKeySsb = new SpannableStringBuilder();
473 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800474
475 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700476 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
477 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800478
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800479 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700480 // In case we are on a device with locked rotation, we should look at preferences to check
481 // if the user has specifically allowed rotation.
482 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700483 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700484 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
485 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700486 }
487
488 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
489 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400490 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700491
Adam Cohen9211d422014-10-07 18:14:53 -0700492 if (mLauncherCallbacks != null) {
493 mLauncherCallbacks.onCreate(savedInstanceState);
494 }
495 }
496
Sunny Goyal7779d622015-06-11 16:18:39 -0700497 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700498 public void onExtractedColorsChanged() {
499 loadExtractedColorsAndColorItems();
500 }
501
502 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700503 // TODO: do this in pre-N as well, once the extraction part is complete.
504 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700505 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700506 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700507 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham827cef22016-03-17 15:39:39 -0700508 }
509 }
510
Adam Cohen9211d422014-10-07 18:14:53 -0700511 private LauncherCallbacks mLauncherCallbacks;
512
513 public void onPostCreate(Bundle savedInstanceState) {
514 super.onPostCreate(savedInstanceState);
515 if (mLauncherCallbacks != null) {
516 mLauncherCallbacks.onPostCreate(savedInstanceState);
517 }
518 }
519
Winson1f064272016-07-18 17:18:02 -0700520 public void onInsetsChanged(Rect insets) {
521 mDeviceProfile.updateInsets(insets);
522 mDeviceProfile.layout(this, true /* notifyListeners */);
523 }
524
Sunny Goyal32554d12015-12-03 15:31:25 -0800525 /**
526 * Call this after onCreate to set or clear overlay.
527 */
528 public void setLauncherOverlay(LauncherOverlay overlay) {
529 if (overlay != null) {
530 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
531 }
532 mWorkspace.setLauncherOverlay(overlay);
533 }
534
Adam Cohen9211d422014-10-07 18:14:53 -0700535 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
536 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700537 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700538 private boolean mWorkspaceImportanceStored = false;
539 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700540 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800541 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700542 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
543
544 @Override
545 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700546 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700547 return;
548 }
549 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700550 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700551 if (mWorkspace != null) {
552 mWorkspaceImportanceForAccessibility =
553 mWorkspace.getImportantForAccessibility();
554 mWorkspace.setImportantForAccessibility(
555 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
556 mWorkspaceImportanceStored = true;
557 }
558 if (mHotseat != null) {
559 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
560 mHotseat.setImportantForAccessibility(
561 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
562 mHotseatImportanceStored = true;
563 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700564 }
565
566 @Override
567 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700568 if (mWorkspaceImportanceStored && mWorkspace != null) {
569 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
570 }
571 if (mHotseatImportanceStored && mHotseat != null) {
572 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
573 }
574 mWorkspaceImportanceStored = false;
575 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700576 }
577 });
Adam Cohen9211d422014-10-07 18:14:53 -0700578 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700579 }
580
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700581 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700582 public void onLauncherProviderChange() {
583 if (mLauncherCallbacks != null) {
584 mLauncherCallbacks.onLauncherProviderChange();
585 }
586 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700587
Adam Cohen9211d422014-10-07 18:14:53 -0700588 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700589 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700590 if (mLauncherCallbacks != null) {
591 return mLauncherCallbacks.hasCustomContentToLeft();
592 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700593 return false;
594 }
595
Dave Hawkeya8881582013-09-17 15:55:33 -0600596 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500597 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600598 * {@link #addToCustomContentPage}. This will only be invoked if
599 * {@link #hasCustomContentToLeft()} is {@code true}.
600 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500601 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700602 if (mLauncherCallbacks != null) {
603 mLauncherCallbacks.populateCustomContentContainer();
604 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600605 }
606
607 /**
608 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
609 * ensure the custom content page is added or removed if necessary.
610 */
611 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600612 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600613 // Not bound yet, wait for bindScreens to be called.
614 return;
615 }
616
617 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
618 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500619 mWorkspace.createCustomContentContainer();
620 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600621 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
622 mWorkspace.removeCustomContentPage();
623 }
624 }
625
Hyunyoung Songaa953652016-04-19 18:30:24 -0700626 public UserEventDispatcher getUserEventDispatcher() {
627 if (mLauncherCallbacks != null) {
628 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
629 if (dispatcher != null) {
630 return dispatcher;
631 }
632 }
633
Sunny Goyal64976d52016-06-20 14:56:28 -0700634 // Logger object is a singleton and does not have to be coupled with the foreground
635 // activity. Since most user event logging is done on the UI, the object is retrieved
636 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700637 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700638 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700639 }
640 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800641 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100642
Hyunyoung Song3f471442015-04-08 19:01:34 -0700643 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700644 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
645 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700646 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700647 }
648
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000649 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700650 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
651 // This cast is safe as long as the id < 0x00FFFFFF
652 // Since we jail all the dynamically generated views, there should be no clashes
653 // with any other views.
654 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000655 }
656
657 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700658 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
659 * a configuration step, this allows the proper animations to run after other transitions.
660 */
Adam Cohendb364c32014-05-20 14:23:40 -0700661 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700662 long screenId = args.screenId;
663 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
664 // When the screen id represents an actual screen (as opposed to a rank) we make sure
665 // that the drop page actually exists.
666 screenId = ensurePendingDropLayoutExists(args.screenId);
667 }
Adam Cohendb364c32014-05-20 14:23:40 -0700668
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800669 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800670 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700671 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700672 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700673 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800674 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700675 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700676 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700677 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyald478c832016-04-01 12:04:16 -0700678 completeRestoreAppWidget(args.appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700679 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700680 case REQUEST_BIND_PENDING_APPWIDGET: {
681 int widgetId = args.appWidgetId;
682 LauncherAppWidgetInfo info =
683 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
684 if (info != null) {
685 // Since the view was just bound, also launch the configure activity if needed
686 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
687 .getLauncherAppWidgetInfo(widgetId);
688 if (provider != null && provider.configure != null) {
689 startRestoredWidgetReconfigActivity(provider, info);
690 }
691 }
692 break;
693 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800694 }
Michael Jurka27614d22012-04-02 06:40:22 -0700695 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
696 // if you turned the screen off and then back while in All Apps, Launcher would not
697 // return to the workspace. Clearing mAddInfo.container here fixes this issue
698 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700699 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800700 }
701
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800702 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700703 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700704 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700705 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700706 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800707 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700708
Adam Cohenad4e15c2013-10-17 16:21:35 -0700709 Runnable exitSpringLoaded = new Runnable() {
710 @Override
711 public void run() {
712 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
713 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
714 }
715 };
716
Michael Jurka8b805b12012-04-18 14:23:14 -0700717 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700718 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700719 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700720 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
721 if (resultCode == RESULT_CANCELED) {
722 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700723 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700724 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700725 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800726 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700727 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700728 }
729 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200730 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
731 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700732 // User could have free-scrolled between pages before picking a wallpaper; make sure
733 // we move to the closest one now.
734 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700735 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200736 }
737 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700738 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200739
Adam Cohened66b2b2012-01-23 17:28:51 -0800740 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700741 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700742
Adam Cohendb364c32014-05-20 14:23:40 -0700743 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800744 // We have special handling for widgets
745 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800746 final int appWidgetId;
747 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
748 : -1;
749 if (widgetId < 0) {
750 appWidgetId = pendingAddWidgetId;
751 } else {
752 appWidgetId = widgetId;
753 }
754
Adam Cohenad4e15c2013-10-17 16:21:35 -0700755 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800756 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700757 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
758 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700759 result = RESULT_CANCELED;
760 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700761 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700762 @Override
763 public void run() {
764 exitSpringLoadedDragModeDelayed(false, 0, null);
765 }
766 };
Adam Cohendb364c32014-05-20 14:23:40 -0700767 if (workspaceLocked) {
768 // No need to remove the empty screen if we're mid-binding, as the
769 // the bind will not add the empty screen.
770 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
771 } else {
772 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
773 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
774 }
Winson Chung5aaab772012-04-27 15:24:02 -0700775 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700776 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700777 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
778 // When the screen id represents an actual screen (as opposed to a rank)
779 // we make sure that the drop page actually exists.
780 mPendingAddInfo.screenId =
781 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
782 }
Adam Cohendb364c32014-05-20 14:23:40 -0700783 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
784
785 dropLayout.setDropPending(true);
786 final Runnable onComplete = new Runnable() {
787 @Override
788 public void run() {
789 completeTwoStageWidgetDrop(resultCode, appWidgetId);
790 dropLayout.setDropPending(false);
791 }
792 };
793 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
794 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
795 } else {
796 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
797 mPendingAddInfo);
798 sPendingAddItem = args;
799 }
Winson Chung5aaab772012-04-27 15:24:02 -0700800 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800801 return;
802 }
803
Sunny Goyald478c832016-04-01 12:04:16 -0700804 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
805 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700806 if (resultCode == RESULT_OK) {
807 // Update the widget view.
808 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
809 pendingAddWidgetId, mPendingAddInfo);
810 if (workspaceLocked) {
811 sPendingAddItem = args;
812 } else {
813 completeAdd(args);
814 }
815 }
816 // Leave the widget in the pending state if the user canceled the configure.
817 return;
818 }
819
Sunny Goyalaad90582015-07-09 14:22:50 -0700820 if (requestCode == REQUEST_CREATE_SHORTCUT) {
821 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
822 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
823 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
824 mPendingAddInfo);
825 if (isWorkspaceLocked()) {
826 sPendingAddItem = args;
827 } else {
828 completeAdd(args);
829 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
830 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
831 }
832 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700833 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
834 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800837 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800838
839 }
840
841 @Override
842 protected void onActivityResult(
843 final int requestCode, final int resultCode, final Intent data) {
844 handleActivityResult(requestCode, resultCode, data);
845 if (mLauncherCallbacks != null) {
846 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
847 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800848 }
849
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600850 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700851 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600852 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700853 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
854 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
855 View v = null;
856 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
857 if (layout != null) {
858 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
859 }
860 Intent intent = sPendingAddItem.intent;
861 sPendingAddItem = null;
862 if (grantResults.length > 0
863 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700864 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700865 } else {
866 // TODO: Show a snack bar with link to settings
867 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700868 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700869 }
870 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600871 if (mLauncherCallbacks != null) {
872 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
873 grantResults);
874 }
875 }
876
Adam Cohendb364c32014-05-20 14:23:40 -0700877 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
878 appWidgetId, ItemInfo info) {
879 PendingAddArguments args = new PendingAddArguments();
880 args.requestCode = requestCode;
881 args.intent = data;
882 args.container = info.container;
883 args.screenId = info.screenId;
884 args.cellX = info.cellX;
885 args.cellY = info.cellY;
886 args.appWidgetId = appWidgetId;
887 return args;
888 }
889
890 /**
891 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
892 *
893 * @param screenId the screen id to check
894 * @return the new screen, or screenId if it exists
895 */
896 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800897 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700898 if (dropLayout == null) {
899 // it's possible that the add screen was removed because it was
900 // empty and a re-bind occurred
901 mWorkspace.addExtraEmptyScreen();
902 return mWorkspace.commitExtraEmptyScreen();
903 } else {
904 return screenId;
905 }
906 }
907
Adam Cohen091440a2015-03-18 14:16:05 -0700908 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800909 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800910 Runnable onCompleteRunnable = null;
911 int animationType = 0;
912
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700913 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800914 if (resultCode == RESULT_OK) {
915 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
916 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700917 mPendingAddWidgetInfo);
918 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800919 onCompleteRunnable = new Runnable() {
920 @Override
921 public void run() {
922 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700923 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700924 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
925 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800926 }
927 };
928 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800929 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800930 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800931 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700932 if (mDragLayer.getAnimatedView() != null) {
933 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
934 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
935 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700936 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700937 // The animated view may be null in the case of a rotation during widget configuration
938 onCompleteRunnable.run();
939 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 }
941
942 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700943 protected void onStop() {
944 super.onStop();
945 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700946
947 if (mLauncherCallbacks != null) {
948 mLauncherCallbacks.onStop();
949 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700950
951 if (Utilities.isNycMR1OrAbove()) {
952 mAppWidgetHost.stopListening();
953 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700954 }
955
956 @Override
957 protected void onStart() {
958 super.onStart();
959 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700960
961 if (mLauncherCallbacks != null) {
962 mLauncherCallbacks.onStart();
963 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700964
965 if (Utilities.isNycMR1OrAbove()) {
966 mAppWidgetHost.startListening();
967 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700968 }
969
970 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200972 long startTime = 0;
973 if (DEBUG_RESUME_TIME) {
974 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400975 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200976 }
Adam Cohen9211d422014-10-07 18:14:53 -0700977
978 if (mLauncherCallbacks != null) {
979 mLauncherCallbacks.preOnResume();
980 }
981
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700983 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700984
Winson Chung4a2afa32012-07-19 14:53:05 -0700985 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700986 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700987 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800988 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700989 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700990 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700991 // view after launching an app, as they may be depending on the UI to be static to
992 // switch to another app, otherwise, if it was
Hyunyoung Songdd60ce42016-07-27 17:08:38 -0700993 showAppsView(false /* animated */, !launchedFromApp /* updatePredictedApps */,
994 false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800995 } else if (mOnResumeState == State.WIDGETS) {
996 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700997 }
998 mOnResumeState = State.NONE;
999
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001000 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001001 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001002 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001003 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001004 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001005 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001007 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001008 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1009 // execute them here
1010 long startTimeCallbacks = 0;
1011 if (DEBUG_RESUME_TIME) {
1012 startTimeCallbacks = System.currentTimeMillis();
1013 }
1014
Michael Jurka1e2f4652013-07-08 18:03:46 -07001015 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1016 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001017 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001018 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001019 if (DEBUG_RESUME_TIME) {
1020 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1021 (System.currentTimeMillis() - startTimeCallbacks));
1022 }
Michael Jurka447bf842013-05-15 14:52:15 +02001023 }
Michael Jurka54554252013-08-01 12:52:23 +02001024 if (mOnResumeCallbacks.size() > 0) {
1025 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1026 mOnResumeCallbacks.get(i).run();
1027 }
1028 mOnResumeCallbacks.clear();
1029 }
Winson Chunge4e50662012-01-23 14:45:13 -08001030
1031 // Reset the pressed state of icons that were locked in the press state while activities
1032 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001033 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001034 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001035 mWaitingForResume.setStayPressed(false);
1036 }
Winson Chung82963d52013-10-09 11:20:57 -07001037
Adam Cohen06dff352012-06-01 17:17:08 -07001038 // It is possible that widgets can receive updates while launcher is not in the foreground.
1039 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1040 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1041 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001042 if (!isWorkspaceLoading()) {
1043 getWorkspace().reinflateWidgetsIfNecessary();
1044 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001045
Michael Jurka447bf842013-05-15 14:52:15 +02001046 if (DEBUG_RESUME_TIME) {
1047 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1048 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001049
Adam Cohen4b66bf32015-09-18 12:15:19 -07001050 // We want to suppress callbacks about CustomContent being shown if we have just received
1051 // onNewIntent while the user was present within launcher. In that case, we post a call
1052 // to move the user to the main screen (which will occur after onResume). We don't want to
1053 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1054 // suppress here.
1055 if (mWorkspace.getCustomContentCallbacks() != null
1056 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001057 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001058 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001059 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1060 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001061 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001062 }
1063 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001064 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001065 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001066 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001067
Sunny Goyal756adbc2015-04-16 15:20:51 -07001068 if (!isWorkspaceLoading()) {
1069 // Process any items that were added while Launcher was away.
1070 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001071
1072 // Refresh shortcuts if the permission changed.
1073 mModel.refreshShortcutsIfRequired();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001074 }
Adam Cohen9211d422014-10-07 18:14:53 -07001075
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001076 if (shouldShowDiscoveryBounce()) {
1077 mAllAppsController.showDiscoveryBounce();
1078 }
1079 mIsResumeFromActionScreenOff = false;
Adam Cohen9211d422014-10-07 18:14:53 -07001080 if (mLauncherCallbacks != null) {
1081 mLauncherCallbacks.onResume();
1082 }
Adam Cohen06dff352012-06-01 17:17:08 -07001083 }
1084
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001086 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001087 // Ensure that items added to Launcher are queued until Launcher returns
1088 InstallShortcutReceiver.enableInstallQueue();
1089
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001090 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001091 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001092 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001093 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001094
1095 // We call onHide() aggressively. The custom content callbacks should be able to
1096 // debounce excess onHide calls.
1097 if (mWorkspace.getCustomContentCallbacks() != null) {
1098 mWorkspace.getCustomContentCallbacks().onHide();
1099 }
Romain Guycbb89e42009-06-08 15:52:54 -07001100
Adam Cohen9211d422014-10-07 18:14:53 -07001101 if (mLauncherCallbacks != null) {
1102 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001103 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001104 }
1105
1106 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001107 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1108 // by a onResume or by scrolling otherwise.
1109 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001110
1111 // Custom content is completely hidden
1112 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001113
1114 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1115 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001116
1117 // Indicates whether the user is allowed to scroll away from the custom content.
1118 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001119 }
1120
Adam Cohenc2d6e892014-10-16 09:49:24 -07001121 public interface LauncherOverlay {
1122
1123 /**
1124 * Touch interaction leading to overscroll has begun
1125 */
1126 public void onScrollInteractionBegin();
1127
1128 /**
1129 * Touch interaction related to overscroll has ended
1130 */
1131 public void onScrollInteractionEnd();
1132
1133 /**
1134 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1135 * screen (or in the case of RTL, the rightmost screen).
1136 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001137 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001138
1139 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001140 * Called when the launcher is ready to use the overlay
1141 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001142 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001143 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001144 }
1145
Jun Mukai8af0cd82015-05-12 12:32:12 -07001146 public interface LauncherSearchCallbacks {
1147 /**
1148 * Called when the search overlay is shown.
1149 */
1150 public void onSearchOverlayOpened();
1151
1152 /**
1153 * Called when the search overlay is dismissed.
1154 */
1155 public void onSearchOverlayClosed();
1156 }
1157
Adam Cohenc2d6e892014-10-16 09:49:24 -07001158 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001159 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001160 }
1161
1162 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1163
Sunny Goyalc86df472016-02-25 09:19:38 -08001164 public void onScrollChanged(float progress) {
1165 if (mWorkspace != null) {
1166 mWorkspace.onOverlayScrollChanged(progress);
1167 }
1168 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001169 }
1170
Jorim Jaggid017f882014-01-14 17:08:48 -08001171 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001172 if (mLauncherCallbacks != null) {
1173 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001174 } else {
1175 // On devices with a locked orientation, we will at least have the allow rotation
1176 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001177 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001178 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001179 }
1180
Adam Cohen9211d422014-10-07 18:14:53 -07001181 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001182 CustomContentCallbacks callbacks, String description) {
1183 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001184 }
1185
Adam Cohenedb40762013-07-18 16:45:45 -07001186 // The custom content needs to offset its content to account for the QSB
1187 public int getTopOffsetForCustomContent() {
1188 return mWorkspace.getPaddingTop();
1189 }
1190
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001191 @Override
1192 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001193 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001194 if (mModel.isCurrentCallbacks(this)) {
1195 mModel.stopLoader();
1196 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001197 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1198
Romain Guy13c2e7b2010-03-10 19:45:00 -08001199 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001200 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001201
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001202 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001203 @Override
1204 public void onWindowFocusChanged(boolean hasFocus) {
1205 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001206 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001207
1208 if (mLauncherCallbacks != null) {
1209 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1210 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001211 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001212
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213 private boolean acceptFilter() {
1214 final InputMethodManager inputManager = (InputMethodManager)
1215 getSystemService(Context.INPUT_METHOD_SERVICE);
1216 return !inputManager.isFullscreenMode();
1217 }
1218
1219 @Override
1220 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001221 final int uniChar = event.getUnicodeChar();
1222 final boolean handled = super.onKeyDown(keyCode, event);
1223 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1224 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1226 keyCode, event);
1227 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001228 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001229 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001230 // showSearchDialog()
1231 // If there are multiple keystrokes before the search dialog takes focus,
1232 // onSearchRequested() will be called for every keystroke,
1233 // but it is idempotent, so it's fine.
1234 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 }
1236 }
1237
Joe Onorato8a9625e2010-01-28 15:55:35 -08001238 // Eat the long press event so the keyboard doesn't come up.
1239 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1240 return true;
1241 }
1242
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 return handled;
1244 }
1245
Winson46163472015-09-22 17:31:56 -07001246 @Override
1247 public boolean onKeyUp(int keyCode, KeyEvent event) {
1248 if (keyCode == KeyEvent.KEYCODE_MENU) {
1249 // Ignore the menu key if we are currently dragging or are on the custom content screen
1250 if (!isOnCustomContent() && !mDragController.isDragging()) {
1251 // Close any open folders
1252 closeFolder();
1253
Tony Wickham49c8d292016-07-01 11:44:34 -07001254 // Close any shortcuts containers
1255 closeShortcutsContainer();
1256
Winson46163472015-09-22 17:31:56 -07001257 // Stop resizing any widgets
1258 mWorkspace.exitWidgetResizeMode();
1259
1260 // Show the overview mode if we are on the workspace
1261 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1262 !mWorkspace.isSwitchingState()) {
1263 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001264 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001265 }
1266 }
1267 return true;
1268 }
1269 return super.onKeyUp(keyCode, event);
1270 }
1271
Karl Rosaen138a0412009-04-23 19:00:21 -07001272 private String getTypedText() {
1273 return mDefaultKeySsb.toString();
1274 }
1275
1276 private void clearTypedText() {
1277 mDefaultKeySsb.clear();
1278 mDefaultKeySsb.clearSpans();
1279 Selection.setSelection(mDefaultKeySsb, 0);
1280 }
1281
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001283 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1284 * State
1285 */
1286 private static State intToState(int stateOrdinal) {
1287 State state = State.WORKSPACE;
1288 final State[] stateValues = State.values();
1289 for (int i = 0; i < stateValues.length; i++) {
1290 if (stateValues[i].ordinal() == stateOrdinal) {
1291 state = stateValues[i];
1292 break;
1293 }
1294 }
1295 return state;
1296 }
1297
1298 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 * Restores the previous state, if it exists.
1300 *
1301 * @param savedState The previous state.
1302 */
1303 private void restoreState(Bundle savedState) {
1304 if (savedState == null) {
1305 return;
1306 }
1307
Michael Jurka883f55b2010-10-21 15:47:14 -07001308 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001309 if (state == State.APPS || state == State.WIDGETS) {
1310 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001311 }
1312
Adam Cohen21cd0022013-10-09 18:57:02 -07001313 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1314 PagedView.INVALID_RESTORE_PAGE);
1315 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001316 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 }
1318
Sunny Goyal756cd262015-08-20 12:33:21 -07001319 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1320 if (itemValues != null) {
1321 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001322 AppWidgetProviderInfo info = savedState.getParcelable(
1323 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001324 mPendingAddWidgetInfo = info == null ?
1325 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1326
Adam Cohen4637b5a2013-11-04 18:21:24 -08001327 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001328 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 mRestoring = true;
1330 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 }
1332
1333 /**
1334 * Finds all the views we need and configure them properly.
1335 */
1336 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001337 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001338 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001339 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001340 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyal6178f132016-07-11 17:30:03 -07001341 mQsbContainer = mDragLayer.findViewById(mDeviceProfile.isVerticalBarLayout()
1342 ? R.id.workspace_blocked_row : R.id.qsb_container);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001343 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001344
Sunny Goyal784f9c32016-03-23 16:56:54 -07001345 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1346 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1347 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348
Winson Chung4c98d922011-05-31 16:50:48 -07001349 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001350 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001351
Winson Chung3d503fb2011-07-13 17:25:49 -07001352 // Setup the hotseat
1353 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1354 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001355 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001356 }
1357
Winsone9f27272015-10-13 10:47:51 -07001358 // Setup the overview panel
1359 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001360
Winson Chung4c98d922011-05-31 16:50:48 -07001361 // Setup the workspace
1362 mWorkspace.setHapticFeedbackEnabled(false);
1363 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001364 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001365 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1366 // default state, otherwise we will update to the wrong offsets in RTL
1367 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001368 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001369 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001370
Tony Wickham34d2c912015-09-11 09:27:58 -07001371 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001372 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001373
Winson Chungef7f8742015-06-04 17:18:17 -07001374 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001375 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001376 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001377 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1378 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1379 } else {
1380 mAppsView.setSearchBarController(new DefaultAppSearchController());
1381 }
Craig Mautner360310b2012-10-26 15:13:08 -07001382
Winson Chung3d503fb2011-07-13 17:25:49 -07001383 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001384 mDragController.setDragScoller(mWorkspace);
1385 mDragController.setScrollView(mDragLayer);
1386 mDragController.setMoveTarget(mWorkspace);
1387 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001388 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001389
Peter Schiller310a9882016-06-30 13:52:36 -07001390 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1391 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1392 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001393
Sunny Goyal322d5562015-06-25 19:35:49 -07001394 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1395 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001396 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 }
1398
Winsone9f27272015-10-13 10:47:51 -07001399 private void setupOverviewPanel() {
1400 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1401
1402 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1403 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1404 @Override
1405 public boolean onLongClick(View v) {
1406 return v.performClick();
1407 }
1408 };
1409
1410 // Bind wallpaper button actions
1411 View wallpaperButton = findViewById(R.id.wallpaper_button);
1412 wallpaperButton.setOnClickListener(new OnClickListener() {
1413 @Override
1414 public void onClick(View view) {
1415 if (!mWorkspace.isSwitchingState()) {
1416 onClickWallpaperPicker(view);
1417 }
1418 }
1419 });
1420 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1421 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1422
1423 // Bind widget button actions
1424 mWidgetsButton = findViewById(R.id.widget_button);
1425 mWidgetsButton.setOnClickListener(new OnClickListener() {
1426 @Override
1427 public void onClick(View view) {
1428 if (!mWorkspace.isSwitchingState()) {
1429 onClickAddWidgetButton(view);
1430 }
1431 }
1432 });
1433 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1434 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1435
1436 // Bind settings actions
1437 View settingsButton = findViewById(R.id.settings_button);
1438 boolean hasSettings = hasSettings();
1439 if (hasSettings) {
1440 settingsButton.setOnClickListener(new OnClickListener() {
1441 @Override
1442 public void onClick(View view) {
1443 if (!mWorkspace.isSwitchingState()) {
1444 onClickSettingsButton(view);
1445 }
1446 }
1447 });
1448 settingsButton.setOnLongClickListener(performClickOnLongClick);
1449 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1450 } else {
1451 settingsButton.setVisibility(View.GONE);
1452 }
1453
1454 mOverviewPanel.setAlpha(0f);
1455 }
1456
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001458 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001459 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001460 */
1461 public void setAllAppsButton(View allAppsButton) {
1462 mAllAppsButton = allAppsButton;
1463 }
1464
Sunny Goyalbb011da2016-06-15 15:42:29 -07001465 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001466 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001467 }
1468
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001469 public View getWidgetsButton() {
1470 return mWidgetsButton;
1471 }
1472
Anjali Koppal5ad44842014-03-10 20:34:39 -07001473 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 * Creates a view representing a shortcut.
1475 *
1476 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001478 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001479 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 }
1481
1482 /**
1483 * Creates a view representing a shortcut inflated from the specified resource.
1484 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 * @param parent The group the shortcut belongs to.
1486 * @param info The data structure describing the shortcut.
1487 *
1488 * @return A View inflated from layoutResId.
1489 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001490 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001491 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001492 parent, false);
1493 favorite.applyFromShortcutInfo(info, mIconCache);
1494 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001496 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 return favorite;
1498 }
1499
1500 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 * Add a shortcut to the workspace.
1502 *
1503 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001505 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001506 int cellY) {
1507 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001508 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001509
Sunny Goyal5c97f512015-05-19 16:03:28 -07001510 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001511 if (info == null) {
1512 return;
1513 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001514 final View view = createShortcut(info);
1515
Sunny Goyal5c97f512015-05-19 16:03:28 -07001516 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001517 // First we check if we already know the exact location where we want to add this item.
1518 if (cellX >= 0 && cellY >= 0) {
1519 cellXY[0] = cellX;
1520 cellXY[1] = cellY;
1521 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001522
1523 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001524 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001525 true, null,null)) {
1526 return;
1527 }
1528 DragObject dragObject = new DragObject();
1529 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001530 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1531 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001532 return;
1533 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001534 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001535 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001536 }
1537
1538 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001539 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001540 return;
1541 }
1542
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001543 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544
1545 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001546 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001547 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 }
1549 }
1550
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001552 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001554 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 */
Adam Cohen091440a2015-03-18 14:16:05 -07001556 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001557 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1558
1559 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001560 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001561 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001562 }
Romain Guycbb89e42009-06-08 15:52:54 -07001563
Adam Cohen59400422014-03-05 18:07:04 -08001564 if (appWidgetInfo.isCustomWidget) {
1565 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001566 }
1567
Adam Cohen59400422014-03-05 18:07:04 -08001568 LauncherAppWidgetInfo launcherInfo;
1569 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1570 launcherInfo.spanX = info.spanX;
1571 launcherInfo.spanY = info.spanY;
1572 launcherInfo.minSpanX = info.minSpanX;
1573 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001574 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001575
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001577 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578
1579 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001580 if (hostView == null) {
1581 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001582 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001583 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001584 hostView.setVisibility(View.VISIBLE);
1585 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001587 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588 }
Romain Guycbb89e42009-06-08 15:52:54 -07001589
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001590 private void addAppWidgetToWorkspace(
1591 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001592 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001593 hostView.setTag(item);
1594 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001595
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001596 hostView.setFocusable(true);
1597 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001598
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001599 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001600 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1601
1602 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001603 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001604 }
1605 }
1606
Adam Cohended9f8d2010-11-03 13:25:16 -07001607 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1608 @Override
1609 public void onReceive(Context context, Intent intent) {
1610 final String action = intent.getAction();
1611 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1612 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001613 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001614 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001615
Winson Chungc100e8e2011-08-09 16:02:43 -07001616 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001617 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001618 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001619 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001620 if (!showWorkspace(false)) {
1621 // If we are already on the workspace, then manually reset all apps
1622 mAppsView.reset();
1623 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001624 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001625 mIsResumeFromActionScreenOff = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001626 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1627 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001628 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001629 }
1630 }
1631 };
1632
1633 @Override
1634 public void onAttachedToWindow() {
1635 super.onAttachedToWindow();
1636
1637 // Listen for broadcasts related to user-presence
1638 final IntentFilter filter = new IntentFilter();
1639 filter.addAction(Intent.ACTION_SCREEN_OFF);
1640 filter.addAction(Intent.ACTION_USER_PRESENT);
1641 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001642 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001643 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001644 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001645
1646 if (mLauncherCallbacks != null) {
1647 mLauncherCallbacks.onAttachedToWindow();
1648 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001649 }
1650
1651 @Override
1652 public void onDetachedFromWindow() {
1653 super.onDetachedFromWindow();
1654 mVisible = false;
1655
Adam Cohend113e0c2010-11-11 10:48:05 -08001656 if (mAttached) {
1657 unregisterReceiver(mReceiver);
1658 mAttached = false;
1659 }
Winson Chungb745afb2015-03-02 11:51:23 -08001660 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001661
1662 if (mLauncherCallbacks != null) {
1663 mLauncherCallbacks.onDetachedFromWindow();
1664 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001665 }
1666
1667 public void onWindowVisibilityChanged(int visibility) {
1668 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001669 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001670 // The following code used to be in onResume, but it turns out onResume is called when
1671 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1672 // is a more appropriate event to handle
1673 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001674 if (!mWorkspaceLoading) {
1675 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001676 // We want to let Launcher draw itself at least once before we force it to build
1677 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001678 // apps is nice and speedy.
1679 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001680 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001681 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001682 if (mStarted) return;
1683 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001684 // We delay the layer building a bit in order to give
1685 // other message processing a time to run. In particular
1686 // this avoids a delay in hiding the IME if it was
1687 // currently shown, because doing that may involve
1688 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001689 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001690 final ViewTreeObserver.OnDrawListener listener = this;
1691 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001692 public void run() {
1693 if (mWorkspace != null &&
1694 mWorkspace.getViewTreeObserver() != null) {
1695 mWorkspace.getViewTreeObserver().
1696 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001697 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001698 }
1699 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001700 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001701 }
1702 });
1703 }
1704 clearTypedText();
1705 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001706 }
1707
Adam Cohen091440a2015-03-18 14:16:05 -07001708 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001709 mHandler.removeMessages(ADVANCE_MSG);
1710 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1711 mHandler.sendMessageDelayed(msg, delay);
1712 mAutoAdvanceSentTime = System.currentTimeMillis();
1713 }
1714
Adam Cohen091440a2015-03-18 14:16:05 -07001715 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001716 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1717 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1718 mAutoAdvanceRunning = autoAdvanceRunning;
1719 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001720 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001721 sendAdvanceMessage(delay);
1722 } else {
1723 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001724 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001725 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1726 }
1727 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001728 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001729 }
1730 }
1731 }
1732
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001733 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1734
Adam Cohended9f8d2010-11-03 13:25:16 -07001735 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001736 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001737 if (msg.what == ADVANCE_MSG) {
1738 int i = 0;
1739 for (View key: mWidgetsToAdvance.keySet()) {
1740 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001741 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001742 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001743 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001744 public void run() {
1745 ((Advanceable) v).advance();
1746 }
1747 }, delay);
1748 }
1749 i++;
1750 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001751 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001752 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001753 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001754 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001755 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001756
Winsonc0b52fe2015-09-09 16:38:15 -07001757 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001758 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001759 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1760 if (v instanceof Advanceable) {
1761 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001762 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001763 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001764 }
1765 }
1766
Winsonc0b52fe2015-09-09 16:38:15 -07001767 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001768 if (mWidgetsToAdvance.containsKey(hostView)) {
1769 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001770 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001771 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001772 }
1773
Hyunyoung Song3f471442015-04-08 19:01:34 -07001774 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001775 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1776 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001777 }
1778
Winson Chunga6945242014-01-08 14:04:34 -08001779 public DragLayer getDragLayer() {
1780 return mDragLayer;
1781 }
1782
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001783 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001784 return mAppsView;
1785 }
1786
Hyunyoung Song3f471442015-04-08 19:01:34 -07001787 public WidgetsContainerView getWidgetsView() {
1788 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001789 }
1790
Winson Chunga6945242014-01-08 14:04:34 -08001791 public Workspace getWorkspace() {
1792 return mWorkspace;
1793 }
1794
Sunny Goyal6178f132016-07-11 17:30:03 -07001795 public View getQsbContainer() {
1796 return mQsbContainer;
1797 }
1798
Winson Chunga6945242014-01-08 14:04:34 -08001799 public Hotseat getHotseat() {
1800 return mHotseat;
1801 }
1802
Jorim Jaggid017f882014-01-14 17:08:48 -08001803 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001804 return mOverviewPanel;
1805 }
1806
Sunny Goyal47328fd2016-05-25 18:56:41 -07001807 public DropTargetBar getDropTargetBar() {
1808 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001809 }
1810
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001811 public LauncherAppWidgetHost getAppWidgetHost() {
1812 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001813 }
Romain Guycbb89e42009-06-08 15:52:54 -07001814
Winson Chunga9abd0e2010-10-27 17:18:37 -07001815 public LauncherModel getModel() {
1816 return mModel;
1817 }
1818
Adam Cohen79d90c52016-04-22 13:29:20 -07001819 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001820 return mSharedPrefs;
1821 }
1822
Adam Cohen2e6da152015-05-06 11:42:25 -07001823 public DeviceProfile getDeviceProfile() {
1824 return mDeviceProfile;
1825 }
1826
Bjorn Bringertc459e522013-06-07 19:36:01 +01001827 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001828 getWindow().closeAllPanels();
1829
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001830 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001831 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001832 }
1833
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834 @Override
1835 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001836 long startTime = 0;
1837 if (DEBUG_RESUME_TIME) {
1838 startTime = System.currentTimeMillis();
1839 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840 super.onNewIntent(intent);
1841
1842 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001843 Folder openFolder = mWorkspace.getOpenFolder();
1844 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1845 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1846 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1847 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1848 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001849 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001850 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001851
Adam Cohen6fecd412013-10-02 17:41:50 -07001852 if (mWorkspace == null) {
1853 // Can be cases where mWorkspace is null, this prevents a NPE
1854 return;
1855 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001856 // In all these cases, only animate if we're already on home
1857 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001858
Sunny Goyal935fca12015-10-13 10:19:01 -07001859 closeFolder(alreadyOnHome);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001860 closeShortcutsContainer(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001861 exitSpringLoadedDragMode();
1862
1863 // If we are already on home, then just animate back to the workspace,
1864 // otherwise, just wait until onResume to set the state back to Workspace
1865 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001866 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001867 } else {
1868 mOnResumeState = State.WORKSPACE;
1869 }
1870
1871 final View v = getWindow().peekDecorView();
1872 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001873 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001874 INPUT_METHOD_SERVICE);
1875 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1876 }
1877
Winson Chungb745afb2015-03-02 11:51:23 -08001878 // Reset the apps view
1879 if (!alreadyOnHome && mAppsView != null) {
1880 mAppsView.scrollToTop();
1881 }
1882
Hyunyoung Song3f471442015-04-08 19:01:34 -07001883 // Reset the widgets view
1884 if (!alreadyOnHome && mWidgetsView != null) {
1885 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001886 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001887
Adam Cohen9211d422014-10-07 18:14:53 -07001888 if (mLauncherCallbacks != null) {
1889 mLauncherCallbacks.onHomeIntent();
1890 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 }
Adam Cohened307df2013-10-02 09:37:31 -07001892
Adam Cohen9211d422014-10-07 18:14:53 -07001893 if (mLauncherCallbacks != null) {
1894 mLauncherCallbacks.onNewIntent(intent);
1895 }
Winson15f8b172015-08-19 11:02:39 -07001896
1897 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1898 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1899 // animation.
1900 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001901 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1902 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001903 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1904 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001905
1906 // We use this flag to suppress noisy callbacks above custom content state
1907 // from onResume.
1908 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001909 mWorkspace.post(new Runnable() {
1910 @Override
1911 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001912 if (mWorkspace != null) {
1913 mWorkspace.moveToDefaultScreen(true);
1914 }
Winson15f8b172015-08-19 11:02:39 -07001915 }
1916 });
1917 }
1918 }
1919
1920 if (DEBUG_RESUME_TIME) {
1921 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1922 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001923 }
1924
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001926 public void onRestoreInstanceState(Bundle state) {
1927 super.onRestoreInstanceState(state);
1928 for (int page: mSynchronouslyBoundPages) {
1929 mWorkspace.restoreInstanceStateForChild(page);
1930 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 }
1932
1933 @Override
1934 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001935 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001936 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1937 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001938
Adam Cohen21cd0022013-10-09 18:57:02 -07001939 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001940 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941
Michael Jurka883f55b2010-10-21 15:47:14 -07001942 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001943 // We close any open folder since it will not be re-opened, and we need to make sure
1944 // this state is reflected.
1945 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1946 closeFolder(false);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001947 closeShortcutsContainer(false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001948
Adam Cohendcd297f2013-06-18 13:13:40 -07001949 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001950 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001951 ContentValues itemValues = new ContentValues();
1952 mPendingAddInfo.writeToValues(itemValues);
1953 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001954 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001955 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956 }
1957
Adam Cohen9211d422014-10-07 18:14:53 -07001958 if (mLauncherCallbacks != null) {
1959 mLauncherCallbacks.onSaveInstanceState(outState);
1960 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001961 }
1962
1963 @Override
1964 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001966
Winson Chunge7a03942011-08-05 15:05:12 -07001967 // Remove all pending runnables
1968 mHandler.removeMessages(ADVANCE_MSG);
1969 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001970 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001971 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001972
Winson Chungcd2b0142011-06-08 16:02:26 -07001973 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001974 // It's possible to receive onDestroy after a new Launcher activity has
1975 // been created. In this case, don't interfere with the new Launcher.
1976 if (mModel.isCurrentCallbacks(this)) {
1977 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001978 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001979 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001980
Sunny Goyal745bad92016-05-02 10:54:12 -07001981 if (mRotationPrefChangeHandler != null) {
1982 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1983 }
1984
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001986 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001988 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001990 mAppWidgetHost = null;
1991
1992 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993
1994 TextKeyListener.getInstance().release();
1995
Tony Wickhame2217252016-03-22 16:34:23 -07001996 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1997 .removeAccessibilityStateChangeListener(this);
1998
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001999 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002000
Michael Jurka2ecf9952012-06-18 12:52:28 -07002001 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002002
2003 if (mLauncherCallbacks != null) {
2004 mLauncherCallbacks.onDestroy();
2005 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002006 }
2007
Sunny Goyalae502842016-06-17 08:43:56 -07002008 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
2009 return mAccessibilityDelegate;
2010 }
2011
Adam Cohena9cf38f2011-05-02 15:36:58 -07002012 public DragController getDragController() {
2013 return mDragController;
2014 }
2015
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002016 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002017 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07002018 onStartForResult(requestCode);
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002019 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07002020 }
2021
2022 @Override
2023 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2024 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2025 onStartForResult(requestCode);
2026 try {
2027 super.startIntentSenderForResult(intent, requestCode,
2028 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2029 } catch (IntentSender.SendIntentException e) {
2030 throw new ActivityNotFoundException();
2031 }
2032 }
2033
2034 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002035 if (requestCode >= 0) {
2036 setWaitingForResult(true);
2037 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 }
2039
Winson Chung70d72102011-08-12 11:24:35 -07002040 /**
2041 * Indicates that we want global search for this activity by setting the globalSearch
2042 * argument for {@link #startSearch} to true.
2043 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002044 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002045 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002047
Karl Rosaen138a0412009-04-23 19:00:21 -07002048 if (initialQuery == null) {
2049 // Use any text typed in the launcher as the initial query
2050 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002051 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002052 if (appSearchData == null) {
2053 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002054 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002056
2057 // TODO send proper bounds.
2058 Rect sourceBounds = null;
Romain Guycbb89e42009-06-08 15:52:54 -07002059
Ian Parkinson047036e2014-09-01 15:40:53 +01002060 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002061 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002062 if (clearTextImmediately) {
2063 clearTypedText();
2064 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002065
2066 // We need to show the workspace after starting the search
2067 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002068 }
2069
Ian Parkinson047036e2014-09-01 15:40:53 +01002070 /**
2071 * Start a text search.
2072 *
2073 * @return {@code true} if the search will start immediately, so any further keypresses
2074 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2075 * to buffer keypresses.
2076 */
2077 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002078 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002079 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2080 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2081 sourceBounds);
2082 }
2083
Michael Jurkaa33411c2012-06-14 16:18:21 -07002084 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002085 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002086 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002087 }
2088
2089 /**
2090 * Starts the global search activity. This code is a copied from SearchManager
2091 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002092 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002093 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002094 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002095 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2096 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2097 if (globalSearchActivity == null) {
2098 Log.w(TAG, "No global search activity found.");
2099 return;
2100 }
2101 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2102 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2103 intent.setComponent(globalSearchActivity);
2104 // Make sure that we have a Bundle to put source in
2105 if (appSearchData == null) {
2106 appSearchData = new Bundle();
2107 } else {
2108 appSearchData = new Bundle(appSearchData);
2109 }
Adam Cohen9211d422014-10-07 18:14:53 -07002110 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002111 if (!appSearchData.containsKey("source")) {
2112 appSearchData.putString("source", getPackageName());
2113 }
2114 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2115 if (!TextUtils.isEmpty(initialQuery)) {
2116 intent.putExtra(SearchManager.QUERY, initialQuery);
2117 }
2118 if (selectInitialQuery) {
2119 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2120 }
2121 intent.setSourceBounds(sourceBounds);
2122 try {
2123 startActivity(intent);
2124 } catch (ActivityNotFoundException ex) {
2125 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2126 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002127 }
2128
Yura4f93ec62014-02-04 14:15:21 +00002129 public boolean isOnCustomContent() {
2130 return mWorkspace.isOnOrMovingToCustomContent();
2131 }
2132
Winson Chung70d72102011-08-12 11:24:35 -07002133 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002134 public boolean onPrepareOptionsMenu(Menu menu) {
2135 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002136 if (mLauncherCallbacks != null) {
2137 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2138 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002139 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002140 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002141
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002142 @Override
2143 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002144 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002145 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002146 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002147 }
2148
Joe Onorato9c1289c2009-08-17 11:03:03 -04002149 public boolean isWorkspaceLocked() {
2150 return mWorkspaceLoading || mWaitingForResult;
2151 }
2152
Adam Cohen517a7f52014-03-01 12:12:59 -08002153 public boolean isWorkspaceLoading() {
2154 return mWorkspaceLoading;
2155 }
2156
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002157 private void setWorkspaceLoading(boolean value) {
2158 boolean isLocked = isWorkspaceLocked();
2159 mWorkspaceLoading = value;
2160 if (isLocked != isWorkspaceLocked()) {
2161 onWorkspaceLockedChanged();
2162 }
2163 }
2164
2165 private void setWaitingForResult(boolean value) {
2166 boolean isLocked = isWorkspaceLocked();
2167 mWaitingForResult = value;
2168 if (isLocked != isWorkspaceLocked()) {
2169 onWorkspaceLockedChanged();
2170 }
2171 }
2172
Adam Cohen9211d422014-10-07 18:14:53 -07002173 protected void onWorkspaceLockedChanged() {
2174 if (mLauncherCallbacks != null) {
2175 mLauncherCallbacks.onWorkspaceLockedChanged();
2176 }
2177 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002178
Michael Jurka0280c3b2010-09-17 15:00:07 -07002179 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002180 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002181 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002182 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2183 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002184 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002185 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002186
Tony Wickhama0628cc2015-10-14 15:23:04 -07002187 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002188 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002189 if (LOGD) {
2190 Log.d(TAG, "Adding widget from drop");
2191 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002192 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2193 }
2194
Sunny Goyale6b63a32015-01-16 16:14:29 -08002195 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002196 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2197 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002198 if (appWidgetInfo.configure != null) {
2199 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002200 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002201
Bjorn Bringert7984c942009-12-09 15:38:25 +00002202 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002203 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2204 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2205
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002206 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002207 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002208 Runnable onComplete = new Runnable() {
2209 @Override
2210 public void run() {
2211 // Exit spring loaded mode if necessary after adding the widget
2212 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2213 null);
2214 }
2215 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002216 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002217 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002218 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002219 }
2220 }
Romain Guycbb89e42009-06-08 15:52:54 -07002221
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002222 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002223 // Close any folders that may be open.
2224 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002225 mWorkspace.moveToCustomContentScreen(animate);
2226 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002227
2228 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2229 int[] cell, int spanX, int spanY) {
2230 switch (info.itemType) {
2231 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2232 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2233 int span[] = new int[2];
2234 span[0] = spanX;
2235 span[1] = spanY;
2236 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2237 container, screenId, cell, span);
2238 break;
2239 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2240 processShortcutFromDrop(info.componentName, container, screenId, cell);
2241 break;
2242 default:
2243 throw new IllegalStateException("Unknown item type: " + info.itemType);
2244 }
2245 }
2246
Adam Cohenfbba09b2011-07-18 21:43:05 -07002247 /**
2248 * Process a shortcut drop.
2249 *
2250 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002251 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002252 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002253 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002254 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2255 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002256 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002257 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002258 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002259
2260 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002261 mPendingAddInfo.cellX = cell[0];
2262 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002263 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002264
2265 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2266 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002267 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002268 }
2269
Adam Cohenfbba09b2011-07-18 21:43:05 -07002270 /**
2271 * Process a widget drop.
2272 *
2273 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002274 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002275 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002276 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002277 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2278 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002279 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002280 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002281 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002282 mPendingAddInfo.minSpanX = info.minSpanX;
2283 mPendingAddInfo.minSpanY = info.minSpanY;
2284
Adam Cohenfbba09b2011-07-18 21:43:05 -07002285 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002286 mPendingAddInfo.cellX = cell[0];
2287 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002288 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002289 if (span != null) {
2290 mPendingAddInfo.spanX = span[0];
2291 mPendingAddInfo.spanY = span[1];
2292 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002293
Adam Cohened66b2b2012-01-23 17:28:51 -08002294 AppWidgetHostView hostView = info.boundWidget;
2295 int appWidgetId;
2296 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002297 // In the case where we've prebound the widget, we remove it from the DragLayer
2298 if (LOGD) {
2299 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2300 }
2301 getDragLayer().removeView(hostView);
2302
Adam Cohened66b2b2012-01-23 17:28:51 -08002303 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002304 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002305
2306 // Clear the boundWidget so that it doesn't get destroyed.
2307 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002308 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002309 // In this case, we either need to start an activity to get permission to bind
2310 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002311 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002312 Bundle options = info.bindOptions;
2313
Sunny Goyalffe83f12014-08-14 17:39:34 -07002314 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2315 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002316 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002317 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002318 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002319 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002320 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2321 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2322 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002323 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2324 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002325 // TODO: we need to make sure that this accounts for the options bundle.
2326 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002327 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2328 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002329 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002330 }
2331
Adam Cohendcd297f2013-06-18 13:13:40 -07002332 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002333 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002334 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002335 folderInfo.title = getText(R.string.folder_name);
2336
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002337 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002338 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2339 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002340
2341 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002342 FolderIcon newFolder =
2343 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002344 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002345 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002346 // Force measure the new folder icon
2347 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2348 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002349 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002350 }
Romain Guycbb89e42009-06-08 15:52:54 -07002351
Winsonc0b52fe2015-09-09 16:38:15 -07002352 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002353 * Unbinds the view for the specified item, and removes the item and all its children.
2354 *
2355 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002356 * @param itemInfo the {@link ItemInfo} for this view.
2357 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002358 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002359 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002360 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002361 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002362 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2363 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002364 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002365 } else {
2366 mWorkspace.removeWorkspaceItem(v);
2367 }
Winsonc0b52fe2015-09-09 16:38:15 -07002368 if (deleteFromDb) {
2369 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2370 }
2371 } else if (itemInfo instanceof FolderInfo) {
2372 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002373 if (v instanceof FolderIcon) {
2374 ((FolderIcon) v).removeListeners();
2375 }
Winsonc0b52fe2015-09-09 16:38:15 -07002376 mWorkspace.removeWorkspaceItem(v);
2377 if (deleteFromDb) {
2378 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2379 }
2380 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2381 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002382 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002383 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002384 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002385 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002386 }
2387 } else {
2388 return false;
2389 }
2390 return true;
2391 }
2392
2393 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002394 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002395 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002396 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002397 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002398 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002399 // Deleting an app widget ID is a void call but writes to disk before returning
2400 // to the caller...
2401 new AsyncTask<Void, Void, Void>() {
2402 public Void doInBackground(Void ... args) {
2403 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2404 return null;
2405 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002406 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002407 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002408 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002409 }
2410
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411 @Override
2412 public boolean dispatchKeyEvent(KeyEvent event) {
2413 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2414 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002415 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002416 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002417 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002418 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002419 dumpState();
2420 return true;
2421 }
2422 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002423 }
2424 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2425 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002426 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002427 return true;
2428 }
2429 }
2430
2431 return super.dispatchKeyEvent(event);
2432 }
2433
Joe Onorato88ec0992009-11-19 13:16:06 -08002434 @Override
2435 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002436 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2437 return;
2438 }
2439
Sunny Goyal45478022015-06-08 16:52:41 -07002440 if (mDragController.isDragging()) {
2441 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002442 return;
2443 }
2444
Tony Wickham49c8d292016-07-01 11:44:34 -07002445 if (getOpenShortcutsContainer() != null) {
2446 closeShortcutsContainer();
2447 } else if (isAppsViewVisible()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002448 showWorkspace(true);
2449 } else if (isWidgetsViewVisible()) {
2450 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002451 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002452 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002453 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002454 Folder openFolder = mWorkspace.getOpenFolder();
2455 if (openFolder.isEditingName()) {
2456 openFolder.dismissEditingName();
2457 } else {
2458 closeFolder();
2459 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002460 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002461 mWorkspace.exitWidgetResizeMode();
2462
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002463 // Back button is a no-op here, but give at least some feedback for the button press
2464 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002465 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002466 }
2467
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002468 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002469 * Launches the intent referred by the clicked shortcut.
2470 *
2471 * @param v The view representing the clicked shortcut.
2472 */
2473 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002474 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2475 // view has detached (it's possible for this to happen if the view is removed mid touch).
2476 if (v.getWindowToken() == null) {
2477 return;
2478 }
2479
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002480 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002481 return;
2482 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002483
Adam Cohen1697b792013-09-17 19:08:21 -07002484 if (v instanceof Workspace) {
2485 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002486 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002487 }
2488 return;
2489 }
2490
2491 if (v instanceof CellLayout) {
2492 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002493 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2494 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002495 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002496 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002497 }
2498
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002499 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002500 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002501 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002502 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002503 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002504 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002505 }
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002506 } else if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2507 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002508 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002509 } else if (tag instanceof AppInfo) {
2510 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002511 } else if (tag instanceof LauncherAppWidgetInfo) {
2512 if (v instanceof PendingAppWidgetHostView) {
2513 onClickPendingWidget((PendingAppWidgetHostView) v);
2514 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002515 }
2516 }
2517
Sunny Goyal70660032015-05-14 00:07:08 -07002518 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002519 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002520 return false;
2521 }
2522
Michael Jurkaaf442092010-06-10 17:01:57 -07002523 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002524 * Event handler for the app widget view which has not fully restored.
2525 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002526 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002527 if (mIsSafeModeEnabled) {
2528 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2529 return;
2530 }
2531
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002532 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002533 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002534 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2535 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2536 // This should not happen, as we make sure that an Id is allocated during bind.
2537 return;
2538 }
2539 LauncherAppWidgetProviderInfo appWidgetInfo =
2540 mAppWidgetManager.findProvider(info.providerName, info.user);
2541 if (appWidgetInfo != null) {
2542 mPendingAddWidgetId = info.appWidgetId;
2543 mPendingAddInfo.copyFrom(info);
2544 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002545
Sunny Goyald478c832016-04-01 12:04:16 -07002546 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2547 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingAddWidgetId);
2548 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
2549 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2550 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2551 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2552 }
2553 } else {
2554 LauncherAppWidgetProviderInfo appWidgetInfo =
2555 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2556 if (appWidgetInfo != null) {
2557 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2558 }
Sunny Goyalff572272014-07-23 13:58:07 -07002559 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002560 } else if (info.installProgress < 0) {
2561 // The install has not been queued
2562 final String packageName = info.providerName.getPackageName();
2563 showBrokenAppInstallDialog(packageName,
2564 new DialogInterface.OnClickListener() {
2565 public void onClick(DialogInterface dialog, int id) {
2566 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2567 }
2568 });
2569 } else {
2570 // Download has started.
2571 final String packageName = info.providerName.getPackageName();
2572 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002573 }
2574 }
2575
Sunny Goyald478c832016-04-01 12:04:16 -07002576 private void startRestoredWidgetReconfigActivity(
2577 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
2578 mPendingAddWidgetInfo = provider;
2579 mPendingAddInfo.copyFrom(info);
2580 mPendingAddWidgetId = info.appWidgetId;
2581 mAppWidgetManager.startConfigActivity(provider,
2582 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2583 }
2584
Sunny Goyalff572272014-07-23 13:58:07 -07002585 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002586 * Event handler for the "grid" button that appears on the home screen, which
2587 * enters all apps mode.
2588 *
2589 * @param v The view that was clicked.
2590 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002591 protected void onClickAllAppsButton(View v) {
2592 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002593 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002594 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.TAP,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002595 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002596 showAppsView(true /* animated */, true /* updatePredictedApps */,
2597 false /* focusSearchBar */);
Winson Chung76648c52015-07-10 14:33:23 -07002598 }
2599 }
2600
2601 protected void onLongClickAllAppsButton(View v) {
2602 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2603 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002604 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.LONGPRESS,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002605 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002606 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07002607 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002608 }
2609 }
2610
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002611 private void showBrokenAppInstallDialog(final String packageName,
2612 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002613 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002614 .setTitle(R.string.abandoned_promises_title)
2615 .setMessage(R.string.abandoned_promise_explanation)
2616 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2617 .setNeutralButton(R.string.abandoned_clean_this,
2618 new DialogInterface.OnClickListener() {
2619 public void onClick(DialogInterface dialog, int id) {
2620 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2621 mWorkspace.removeAbandonedPromise(packageName, user);
2622 }
2623 })
2624 .create().show();
2625 return;
2626 }
2627
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002628 /**
2629 * Event handler for an app shortcut click.
2630 *
2631 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2632 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002633 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002634 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2635 Object tag = v.getTag();
2636 if (!(tag instanceof ShortcutInfo)) {
2637 throw new IllegalArgumentException("Input must be a Shortcut");
2638 }
2639
2640 // Open shortcut
2641 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002642
2643 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002644 if ((shortcut.isDisabled &
2645 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2646 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2647 // If the app is only disabled because of the above flags, launch activity anyway.
2648 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002649 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002650 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2651 // Use a message specific to this shortcut, if it has one.
2652 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2653 return;
2654 }
2655 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002656 int error = R.string.activity_not_available;
2657 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2658 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002659 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2660 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002661 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002662 }
2663 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2664 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002665 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002666 }
2667
Chris Wren40c5ed32014-06-24 18:24:23 -04002668 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002669 if ((v instanceof BubbleTextView)
2670 && shortcut.isPromise()
2671 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002672 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002673 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002674 new DialogInterface.OnClickListener() {
2675 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002676 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002677 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002678 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002679 return;
2680 }
2681
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002682 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002683 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002684 }
2685
Sunny Goyala7ce1662016-05-31 15:01:35 -07002686 private void startAppShortcutOrInfoActivity(View v) {
2687 ItemInfo item = (ItemInfo) v.getTag();
2688 Intent intent = item.getIntent();
2689 if (intent == null) {
2690 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002691 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002692 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002693 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002694
2695 if (success && v instanceof BubbleTextView) {
2696 mWaitingForResume = (BubbleTextView) v;
2697 mWaitingForResume.setStayPressed(true);
2698 }
2699 }
2700
2701 /**
2702 * Event handler for a folder icon click.
2703 *
2704 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2705 */
2706 protected void onClickFolderIcon(View v) {
2707 if (LOGD) Log.d(TAG, "onClickFolder");
2708 if (!(v instanceof FolderIcon)){
2709 throw new IllegalArgumentException("Input must be a FolderIcon");
2710 }
2711
2712 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002713 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002714 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002715 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002716 }
Michael Jurka5130e402011-10-13 04:55:35 -07002717 }
2718
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002719 /**
2720 * Event handler for the (Add) Widgets button that appears after a long press
2721 * on the home screen.
2722 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002723 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002724 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002725 if (mIsSafeModeEnabled) {
2726 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2727 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002728 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002729 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002730 }
2731
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002732 /**
2733 * Event handler for the wallpaper picker button that appears after a long press
2734 * on the home screen.
2735 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002736 public void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002737 if (!Utilities.isWallapaperAllowed(this)) {
2738 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2739 return;
2740 }
2741
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002742 String pickerPackage = getString(R.string.wallpaper_picker_package);
2743 if (TextUtils.isEmpty(pickerPackage)) {
2744 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2745 }
2746
Tony Wickham785f7a52015-08-31 17:28:32 -07002747 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2748 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002749
2750 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002751 .setPackage(pickerPackage)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002752 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
2753 intent.setSourceBounds(getViewBounds(v));
2754 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002755 }
2756
2757 /**
2758 * Event handler for a click on the settings button that appears after a long press
2759 * on the home screen.
2760 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002761 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002762 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002763 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2764 .setPackage(getPackageName());
2765 intent.setSourceBounds(getViewBounds(v));
2766 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002767 }
2768
Adam Cohen61f560d2013-09-30 15:58:20 -07002769 public View.OnTouchListener getHapticFeedbackTouchListener() {
2770 if (mHapticFeedbackTouchListener == null) {
2771 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002772 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002773 @Override
2774 public boolean onTouch(View v, MotionEvent event) {
2775 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2776 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2777 }
2778 return false;
2779 }
2780 };
2781 }
2782 return mHapticFeedbackTouchListener;
2783 }
2784
Tony Wickhame2217252016-03-22 16:34:23 -07002785 @Override
2786 public void onAccessibilityStateChanged(boolean enabled) {
2787 mDragLayer.onAccessibilityStateChanged(enabled);
2788 }
2789
Sunny Goyal06e21a22016-08-11 16:02:02 -07002790 public void onDragStarted() {
Adam Cohen9211d422014-10-07 18:14:53 -07002791 if (isOnCustomContent()) {
2792 // Custom content screen doesn't participate in drag and drop. If on custom
2793 // content screen, move to default.
2794 moveWorkspaceToDefaultScreen();
2795 }
Adam Cohen9211d422014-10-07 18:14:53 -07002796 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002797
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002798 /**
2799 * Called when the user stops interacting with the launcher.
2800 * This implies that the user is now on the homescreen and is not doing housekeeping.
2801 */
Adam Cohen9211d422014-10-07 18:14:53 -07002802 protected void onInteractionEnd() {
2803 if (mLauncherCallbacks != null) {
2804 mLauncherCallbacks.onInteractionEnd();
2805 }
2806 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002807
2808 /**
2809 * Called when the user starts interacting with the launcher.
2810 * The possible interactions are:
2811 * - open all apps
2812 * - reorder an app shortcut, or a widget
2813 * - open the overview mode.
2814 * This is a good time to stop doing things that only make sense
2815 * when the user is on the homescreen and not doing housekeeping.
2816 */
Adam Cohen9211d422014-10-07 18:14:53 -07002817 protected void onInteractionBegin() {
2818 if (mLauncherCallbacks != null) {
2819 mLauncherCallbacks.onInteractionBegin();
2820 }
2821 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002822
Winson Chungcd99cd32015-04-29 11:03:24 -07002823 /** Updates the interaction state. */
2824 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002825 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002826 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002827 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2828 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002829 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002830 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002831 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002832 onInteractionEnd();
2833 }
2834 }
2835
Sunny Goyala7ce1662016-05-31 15:01:35 -07002836 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002837 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002838 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2839 try {
2840 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2841 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2842 // is enabled by default on NYC.
2843 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2844 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002845
2846 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2847 String id = ((ShortcutInfo) info).getDeepShortcutId();
2848 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002849 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2850 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002851 } else {
2852 // Could be launching some bookkeeping activity
2853 startActivity(intent, optsBundle);
2854 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002855 } finally {
2856 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002857 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002858 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002859 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2860 // corresponding permission. Show the appropriate permission prompt if that
2861 // is the case.
2862 if (intent.getComponent() == null
2863 && Intent.ACTION_CALL.equals(intent.getAction())
2864 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2865 PackageManager.PERMISSION_GRANTED) {
2866 // TODO: Rename sPendingAddItem to a generic name.
2867 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2868 0, info);
2869 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2870 REQUEST_PERMISSION_CALL_PHONE);
2871 } else {
2872 // No idea why this was thrown.
2873 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002874 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002875 }
2876 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002877
Sunny Goyala7ce1662016-05-31 15:01:35 -07002878 private Bundle getActivityLaunchOptions(View v) {
2879 if (Utilities.ATLEAST_MARSHMALLOW) {
2880 int left = 0, top = 0;
2881 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2882 if (v instanceof TextView) {
2883 // Launch from center of icon, not entire view
2884 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2885 if (icon != null) {
2886 Rect bounds = icon.getBounds();
2887 left = (width - bounds.width()) / 2;
2888 top = v.getPaddingTop();
2889 width = bounds.width();
2890 height = bounds.height();
2891 }
2892 }
2893 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2894 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2895 // On L devices, we use the device default slide-up transition.
2896 // On L MR1 devices, we use a custom version of the slide-up transition which
2897 // doesn't have the delay present in the device default.
2898 return ActivityOptions.makeCustomAnimation(
2899 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2900 }
2901 return null;
2902 }
2903
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002904 private Rect getViewBounds(View v) {
2905 int[] pos = new int[2];
2906 v.getLocationOnScreen(pos);
2907 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2908 }
2909
Sunny Goyala7ce1662016-05-31 15:01:35 -07002910 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002911 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2912 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2913 return false;
2914 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002915 // Only launch using the new animation if the shortcut has not opted out (this is a
2916 // private contract between launcher and may be ignored in the future).
2917 boolean useLaunchAnimation = (v != null) &&
2918 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2919 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2920
2921 UserHandleCompat user = null;
2922 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2923 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2924 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002925 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002926
2927 // Prepare intent
2928 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2929 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002930 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002931 }
2932 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002933 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2934 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07002935 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
2936 && ((ShortcutInfo) item).promisedIntent == null) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002937 // Shortcuts need some special checks due to legacy reasons.
2938 startShortcutIntentSafely(intent, optsBundle, item);
2939 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2940 // Could be launching some bookkeeping activity
2941 startActivity(intent, optsBundle);
2942 } else {
2943 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2944 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2945 }
2946 return true;
2947 } catch (ActivityNotFoundException|SecurityException e) {
2948 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2949 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2950 }
2951 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002952 }
2953
Adam Cohen268c4752012-06-06 17:47:33 -07002954 /**
2955 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2956 * in the DragLayer in the exact absolute location of the original FolderIcon.
2957 */
2958 private void copyFolderIconToImage(FolderIcon fi) {
2959 final int width = fi.getMeasuredWidth();
2960 final int height = fi.getMeasuredHeight();
2961
2962 // Lazy load ImageView, Bitmap and Canvas
2963 if (mFolderIconImageView == null) {
2964 mFolderIconImageView = new ImageView(this);
2965 }
2966 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2967 mFolderIconBitmap.getHeight() != height) {
2968 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2969 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2970 }
2971
2972 DragLayer.LayoutParams lp;
2973 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2974 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2975 } else {
2976 lp = new DragLayer.LayoutParams(width, height);
2977 }
2978
Adam Cohen307fe232012-08-16 17:55:58 -07002979 // The layout from which the folder is being opened may be scaled, adjust the starting
2980 // view size by this scale factor.
2981 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002982 lp.customPosition = true;
2983 lp.x = mRectForFolderAnimation.left;
2984 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002985 lp.width = (int) (scale * width);
2986 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002987
2988 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2989 fi.draw(mFolderIconCanvas);
2990 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002991 if (fi.getFolder() != null) {
2992 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2993 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002994 }
Adam Cohen268c4752012-06-06 17:47:33 -07002995 // Just in case this image view is still in the drag layer from a previous animation,
2996 // we remove it and re-add it.
2997 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2998 mDragLayer.removeView(mFolderIconImageView);
2999 }
3000 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003001 if (fi.getFolder() != null) {
3002 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003003 }
Adam Cohen268c4752012-06-06 17:47:33 -07003004 }
3005
Adam Cohen37b2a492016-04-06 18:36:06 -07003006 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003007 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07003008 FolderInfo info = (FolderInfo) fi.getTag();
3009 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3010 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003011 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3012 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003013 }
3014
Adam Cohen268c4752012-06-06 17:47:33 -07003015 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3016 copyFolderIconToImage(fi);
3017 fi.setVisibility(View.INVISIBLE);
3018
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003019 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
3020 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003021 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003022 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3023 }
3024 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003025 oa.start();
3026 }
3027
Sunny Goyal935fca12015-10-13 10:19:01 -07003028 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003029 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003030 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003031
Adam Cohen268c4752012-06-06 17:47:33 -07003032 // We remove and re-draw the FolderIcon in-case it has changed
3033 mDragLayer.removeView(mFolderIconImageView);
3034 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08003035
3036 if (cl != null) {
3037 cl.clearFolderLeaveBehind();
3038 }
3039
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003040 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003041 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003042 oa.addListener(new AnimatorListenerAdapter() {
3043 @Override
3044 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003045 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003046 // Remove the ImageView copy of the FolderIcon and make the original visible.
3047 mDragLayer.removeView(mFolderIconImageView);
3048 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003049 }
3050 }
3051 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003052 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003053 if (!animate) {
3054 oa.end();
3055 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003056 }
3057
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003058 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003059 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003060 * is animated relative to the specified View. If the View is null, no animation
3061 * is played.
3062 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003063 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003064 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003065 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003066
Adam Cohenfb91f302012-06-11 15:45:18 -07003067 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003068 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3069 if (openFolder != null && openFolder != folder) {
3070 // Close any open folder before opening a folder.
3071 closeFolder();
3072 }
3073
Adam Cohena9cf38f2011-05-02 15:36:58 -07003074 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003075
Adam Cohena9cf38f2011-05-02 15:36:58 -07003076 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003077
Sunny Goyalf4066152015-04-15 09:42:19 -07003078 // While the folder is open, the position of the icon cannot change.
3079 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3080
Adam Cohen4554ee12011-08-03 16:13:21 -07003081 // Just verify that the folder hasn't already been added to the DragLayer.
3082 // There was a one-off crash where the folder had a parent already.
3083 if (folder.getParent() == null) {
3084 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003085 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003086 } else {
3087 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3088 folder.getParent() + ").");
3089 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003090 folder.animateOpen();
3091
3092 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003093
3094 // Notify the accessibility manager that this folder "window" has appeared and occluded
3095 // the workspace items
3096 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3097 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003098 }
3099
3100 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003101 closeFolder(true);
3102 }
3103
3104 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003105 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003106 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003107 if (folder.isEditingName()) {
3108 folder.dismissEditingName();
3109 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003110 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003111 }
3112 }
3113
Sunny Goyal935fca12015-10-13 10:19:01 -07003114 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003115 animate &= !Utilities.isPowerSaverOn(this);
3116
Adam Cohen4554ee12011-08-03 16:13:21 -07003117 folder.getInfo().opened = false;
3118
3119 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3120 if (parent != null) {
3121 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003122 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003123 if (fi != null) {
3124 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3125 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003126 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003127 if (animate) {
3128 folder.animateClosed();
3129 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003130 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003131 }
Winson Chung83ca4802013-04-12 15:10:52 -07003132
Sunny Goyal935fca12015-10-13 10:19:01 -07003133 // Notify the accessibility manager that this folder "window" has disappeared and no
3134 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003135 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003136 }
3137
Tony Wickham49c8d292016-07-01 11:44:34 -07003138 public void closeShortcutsContainer() {
Sunny Goyala2454ad2016-07-22 10:50:11 -07003139 closeShortcutsContainer(true);
3140 }
3141
3142 public void closeShortcutsContainer(boolean animate) {
Tony Wickham49c8d292016-07-01 11:44:34 -07003143 DeepShortcutsContainer deepShortcutsContainer = getOpenShortcutsContainer();
3144 if (deepShortcutsContainer != null) {
Sunny Goyala2454ad2016-07-22 10:50:11 -07003145 if (animate) {
3146 deepShortcutsContainer.animateClose();
3147 } else {
3148 deepShortcutsContainer.close();
3149 }
Tony Wickham49c8d292016-07-01 11:44:34 -07003150 }
3151 }
3152
3153 /**
3154 * @return The open shortcuts container, or null if there is none
3155 */
3156 public DeepShortcutsContainer getOpenShortcutsContainer() {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07003157 // Iterate in reverse order. Shortcuts container is added later to the dragLayer,
3158 // and will be one of the last views.
3159 for (int i = mDragLayer.getChildCount() - 1; i >= 0; i--) {
3160 View child = mDragLayer.getChildAt(i);
Sunny Goyala2454ad2016-07-22 10:50:11 -07003161 if (child instanceof DeepShortcutsContainer
3162 && ((DeepShortcutsContainer) child).isOpen()) {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07003163 return (DeepShortcutsContainer) child;
3164 }
3165 }
3166 return null;
Tony Wickham49c8d292016-07-01 11:44:34 -07003167 }
3168
Tony Wickhamdadb3042016-02-24 11:07:00 -08003169 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003170 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003171 if (!isDraggingEnabled()) return false;
3172 if (isWorkspaceLocked()) return false;
3173 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003174
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07003175 if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
3176 (v == mAllAppsButton && mAllAppsButton != null)) {
Winson Chung76648c52015-07-10 14:33:23 -07003177 onLongClickAllAppsButton(v);
3178 return true;
3179 }
3180
Adam Cohen1697b792013-09-17 19:08:21 -07003181 if (v instanceof Workspace) {
3182 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003183 if (!mWorkspace.isTouchActive()) {
3184 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003185 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3186 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3187 return true;
3188 } else {
3189 return false;
3190 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003191 } else {
3192 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003193 }
Adam Cohen1697b792013-09-17 19:08:21 -07003194 }
3195
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003196 CellLayout.CellInfo longClickCellInfo = null;
3197 View itemUnderLongClick = null;
3198 if (v.getTag() instanceof ItemInfo) {
3199 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003200 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003201 itemUnderLongClick = longClickCellInfo.cell;
3202 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003203 }
3204
Winson Chung3d503fb2011-07-13 17:25:49 -07003205 // The hotseat touch handling does not go through Workspace, and we always allow long press
3206 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003207 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003208 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003209 // User long pressed on empty space
3210 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3211 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003212 if (mWorkspace.isInOverviewMode()) {
3213 mWorkspace.startReordering(v);
3214 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003215 showOverviewMode(true);
Hyunyoung Song645764e2016-06-06 14:19:02 -07003216 mHotseat.requestDisallowInterceptTouchEvent(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003217 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003218 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003219 final boolean isAllAppsButton =
3220 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3221 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3222 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003223 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003224 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003225 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003226 }
3227 }
3228 }
3229 return true;
3230 }
3231
Winson Chung3d503fb2011-07-13 17:25:49 -07003232 boolean isHotseatLayout(View layout) {
3233 return mHotseat != null && layout != null &&
3234 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3235 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003236
Winson Chung3d503fb2011-07-13 17:25:49 -07003237 /**
3238 * Returns the CellLayout of the specified container at the specified screen.
3239 */
Sunny Goyal92820592015-03-02 11:31:35 -08003240 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003241 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3242 if (mHotseat != null) {
3243 return mHotseat.getLayout();
3244 } else {
3245 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003246 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003247 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003248 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003249 }
3250 }
3251
Winson Chungb745afb2015-03-02 11:51:23 -08003252 /**
3253 * For overridden classes.
3254 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003255 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003256 return isAppsViewVisible();
3257 }
3258
3259 public boolean isAppsViewVisible() {
3260 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3261 }
3262
3263 public boolean isWidgetsViewVisible() {
3264 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003265 }
3266
Michael Jurkae326f182011-11-21 14:05:46 -08003267 @Override
3268 public void onTrimMemory(int level) {
3269 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003270 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3271 // The widget preview db can result in holding onto over
3272 // 3MB of memory for caching which isn't necessary.
3273 SQLiteDatabase.releaseMemory();
3274
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003275 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003276 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003277 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003278 if (mLauncherCallbacks != null) {
3279 mLauncherCallbacks.onTrimMemory(level);
3280 }
Michael Jurkae326f182011-11-21 14:05:46 -08003281 }
3282
Winson5c6bdbb2015-09-03 11:36:19 -07003283 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003284 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003285 }
3286
Winson5c6bdbb2015-09-03 11:36:19 -07003287 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003288 boolean changed = mState != State.WORKSPACE ||
3289 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003290 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003291 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003292 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003293 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003294
Michael Jurkab3e22d92011-10-31 15:58:33 -07003295 // Set focus to the AppsCustomize button
3296 if (mAllAppsButton != null) {
3297 mAllAppsButton.requestFocus();
3298 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003299 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003300
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003301 // Change the state *after* we've called all the transition code
3302 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003303
Winson Chung5fb63472011-02-02 17:03:37 -08003304 // Resume the auto-advance of widgets
3305 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003306 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003307
Winson Chung0f785722015-04-08 10:27:49 -07003308 if (changed) {
3309 // Send an accessibility event to announce the context change
3310 getWindow().getDecorView()
3311 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003312 }
Winson5c6bdbb2015-09-03 11:36:19 -07003313 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003314 }
3315
Winsone9f27272015-10-13 10:47:51 -07003316 /**
3317 * Shows the overview button.
3318 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07003319 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003320 showOverviewMode(animated, false);
3321 }
3322
3323 /**
3324 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3325 * onto one of the overview panel buttons.
3326 */
3327 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3328 Runnable postAnimRunnable = null;
3329 if (requestButtonFocus) {
3330 postAnimRunnable = new Runnable() {
3331 @Override
3332 public void run() {
3333 // Hitting the menu button when in touch mode does not trigger touch mode to
3334 // be disabled, so if requested, force focus on one of the overview panel
3335 // buttons.
3336 mOverviewPanel.requestFocusFromTouch();
3337 }
3338 };
3339 }
Adam Cohened307df2013-10-02 09:37:31 -07003340 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003341 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003342 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003343 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003344 }
3345
Winson Chungb745afb2015-03-02 11:51:23 -08003346 /**
3347 * Shows the apps view.
3348 */
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003349 public void showAppsView(boolean animated, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003350 boolean focusSearchBar) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003351 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07003352 if (updatePredictedApps) {
3353 tryAndUpdatePredictedApps();
3354 }
Winson Chung76648c52015-07-10 14:33:23 -07003355 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003356 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003357
Winson Chungb745afb2015-03-02 11:51:23 -08003358 /**
3359 * Shows the widgets view.
3360 */
3361 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003362 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003363 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003364 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003365 }
Winson Chung76648c52015-07-10 14:33:23 -07003366 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003367
3368 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003369 @Override
3370 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003371 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003372 }
3373 });
Winson Chungb745afb2015-03-02 11:51:23 -08003374 }
3375
3376 /**
3377 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003378 *
3379 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003380 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003381 // TODO: calling method should use the return value so that when {@code false} is returned
3382 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003383 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003384 if (!(mState == State.WORKSPACE ||
3385 mState == State.APPS_SPRING_LOADED ||
3386 mState == State.WIDGETS_SPRING_LOADED ||
3387 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003388 return false;
3389 }
3390 if (toState != State.APPS && toState != State.WIDGETS) {
3391 return false;
3392 }
Winson Chungb745afb2015-03-02 11:51:23 -08003393
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003394 // This is a safe and supported transition to bypass spring_loaded mode.
3395 if (mExitSpringLoadedModeRunnable != null) {
3396 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3397 mExitSpringLoadedModeRunnable = null;
3398 }
3399
Winson Chungb745afb2015-03-02 11:51:23 -08003400 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003401 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3402 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003403 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003404 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003405 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003406
Michael Jurkab3e22d92011-10-31 15:58:33 -07003407 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003408 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003409
3410 // Pause the auto-advance of widgets until we are out of AllApps
3411 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003412 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003413 closeFolder();
Tony Wickham49c8d292016-07-01 11:44:34 -07003414 closeShortcutsContainer();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003415
3416 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003417 getWindow().getDecorView()
3418 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003419 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003420 }
3421
Winson Chungcd99cd32015-04-29 11:03:24 -07003422 /**
3423 * Updates the workspace and interaction state on state change, and return the animation to this
3424 * new state.
3425 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003426 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003427 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003428 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003429 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003430 updateInteraction(fromState, toState);
3431 return anim;
3432 }
3433
Hyunyoung Song3f471442015-04-08 19:01:34 -07003434 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003435 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003436 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003437 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003438 }
Winson Chungb745afb2015-03-02 11:51:23 -08003439
Winson Chung006ee262015-08-03 14:40:11 -07003440 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003441 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003442 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003443
3444 if (isAppsViewVisible()) {
3445 mState = State.APPS_SPRING_LOADED;
3446 } else if (isWidgetsViewVisible()) {
3447 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003448 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003449 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003450 } else {
3451 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003452 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003453 }
Adam Cohen7777d962011-08-18 18:58:38 -07003454
Hyunyoung Song3f471442015-04-08 19:01:34 -07003455 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003456 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003457 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003458
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003459 if (mExitSpringLoadedModeRunnable != null) {
3460 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3461 }
3462 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003463 @Override
3464 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003465 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003466 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3467 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003468 // Before we show workspace, hide all apps again because
3469 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3470 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003471 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003472 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003473 } else {
3474 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003475 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003476 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07003477 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003478 };
3479 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003480 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003481
Tony Wickhame0c33232016-02-08 11:37:04 -08003482 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003483 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3484 || mState == State.WIDGETS_SPRING_LOADED;
3485 }
3486
Michael Jurkad3ef3062010-11-23 16:23:58 -08003487 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003488 if (mState == State.APPS_SPRING_LOADED) {
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003489 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07003490 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003491 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003492 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003493 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3494 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003495 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003496 }
3497
Winson Chung4ac30062015-05-08 17:34:17 -07003498 /**
3499 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3500 * resumed.
3501 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003502 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003503 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003504 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003505 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003506 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003507 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003508 }
3509 }
3510 }
3511
Michael Jurkab3e22d92011-10-31 15:58:33 -07003512 void lockAllApps() {
3513 // TODO
3514 }
3515
3516 void unlockAllApps() {
3517 // TODO
3518 }
3519
Winsonf768d932015-09-25 16:12:35 -07003520 public boolean launcherCallbacksProvidesSearch() {
3521 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3522 }
3523
Michael Jurkad7c28052012-04-27 15:43:36 -07003524 @Override
3525 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003526 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003527 final List<CharSequence> text = event.getText();
3528 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003529 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003530 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003531 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003532 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003533 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003534 } else if (mWorkspace != null) {
3535 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003536 } else {
3537 text.add(getString(R.string.all_apps_home_button_label));
3538 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003539 return result;
3540 }
3541
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003542 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003543 * If the activity is currently paused, signal that we need to run the passed Runnable
3544 * in onResume.
3545 *
3546 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003547 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3548 * wrong when we're not running, and if the activity comes back to what the configuration was
3549 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003550 *
3551 * Implementation of the method from LauncherModel.Callbacks.
3552 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003553 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003554 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003555 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003556 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003557 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003558 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003559 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003560 }
3561 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003562 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003563 return true;
3564 } else {
3565 return false;
3566 }
3567 }
3568
Michael Jurkac402cd92013-05-20 15:49:32 +02003569 private boolean waitUntilResume(Runnable run) {
3570 return waitUntilResume(run, false);
3571 }
3572
Michael Jurka1e2f4652013-07-08 18:03:46 -07003573 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003574 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003575 }
3576
Michael Jurka7607c2f2013-04-03 14:33:19 -07003577 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003578 * If the activity is currently paused, signal that we need to re-run the loader
3579 * in onResume.
3580 *
3581 * This needs to be called from incoming places where resources might have been loaded
3582 * while we are paused. That is becaues the Configuration might be wrong
3583 * when we're not running, and if it comes back to what it was when we
3584 * were paused, we are not restarted.
3585 *
3586 * Implementation of the method from LauncherModel.Callbacks.
3587 *
3588 * @return true if we are currently paused. The caller might be able to
3589 * skip some work in that case since we will come back again.
3590 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003591 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003592 public boolean setLoadOnResume() {
3593 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003594 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003595 mOnResumeNeedsLoad = true;
3596 return true;
3597 } else {
3598 return false;
3599 }
3600 }
3601
3602 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003603 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003604 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003605 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003606 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003607 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003608 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003609 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003610 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003611 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003612 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003613
Joe Onorato9c1289c2009-08-17 11:03:03 -04003614 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003615 * Clear any pending bind callbacks. This is called when is loader is planning to
3616 * perform a full rebind from scratch.
3617 */
3618 @Override
3619 public void clearPendingBinds() {
3620 mBindOnResumeCallbacks.clear();
3621 if (mPendingExecutor != null) {
3622 mPendingExecutor.markCompleted();
3623 mPendingExecutor = null;
3624 }
3625 }
3626
3627 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003628 * Refreshes the shortcuts shown on the workspace.
3629 *
3630 * Implementation of the method from LauncherModel.Callbacks.
3631 */
3632 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003633 if (LauncherAppState.PROFILE_STARTUP) {
3634 Trace.beginSection("Starting page bind");
3635 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003636 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003637
Winson Chungd64d1762013-08-20 14:37:16 -07003638 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003639 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003640 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003641
Michael Jurka05bf644e2011-11-30 20:28:53 -08003642 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003643 if (mHotseat != null) {
3644 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003645 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003646 if (LauncherAppState.PROFILE_STARTUP) {
3647 Trace.endSection();
3648 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003649 }
3650
Adam Cohendcd297f2013-06-18 13:13:40 -07003651 @Override
3652 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003653 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003654 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3655 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003656 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3657 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3658 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003659 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3660 // If there are no screens, we need to have an empty screen
3661 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003662 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003663 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003664
3665 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003666 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003667 if (hasCustomContentToLeft()) {
3668 mWorkspace.createCustomContentContainer();
3669 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003670 }
Winsonc7d2e832016-07-28 12:24:55 -07003671
3672 // After we have added all the screens, if the wallpaper was locked to the default state,
3673 // then notify to indicate that it can be released and a proper wallpaper offset can be
3674 // computed before the next layout
3675 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07003676 }
3677
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003678 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003679 int count = orderedScreenIds.size();
3680 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003681 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003682 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003683 // No need to bind the first screen, as its always bound.
3684 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3685 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003686 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003687 }
3688
Winson Chungd64d1762013-08-20 14:37:16 -07003689 public void bindAppsAdded(final ArrayList<Long> newScreens,
3690 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003691 final ArrayList<ItemInfo> addAnimated,
3692 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003693 Runnable r = new Runnable() {
3694 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003695 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003696 }
3697 };
3698 if (waitUntilResume(r)) {
3699 return;
3700 }
3701
Winson Chungd64d1762013-08-20 14:37:16 -07003702 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003703 if (newScreens != null) {
3704 bindAddScreens(newScreens);
3705 }
Winson Chungd64d1762013-08-20 14:37:16 -07003706
3707 // We add the items without animation on non-visible pages, and with
3708 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003709 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003710 bindItems(addNotAnimated, 0,
3711 addNotAnimated.size(), false);
3712 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003713 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003714 bindItems(addAnimated, 0,
3715 addAnimated.size(), true);
3716 }
Winson Chungc58497e2013-09-03 17:48:37 -07003717
Winson Chung87412982013-10-03 18:34:14 -07003718 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003719 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003720
Winson Chungb745afb2015-03-02 11:51:23 -08003721 if (addedApps != null && mAppsView != null) {
3722 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003723 }
Winson Chungd64d1762013-08-20 14:37:16 -07003724 }
3725
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003726 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003727 * Bind the items start-end from the list.
3728 *
3729 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003730 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003731 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003732 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3733 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003734 Runnable r = new Runnable() {
3735 public void run() {
3736 bindItems(shortcuts, start, end, forceAnimateIcons);
3737 }
3738 };
3739 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003740 return;
3741 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003742
Winson Chungf0c6ae02012-03-21 16:10:31 -07003743 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003744 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3745 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003746 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003747 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003748 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003749 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003750 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003751
3752 // Short circuit if we are loading dock items for a configuration which has no dock
3753 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3754 mHotseat == null) {
3755 continue;
3756 }
3757
Sunny Goyal639e9062015-08-19 19:17:06 -07003758 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003759 switch (item.itemType) {
3760 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3761 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003762 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003763 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003764 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003765 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003766 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003767 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003768 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003769 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003770 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003771 default:
3772 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003773 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003774
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003775 /*
3776 * Remove colliding items.
3777 */
3778 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3779 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3780 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3781 View v = cl.getChildAt(item.cellX, item.cellY);
3782 Object tag = v.getTag();
3783 String desc = "Collision while binding workspace item: " + item
3784 + ". Collides with " + tag;
3785 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3786 throw (new RuntimeException(desc));
3787 } else {
3788 Log.d(TAG, desc);
3789 LauncherModel.deleteItemFromDatabase(this, item);
3790 continue;
3791 }
3792 }
3793 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003794 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3795 item.cellY, 1, 1);
3796 if (animateIcons) {
3797 // Animate all the applications up now
3798 view.setAlpha(0f);
3799 view.setScaleX(0f);
3800 view.setScaleY(0f);
3801 bounceAnims.add(createNewAppBounceAnimation(view, i));
3802 newShortcutsScreenId = item.screenId;
3803 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003804 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003805
Winson Chung997a9232013-07-24 15:33:46 -07003806 if (animateIcons) {
3807 // Animate to the correct page
3808 if (newShortcutsScreenId > -1) {
3809 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003810 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003811 final Runnable startBounceAnimRunnable = new Runnable() {
3812 public void run() {
3813 anim.playTogether(bounceAnims);
3814 anim.start();
3815 }
3816 };
Winson Chung997a9232013-07-24 15:33:46 -07003817 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003818 // We post the animation slightly delayed to prevent slowdowns
3819 // when we are loading right after we return to launcher.
3820 mWorkspace.postDelayed(new Runnable() {
3821 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003822 if (mWorkspace != null) {
3823 mWorkspace.snapToPage(newScreenIndex);
3824 mWorkspace.postDelayed(startBounceAnimRunnable,
3825 NEW_APPS_ANIMATION_DELAY);
3826 }
Winson Chung94d67682013-09-25 16:29:40 -07003827 }
3828 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003829 } else {
3830 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003831 }
3832 }
Winson Chung64359a52013-07-08 17:17:08 -07003833 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003834 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003835 }
3836
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003837 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3838 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3839 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003840 view.updateAppWidget(null);
3841 view.setOnClickListener(this);
3842 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003843 mWorkspace.requestLayout();
3844 }
3845
Joe Onorato9c1289c2009-08-17 11:03:03 -04003846 /**
3847 * Add the views for a widget to the workspace.
3848 *
3849 * Implementation of the method from LauncherModel.Callbacks.
3850 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003851 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003852 Runnable r = new Runnable() {
3853 public void run() {
3854 bindAppWidget(item);
3855 }
3856 };
3857 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003858 return;
3859 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003860
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003861 if (mIsSafeModeEnabled) {
3862 bindSafeModeWidget(item);
3863 return;
3864 }
3865
Daniel Sandler843e8602010-06-07 14:59:01 -04003866 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3867 if (DEBUG_WIDGETS) {
3868 Log.d(TAG, "bindAppWidget: " + item);
3869 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003870
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003871 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003872
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003873 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3874 // If the provider is not ready, bind as a pending widget.
3875 appWidgetInfo = null;
3876 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3877 // The widget id is not valid. Try to find the widget based on the provider info.
3878 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3879 } else {
3880 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3881 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003882
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003883 // If the provider is ready, but the width is not yet restored, try to restore it.
3884 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3885 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003886 if (appWidgetInfo == null) {
3887 if (DEBUG_WIDGETS) {
3888 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3889 + " belongs to component " + item.providerName
3890 + ", as the povider is null");
3891 }
3892 LauncherModel.deleteItemFromDatabase(this, item);
3893 return;
3894 }
Sunny Goyalff572272014-07-23 13:58:07 -07003895
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003896 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003897 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003898 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3899 // Id has not been allocated yet. Allocate a new id.
3900 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3901 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003902
Sunny Goyald478c832016-04-01 12:04:16 -07003903 // Also try to bind the widget. If the bind fails, the user will be shown
3904 // a click to setup UI, which will ask for the bind permission.
3905 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3906 pendingInfo.spanX = item.spanX;
3907 pendingInfo.spanY = item.spanY;
3908 pendingInfo.minSpanX = item.minSpanX;
3909 pendingInfo.minSpanY = item.minSpanY;
3910 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003911
3912 boolean isDirectConfig =
3913 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3914 if (isDirectConfig && item.bindOptions != null) {
3915 Bundle newOptions = item.bindOptions.getExtras();
3916 if (options != null) {
3917 newOptions.putAll(options);
3918 }
3919 options = newOptions;
3920 }
Sunny Goyald478c832016-04-01 12:04:16 -07003921 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3922 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003923
Sunny Goyal86df1382016-08-10 15:03:22 -07003924 // We tried to bind once. If we were not able to bind, we would need to
3925 // go through the permission dialog, which means we cannot skip the config
3926 // activity.
3927 item.bindOptions = null;
3928 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3929
Sunny Goyald478c832016-04-01 12:04:16 -07003930 // Bind succeeded
3931 if (success) {
3932 // If the widget has a configure activity, it is still needs to set it up,
3933 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003934 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003935 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3936 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003937 }
Sunny Goyald478c832016-04-01 12:04:16 -07003938
3939 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003940 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003941 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003942 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003943 // The widget was marked as UI not ready, but there is no configure activity to
3944 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003945 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3946 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003947 }
Sunny Goyalff572272014-07-23 13:58:07 -07003948 }
3949
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003950 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003951 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003952 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3953 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003954 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003955
Sunny Goyal56c73602015-09-25 12:55:01 -07003956 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003957 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003958 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003959 deleteWidgetInfo(item);
3960 return;
3961 }
3962
Sunny Goyal233ee962015-08-03 13:05:01 -07003963 item.minSpanX = appWidgetInfo.minSpanX;
3964 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003965 addAppWidgetToWorkspace(
3966 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3967 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003968 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003969 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003970 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003971 view.updateAppWidget(null);
3972 view.setOnClickListener(this);
3973 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003974 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003975 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003976
Daniel Sandler843e8602010-06-07 14:59:01 -04003977 if (DEBUG_WIDGETS) {
3978 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3979 + (SystemClock.uptimeMillis()-start) + "ms");
3980 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003981 }
3982
Sunny Goyalff572272014-07-23 13:58:07 -07003983 /**
3984 * Restores a pending widget.
3985 *
3986 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003987 */
Sunny Goyald478c832016-04-01 12:04:16 -07003988 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003989 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3990 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3991 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003992 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003993 }
3994
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003995 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003996 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003997
3998 mWorkspace.reinflateWidgetsIfNecessary();
3999 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07004000 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07004001 }
4002
Adam Cohen1462de32012-07-24 22:34:36 -07004003 public void onPageBoundSynchronously(int page) {
4004 mSynchronouslyBoundPages.add(page);
4005 }
4006
Sunny Goyal527c7d32015-08-28 15:19:36 -07004007 @Override
4008 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
4009 if (mPendingExecutor != null) {
4010 mPendingExecutor.markCompleted();
4011 }
4012 mPendingExecutor = executor;
4013 executor.attachTo(this);
4014 }
4015
4016 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4017 if (mPendingExecutor == executor) {
4018 mPendingExecutor = null;
4019 }
4020 }
4021
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07004022 @Override
4023 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
4024 Runnable r = new Runnable() {
4025 public void run() {
4026 finishFirstPageBind(executor);
4027 }
4028 };
4029 if (waitUntilResume(r)) {
4030 return;
4031 }
4032
4033 Runnable onComplete = new Runnable() {
4034 @Override
4035 public void run() {
4036 if (executor != null) {
4037 executor.onLoadAnimationCompleted();
4038 }
4039 }
4040 };
4041 if (mDragLayer.getAlpha() < 1) {
4042 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
4043 } else {
4044 onComplete.run();
4045 }
4046 }
4047
Joe Onorato9c1289c2009-08-17 11:03:03 -04004048 /**
4049 * Callback saying that there aren't any more items to bind.
4050 *
4051 * Implementation of the method from LauncherModel.Callbacks.
4052 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004053 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004054 Runnable r = new Runnable() {
4055 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004056 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004057 }
4058 };
4059 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004060 return;
4061 }
Sunny Goyale26d1002016-06-20 14:52:14 -07004062 if (LauncherAppState.PROFILE_STARTUP) {
4063 Trace.beginSection("Page bind completed");
4064 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004065 if (mSavedState != null) {
4066 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004067 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004068 }
Sunny Goyal08442b82015-10-21 17:15:08 -07004069
Joe Onorato9c1289c2009-08-17 11:03:03 -04004070 mSavedState = null;
4071 }
4072
Adam Cohen1462de32012-07-24 22:34:36 -07004073 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004074
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004075 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07004076
4077 // If we received the result of any pending adds while the loader was running (e.g. the
4078 // widget configuration forced an orientation change), process them now.
4079 if (sPendingAddItem != null) {
4080 final long screenId = completeAdd(sPendingAddItem);
4081
4082 // TODO: this moves the user to the page where the pending item was added. Ideally,
4083 // the screen would be guaranteed to exist after bind, and the page would be set through
4084 // the workspace restore process.
4085 mWorkspace.post(new Runnable() {
4086 @Override
4087 public void run() {
4088 mWorkspace.snapToScreenId(screenId);
4089 }
4090 });
4091 sPendingAddItem = null;
4092 }
4093
Sunny Goyal756adbc2015-04-16 15:20:51 -07004094 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004095
4096 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004097 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004098 }
Sunny Goyale26d1002016-06-20 14:52:14 -07004099 if (LauncherAppState.PROFILE_STARTUP) {
4100 Trace.endSection();
4101 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004102 }
4103
Winson Chunga2413752012-04-03 14:22:34 -07004104 private boolean canRunNewAppsAnimation() {
4105 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07004106 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07004107 }
4108
Winson Chungc9168342013-06-26 14:54:55 -07004109 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004110 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004111 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4112 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004113 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004114 return bounceAnim;
4115 }
4116
Adam Cohen27772732013-11-11 14:00:56 +00004117 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004118 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004119 }
4120
Tony Wickham55616cd2015-09-23 14:55:17 -07004121 public int getSearchBarHeight() {
4122 if (mLauncherCallbacks != null) {
4123 return mLauncherCallbacks.getSearchBarHeight();
4124 }
4125 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4126 }
4127
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004128 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004129 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4130 * multiple calls to bind the same list.)
4131 */
4132 @Thunk ArrayList<AppInfo> mTmpAppsList;
4133 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4134 public void run() {
4135 bindAllApplications(mTmpAppsList);
4136 mTmpAppsList = null;
4137 }
4138 };
4139
4140 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004141 * Add the icons for all apps.
4142 *
4143 * Implementation of the method from LauncherModel.Callbacks.
4144 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004145 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004146 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4147 mTmpAppsList = apps;
4148 return;
4149 }
4150
Winson Chungb745afb2015-03-02 11:51:23 -08004151 if (mAppsView != null) {
4152 mAppsView.setApps(apps);
4153 }
Adam Cohen9211d422014-10-07 18:14:53 -07004154 if (mLauncherCallbacks != null) {
4155 mLauncherCallbacks.bindAllApplications(apps);
4156 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004157 }
4158
4159 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004160 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
4161 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
4162 */
4163 @Override
4164 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
4165 mDeepShortcutMap = deepShortcutMapCopy;
4166 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
4167 }
4168
Tony Wickham1bce7fd2016-04-28 17:39:03 -07004169 public List<String> getShortcutIdsForItem(ItemInfo info) {
4170 if (!DeepShortcutManager.supportsShortcuts(info)) {
4171 return Collections.EMPTY_LIST;
4172 }
4173 ComponentName component = info.getTargetComponent();
4174 List<String> ids = mDeepShortcutMap.get(new ComponentKey(component, info.user));
4175 return ids == null ? Collections.EMPTY_LIST : ids;
4176 }
4177
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004178 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004179 * A package was updated.
4180 *
4181 * Implementation of the method from LauncherModel.Callbacks.
4182 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004183 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004184 Runnable r = new Runnable() {
4185 public void run() {
4186 bindAppsUpdated(apps);
4187 }
4188 };
4189 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004190 return;
4191 }
4192
Winson Chungb745afb2015-03-02 11:51:23 -08004193 if (mAppsView != null) {
4194 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004195 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004196 }
4197
Sunny Goyal4390ace2014-10-13 11:33:11 -07004198 @Override
4199 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4200 Runnable r = new Runnable() {
4201 public void run() {
4202 bindWidgetsRestored(widgets);
4203 }
4204 };
4205 if (waitUntilResume(r)) {
4206 return;
4207 }
4208 mWorkspace.widgetsRestored(widgets);
4209 }
4210
Joe Onorato9c1289c2009-08-17 11:03:03 -04004211 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004212 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004213 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004214 *
4215 * @param updated list of shortcuts which have changed.
4216 * @param removed list of shortcuts which were deleted in the background. This can happen when
4217 * an app gets removed from the system or some of its components are no longer
4218 * available.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004219 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004220 @Override
4221 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4222 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004223 Runnable r = new Runnable() {
4224 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004225 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004226 }
4227 };
4228 if (waitUntilResume(r)) {
4229 return;
4230 }
4231
Sunny Goyal4390ace2014-10-13 11:33:11 -07004232 if (!updated.isEmpty()) {
4233 mWorkspace.updateShortcuts(updated);
4234 }
4235
4236 if (!removed.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004237 HashSet<ComponentName> removedComponents = new HashSet<>();
4238 HashSet<ShortcutKey> removedDeepShortcuts = new HashSet<>();
4239
Sunny Goyal4390ace2014-10-13 11:33:11 -07004240 for (ShortcutInfo si : removed) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004241 if (si.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07004242 removedDeepShortcuts.add(ShortcutKey.fromShortcutInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004243 } else {
4244 removedComponents.add(si.getTargetComponent());
4245 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004246 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004247
4248 if (!removedComponents.isEmpty()) {
4249 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(removedComponents, user);
4250 mWorkspace.removeItemsByMatcher(matcher);
4251 mDragController.onAppsRemoved(matcher);
4252 }
4253
4254 if (!removedDeepShortcuts.isEmpty()) {
4255 ItemInfoMatcher matcher = ItemInfoMatcher.ofShortcutKeys(removedDeepShortcuts);
4256 mWorkspace.removeItemsByMatcher(matcher);
4257 mDragController.onAppsRemoved(matcher);
4258 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004259 }
4260 }
4261
4262 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004263 * Update the state of a package, typically related to install state.
4264 *
4265 * Implementation of the method from LauncherModel.Callbacks.
4266 */
Sunny Goyale755d462014-07-22 13:48:29 -07004267 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004268 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4269 Runnable r = new Runnable() {
4270 public void run() {
4271 bindRestoreItemsChange(updates);
4272 }
4273 };
4274 if (waitUntilResume(r)) {
4275 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004276 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004277
Sunny Goyal756adbc2015-04-16 15:20:51 -07004278 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004279 }
4280
4281 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004282 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004283 * in addition to specific applications to remove, the reason being that
4284 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004285 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004286 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004287 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004288 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004289 public void bindWorkspaceComponentsRemoved(
4290 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4291 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004292 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004293 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004294 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004295 }
Winson Chungd64d1762013-08-20 14:37:16 -07004296 };
4297 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004298 return;
4299 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004300 if (!packageNames.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004301 ItemInfoMatcher matcher = ItemInfoMatcher.ofPackages(packageNames, user);
4302 mWorkspace.removeItemsByMatcher(matcher);
4303 mDragController.onAppsRemoved(matcher);
4304
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004305 }
4306 if (!components.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004307 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(components, user);
4308 mWorkspace.removeItemsByMatcher(matcher);
4309 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004310 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004311 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004312
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004313 @Override
4314 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4315 Runnable r = new Runnable() {
4316 public void run() {
4317 bindAppInfosRemoved(appInfos);
4318 }
4319 };
4320 if (waitUntilResume(r)) {
4321 return;
Joe Onorato36115782010-06-17 13:28:48 -04004322 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004323
Winson Chungdf95eb12013-10-16 14:57:07 -07004324 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004325 if (mAppsView != null) {
4326 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004327 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004328 }
Joe Onoratobe386092009-11-17 17:32:16 -08004329
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004330 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004331 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004332 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004333 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004334 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004335
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004336 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004337 public void bindWidgetsModel(WidgetsModel model) {
4338 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004339 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004340 return;
4341 }
4342
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004343 if (mWidgetsView != null && model != null) {
4344 mWidgetsView.addWidgets(model);
4345 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004346 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004347 }
4348
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004349 @Override
4350 public void notifyWidgetProvidersChanged() {
4351 if (mWorkspace.getState().shouldUpdateWidget) {
4352 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4353 }
4354 }
4355
Winson Chung400438b2011-01-16 17:53:48 -08004356 private int mapConfigurationOriActivityInfoOri(int configOri) {
4357 final Display d = getWindowManager().getDefaultDisplay();
4358 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4359 switch (d.getRotation()) {
4360 case Surface.ROTATION_0:
4361 case Surface.ROTATION_180:
4362 // We are currently in the same basic orientation as the natural orientation
4363 naturalOri = configOri;
4364 break;
4365 case Surface.ROTATION_90:
4366 case Surface.ROTATION_270:
4367 // We are currently in the other basic orientation to the natural orientation
4368 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4369 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4370 break;
4371 }
4372
4373 int[] oriMap = {
4374 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4375 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4376 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4377 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4378 };
4379 // Since the map starts at portrait, we need to offset if this device's natural orientation
4380 // is landscape.
4381 int indexOffset = 0;
4382 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4383 indexOffset = 1;
4384 }
4385 return oriMap[(d.getRotation() + indexOffset) % 4];
4386 }
Adam Cohen446e9402011-09-15 18:21:21 -07004387
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004388 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004389 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004390 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004391 if (Utilities.ATLEAST_JB_MR2) {
4392 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4393 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004394 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4395 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004396 }
Winson Chung4b919f82012-05-01 10:44:08 -07004397 }
4398 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004399
Winson Chung4b919f82012-05-01 10:44:08 -07004400 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004401 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004402 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004403 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004404 } else {
4405 mHandler.postDelayed(new Runnable() {
4406 public void run() {
4407 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4408 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004409 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004410 }
Winson Chung4b919f82012-05-01 10:44:08 -07004411 }
Winson Chung400438b2011-01-16 17:53:48 -08004412 }
4413
Hyunyoung Songc001cf52016-07-21 17:32:43 -07004414 private void markAppsViewShown() {
4415 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4416 return;
4417 }
4418 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
4419 }
4420
4421 private boolean shouldShowDiscoveryBounce() {
4422 if (mState != mState.WORKSPACE) {
4423 return false;
4424 }
4425 if (mLauncherCallbacks != null && mLauncherCallbacks.shouldShowDiscoveryBounce()) {
4426 return true;
4427 }
4428 if (!mIsResumeFromActionScreenOff) {
4429 return false;
4430 }
4431 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4432 return false;
4433 }
4434 return true;
4435 }
4436
Winson Chung5ffd51d2015-06-25 11:36:50 -07004437 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004438 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004439 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004440 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004441 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004442 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004443 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4444 if (userHandle != null) {
4445 user = UserHandleCompat.fromUser(userHandle);
4446 }
4447 }
4448 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004449 }
4450
4451 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004452 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004453 if (user == null) {
4454 user = UserHandleCompat.myUserHandle();
4455 }
4456
4457 // Called from search suggestion, add the profile extra to the intent to ensure that we
4458 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004459 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004460 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004461 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004462 return null;
4463 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004464 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004465 }
4466
Winson Chung5ffd51d2015-06-25 11:36:50 -07004467 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004468 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4469 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004470 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004471 UserHandleCompat.myUserHandle());
4472 }
4473
Winson Chung5ffd51d2015-06-25 11:36:50 -07004474 protected void moveWorkspaceToDefaultScreen() {
4475 mWorkspace.moveToDefaultScreen(false);
4476 }
4477
Winson Chung80baf5a2010-08-09 16:03:15 -07004478 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004479 * Returns a FastBitmapDrawable with the icon, accurately sized.
4480 */
4481 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4482 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4483 d.setFilterBitmap(true);
4484 resizeIconDrawable(d);
4485 return d;
4486 }
4487
4488 /**
4489 * Resizes an icon drawable to the correct icon size.
4490 */
Winson5fbe0742015-09-30 15:33:00 -07004491 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004492 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004493 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004494 }
4495
4496 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004497 * Prints out out state for debugging.
4498 */
4499 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004500 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004501 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004502 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4503 Log.d(TAG, "mRestoring=" + mRestoring);
4504 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004505 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004506 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004507
Daniel Sandler325dc232013-06-05 22:57:57 -04004508 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004509 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004510
4511 @Override
4512 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4513 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004514 // Dump workspace
4515 writer.println(prefix + "Workspace Items");
4516 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4517 writer.println(prefix + " Homescreen " + i);
4518
4519 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4520 for (int j = 0; j < layout.getChildCount(); j++) {
4521 Object tag = layout.getChildAt(j).getTag();
4522 if (tag != null) {
4523 writer.println(prefix + " " + tag.toString());
4524 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004525 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004526 }
Sunny Goyala1365452015-10-01 15:46:24 -07004527
4528 writer.println(prefix + " Hotseat");
4529 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4530 for (int j = 0; j < layout.getChildCount(); j++) {
4531 Object tag = layout.getChildAt(j).getTag();
4532 if (tag != null) {
4533 writer.println(prefix + " " + tag.toString());
4534 }
4535 }
4536
Sunny Goyal713edfc2016-05-06 09:58:34 -07004537 try {
4538 FileLog.flushAll(writer);
4539 } catch (Exception e) {
4540 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004541 }
4542
Adam Cohen9211d422014-10-07 18:14:53 -07004543 if (mLauncherCallbacks != null) {
4544 mLauncherCallbacks.dump(prefix, fd, writer, args);
4545 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004546 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004547
Adam Cohen59400422014-03-05 18:07:04 -08004548 public static CustomAppWidget getCustomAppWidget(String name) {
4549 return sCustomAppWidgets.get(name);
4550 }
4551
4552 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4553 return sCustomAppWidgets;
4554 }
4555
Sunny Goyal29538332016-02-18 09:10:19 -08004556 public static List<View> getFolderContents(View icon) {
4557 if (icon instanceof FolderIcon) {
4558 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4559 } else {
4560 return Collections.EMPTY_LIST;
4561 }
4562 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004563
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004564 public static Launcher getLauncher(Context context) {
4565 if (context instanceof Launcher) {
4566 return (Launcher) context;
4567 }
4568 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4569 }
4570
Sunny Goyal745bad92016-05-02 10:54:12 -07004571 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4572
4573 @Override
4574 public void onSharedPreferenceChanged(
4575 SharedPreferences sharedPreferences, String key) {
4576 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4577 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4578 if (!waitUntilResume(this, true)) {
4579 run();
4580 }
4581 }
4582 }
4583
4584 @Override
4585 public void run() {
4586 setOrientation();
4587 }
4588 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004589}