blob: 7bcd63172aba654ba696f926da59f249095a7343 [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;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700105import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700106import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -0700107import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000108import com.android.launcher3.folder.Folder;
109import com.android.launcher3.folder.FolderIcon;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700110import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700111import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700112import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700113import com.android.launcher3.model.WidgetsModel;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700114import com.android.launcher3.pageindicators.PageIndicator;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700115import com.android.launcher3.shortcuts.DeepShortcutManager;
Tony Wickham49c8d292016-07-01 11:44:34 -0700116import com.android.launcher3.shortcuts.DeepShortcutsContainer;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700117import com.android.launcher3.shortcuts.ShortcutKey;
Hyunyoung Song5aa27142016-07-21 11:48:37 -0700118import com.android.launcher3.userevent.nano.LauncherLogProto;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700119import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700120import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700121import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700122import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal322d5562015-06-25 19:35:49 -0700123import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700124import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700125import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700126import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700127import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700128import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700129
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700130import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700131import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700132import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700133import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800134import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700135import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700136import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700137import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700138
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139/**
140 * Default launcher application.
141 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100142public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700143 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
144 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700145 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700146 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700147 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700149 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700150 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400151 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700154 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700155 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700156 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
Michael Jurka8b805b12012-04-18 14:23:14 -0700158 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700159 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700160 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700161
Sunny Goyal28c6b962015-10-12 11:42:05 -0700162 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
163
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700164 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
165
Mathew Inwood876a8462013-06-14 14:12:41 +0100166 /**
167 * IntentStarter uses request codes starting with this. This must be greater than all activity
168 * request codes used internally.
169 */
170 protected static final int REQUEST_LAST = 100;
171
Michael Jurka0a457bf2012-11-19 14:05:05 -0800172 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700173 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800174 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175
Winson Chung2672ff92012-05-04 16:22:30 -0700176 // The Intent extra that defines whether to ignore the launch animation
177 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400178 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700179
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700180 public static final String ACTION_APPWIDGET_HOST_RESET =
181 "com.android.launcher3.intent.ACTION_APPWIDGET_HOST_RESET";
182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 // Type: int
184 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700185 // Type: int
186 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700187 // Type: Content Values / parcelable
188 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700189 // Type: parcelable
190 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000191 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800192 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700194 static final String APPS_VIEW_SHOWN = "launcher.apps_view_shown";
Adam Cohenb54a5982014-01-08 15:21:04 -0800195
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700196 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700197 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
198 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700199
Adam Cohen091440a2015-03-18 14:16:05 -0700200 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700201 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700202
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700203 private boolean mIsSafeModeEnabled;
204
Joe Onorato9c1289c2009-08-17 11:03:03 -0400205 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800206 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700207 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700208 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209
Winson Chunga2413752012-04-03 14:22:34 -0700210 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700211 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
212 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700213 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700214
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700215 private final BroadcastReceiver mUiBroadcastReceiver = new BroadcastReceiver() {
216
217 @Override
218 public void onReceive(Context context, Intent intent) {
219 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
220 closeSystemDialogs();
221 } else if (ACTION_APPWIDGET_HOST_RESET.equals(intent.getAction())) {
222 if (mAppWidgetHost != null) {
223 mAppWidgetHost.startListening();
224 }
225 }
226 }
227 };
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800228
Adam Cohen091440a2015-03-18 14:16:05 -0700229 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700230 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700231 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800232 private DragController mDragController;
Sunny Goyal6178f132016-07-11 17:30:03 -0700233 private View mQsbContainer;
Sunny Goyal322d5562015-06-25 19:35:49 -0700234
235 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700236
Sunny Goyalffe83f12014-08-14 17:39:34 -0700237 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700238 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700239
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700240 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800241 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800242 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700243
Michael Jurka0280c3b2010-09-17 15:00:07 -0700244 private int[] mTmpAddItemCellCoordinates = new int[2];
245
Sunny Goyal316490e2015-06-02 09:38:28 -0700246 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800247 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700248
Michael Jurka838a4ca2011-02-07 13:33:06 -0800249 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700250 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700251
Sunny Goyal47328fd2016-05-25 18:56:41 -0700252 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700253
254 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700255 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700256 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700257
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700258 // Main container view and the model for the widget tray screen.
259 @Thunk WidgetsContainerView mWidgetsView;
260 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700261
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700263 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
264 // scroll issues (because the workspace may not have been measured yet) and extra work.
265 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
266 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267
268 private SpannableStringBuilder mDefaultKeySsb = null;
269
Adam Cohen091440a2015-03-18 14:16:05 -0700270 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400271
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800272 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273 private boolean mRestoring;
274 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700275 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276
Michael Jurka1e2f4652013-07-08 18:03:46 -0700277 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700278 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700279 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700280
Joe Onorato9c1289c2009-08-17 11:03:03 -0400281 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800282 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700283 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700284 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700285 private boolean mIsResumeFromActionScreenOff;
Adam Cohen091440a2015-03-18 14:16:05 -0700286 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800287 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700288 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800289 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400290
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700291 /** Maps launcher activity components to their list of shortcut ids. */
292 private MultiHashMap<ComponentKey, String> mDeepShortcutMap = new MultiHashMap<>();
293
Adam Cohen61f560d2013-09-30 15:58:20 -0700294 private View.OnTouchListener mHapticFeedbackTouchListener;
295
Adam Cohended9f8d2010-11-03 13:25:16 -0700296 // Related to the auto-advancing of widgets
297 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700298 private static final int ADVANCE_INTERVAL = 20000;
299 private static final int ADVANCE_STAGGER = 250;
300
301 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700302 private long mAutoAdvanceSentTime;
303 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700304 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700305
Winson Chung400438b2011-01-16 17:53:48 -0800306 // Determines how long to wait after a rotation before restoring the screen orientation to
307 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700308 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800309
Adam Cohen1462de32012-07-24 22:34:36 -0700310 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700311 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700312
Winson Chung46353de2012-02-16 14:05:10 -0800313 // We only want to get the SharedPreferences once since it does an FS stat each time we get
314 // it from the context.
315 private SharedPreferences mSharedPrefs;
316
Winson Chungf0c6ae02012-03-21 16:10:31 -0700317 // Holds the page that we need to animate to, and the icon views that we need to animate up
318 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700319 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700320 private Bitmap mFolderIconBitmap;
321 private Canvas mFolderIconCanvas;
322 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700323
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700324 private DeviceProfile mDeviceProfile;
325
Adam Cohen4b66bf32015-09-18 12:15:19 -0700326 private boolean mMoveToDefaultScreenFromNewIntent;
327
Winson Chung13eb5272015-05-11 16:30:13 -0700328 // This is set to the view that launched the activity that navigated the user away from
329 // launcher. Since there is no callback for when the activity has finished launching, enable
330 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800331 private BubbleTextView mWaitingForResume;
332
Adam Cohen59400422014-03-05 18:07:04 -0800333 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
334 new HashMap<String, CustomAppWidget>();
335
Adam Cohen59400422014-03-05 18:07:04 -0800336 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700337 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
338 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800339 }
340 }
341
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700342 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
343 // state transition. If another state transition happened during this delay,
344 // simply unregister this runnable.
345 private Runnable mExitSpringLoadedModeRunnable;
346
Adam Cohen091440a2015-03-18 14:16:05 -0700347 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700348 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700349 if (mWorkspace != null) {
350 mWorkspace.buildPageHardwareLayers();
351 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700352 }
353 };
354
Adam Cohendb364c32014-05-20 14:23:40 -0700355 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800356
Adam Cohen091440a2015-03-18 14:16:05 -0700357 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800358 int requestCode;
359 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700360 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700361 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800362 int cellX;
363 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700364 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800365 }
366
Hyunyoung Songaa953652016-04-19 18:30:24 -0700367 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800368
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700369 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700370 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400371
372 @Thunk void setOrientation() {
373 if (mRotationEnabled) {
374 unlockScreenOrientation(true);
375 } else {
376 setRequestedOrientation(
377 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700378 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400379 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700380
Sunny Goyal745bad92016-05-02 10:54:12 -0700381 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700382
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 @Override
384 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700385 if (DEBUG_STRICT_MODE) {
386 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
387 .detectDiskReads()
388 .detectDiskWrites()
389 .detectNetwork() // or .detectAll() for all detectable problems
390 .penaltyLog()
391 .build());
392 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
393 .detectLeakedSqlLiteObjects()
394 .detectLeakedClosableObjects()
395 .penaltyLog()
396 .penaltyDeath()
397 .build());
398 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700399 if (LauncherAppState.PROFILE_STARTUP) {
400 Trace.beginSection("Launcher-onCreate");
401 }
Winson Chunga2413752012-04-03 14:22:34 -0700402
Adam Cohen9211d422014-10-07 18:14:53 -0700403 if (mLauncherCallbacks != null) {
404 mLauncherCallbacks.preOnCreate();
405 }
406
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400408
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400409 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700410
Adam Cohen2e6da152015-05-06 11:42:25 -0700411 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700412 mDeviceProfile = getResources().getConfiguration().orientation
413 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700414 app.getInvariantDeviceProfile().landscapeProfile
415 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700416
Sunny Goyalf7258242015-10-19 16:59:07 -0700417 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700418 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800419 mModel = app.setLauncher(this);
420 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700421 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700422
Joe Onorato41a12d22009-10-31 18:30:00 -0400423 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700424 mAllAppsController = new AllAppsTransitionController(this);
425 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700426
Sunny Goyalffe83f12014-08-14 17:39:34 -0700427 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700428
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700429 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
430 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700431
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700432 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
433 // this also ensures that any synchronous binding below doesn't re-trigger another
434 // LauncherModel load.
435 mPaused = false;
436
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700438
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 setupViews();
Winson1f064272016-07-18 17:18:02 -0700440 mDeviceProfile.layout(this, false /* notifyListeners */);
Tony Wickham827cef22016-03-17 15:39:39 -0700441 mExtractedColors = new ExtractedColors();
442 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443
Tony Wickhame2217252016-03-22 16:34:23 -0700444 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
445 .addAccessibilityStateChangeListener(this);
446
Joe Onorato7c312c12009-08-13 21:36:53 -0700447 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700448
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 mSavedState = savedInstanceState;
450 restoreState(mSavedState);
451
Sunny Goyale26d1002016-06-20 14:52:14 -0700452 if (LauncherAppState.PROFILE_STARTUP) {
453 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 }
455
456 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700457 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700458 // If the user leaves launcher, then we should just load items asynchronously when
459 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700460 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700461 } else {
462 // We only load the page synchronously if the user rotates (or triggers a
463 // configuration change) while launcher is in the foreground
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700464 if (!mModel.startLoader(mWorkspace.getRestorePage())) {
465 // If we are not binding synchronously, show a fade in animation when
466 // the first page bind completes.
467 mDragLayer.setAlpha(0);
468 }
Winson Chung4a2afa32012-07-19 14:53:05 -0700469 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470 }
471
472 // For handling default keys
473 mDefaultKeySsb = new SpannableStringBuilder();
474 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800475
476 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700477 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
478 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800479
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800480 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700481 // In case we are on a device with locked rotation, we should look at preferences to check
482 // if the user has specifically allowed rotation.
483 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700484 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700485 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
486 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700487 }
488
489 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
490 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400491 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700492
Adam Cohen9211d422014-10-07 18:14:53 -0700493 if (mLauncherCallbacks != null) {
494 mLauncherCallbacks.onCreate(savedInstanceState);
495 }
496 }
497
Sunny Goyal7779d622015-06-11 16:18:39 -0700498 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700499 public void onExtractedColorsChanged() {
500 loadExtractedColorsAndColorItems();
501 }
502
503 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700504 // TODO: do this in pre-N as well, once the extraction part is complete.
505 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700506 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700507 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700508 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham827cef22016-03-17 15:39:39 -0700509 }
510 }
511
Adam Cohen9211d422014-10-07 18:14:53 -0700512 private LauncherCallbacks mLauncherCallbacks;
513
514 public void onPostCreate(Bundle savedInstanceState) {
515 super.onPostCreate(savedInstanceState);
516 if (mLauncherCallbacks != null) {
517 mLauncherCallbacks.onPostCreate(savedInstanceState);
518 }
519 }
520
Winson1f064272016-07-18 17:18:02 -0700521 public void onInsetsChanged(Rect insets) {
522 mDeviceProfile.updateInsets(insets);
523 mDeviceProfile.layout(this, true /* notifyListeners */);
524 }
525
Sunny Goyal32554d12015-12-03 15:31:25 -0800526 /**
527 * Call this after onCreate to set or clear overlay.
528 */
529 public void setLauncherOverlay(LauncherOverlay overlay) {
530 if (overlay != null) {
531 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
532 }
533 mWorkspace.setLauncherOverlay(overlay);
534 }
535
Adam Cohen9211d422014-10-07 18:14:53 -0700536 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
537 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700538 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700539 private boolean mWorkspaceImportanceStored = false;
540 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700541 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800542 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700543 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
544
545 @Override
546 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700547 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700548 return;
549 }
550 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700551 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700552 if (mWorkspace != null) {
553 mWorkspaceImportanceForAccessibility =
554 mWorkspace.getImportantForAccessibility();
555 mWorkspace.setImportantForAccessibility(
556 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
557 mWorkspaceImportanceStored = true;
558 }
559 if (mHotseat != null) {
560 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
561 mHotseat.setImportantForAccessibility(
562 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
563 mHotseatImportanceStored = true;
564 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700565 }
566
567 @Override
568 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700569 if (mWorkspaceImportanceStored && mWorkspace != null) {
570 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
571 }
572 if (mHotseatImportanceStored && mHotseat != null) {
573 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
574 }
575 mWorkspaceImportanceStored = false;
576 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700577 }
578 });
Adam Cohen9211d422014-10-07 18:14:53 -0700579 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700580 }
581
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700582 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700583 public void onLauncherProviderChange() {
584 if (mLauncherCallbacks != null) {
585 mLauncherCallbacks.onLauncherProviderChange();
586 }
587 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700588
Adam Cohen9211d422014-10-07 18:14:53 -0700589 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700590 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700591 if (mLauncherCallbacks != null) {
592 return mLauncherCallbacks.hasCustomContentToLeft();
593 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700594 return false;
595 }
596
Dave Hawkeya8881582013-09-17 15:55:33 -0600597 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500598 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600599 * {@link #addToCustomContentPage}. This will only be invoked if
600 * {@link #hasCustomContentToLeft()} is {@code true}.
601 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500602 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700603 if (mLauncherCallbacks != null) {
604 mLauncherCallbacks.populateCustomContentContainer();
605 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600606 }
607
608 /**
609 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
610 * ensure the custom content page is added or removed if necessary.
611 */
612 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600613 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600614 // Not bound yet, wait for bindScreens to be called.
615 return;
616 }
617
618 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
619 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500620 mWorkspace.createCustomContentContainer();
621 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600622 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
623 mWorkspace.removeCustomContentPage();
624 }
625 }
626
Hyunyoung Songaa953652016-04-19 18:30:24 -0700627 public UserEventDispatcher getUserEventDispatcher() {
628 if (mLauncherCallbacks != null) {
629 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
630 if (dispatcher != null) {
631 return dispatcher;
632 }
633 }
634
Sunny Goyal64976d52016-06-20 14:56:28 -0700635 // Logger object is a singleton and does not have to be coupled with the foreground
636 // activity. Since most user event logging is done on the UI, the object is retrieved
637 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700638 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700639 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700640 }
641 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800642 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100643
Hyunyoung Song3f471442015-04-08 19:01:34 -0700644 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700645 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
646 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700647 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700648 }
649
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000650 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700651 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
652 // This cast is safe as long as the id < 0x00FFFFFF
653 // Since we jail all the dynamically generated views, there should be no clashes
654 // with any other views.
655 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000656 }
657
658 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700659 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
660 * a configuration step, this allows the proper animations to run after other transitions.
661 */
Adam Cohendb364c32014-05-20 14:23:40 -0700662 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700663 long screenId = args.screenId;
664 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
665 // When the screen id represents an actual screen (as opposed to a rank) we make sure
666 // that the drop page actually exists.
667 screenId = ensurePendingDropLayoutExists(args.screenId);
668 }
Adam Cohendb364c32014-05-20 14:23:40 -0700669
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800670 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800671 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700672 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700673 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700674 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800675 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700676 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700677 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700678 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyald478c832016-04-01 12:04:16 -0700679 completeRestoreAppWidget(args.appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700680 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700681 case REQUEST_BIND_PENDING_APPWIDGET: {
682 int widgetId = args.appWidgetId;
683 LauncherAppWidgetInfo info =
684 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
685 if (info != null) {
686 // Since the view was just bound, also launch the configure activity if needed
687 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
688 .getLauncherAppWidgetInfo(widgetId);
689 if (provider != null && provider.configure != null) {
690 startRestoredWidgetReconfigActivity(provider, info);
691 }
692 }
693 break;
694 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800695 }
Michael Jurka27614d22012-04-02 06:40:22 -0700696 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
697 // if you turned the screen off and then back while in All Apps, Launcher would not
698 // return to the workspace. Clearing mAddInfo.container here fixes this issue
699 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700700 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800701 }
702
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800703 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700704 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700705 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700706 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700707 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800708 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700709
Adam Cohenad4e15c2013-10-17 16:21:35 -0700710 Runnable exitSpringLoaded = new Runnable() {
711 @Override
712 public void run() {
713 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
714 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
715 }
716 };
717
Michael Jurka8b805b12012-04-18 14:23:14 -0700718 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700719 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700720 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700721 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
722 if (resultCode == RESULT_CANCELED) {
723 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700724 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700725 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700726 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800727 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700728 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700729 }
730 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200731 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
732 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700733 // User could have free-scrolled between pages before picking a wallpaper; make sure
734 // we move to the closest one now.
735 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700736 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200737 }
738 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700739 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200740
Adam Cohened66b2b2012-01-23 17:28:51 -0800741 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700742 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700743
Adam Cohendb364c32014-05-20 14:23:40 -0700744 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800745 // We have special handling for widgets
746 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800747 final int appWidgetId;
748 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
749 : -1;
750 if (widgetId < 0) {
751 appWidgetId = pendingAddWidgetId;
752 } else {
753 appWidgetId = widgetId;
754 }
755
Adam Cohenad4e15c2013-10-17 16:21:35 -0700756 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800757 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700758 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
759 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700760 result = RESULT_CANCELED;
761 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700762 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700763 @Override
764 public void run() {
765 exitSpringLoadedDragModeDelayed(false, 0, null);
766 }
767 };
Adam Cohendb364c32014-05-20 14:23:40 -0700768 if (workspaceLocked) {
769 // No need to remove the empty screen if we're mid-binding, as the
770 // the bind will not add the empty screen.
771 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
772 } else {
773 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
774 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
775 }
Winson Chung5aaab772012-04-27 15:24:02 -0700776 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700777 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700778 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
779 // When the screen id represents an actual screen (as opposed to a rank)
780 // we make sure that the drop page actually exists.
781 mPendingAddInfo.screenId =
782 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
783 }
Adam Cohendb364c32014-05-20 14:23:40 -0700784 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
785
786 dropLayout.setDropPending(true);
787 final Runnable onComplete = new Runnable() {
788 @Override
789 public void run() {
790 completeTwoStageWidgetDrop(resultCode, appWidgetId);
791 dropLayout.setDropPending(false);
792 }
793 };
794 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
795 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
796 } else {
797 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
798 mPendingAddInfo);
799 sPendingAddItem = args;
800 }
Winson Chung5aaab772012-04-27 15:24:02 -0700801 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800802 return;
803 }
804
Sunny Goyald478c832016-04-01 12:04:16 -0700805 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
806 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700807 if (resultCode == RESULT_OK) {
808 // Update the widget view.
809 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
810 pendingAddWidgetId, mPendingAddInfo);
811 if (workspaceLocked) {
812 sPendingAddItem = args;
813 } else {
814 completeAdd(args);
815 }
816 }
817 // Leave the widget in the pending state if the user canceled the configure.
818 return;
819 }
820
Sunny Goyalaad90582015-07-09 14:22:50 -0700821 if (requestCode == REQUEST_CREATE_SHORTCUT) {
822 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
823 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
824 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
825 mPendingAddInfo);
826 if (isWorkspaceLocked()) {
827 sPendingAddItem = args;
828 } else {
829 completeAdd(args);
830 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
831 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
832 }
833 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700834 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
835 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800838 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800839
840 }
841
842 @Override
843 protected void onActivityResult(
844 final int requestCode, final int resultCode, final Intent data) {
845 handleActivityResult(requestCode, resultCode, data);
846 if (mLauncherCallbacks != null) {
847 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
848 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800849 }
850
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600851 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700852 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600853 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700854 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
855 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
856 View v = null;
857 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
858 if (layout != null) {
859 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
860 }
861 Intent intent = sPendingAddItem.intent;
862 sPendingAddItem = null;
863 if (grantResults.length > 0
864 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700865 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700866 } else {
867 // TODO: Show a snack bar with link to settings
868 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700869 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700870 }
871 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600872 if (mLauncherCallbacks != null) {
873 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
874 grantResults);
875 }
876 }
877
Adam Cohendb364c32014-05-20 14:23:40 -0700878 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
879 appWidgetId, ItemInfo info) {
880 PendingAddArguments args = new PendingAddArguments();
881 args.requestCode = requestCode;
882 args.intent = data;
883 args.container = info.container;
884 args.screenId = info.screenId;
885 args.cellX = info.cellX;
886 args.cellY = info.cellY;
887 args.appWidgetId = appWidgetId;
888 return args;
889 }
890
891 /**
892 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
893 *
894 * @param screenId the screen id to check
895 * @return the new screen, or screenId if it exists
896 */
897 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800898 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700899 if (dropLayout == null) {
900 // it's possible that the add screen was removed because it was
901 // empty and a re-bind occurred
902 mWorkspace.addExtraEmptyScreen();
903 return mWorkspace.commitExtraEmptyScreen();
904 } else {
905 return screenId;
906 }
907 }
908
Adam Cohen091440a2015-03-18 14:16:05 -0700909 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800910 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800911 Runnable onCompleteRunnable = null;
912 int animationType = 0;
913
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700914 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800915 if (resultCode == RESULT_OK) {
916 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
917 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700918 mPendingAddWidgetInfo);
919 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800920 onCompleteRunnable = new Runnable() {
921 @Override
922 public void run() {
923 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700924 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700925 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
926 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800927 }
928 };
929 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800930 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800931 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800932 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700933 if (mDragLayer.getAnimatedView() != null) {
934 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
935 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
936 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700937 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700938 // The animated view may be null in the case of a rotation during widget configuration
939 onCompleteRunnable.run();
940 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 }
942
943 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700944 protected void onStop() {
945 super.onStop();
946 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700947
948 if (mLauncherCallbacks != null) {
949 mLauncherCallbacks.onStop();
950 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700951
952 if (Utilities.isNycMR1OrAbove()) {
953 mAppWidgetHost.stopListening();
954 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700955 }
956
957 @Override
958 protected void onStart() {
959 super.onStart();
960 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700961
962 if (mLauncherCallbacks != null) {
963 mLauncherCallbacks.onStart();
964 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700965
966 if (Utilities.isNycMR1OrAbove()) {
967 mAppWidgetHost.startListening();
968 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700969 }
970
971 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200973 long startTime = 0;
974 if (DEBUG_RESUME_TIME) {
975 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400976 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200977 }
Adam Cohen9211d422014-10-07 18:14:53 -0700978
979 if (mLauncherCallbacks != null) {
980 mLauncherCallbacks.preOnResume();
981 }
982
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700984 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700985
Winson Chung4a2afa32012-07-19 14:53:05 -0700986 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700987 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700988 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800989 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700990 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700991 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700992 // view after launching an app, as they may be depending on the UI to be static to
993 // switch to another app, otherwise, if it was
Hyunyoung Songdd60ce42016-07-27 17:08:38 -0700994 showAppsView(false /* animated */, !launchedFromApp /* updatePredictedApps */,
995 false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800996 } else if (mOnResumeState == State.WIDGETS) {
997 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700998 }
999 mOnResumeState = State.NONE;
1000
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001001 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001002 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001003 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001004 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001005 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001006 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001008 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001009 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1010 // execute them here
1011 long startTimeCallbacks = 0;
1012 if (DEBUG_RESUME_TIME) {
1013 startTimeCallbacks = System.currentTimeMillis();
1014 }
1015
Michael Jurka1e2f4652013-07-08 18:03:46 -07001016 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1017 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001018 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001019 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001020 if (DEBUG_RESUME_TIME) {
1021 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1022 (System.currentTimeMillis() - startTimeCallbacks));
1023 }
Michael Jurka447bf842013-05-15 14:52:15 +02001024 }
Michael Jurka54554252013-08-01 12:52:23 +02001025 if (mOnResumeCallbacks.size() > 0) {
1026 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1027 mOnResumeCallbacks.get(i).run();
1028 }
1029 mOnResumeCallbacks.clear();
1030 }
Winson Chunge4e50662012-01-23 14:45:13 -08001031
1032 // Reset the pressed state of icons that were locked in the press state while activities
1033 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001034 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001035 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001036 mWaitingForResume.setStayPressed(false);
1037 }
Winson Chung82963d52013-10-09 11:20:57 -07001038
Adam Cohen06dff352012-06-01 17:17:08 -07001039 // It is possible that widgets can receive updates while launcher is not in the foreground.
1040 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1041 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1042 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001043 if (!isWorkspaceLoading()) {
1044 getWorkspace().reinflateWidgetsIfNecessary();
1045 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001046
Michael Jurka447bf842013-05-15 14:52:15 +02001047 if (DEBUG_RESUME_TIME) {
1048 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1049 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001050
Adam Cohen4b66bf32015-09-18 12:15:19 -07001051 // We want to suppress callbacks about CustomContent being shown if we have just received
1052 // onNewIntent while the user was present within launcher. In that case, we post a call
1053 // to move the user to the main screen (which will occur after onResume). We don't want to
1054 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1055 // suppress here.
1056 if (mWorkspace.getCustomContentCallbacks() != null
1057 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001058 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001059 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001060 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1061 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001062 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001063 }
1064 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001065 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001066 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001067 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001068
Sunny Goyal756adbc2015-04-16 15:20:51 -07001069 if (!isWorkspaceLoading()) {
1070 // Process any items that were added while Launcher was away.
1071 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001072
1073 // Refresh shortcuts if the permission changed.
1074 mModel.refreshShortcutsIfRequired();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001075 }
Adam Cohen9211d422014-10-07 18:14:53 -07001076
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001077 if (shouldShowDiscoveryBounce()) {
1078 mAllAppsController.showDiscoveryBounce();
1079 }
1080 mIsResumeFromActionScreenOff = false;
Adam Cohen9211d422014-10-07 18:14:53 -07001081 if (mLauncherCallbacks != null) {
1082 mLauncherCallbacks.onResume();
1083 }
Adam Cohen06dff352012-06-01 17:17:08 -07001084 }
1085
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001087 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001088 // Ensure that items added to Launcher are queued until Launcher returns
1089 InstallShortcutReceiver.enableInstallQueue();
1090
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001091 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001092 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001093 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001094 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001095
1096 // We call onHide() aggressively. The custom content callbacks should be able to
1097 // debounce excess onHide calls.
1098 if (mWorkspace.getCustomContentCallbacks() != null) {
1099 mWorkspace.getCustomContentCallbacks().onHide();
1100 }
Romain Guycbb89e42009-06-08 15:52:54 -07001101
Adam Cohen9211d422014-10-07 18:14:53 -07001102 if (mLauncherCallbacks != null) {
1103 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001104 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001105 }
1106
1107 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001108 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1109 // by a onResume or by scrolling otherwise.
1110 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001111
1112 // Custom content is completely hidden
1113 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001114
1115 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1116 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001117
1118 // Indicates whether the user is allowed to scroll away from the custom content.
1119 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001120 }
1121
Adam Cohenc2d6e892014-10-16 09:49:24 -07001122 public interface LauncherOverlay {
1123
1124 /**
1125 * Touch interaction leading to overscroll has begun
1126 */
1127 public void onScrollInteractionBegin();
1128
1129 /**
1130 * Touch interaction related to overscroll has ended
1131 */
1132 public void onScrollInteractionEnd();
1133
1134 /**
1135 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1136 * screen (or in the case of RTL, the rightmost screen).
1137 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001138 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001139
1140 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001141 * Called when the launcher is ready to use the overlay
1142 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001143 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001144 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001145 }
1146
Jun Mukai8af0cd82015-05-12 12:32:12 -07001147 public interface LauncherSearchCallbacks {
1148 /**
1149 * Called when the search overlay is shown.
1150 */
1151 public void onSearchOverlayOpened();
1152
1153 /**
1154 * Called when the search overlay is dismissed.
1155 */
1156 public void onSearchOverlayClosed();
1157 }
1158
Adam Cohenc2d6e892014-10-16 09:49:24 -07001159 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001160 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001161 }
1162
1163 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1164
Sunny Goyalc86df472016-02-25 09:19:38 -08001165 public void onScrollChanged(float progress) {
1166 if (mWorkspace != null) {
1167 mWorkspace.onOverlayScrollChanged(progress);
1168 }
1169 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001170 }
1171
Jorim Jaggid017f882014-01-14 17:08:48 -08001172 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001173 if (mLauncherCallbacks != null) {
1174 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001175 } else {
1176 // On devices with a locked orientation, we will at least have the allow rotation
1177 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001178 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001179 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001180 }
1181
Adam Cohen9211d422014-10-07 18:14:53 -07001182 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001183 CustomContentCallbacks callbacks, String description) {
1184 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001185 }
1186
Adam Cohenedb40762013-07-18 16:45:45 -07001187 // The custom content needs to offset its content to account for the QSB
1188 public int getTopOffsetForCustomContent() {
1189 return mWorkspace.getPaddingTop();
1190 }
1191
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001192 @Override
1193 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001194 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001195 if (mModel.isCurrentCallbacks(this)) {
1196 mModel.stopLoader();
1197 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001198 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1199
Romain Guy13c2e7b2010-03-10 19:45:00 -08001200 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001201 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001202
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001203 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001204 @Override
1205 public void onWindowFocusChanged(boolean hasFocus) {
1206 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001207 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001208
1209 if (mLauncherCallbacks != null) {
1210 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1211 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001212 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001213
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001214 private boolean acceptFilter() {
1215 final InputMethodManager inputManager = (InputMethodManager)
1216 getSystemService(Context.INPUT_METHOD_SERVICE);
1217 return !inputManager.isFullscreenMode();
1218 }
1219
1220 @Override
1221 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001222 final int uniChar = event.getUnicodeChar();
1223 final boolean handled = super.onKeyDown(keyCode, event);
1224 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1225 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1227 keyCode, event);
1228 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001229 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001230 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001231 // showSearchDialog()
1232 // If there are multiple keystrokes before the search dialog takes focus,
1233 // onSearchRequested() will be called for every keystroke,
1234 // but it is idempotent, so it's fine.
1235 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 }
1237 }
1238
Joe Onorato8a9625e2010-01-28 15:55:35 -08001239 // Eat the long press event so the keyboard doesn't come up.
1240 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1241 return true;
1242 }
1243
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 return handled;
1245 }
1246
Winson46163472015-09-22 17:31:56 -07001247 @Override
1248 public boolean onKeyUp(int keyCode, KeyEvent event) {
1249 if (keyCode == KeyEvent.KEYCODE_MENU) {
1250 // Ignore the menu key if we are currently dragging or are on the custom content screen
1251 if (!isOnCustomContent() && !mDragController.isDragging()) {
1252 // Close any open folders
1253 closeFolder();
1254
Tony Wickham49c8d292016-07-01 11:44:34 -07001255 // Close any shortcuts containers
1256 closeShortcutsContainer();
1257
Winson46163472015-09-22 17:31:56 -07001258 // Stop resizing any widgets
1259 mWorkspace.exitWidgetResizeMode();
1260
1261 // Show the overview mode if we are on the workspace
1262 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1263 !mWorkspace.isSwitchingState()) {
1264 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001265 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001266 }
1267 }
1268 return true;
1269 }
1270 return super.onKeyUp(keyCode, event);
1271 }
1272
Karl Rosaen138a0412009-04-23 19:00:21 -07001273 private String getTypedText() {
1274 return mDefaultKeySsb.toString();
1275 }
1276
1277 private void clearTypedText() {
1278 mDefaultKeySsb.clear();
1279 mDefaultKeySsb.clearSpans();
1280 Selection.setSelection(mDefaultKeySsb, 0);
1281 }
1282
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001284 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1285 * State
1286 */
1287 private static State intToState(int stateOrdinal) {
1288 State state = State.WORKSPACE;
1289 final State[] stateValues = State.values();
1290 for (int i = 0; i < stateValues.length; i++) {
1291 if (stateValues[i].ordinal() == stateOrdinal) {
1292 state = stateValues[i];
1293 break;
1294 }
1295 }
1296 return state;
1297 }
1298
1299 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 * Restores the previous state, if it exists.
1301 *
1302 * @param savedState The previous state.
1303 */
1304 private void restoreState(Bundle savedState) {
1305 if (savedState == null) {
1306 return;
1307 }
1308
Michael Jurka883f55b2010-10-21 15:47:14 -07001309 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001310 if (state == State.APPS || state == State.WIDGETS) {
1311 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001312 }
1313
Adam Cohen21cd0022013-10-09 18:57:02 -07001314 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1315 PagedView.INVALID_RESTORE_PAGE);
1316 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001317 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 }
1319
Sunny Goyal756cd262015-08-20 12:33:21 -07001320 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1321 if (itemValues != null) {
1322 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001323 AppWidgetProviderInfo info = savedState.getParcelable(
1324 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001325 mPendingAddWidgetInfo = info == null ?
1326 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1327
Adam Cohen4637b5a2013-11-04 18:21:24 -08001328 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001329 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 mRestoring = true;
1331 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 }
1333
1334 /**
1335 * Finds all the views we need and configure them properly.
1336 */
1337 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001338 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001339 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001340 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001341 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyal6178f132016-07-11 17:30:03 -07001342 mQsbContainer = mDragLayer.findViewById(mDeviceProfile.isVerticalBarLayout()
1343 ? R.id.workspace_blocked_row : R.id.qsb_container);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001344 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001345
Sunny Goyal784f9c32016-03-23 16:56:54 -07001346 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1347 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1348 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349
Winson Chung4c98d922011-05-31 16:50:48 -07001350 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001351 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001352
Winson Chung3d503fb2011-07-13 17:25:49 -07001353 // Setup the hotseat
1354 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1355 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001356 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001357 }
1358
Winsone9f27272015-10-13 10:47:51 -07001359 // Setup the overview panel
1360 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001361
Winson Chung4c98d922011-05-31 16:50:48 -07001362 // Setup the workspace
1363 mWorkspace.setHapticFeedbackEnabled(false);
1364 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001365 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001366 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1367 // default state, otherwise we will update to the wrong offsets in RTL
1368 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001369 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001370 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001371
Tony Wickham34d2c912015-09-11 09:27:58 -07001372 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001373 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001374
Winson Chungef7f8742015-06-04 17:18:17 -07001375 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001376 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001377 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001378 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1379 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1380 } else {
1381 mAppsView.setSearchBarController(new DefaultAppSearchController());
1382 }
Craig Mautner360310b2012-10-26 15:13:08 -07001383
Winson Chung3d503fb2011-07-13 17:25:49 -07001384 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001385 mDragController.setDragScoller(mWorkspace);
1386 mDragController.setScrollView(mDragLayer);
1387 mDragController.setMoveTarget(mWorkspace);
1388 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001389 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001390
Peter Schiller310a9882016-06-30 13:52:36 -07001391 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1392 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1393 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001394
Sunny Goyal322d5562015-06-25 19:35:49 -07001395 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1396 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001397 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 }
1399
Winsone9f27272015-10-13 10:47:51 -07001400 private void setupOverviewPanel() {
1401 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1402
1403 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1404 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1405 @Override
1406 public boolean onLongClick(View v) {
1407 return v.performClick();
1408 }
1409 };
1410
1411 // Bind wallpaper button actions
1412 View wallpaperButton = findViewById(R.id.wallpaper_button);
1413 wallpaperButton.setOnClickListener(new OnClickListener() {
1414 @Override
1415 public void onClick(View view) {
1416 if (!mWorkspace.isSwitchingState()) {
1417 onClickWallpaperPicker(view);
1418 }
1419 }
1420 });
1421 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1422 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1423
1424 // Bind widget button actions
1425 mWidgetsButton = findViewById(R.id.widget_button);
1426 mWidgetsButton.setOnClickListener(new OnClickListener() {
1427 @Override
1428 public void onClick(View view) {
1429 if (!mWorkspace.isSwitchingState()) {
1430 onClickAddWidgetButton(view);
1431 }
1432 }
1433 });
1434 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1435 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1436
1437 // Bind settings actions
1438 View settingsButton = findViewById(R.id.settings_button);
1439 boolean hasSettings = hasSettings();
1440 if (hasSettings) {
1441 settingsButton.setOnClickListener(new OnClickListener() {
1442 @Override
1443 public void onClick(View view) {
1444 if (!mWorkspace.isSwitchingState()) {
1445 onClickSettingsButton(view);
1446 }
1447 }
1448 });
1449 settingsButton.setOnLongClickListener(performClickOnLongClick);
1450 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1451 } else {
1452 settingsButton.setVisibility(View.GONE);
1453 }
1454
1455 mOverviewPanel.setAlpha(0f);
1456 }
1457
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001459 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001460 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001461 */
1462 public void setAllAppsButton(View allAppsButton) {
1463 mAllAppsButton = allAppsButton;
1464 }
1465
Sunny Goyalbb011da2016-06-15 15:42:29 -07001466 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001467 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001468 }
1469
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001470 public View getWidgetsButton() {
1471 return mWidgetsButton;
1472 }
1473
Anjali Koppal5ad44842014-03-10 20:34:39 -07001474 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 * Creates a view representing a shortcut.
1476 *
1477 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001479 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001480 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 }
1482
1483 /**
1484 * Creates a view representing a shortcut inflated from the specified resource.
1485 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 * @param parent The group the shortcut belongs to.
1487 * @param info The data structure describing the shortcut.
1488 *
1489 * @return A View inflated from layoutResId.
1490 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001491 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001492 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001493 parent, false);
1494 favorite.applyFromShortcutInfo(info, mIconCache);
1495 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001497 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 return favorite;
1499 }
1500
1501 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 * Add a shortcut to the workspace.
1503 *
1504 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001506 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001507 int cellY) {
1508 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001509 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001510
Sunny Goyal5c97f512015-05-19 16:03:28 -07001511 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001512 if (info == null) {
1513 return;
1514 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001515 final View view = createShortcut(info);
1516
Sunny Goyal5c97f512015-05-19 16:03:28 -07001517 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001518 // First we check if we already know the exact location where we want to add this item.
1519 if (cellX >= 0 && cellY >= 0) {
1520 cellXY[0] = cellX;
1521 cellXY[1] = cellY;
1522 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001523
1524 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001525 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001526 true, null,null)) {
1527 return;
1528 }
1529 DragObject dragObject = new DragObject();
1530 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001531 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1532 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001533 return;
1534 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001535 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001536 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001537 }
1538
1539 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001540 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001541 return;
1542 }
1543
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001544 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545
1546 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001547 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001548 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 }
1550 }
1551
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001553 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001555 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556 */
Adam Cohen091440a2015-03-18 14:16:05 -07001557 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001558 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1559
1560 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001561 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001562 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001563 }
Romain Guycbb89e42009-06-08 15:52:54 -07001564
Adam Cohen59400422014-03-05 18:07:04 -08001565 if (appWidgetInfo.isCustomWidget) {
1566 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001567 }
1568
Adam Cohen59400422014-03-05 18:07:04 -08001569 LauncherAppWidgetInfo launcherInfo;
1570 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1571 launcherInfo.spanX = info.spanX;
1572 launcherInfo.spanY = info.spanY;
1573 launcherInfo.minSpanX = info.minSpanX;
1574 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001575 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001576
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001578 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579
1580 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001581 if (hostView == null) {
1582 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001583 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001584 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001585 hostView.setVisibility(View.VISIBLE);
1586 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001588 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 }
Romain Guycbb89e42009-06-08 15:52:54 -07001590
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001591 private void addAppWidgetToWorkspace(
1592 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001593 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001594 hostView.setTag(item);
1595 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001596
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001597 hostView.setFocusable(true);
1598 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001599
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001600 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001601 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1602
1603 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001604 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001605 }
1606 }
1607
Adam Cohended9f8d2010-11-03 13:25:16 -07001608 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1609 @Override
1610 public void onReceive(Context context, Intent intent) {
1611 final String action = intent.getAction();
1612 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1613 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001614 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001615 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001616
Winson Chungc100e8e2011-08-09 16:02:43 -07001617 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001618 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001619 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001620 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001621 if (!showWorkspace(false)) {
1622 // If we are already on the workspace, then manually reset all apps
1623 mAppsView.reset();
1624 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001625 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001626 mIsResumeFromActionScreenOff = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001627 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1628 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001629 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001630 }
1631 }
1632 };
1633
1634 @Override
1635 public void onAttachedToWindow() {
1636 super.onAttachedToWindow();
1637
1638 // Listen for broadcasts related to user-presence
1639 final IntentFilter filter = new IntentFilter();
1640 filter.addAction(Intent.ACTION_SCREEN_OFF);
1641 filter.addAction(Intent.ACTION_USER_PRESENT);
1642 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001643 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001644 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001645 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001646
1647 if (mLauncherCallbacks != null) {
1648 mLauncherCallbacks.onAttachedToWindow();
1649 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001650 }
1651
1652 @Override
1653 public void onDetachedFromWindow() {
1654 super.onDetachedFromWindow();
1655 mVisible = false;
1656
Adam Cohend113e0c2010-11-11 10:48:05 -08001657 if (mAttached) {
1658 unregisterReceiver(mReceiver);
1659 mAttached = false;
1660 }
Winson Chungb745afb2015-03-02 11:51:23 -08001661 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001662
1663 if (mLauncherCallbacks != null) {
1664 mLauncherCallbacks.onDetachedFromWindow();
1665 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001666 }
1667
1668 public void onWindowVisibilityChanged(int visibility) {
1669 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001670 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001671 // The following code used to be in onResume, but it turns out onResume is called when
1672 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1673 // is a more appropriate event to handle
1674 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001675 if (!mWorkspaceLoading) {
1676 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001677 // We want to let Launcher draw itself at least once before we force it to build
1678 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001679 // apps is nice and speedy.
1680 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001681 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001682 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001683 if (mStarted) return;
1684 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001685 // We delay the layer building a bit in order to give
1686 // other message processing a time to run. In particular
1687 // this avoids a delay in hiding the IME if it was
1688 // currently shown, because doing that may involve
1689 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001690 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001691 final ViewTreeObserver.OnDrawListener listener = this;
1692 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001693 public void run() {
1694 if (mWorkspace != null &&
1695 mWorkspace.getViewTreeObserver() != null) {
1696 mWorkspace.getViewTreeObserver().
1697 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001698 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001699 }
1700 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001701 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001702 }
1703 });
1704 }
1705 clearTypedText();
1706 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001707 }
1708
Adam Cohen091440a2015-03-18 14:16:05 -07001709 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001710 mHandler.removeMessages(ADVANCE_MSG);
1711 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1712 mHandler.sendMessageDelayed(msg, delay);
1713 mAutoAdvanceSentTime = System.currentTimeMillis();
1714 }
1715
Adam Cohen091440a2015-03-18 14:16:05 -07001716 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001717 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1718 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1719 mAutoAdvanceRunning = autoAdvanceRunning;
1720 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001721 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001722 sendAdvanceMessage(delay);
1723 } else {
1724 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001725 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001726 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1727 }
1728 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001729 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001730 }
1731 }
1732 }
1733
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001734 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1735
Adam Cohended9f8d2010-11-03 13:25:16 -07001736 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001737 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001738 if (msg.what == ADVANCE_MSG) {
1739 int i = 0;
1740 for (View key: mWidgetsToAdvance.keySet()) {
1741 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001742 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001743 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001744 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001745 public void run() {
1746 ((Advanceable) v).advance();
1747 }
1748 }, delay);
1749 }
1750 i++;
1751 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001752 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001753 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001754 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001755 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001756 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001757
Winsonc0b52fe2015-09-09 16:38:15 -07001758 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001759 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001760 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1761 if (v instanceof Advanceable) {
1762 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001763 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001764 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001765 }
1766 }
1767
Winsonc0b52fe2015-09-09 16:38:15 -07001768 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001769 if (mWidgetsToAdvance.containsKey(hostView)) {
1770 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001771 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001772 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001773 }
1774
Hyunyoung Song3f471442015-04-08 19:01:34 -07001775 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001776 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1777 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001778 }
1779
Winson Chunga6945242014-01-08 14:04:34 -08001780 public DragLayer getDragLayer() {
1781 return mDragLayer;
1782 }
1783
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001784 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001785 return mAppsView;
1786 }
1787
Hyunyoung Song3f471442015-04-08 19:01:34 -07001788 public WidgetsContainerView getWidgetsView() {
1789 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001790 }
1791
Winson Chunga6945242014-01-08 14:04:34 -08001792 public Workspace getWorkspace() {
1793 return mWorkspace;
1794 }
1795
Sunny Goyal6178f132016-07-11 17:30:03 -07001796 public View getQsbContainer() {
1797 return mQsbContainer;
1798 }
1799
Winson Chunga6945242014-01-08 14:04:34 -08001800 public Hotseat getHotseat() {
1801 return mHotseat;
1802 }
1803
Jorim Jaggid017f882014-01-14 17:08:48 -08001804 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001805 return mOverviewPanel;
1806 }
1807
Sunny Goyal47328fd2016-05-25 18:56:41 -07001808 public DropTargetBar getDropTargetBar() {
1809 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001810 }
1811
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001812 public LauncherAppWidgetHost getAppWidgetHost() {
1813 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814 }
Romain Guycbb89e42009-06-08 15:52:54 -07001815
Winson Chunga9abd0e2010-10-27 17:18:37 -07001816 public LauncherModel getModel() {
1817 return mModel;
1818 }
1819
Adam Cohen79d90c52016-04-22 13:29:20 -07001820 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001821 return mSharedPrefs;
1822 }
1823
Adam Cohen2e6da152015-05-06 11:42:25 -07001824 public DeviceProfile getDeviceProfile() {
1825 return mDeviceProfile;
1826 }
1827
Bjorn Bringertc459e522013-06-07 19:36:01 +01001828 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001829 getWindow().closeAllPanels();
1830
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001831 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001832 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001833 }
1834
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001835 @Override
1836 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001837 long startTime = 0;
1838 if (DEBUG_RESUME_TIME) {
1839 startTime = System.currentTimeMillis();
1840 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001841 super.onNewIntent(intent);
1842
1843 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001844 Folder openFolder = mWorkspace.getOpenFolder();
1845 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1846 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1847 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1848 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1849 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001850 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001851 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001852
Adam Cohen6fecd412013-10-02 17:41:50 -07001853 if (mWorkspace == null) {
1854 // Can be cases where mWorkspace is null, this prevents a NPE
1855 return;
1856 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001857 // In all these cases, only animate if we're already on home
1858 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001859
Sunny Goyal935fca12015-10-13 10:19:01 -07001860 closeFolder(alreadyOnHome);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001861 closeShortcutsContainer(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001862 exitSpringLoadedDragMode();
1863
1864 // If we are already on home, then just animate back to the workspace,
1865 // otherwise, just wait until onResume to set the state back to Workspace
1866 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001867 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001868 } else {
1869 mOnResumeState = State.WORKSPACE;
1870 }
1871
1872 final View v = getWindow().peekDecorView();
1873 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001874 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001875 INPUT_METHOD_SERVICE);
1876 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1877 }
1878
Winson Chungb745afb2015-03-02 11:51:23 -08001879 // Reset the apps view
1880 if (!alreadyOnHome && mAppsView != null) {
1881 mAppsView.scrollToTop();
1882 }
1883
Hyunyoung Song3f471442015-04-08 19:01:34 -07001884 // Reset the widgets view
1885 if (!alreadyOnHome && mWidgetsView != null) {
1886 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001887 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001888
Adam Cohen9211d422014-10-07 18:14:53 -07001889 if (mLauncherCallbacks != null) {
1890 mLauncherCallbacks.onHomeIntent();
1891 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001892 }
Adam Cohened307df2013-10-02 09:37:31 -07001893
Adam Cohen9211d422014-10-07 18:14:53 -07001894 if (mLauncherCallbacks != null) {
1895 mLauncherCallbacks.onNewIntent(intent);
1896 }
Winson15f8b172015-08-19 11:02:39 -07001897
1898 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1899 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1900 // animation.
1901 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001902 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1903 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001904 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1905 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001906
1907 // We use this flag to suppress noisy callbacks above custom content state
1908 // from onResume.
1909 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001910 mWorkspace.post(new Runnable() {
1911 @Override
1912 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001913 if (mWorkspace != null) {
1914 mWorkspace.moveToDefaultScreen(true);
1915 }
Winson15f8b172015-08-19 11:02:39 -07001916 }
1917 });
1918 }
1919 }
1920
1921 if (DEBUG_RESUME_TIME) {
1922 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1923 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001924 }
1925
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001927 public void onRestoreInstanceState(Bundle state) {
1928 super.onRestoreInstanceState(state);
1929 for (int page: mSynchronouslyBoundPages) {
1930 mWorkspace.restoreInstanceStateForChild(page);
1931 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 }
1933
1934 @Override
1935 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001936 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001937 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1938 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001939
Adam Cohen21cd0022013-10-09 18:57:02 -07001940 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001941 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942
Michael Jurka883f55b2010-10-21 15:47:14 -07001943 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001944 // We close any open folder since it will not be re-opened, and we need to make sure
1945 // this state is reflected.
1946 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1947 closeFolder(false);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001948 closeShortcutsContainer(false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001949
Adam Cohendcd297f2013-06-18 13:13:40 -07001950 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001951 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001952 ContentValues itemValues = new ContentValues();
1953 mPendingAddInfo.writeToValues(itemValues);
1954 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001955 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001956 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 }
1958
Adam Cohen9211d422014-10-07 18:14:53 -07001959 if (mLauncherCallbacks != null) {
1960 mLauncherCallbacks.onSaveInstanceState(outState);
1961 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 }
1963
1964 @Override
1965 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001967
Winson Chunge7a03942011-08-05 15:05:12 -07001968 // Remove all pending runnables
1969 mHandler.removeMessages(ADVANCE_MSG);
1970 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001971 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001972 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001973
Winson Chungcd2b0142011-06-08 16:02:26 -07001974 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001975 // It's possible to receive onDestroy after a new Launcher activity has
1976 // been created. In this case, don't interfere with the new Launcher.
1977 if (mModel.isCurrentCallbacks(this)) {
1978 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001979 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001980 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001981
Sunny Goyal745bad92016-05-02 10:54:12 -07001982 if (mRotationPrefChangeHandler != null) {
1983 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1984 }
1985
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001987 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001989 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001991 mAppWidgetHost = null;
1992
1993 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994
1995 TextKeyListener.getInstance().release();
1996
Tony Wickhame2217252016-03-22 16:34:23 -07001997 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1998 .removeAccessibilityStateChangeListener(this);
1999
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002000 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002001
Michael Jurka2ecf9952012-06-18 12:52:28 -07002002 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002003
2004 if (mLauncherCallbacks != null) {
2005 mLauncherCallbacks.onDestroy();
2006 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 }
2008
Sunny Goyalae502842016-06-17 08:43:56 -07002009 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
2010 return mAccessibilityDelegate;
2011 }
2012
Adam Cohena9cf38f2011-05-02 15:36:58 -07002013 public DragController getDragController() {
2014 return mDragController;
2015 }
2016
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002018 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07002019 onStartForResult(requestCode);
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002020 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07002021 }
2022
2023 @Override
2024 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2025 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2026 onStartForResult(requestCode);
2027 try {
2028 super.startIntentSenderForResult(intent, requestCode,
2029 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2030 } catch (IntentSender.SendIntentException e) {
2031 throw new ActivityNotFoundException();
2032 }
2033 }
2034
2035 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002036 if (requestCode >= 0) {
2037 setWaitingForResult(true);
2038 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 }
2040
Winson Chung70d72102011-08-12 11:24:35 -07002041 /**
2042 * Indicates that we want global search for this activity by setting the globalSearch
2043 * argument for {@link #startSearch} to true.
2044 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002046 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002048
Karl Rosaen138a0412009-04-23 19:00:21 -07002049 if (initialQuery == null) {
2050 // Use any text typed in the launcher as the initial query
2051 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002052 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 if (appSearchData == null) {
2054 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002055 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002057
2058 // TODO send proper bounds.
2059 Rect sourceBounds = null;
Romain Guycbb89e42009-06-08 15:52:54 -07002060
Ian Parkinson047036e2014-09-01 15:40:53 +01002061 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002062 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002063 if (clearTextImmediately) {
2064 clearTypedText();
2065 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002066
2067 // We need to show the workspace after starting the search
2068 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002069 }
2070
Ian Parkinson047036e2014-09-01 15:40:53 +01002071 /**
2072 * Start a text search.
2073 *
2074 * @return {@code true} if the search will start immediately, so any further keypresses
2075 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2076 * to buffer keypresses.
2077 */
2078 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002079 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002080 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2081 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2082 sourceBounds);
2083 }
2084
Michael Jurkaa33411c2012-06-14 16:18:21 -07002085 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002086 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002087 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002088 }
2089
2090 /**
2091 * Starts the global search activity. This code is a copied from SearchManager
2092 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002093 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002094 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002095 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002096 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2097 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2098 if (globalSearchActivity == null) {
2099 Log.w(TAG, "No global search activity found.");
2100 return;
2101 }
2102 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2103 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2104 intent.setComponent(globalSearchActivity);
2105 // Make sure that we have a Bundle to put source in
2106 if (appSearchData == null) {
2107 appSearchData = new Bundle();
2108 } else {
2109 appSearchData = new Bundle(appSearchData);
2110 }
Adam Cohen9211d422014-10-07 18:14:53 -07002111 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002112 if (!appSearchData.containsKey("source")) {
2113 appSearchData.putString("source", getPackageName());
2114 }
2115 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2116 if (!TextUtils.isEmpty(initialQuery)) {
2117 intent.putExtra(SearchManager.QUERY, initialQuery);
2118 }
2119 if (selectInitialQuery) {
2120 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2121 }
2122 intent.setSourceBounds(sourceBounds);
2123 try {
2124 startActivity(intent);
2125 } catch (ActivityNotFoundException ex) {
2126 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2127 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002128 }
2129
Yura4f93ec62014-02-04 14:15:21 +00002130 public boolean isOnCustomContent() {
2131 return mWorkspace.isOnOrMovingToCustomContent();
2132 }
2133
Winson Chung70d72102011-08-12 11:24:35 -07002134 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002135 public boolean onPrepareOptionsMenu(Menu menu) {
2136 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002137 if (mLauncherCallbacks != null) {
2138 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2139 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002140 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002141 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002142
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002143 @Override
2144 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002145 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002146 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002147 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002148 }
2149
Joe Onorato9c1289c2009-08-17 11:03:03 -04002150 public boolean isWorkspaceLocked() {
2151 return mWorkspaceLoading || mWaitingForResult;
2152 }
2153
Adam Cohen517a7f52014-03-01 12:12:59 -08002154 public boolean isWorkspaceLoading() {
2155 return mWorkspaceLoading;
2156 }
2157
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002158 private void setWorkspaceLoading(boolean value) {
2159 boolean isLocked = isWorkspaceLocked();
2160 mWorkspaceLoading = value;
2161 if (isLocked != isWorkspaceLocked()) {
2162 onWorkspaceLockedChanged();
2163 }
2164 }
2165
2166 private void setWaitingForResult(boolean value) {
2167 boolean isLocked = isWorkspaceLocked();
2168 mWaitingForResult = value;
2169 if (isLocked != isWorkspaceLocked()) {
2170 onWorkspaceLockedChanged();
2171 }
2172 }
2173
Adam Cohen9211d422014-10-07 18:14:53 -07002174 protected void onWorkspaceLockedChanged() {
2175 if (mLauncherCallbacks != null) {
2176 mLauncherCallbacks.onWorkspaceLockedChanged();
2177 }
2178 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002179
Michael Jurka0280c3b2010-09-17 15:00:07 -07002180 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002181 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002182 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002183 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2184 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002185 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002186 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002187
Tony Wickhama0628cc2015-10-14 15:23:04 -07002188 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002189 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002190 if (LOGD) {
2191 Log.d(TAG, "Adding widget from drop");
2192 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002193 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2194 }
2195
Sunny Goyale6b63a32015-01-16 16:14:29 -08002196 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002197 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2198 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002199 if (appWidgetInfo.configure != null) {
2200 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002201 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002202
Bjorn Bringert7984c942009-12-09 15:38:25 +00002203 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002204 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2205 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2206
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002207 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002208 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002209 Runnable onComplete = new Runnable() {
2210 @Override
2211 public void run() {
2212 // Exit spring loaded mode if necessary after adding the widget
2213 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2214 null);
2215 }
2216 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002217 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002218 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002219 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002220 }
2221 }
Romain Guycbb89e42009-06-08 15:52:54 -07002222
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002223 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002224 // Close any folders that may be open.
2225 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002226 mWorkspace.moveToCustomContentScreen(animate);
2227 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002228
2229 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2230 int[] cell, int spanX, int spanY) {
2231 switch (info.itemType) {
2232 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2233 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2234 int span[] = new int[2];
2235 span[0] = spanX;
2236 span[1] = spanY;
2237 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2238 container, screenId, cell, span);
2239 break;
2240 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2241 processShortcutFromDrop(info.componentName, container, screenId, cell);
2242 break;
2243 default:
2244 throw new IllegalStateException("Unknown item type: " + info.itemType);
2245 }
2246 }
2247
Adam Cohenfbba09b2011-07-18 21:43:05 -07002248 /**
2249 * Process a shortcut drop.
2250 *
2251 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002252 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002253 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002254 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002255 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2256 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002257 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002258 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002259 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002260
2261 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002262 mPendingAddInfo.cellX = cell[0];
2263 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002264 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002265
2266 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2267 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002268 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002269 }
2270
Adam Cohenfbba09b2011-07-18 21:43:05 -07002271 /**
2272 * Process a widget drop.
2273 *
2274 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002275 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002276 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002277 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002278 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2279 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002280 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002281 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002282 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002283 mPendingAddInfo.minSpanX = info.minSpanX;
2284 mPendingAddInfo.minSpanY = info.minSpanY;
2285
Adam Cohenfbba09b2011-07-18 21:43:05 -07002286 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002287 mPendingAddInfo.cellX = cell[0];
2288 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002289 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002290 if (span != null) {
2291 mPendingAddInfo.spanX = span[0];
2292 mPendingAddInfo.spanY = span[1];
2293 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002294
Adam Cohened66b2b2012-01-23 17:28:51 -08002295 AppWidgetHostView hostView = info.boundWidget;
2296 int appWidgetId;
2297 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002298 // In the case where we've prebound the widget, we remove it from the DragLayer
2299 if (LOGD) {
2300 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2301 }
2302 getDragLayer().removeView(hostView);
2303
Adam Cohened66b2b2012-01-23 17:28:51 -08002304 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002305 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002306
2307 // Clear the boundWidget so that it doesn't get destroyed.
2308 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002309 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002310 // In this case, we either need to start an activity to get permission to bind
2311 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002312 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002313 Bundle options = info.bindOptions;
2314
Sunny Goyalffe83f12014-08-14 17:39:34 -07002315 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2316 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002317 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002318 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002319 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002320 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002321 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2322 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2323 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002324 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2325 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002326 // TODO: we need to make sure that this accounts for the options bundle.
2327 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002328 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2329 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002330 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002331 }
2332
Adam Cohendcd297f2013-06-18 13:13:40 -07002333 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002334 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002335 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002336 folderInfo.title = getText(R.string.folder_name);
2337
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002338 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002339 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2340 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002341
2342 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002343 FolderIcon newFolder =
2344 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002345 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002346 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002347 // Force measure the new folder icon
2348 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2349 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002350 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002351 }
Romain Guycbb89e42009-06-08 15:52:54 -07002352
Winsonc0b52fe2015-09-09 16:38:15 -07002353 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002354 * Unbinds the view for the specified item, and removes the item and all its children.
2355 *
2356 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002357 * @param itemInfo the {@link ItemInfo} for this view.
2358 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002359 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002360 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002361 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002362 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002363 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2364 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002365 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002366 } else {
2367 mWorkspace.removeWorkspaceItem(v);
2368 }
Winsonc0b52fe2015-09-09 16:38:15 -07002369 if (deleteFromDb) {
2370 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2371 }
2372 } else if (itemInfo instanceof FolderInfo) {
2373 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002374 if (v instanceof FolderIcon) {
2375 ((FolderIcon) v).removeListeners();
2376 }
Winsonc0b52fe2015-09-09 16:38:15 -07002377 mWorkspace.removeWorkspaceItem(v);
2378 if (deleteFromDb) {
2379 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2380 }
2381 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2382 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002383 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002384 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002385 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002386 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002387 }
2388 } else {
2389 return false;
2390 }
2391 return true;
2392 }
2393
2394 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002395 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002396 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002397 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002398 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002399 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002400 // Deleting an app widget ID is a void call but writes to disk before returning
2401 // to the caller...
2402 new AsyncTask<Void, Void, Void>() {
2403 public Void doInBackground(Void ... args) {
2404 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2405 return null;
2406 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002407 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002408 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002409 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002410 }
2411
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002412 @Override
2413 public boolean dispatchKeyEvent(KeyEvent event) {
2414 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2415 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002416 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002417 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002418 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002419 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002420 dumpState();
2421 return true;
2422 }
2423 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002424 }
2425 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2426 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002427 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002428 return true;
2429 }
2430 }
2431
2432 return super.dispatchKeyEvent(event);
2433 }
2434
Joe Onorato88ec0992009-11-19 13:16:06 -08002435 @Override
2436 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002437 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2438 return;
2439 }
2440
Sunny Goyal45478022015-06-08 16:52:41 -07002441 if (mDragController.isDragging()) {
2442 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002443 return;
2444 }
2445
Tony Wickham49c8d292016-07-01 11:44:34 -07002446 if (getOpenShortcutsContainer() != null) {
2447 closeShortcutsContainer();
2448 } else if (isAppsViewVisible()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002449 showWorkspace(true);
2450 } else if (isWidgetsViewVisible()) {
2451 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002452 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002453 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002454 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002455 Folder openFolder = mWorkspace.getOpenFolder();
2456 if (openFolder.isEditingName()) {
2457 openFolder.dismissEditingName();
2458 } else {
2459 closeFolder();
2460 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002461 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002462 mWorkspace.exitWidgetResizeMode();
2463
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002464 // Back button is a no-op here, but give at least some feedback for the button press
2465 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002466 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002467 }
2468
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002469 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002470 * Launches the intent referred by the clicked shortcut.
2471 *
2472 * @param v The view representing the clicked shortcut.
2473 */
2474 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002475 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2476 // view has detached (it's possible for this to happen if the view is removed mid touch).
2477 if (v.getWindowToken() == null) {
2478 return;
2479 }
2480
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002481 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002482 return;
2483 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002484
Adam Cohen1697b792013-09-17 19:08:21 -07002485 if (v instanceof Workspace) {
2486 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002487 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002488 }
2489 return;
2490 }
2491
2492 if (v instanceof CellLayout) {
2493 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002494 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2495 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002496 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002497 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002498 }
2499
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002500 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002501 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002502 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002503 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002504 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002505 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002506 }
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002507 } else if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2508 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002509 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002510 } else if (tag instanceof AppInfo) {
2511 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002512 } else if (tag instanceof LauncherAppWidgetInfo) {
2513 if (v instanceof PendingAppWidgetHostView) {
2514 onClickPendingWidget((PendingAppWidgetHostView) v);
2515 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002516 }
2517 }
2518
Sunny Goyal70660032015-05-14 00:07:08 -07002519 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002520 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002521 return false;
2522 }
2523
Michael Jurkaaf442092010-06-10 17:01:57 -07002524 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002525 * Event handler for the app widget view which has not fully restored.
2526 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002527 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002528 if (mIsSafeModeEnabled) {
2529 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2530 return;
2531 }
2532
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002533 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002534 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002535 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2536 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2537 // This should not happen, as we make sure that an Id is allocated during bind.
2538 return;
2539 }
2540 LauncherAppWidgetProviderInfo appWidgetInfo =
2541 mAppWidgetManager.findProvider(info.providerName, info.user);
2542 if (appWidgetInfo != null) {
2543 mPendingAddWidgetId = info.appWidgetId;
2544 mPendingAddInfo.copyFrom(info);
2545 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002546
Sunny Goyald478c832016-04-01 12:04:16 -07002547 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2548 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingAddWidgetId);
2549 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
2550 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2551 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2552 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2553 }
2554 } else {
2555 LauncherAppWidgetProviderInfo appWidgetInfo =
2556 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2557 if (appWidgetInfo != null) {
2558 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2559 }
Sunny Goyalff572272014-07-23 13:58:07 -07002560 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002561 } else if (info.installProgress < 0) {
2562 // The install has not been queued
2563 final String packageName = info.providerName.getPackageName();
2564 showBrokenAppInstallDialog(packageName,
2565 new DialogInterface.OnClickListener() {
2566 public void onClick(DialogInterface dialog, int id) {
2567 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2568 }
2569 });
2570 } else {
2571 // Download has started.
2572 final String packageName = info.providerName.getPackageName();
2573 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002574 }
2575 }
2576
Sunny Goyald478c832016-04-01 12:04:16 -07002577 private void startRestoredWidgetReconfigActivity(
2578 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
2579 mPendingAddWidgetInfo = provider;
2580 mPendingAddInfo.copyFrom(info);
2581 mPendingAddWidgetId = info.appWidgetId;
2582 mAppWidgetManager.startConfigActivity(provider,
2583 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2584 }
2585
Sunny Goyalff572272014-07-23 13:58:07 -07002586 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002587 * Event handler for the "grid" button that appears on the home screen, which
2588 * enters all apps mode.
2589 *
2590 * @param v The view that was clicked.
2591 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002592 protected void onClickAllAppsButton(View v) {
2593 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002594 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002595 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.TAP,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002596 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002597 showAppsView(true /* animated */, true /* updatePredictedApps */,
2598 false /* focusSearchBar */);
Winson Chung76648c52015-07-10 14:33:23 -07002599 }
2600 }
2601
2602 protected void onLongClickAllAppsButton(View v) {
2603 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2604 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002605 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.LONGPRESS,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002606 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002607 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07002608 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002609 }
2610 }
2611
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002612 private void showBrokenAppInstallDialog(final String packageName,
2613 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002614 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002615 .setTitle(R.string.abandoned_promises_title)
2616 .setMessage(R.string.abandoned_promise_explanation)
2617 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2618 .setNeutralButton(R.string.abandoned_clean_this,
2619 new DialogInterface.OnClickListener() {
2620 public void onClick(DialogInterface dialog, int id) {
2621 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2622 mWorkspace.removeAbandonedPromise(packageName, user);
2623 }
2624 })
2625 .create().show();
2626 return;
2627 }
2628
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002629 /**
2630 * Event handler for an app shortcut click.
2631 *
2632 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2633 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002634 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002635 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2636 Object tag = v.getTag();
2637 if (!(tag instanceof ShortcutInfo)) {
2638 throw new IllegalArgumentException("Input must be a Shortcut");
2639 }
2640
2641 // Open shortcut
2642 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002643
2644 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002645 if ((shortcut.isDisabled &
2646 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2647 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2648 // If the app is only disabled because of the above flags, launch activity anyway.
2649 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002650 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002651 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2652 // Use a message specific to this shortcut, if it has one.
2653 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2654 return;
2655 }
2656 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002657 int error = R.string.activity_not_available;
2658 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2659 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002660 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2661 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002662 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002663 }
2664 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2665 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002666 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002667 }
2668
Chris Wren40c5ed32014-06-24 18:24:23 -04002669 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002670 if ((v instanceof BubbleTextView)
2671 && shortcut.isPromise()
2672 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002673 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002674 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002675 new DialogInterface.OnClickListener() {
2676 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002677 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002678 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002679 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002680 return;
2681 }
2682
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002683 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002684 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002685 }
2686
Sunny Goyala7ce1662016-05-31 15:01:35 -07002687 private void startAppShortcutOrInfoActivity(View v) {
2688 ItemInfo item = (ItemInfo) v.getTag();
2689 Intent intent = item.getIntent();
2690 if (intent == null) {
2691 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002692 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002693 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002694 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002695
2696 if (success && v instanceof BubbleTextView) {
2697 mWaitingForResume = (BubbleTextView) v;
2698 mWaitingForResume.setStayPressed(true);
2699 }
2700 }
2701
2702 /**
2703 * Event handler for a folder icon click.
2704 *
2705 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2706 */
2707 protected void onClickFolderIcon(View v) {
2708 if (LOGD) Log.d(TAG, "onClickFolder");
2709 if (!(v instanceof FolderIcon)){
2710 throw new IllegalArgumentException("Input must be a FolderIcon");
2711 }
2712
2713 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002714 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002715 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002716 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002717 }
Michael Jurka5130e402011-10-13 04:55:35 -07002718 }
2719
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002720 /**
2721 * Event handler for the (Add) Widgets button that appears after a long press
2722 * on the home screen.
2723 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002724 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002725 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002726 if (mIsSafeModeEnabled) {
2727 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2728 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002729 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002730 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002731 }
2732
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002733 /**
2734 * Event handler for the wallpaper picker button that appears after a long press
2735 * on the home screen.
2736 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002737 public void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002738 if (!Utilities.isWallapaperAllowed(this)) {
2739 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2740 return;
2741 }
2742
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002743 String pickerPackage = getString(R.string.wallpaper_picker_package);
2744 if (TextUtils.isEmpty(pickerPackage)) {
2745 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2746 }
2747
Tony Wickham785f7a52015-08-31 17:28:32 -07002748 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2749 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002750
2751 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002752 .setPackage(pickerPackage)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002753 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
2754 intent.setSourceBounds(getViewBounds(v));
2755 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002756 }
2757
2758 /**
2759 * Event handler for a click on the settings button that appears after a long press
2760 * on the home screen.
2761 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002762 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002763 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002764 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2765 .setPackage(getPackageName());
2766 intent.setSourceBounds(getViewBounds(v));
2767 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002768 }
2769
Adam Cohen61f560d2013-09-30 15:58:20 -07002770 public View.OnTouchListener getHapticFeedbackTouchListener() {
2771 if (mHapticFeedbackTouchListener == null) {
2772 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002773 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002774 @Override
2775 public boolean onTouch(View v, MotionEvent event) {
2776 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2777 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2778 }
2779 return false;
2780 }
2781 };
2782 }
2783 return mHapticFeedbackTouchListener;
2784 }
2785
Tony Wickhame2217252016-03-22 16:34:23 -07002786 @Override
2787 public void onAccessibilityStateChanged(boolean enabled) {
2788 mDragLayer.onAccessibilityStateChanged(enabled);
2789 }
2790
Sunny Goyal06e21a22016-08-11 16:02:02 -07002791 public void onDragStarted() {
Adam Cohen9211d422014-10-07 18:14:53 -07002792 if (isOnCustomContent()) {
2793 // Custom content screen doesn't participate in drag and drop. If on custom
2794 // content screen, move to default.
2795 moveWorkspaceToDefaultScreen();
2796 }
Adam Cohen9211d422014-10-07 18:14:53 -07002797 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002798
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002799 /**
2800 * Called when the user stops interacting with the launcher.
2801 * This implies that the user is now on the homescreen and is not doing housekeeping.
2802 */
Adam Cohen9211d422014-10-07 18:14:53 -07002803 protected void onInteractionEnd() {
2804 if (mLauncherCallbacks != null) {
2805 mLauncherCallbacks.onInteractionEnd();
2806 }
2807 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002808
2809 /**
2810 * Called when the user starts interacting with the launcher.
2811 * The possible interactions are:
2812 * - open all apps
2813 * - reorder an app shortcut, or a widget
2814 * - open the overview mode.
2815 * This is a good time to stop doing things that only make sense
2816 * when the user is on the homescreen and not doing housekeeping.
2817 */
Adam Cohen9211d422014-10-07 18:14:53 -07002818 protected void onInteractionBegin() {
2819 if (mLauncherCallbacks != null) {
2820 mLauncherCallbacks.onInteractionBegin();
2821 }
2822 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002823
Winson Chungcd99cd32015-04-29 11:03:24 -07002824 /** Updates the interaction state. */
2825 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002826 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002827 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002828 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2829 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002830 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002831 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002832 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002833 onInteractionEnd();
2834 }
2835 }
2836
Sunny Goyala7ce1662016-05-31 15:01:35 -07002837 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002838 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002839 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2840 try {
2841 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2842 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2843 // is enabled by default on NYC.
2844 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2845 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002846
2847 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2848 String id = ((ShortcutInfo) info).getDeepShortcutId();
2849 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002850 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2851 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002852 } else {
2853 // Could be launching some bookkeeping activity
2854 startActivity(intent, optsBundle);
2855 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002856 } finally {
2857 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002858 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002859 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002860 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2861 // corresponding permission. Show the appropriate permission prompt if that
2862 // is the case.
2863 if (intent.getComponent() == null
2864 && Intent.ACTION_CALL.equals(intent.getAction())
2865 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2866 PackageManager.PERMISSION_GRANTED) {
2867 // TODO: Rename sPendingAddItem to a generic name.
2868 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2869 0, info);
2870 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2871 REQUEST_PERMISSION_CALL_PHONE);
2872 } else {
2873 // No idea why this was thrown.
2874 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002875 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002876 }
2877 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002878
Sunny Goyala7ce1662016-05-31 15:01:35 -07002879 private Bundle getActivityLaunchOptions(View v) {
2880 if (Utilities.ATLEAST_MARSHMALLOW) {
2881 int left = 0, top = 0;
2882 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2883 if (v instanceof TextView) {
2884 // Launch from center of icon, not entire view
2885 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2886 if (icon != null) {
2887 Rect bounds = icon.getBounds();
2888 left = (width - bounds.width()) / 2;
2889 top = v.getPaddingTop();
2890 width = bounds.width();
2891 height = bounds.height();
2892 }
2893 }
2894 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2895 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2896 // On L devices, we use the device default slide-up transition.
2897 // On L MR1 devices, we use a custom version of the slide-up transition which
2898 // doesn't have the delay present in the device default.
2899 return ActivityOptions.makeCustomAnimation(
2900 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2901 }
2902 return null;
2903 }
2904
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002905 private Rect getViewBounds(View v) {
2906 int[] pos = new int[2];
2907 v.getLocationOnScreen(pos);
2908 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2909 }
2910
Sunny Goyala7ce1662016-05-31 15:01:35 -07002911 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002912 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2913 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2914 return false;
2915 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002916 // Only launch using the new animation if the shortcut has not opted out (this is a
2917 // private contract between launcher and may be ignored in the future).
2918 boolean useLaunchAnimation = (v != null) &&
2919 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2920 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2921
2922 UserHandleCompat user = null;
2923 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2924 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2925 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002926 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002927
2928 // Prepare intent
2929 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2930 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002931 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002932 }
2933 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002934 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2935 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07002936 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
2937 && ((ShortcutInfo) item).promisedIntent == null) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002938 // Shortcuts need some special checks due to legacy reasons.
2939 startShortcutIntentSafely(intent, optsBundle, item);
2940 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2941 // Could be launching some bookkeeping activity
2942 startActivity(intent, optsBundle);
2943 } else {
2944 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2945 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2946 }
2947 return true;
2948 } catch (ActivityNotFoundException|SecurityException e) {
2949 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2950 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2951 }
2952 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002953 }
2954
Adam Cohen268c4752012-06-06 17:47:33 -07002955 /**
2956 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2957 * in the DragLayer in the exact absolute location of the original FolderIcon.
2958 */
2959 private void copyFolderIconToImage(FolderIcon fi) {
2960 final int width = fi.getMeasuredWidth();
2961 final int height = fi.getMeasuredHeight();
2962
2963 // Lazy load ImageView, Bitmap and Canvas
2964 if (mFolderIconImageView == null) {
2965 mFolderIconImageView = new ImageView(this);
2966 }
2967 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2968 mFolderIconBitmap.getHeight() != height) {
2969 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2970 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2971 }
2972
2973 DragLayer.LayoutParams lp;
2974 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2975 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2976 } else {
2977 lp = new DragLayer.LayoutParams(width, height);
2978 }
2979
Adam Cohen307fe232012-08-16 17:55:58 -07002980 // The layout from which the folder is being opened may be scaled, adjust the starting
2981 // view size by this scale factor.
2982 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002983 lp.customPosition = true;
2984 lp.x = mRectForFolderAnimation.left;
2985 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002986 lp.width = (int) (scale * width);
2987 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002988
2989 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2990 fi.draw(mFolderIconCanvas);
2991 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002992 if (fi.getFolder() != null) {
2993 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2994 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002995 }
Adam Cohen268c4752012-06-06 17:47:33 -07002996 // Just in case this image view is still in the drag layer from a previous animation,
2997 // we remove it and re-add it.
2998 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2999 mDragLayer.removeView(mFolderIconImageView);
3000 }
3001 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003002 if (fi.getFolder() != null) {
3003 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003004 }
Adam Cohen268c4752012-06-06 17:47:33 -07003005 }
3006
Adam Cohen37b2a492016-04-06 18:36:06 -07003007 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003008 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07003009 FolderInfo info = (FolderInfo) fi.getTag();
3010 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3011 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003012 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3013 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003014 }
3015
Adam Cohen268c4752012-06-06 17:47:33 -07003016 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3017 copyFolderIconToImage(fi);
3018 fi.setVisibility(View.INVISIBLE);
3019
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003020 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
3021 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003022 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003023 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3024 }
3025 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003026 oa.start();
3027 }
3028
Sunny Goyal935fca12015-10-13 10:19:01 -07003029 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003030 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003031 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003032
Adam Cohen268c4752012-06-06 17:47:33 -07003033 // We remove and re-draw the FolderIcon in-case it has changed
3034 mDragLayer.removeView(mFolderIconImageView);
3035 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08003036
3037 if (cl != null) {
3038 cl.clearFolderLeaveBehind();
3039 }
3040
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003041 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003042 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003043 oa.addListener(new AnimatorListenerAdapter() {
3044 @Override
3045 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003046 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003047 // Remove the ImageView copy of the FolderIcon and make the original visible.
3048 mDragLayer.removeView(mFolderIconImageView);
3049 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003050 }
3051 }
3052 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003053 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003054 if (!animate) {
3055 oa.end();
3056 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003057 }
3058
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003059 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003060 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003061 * is animated relative to the specified View. If the View is null, no animation
3062 * is played.
3063 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003064 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003065 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003066 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003067
Adam Cohenfb91f302012-06-11 15:45:18 -07003068 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003069 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3070 if (openFolder != null && openFolder != folder) {
3071 // Close any open folder before opening a folder.
3072 closeFolder();
3073 }
3074
Adam Cohena9cf38f2011-05-02 15:36:58 -07003075 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003076
Adam Cohena9cf38f2011-05-02 15:36:58 -07003077 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003078
Sunny Goyalf4066152015-04-15 09:42:19 -07003079 // While the folder is open, the position of the icon cannot change.
3080 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3081
Adam Cohen4554ee12011-08-03 16:13:21 -07003082 // Just verify that the folder hasn't already been added to the DragLayer.
3083 // There was a one-off crash where the folder had a parent already.
3084 if (folder.getParent() == null) {
3085 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003086 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003087 } else {
3088 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3089 folder.getParent() + ").");
3090 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003091 folder.animateOpen();
3092
3093 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003094
3095 // Notify the accessibility manager that this folder "window" has appeared and occluded
3096 // the workspace items
3097 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3098 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003099 }
3100
3101 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003102 closeFolder(true);
3103 }
3104
3105 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003106 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003107 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003108 if (folder.isEditingName()) {
3109 folder.dismissEditingName();
3110 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003111 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003112 }
3113 }
3114
Sunny Goyal935fca12015-10-13 10:19:01 -07003115 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003116 animate &= !Utilities.isPowerSaverOn(this);
3117
Adam Cohen4554ee12011-08-03 16:13:21 -07003118 folder.getInfo().opened = false;
3119
3120 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3121 if (parent != null) {
3122 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003123 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003124 if (fi != null) {
3125 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3126 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003127 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003128 if (animate) {
3129 folder.animateClosed();
3130 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003131 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003132 }
Winson Chung83ca4802013-04-12 15:10:52 -07003133
Sunny Goyal935fca12015-10-13 10:19:01 -07003134 // Notify the accessibility manager that this folder "window" has disappeared and no
3135 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003136 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003137 }
3138
Tony Wickham49c8d292016-07-01 11:44:34 -07003139 public void closeShortcutsContainer() {
Sunny Goyala2454ad2016-07-22 10:50:11 -07003140 closeShortcutsContainer(true);
3141 }
3142
3143 public void closeShortcutsContainer(boolean animate) {
Tony Wickham49c8d292016-07-01 11:44:34 -07003144 DeepShortcutsContainer deepShortcutsContainer = getOpenShortcutsContainer();
3145 if (deepShortcutsContainer != null) {
Sunny Goyala2454ad2016-07-22 10:50:11 -07003146 if (animate) {
3147 deepShortcutsContainer.animateClose();
3148 } else {
3149 deepShortcutsContainer.close();
3150 }
Tony Wickham49c8d292016-07-01 11:44:34 -07003151 }
3152 }
3153
3154 /**
3155 * @return The open shortcuts container, or null if there is none
3156 */
3157 public DeepShortcutsContainer getOpenShortcutsContainer() {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07003158 // Iterate in reverse order. Shortcuts container is added later to the dragLayer,
3159 // and will be one of the last views.
3160 for (int i = mDragLayer.getChildCount() - 1; i >= 0; i--) {
3161 View child = mDragLayer.getChildAt(i);
Sunny Goyala2454ad2016-07-22 10:50:11 -07003162 if (child instanceof DeepShortcutsContainer
3163 && ((DeepShortcutsContainer) child).isOpen()) {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07003164 return (DeepShortcutsContainer) child;
3165 }
3166 }
3167 return null;
Tony Wickham49c8d292016-07-01 11:44:34 -07003168 }
3169
Tony Wickhamdadb3042016-02-24 11:07:00 -08003170 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003171 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003172 if (!isDraggingEnabled()) return false;
3173 if (isWorkspaceLocked()) return false;
3174 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003175
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07003176 if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
3177 (v == mAllAppsButton && mAllAppsButton != null)) {
Winson Chung76648c52015-07-10 14:33:23 -07003178 onLongClickAllAppsButton(v);
3179 return true;
3180 }
3181
Adam Cohen1697b792013-09-17 19:08:21 -07003182 if (v instanceof Workspace) {
3183 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003184 if (!mWorkspace.isTouchActive()) {
3185 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003186 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3187 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3188 return true;
3189 } else {
3190 return false;
3191 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003192 } else {
3193 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003194 }
Adam Cohen1697b792013-09-17 19:08:21 -07003195 }
3196
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003197 CellLayout.CellInfo longClickCellInfo = null;
3198 View itemUnderLongClick = null;
3199 if (v.getTag() instanceof ItemInfo) {
3200 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003201 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003202 itemUnderLongClick = longClickCellInfo.cell;
3203 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003204 }
3205
Winson Chung3d503fb2011-07-13 17:25:49 -07003206 // The hotseat touch handling does not go through Workspace, and we always allow long press
3207 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003208 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003209 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003210 // User long pressed on empty space
3211 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3212 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003213 if (mWorkspace.isInOverviewMode()) {
3214 mWorkspace.startReordering(v);
3215 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003216 showOverviewMode(true);
Hyunyoung Song645764e2016-06-06 14:19:02 -07003217 mHotseat.requestDisallowInterceptTouchEvent(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003218 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003219 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003220 final boolean isAllAppsButton =
3221 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3222 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3223 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003224 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003225 // User long pressed on an item
Sunny Goyal94b510c2016-08-16 15:36:48 -07003226 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003227 }
3228 }
3229 }
3230 return true;
3231 }
3232
Winson Chung3d503fb2011-07-13 17:25:49 -07003233 boolean isHotseatLayout(View layout) {
3234 return mHotseat != null && layout != null &&
3235 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3236 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003237
Winson Chung3d503fb2011-07-13 17:25:49 -07003238 /**
3239 * Returns the CellLayout of the specified container at the specified screen.
3240 */
Sunny Goyal92820592015-03-02 11:31:35 -08003241 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003242 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3243 if (mHotseat != null) {
3244 return mHotseat.getLayout();
3245 } else {
3246 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003247 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003248 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003249 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003250 }
3251 }
3252
Winson Chungb745afb2015-03-02 11:51:23 -08003253 /**
3254 * For overridden classes.
3255 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003256 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003257 return isAppsViewVisible();
3258 }
3259
3260 public boolean isAppsViewVisible() {
3261 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3262 }
3263
3264 public boolean isWidgetsViewVisible() {
3265 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003266 }
3267
Michael Jurkae326f182011-11-21 14:05:46 -08003268 @Override
3269 public void onTrimMemory(int level) {
3270 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003271 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3272 // The widget preview db can result in holding onto over
3273 // 3MB of memory for caching which isn't necessary.
3274 SQLiteDatabase.releaseMemory();
3275
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003276 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003277 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003278 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003279 if (mLauncherCallbacks != null) {
3280 mLauncherCallbacks.onTrimMemory(level);
3281 }
Michael Jurkae326f182011-11-21 14:05:46 -08003282 }
3283
Winson5c6bdbb2015-09-03 11:36:19 -07003284 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003285 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003286 }
3287
Winson5c6bdbb2015-09-03 11:36:19 -07003288 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003289 boolean changed = mState != State.WORKSPACE ||
3290 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003291 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003292 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003293 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003294 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003295
Michael Jurkab3e22d92011-10-31 15:58:33 -07003296 // Set focus to the AppsCustomize button
3297 if (mAllAppsButton != null) {
3298 mAllAppsButton.requestFocus();
3299 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003300 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003301
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003302 // Change the state *after* we've called all the transition code
3303 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003304
Winson Chung5fb63472011-02-02 17:03:37 -08003305 // Resume the auto-advance of widgets
3306 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003307 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003308
Winson Chung0f785722015-04-08 10:27:49 -07003309 if (changed) {
3310 // Send an accessibility event to announce the context change
3311 getWindow().getDecorView()
3312 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003313 }
Winson5c6bdbb2015-09-03 11:36:19 -07003314 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003315 }
3316
Winsone9f27272015-10-13 10:47:51 -07003317 /**
3318 * Shows the overview button.
3319 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07003320 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003321 showOverviewMode(animated, false);
3322 }
3323
3324 /**
3325 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3326 * onto one of the overview panel buttons.
3327 */
3328 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3329 Runnable postAnimRunnable = null;
3330 if (requestButtonFocus) {
3331 postAnimRunnable = new Runnable() {
3332 @Override
3333 public void run() {
3334 // Hitting the menu button when in touch mode does not trigger touch mode to
3335 // be disabled, so if requested, force focus on one of the overview panel
3336 // buttons.
3337 mOverviewPanel.requestFocusFromTouch();
3338 }
3339 };
3340 }
Adam Cohened307df2013-10-02 09:37:31 -07003341 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003342 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003343 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003344 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003345 }
3346
Winson Chungb745afb2015-03-02 11:51:23 -08003347 /**
3348 * Shows the apps view.
3349 */
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003350 public void showAppsView(boolean animated, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003351 boolean focusSearchBar) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003352 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07003353 if (updatePredictedApps) {
3354 tryAndUpdatePredictedApps();
3355 }
Winson Chung76648c52015-07-10 14:33:23 -07003356 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003357 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003358
Winson Chungb745afb2015-03-02 11:51:23 -08003359 /**
3360 * Shows the widgets view.
3361 */
3362 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003363 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003364 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003365 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003366 }
Winson Chung76648c52015-07-10 14:33:23 -07003367 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003368
3369 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003370 @Override
3371 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003372 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003373 }
3374 });
Winson Chungb745afb2015-03-02 11:51:23 -08003375 }
3376
3377 /**
3378 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003379 *
3380 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003381 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003382 // TODO: calling method should use the return value so that when {@code false} is returned
3383 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003384 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003385 if (!(mState == State.WORKSPACE ||
3386 mState == State.APPS_SPRING_LOADED ||
3387 mState == State.WIDGETS_SPRING_LOADED ||
3388 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003389 return false;
3390 }
3391 if (toState != State.APPS && toState != State.WIDGETS) {
3392 return false;
3393 }
Winson Chungb745afb2015-03-02 11:51:23 -08003394
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003395 // This is a safe and supported transition to bypass spring_loaded mode.
3396 if (mExitSpringLoadedModeRunnable != null) {
3397 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3398 mExitSpringLoadedModeRunnable = null;
3399 }
3400
Winson Chungb745afb2015-03-02 11:51:23 -08003401 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003402 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3403 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003404 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003405 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003406 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003407
Michael Jurkab3e22d92011-10-31 15:58:33 -07003408 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003409 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003410
3411 // Pause the auto-advance of widgets until we are out of AllApps
3412 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003413 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003414 closeFolder();
Tony Wickham49c8d292016-07-01 11:44:34 -07003415 closeShortcutsContainer();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003416
3417 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003418 getWindow().getDecorView()
3419 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003420 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003421 }
3422
Winson Chungcd99cd32015-04-29 11:03:24 -07003423 /**
3424 * Updates the workspace and interaction state on state change, and return the animation to this
3425 * new state.
3426 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003427 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003428 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003429 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003430 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003431 updateInteraction(fromState, toState);
3432 return anim;
3433 }
3434
Hyunyoung Song3f471442015-04-08 19:01:34 -07003435 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003436 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003437 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003438 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003439 }
Winson Chungb745afb2015-03-02 11:51:23 -08003440
Winson Chung006ee262015-08-03 14:40:11 -07003441 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003442 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003443 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003444
3445 if (isAppsViewVisible()) {
3446 mState = State.APPS_SPRING_LOADED;
3447 } else if (isWidgetsViewVisible()) {
3448 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003449 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003450 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003451 } else {
3452 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003453 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003454 }
Adam Cohen7777d962011-08-18 18:58:38 -07003455
Hyunyoung Song3f471442015-04-08 19:01:34 -07003456 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003457 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003458 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003459
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003460 if (mExitSpringLoadedModeRunnable != null) {
3461 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3462 }
3463 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003464 @Override
3465 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003466 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003467 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3468 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003469 // Before we show workspace, hide all apps again because
3470 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3471 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003472 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003473 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003474 } else {
3475 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003476 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003477 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07003478 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003479 };
3480 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003481 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003482
Tony Wickhame0c33232016-02-08 11:37:04 -08003483 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003484 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3485 || mState == State.WIDGETS_SPRING_LOADED;
3486 }
3487
Michael Jurkad3ef3062010-11-23 16:23:58 -08003488 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003489 if (mState == State.APPS_SPRING_LOADED) {
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003490 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07003491 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003492 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003493 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003494 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3495 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003496 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003497 }
3498
Winson Chung4ac30062015-05-08 17:34:17 -07003499 /**
3500 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3501 * resumed.
3502 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003503 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003504 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003505 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003506 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003507 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003508 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003509 }
3510 }
3511 }
3512
Michael Jurkab3e22d92011-10-31 15:58:33 -07003513 void lockAllApps() {
3514 // TODO
3515 }
3516
3517 void unlockAllApps() {
3518 // TODO
3519 }
3520
Winsonf768d932015-09-25 16:12:35 -07003521 public boolean launcherCallbacksProvidesSearch() {
3522 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3523 }
3524
Michael Jurkad7c28052012-04-27 15:43:36 -07003525 @Override
3526 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003527 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003528 final List<CharSequence> text = event.getText();
3529 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003530 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003531 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003532 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003533 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003534 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003535 } else if (mWorkspace != null) {
3536 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003537 } else {
3538 text.add(getString(R.string.all_apps_home_button_label));
3539 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003540 return result;
3541 }
3542
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003543 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003544 * If the activity is currently paused, signal that we need to run the passed Runnable
3545 * in onResume.
3546 *
3547 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003548 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3549 * wrong when we're not running, and if the activity comes back to what the configuration was
3550 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003551 *
3552 * Implementation of the method from LauncherModel.Callbacks.
3553 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003554 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003555 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003556 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003557 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003558 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003559 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003560 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003561 }
3562 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003563 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003564 return true;
3565 } else {
3566 return false;
3567 }
3568 }
3569
Michael Jurkac402cd92013-05-20 15:49:32 +02003570 private boolean waitUntilResume(Runnable run) {
3571 return waitUntilResume(run, false);
3572 }
3573
Michael Jurka1e2f4652013-07-08 18:03:46 -07003574 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003575 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003576 }
3577
Michael Jurka7607c2f2013-04-03 14:33:19 -07003578 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003579 * If the activity is currently paused, signal that we need to re-run the loader
3580 * in onResume.
3581 *
3582 * This needs to be called from incoming places where resources might have been loaded
3583 * while we are paused. That is becaues the Configuration might be wrong
3584 * when we're not running, and if it comes back to what it was when we
3585 * were paused, we are not restarted.
3586 *
3587 * Implementation of the method from LauncherModel.Callbacks.
3588 *
3589 * @return true if we are currently paused. The caller might be able to
3590 * skip some work in that case since we will come back again.
3591 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003592 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003593 public boolean setLoadOnResume() {
3594 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003595 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003596 mOnResumeNeedsLoad = true;
3597 return true;
3598 } else {
3599 return false;
3600 }
3601 }
3602
3603 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003604 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003605 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003606 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003607 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003608 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003609 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003610 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003611 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003612 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003613 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003614
Joe Onorato9c1289c2009-08-17 11:03:03 -04003615 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003616 * Clear any pending bind callbacks. This is called when is loader is planning to
3617 * perform a full rebind from scratch.
3618 */
3619 @Override
3620 public void clearPendingBinds() {
3621 mBindOnResumeCallbacks.clear();
3622 if (mPendingExecutor != null) {
3623 mPendingExecutor.markCompleted();
3624 mPendingExecutor = null;
3625 }
3626 }
3627
3628 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003629 * Refreshes the shortcuts shown on the workspace.
3630 *
3631 * Implementation of the method from LauncherModel.Callbacks.
3632 */
3633 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003634 if (LauncherAppState.PROFILE_STARTUP) {
3635 Trace.beginSection("Starting page bind");
3636 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003637 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003638
Winson Chungd64d1762013-08-20 14:37:16 -07003639 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003640 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003641 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003642
Michael Jurka05bf644e2011-11-30 20:28:53 -08003643 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003644 if (mHotseat != null) {
3645 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003646 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003647 if (LauncherAppState.PROFILE_STARTUP) {
3648 Trace.endSection();
3649 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003650 }
3651
Adam Cohendcd297f2013-06-18 13:13:40 -07003652 @Override
3653 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003654 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003655 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3656 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003657 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3658 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3659 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003660 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3661 // If there are no screens, we need to have an empty screen
3662 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003663 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003664 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003665
3666 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003667 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003668 if (hasCustomContentToLeft()) {
3669 mWorkspace.createCustomContentContainer();
3670 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003671 }
Winsonc7d2e832016-07-28 12:24:55 -07003672
3673 // After we have added all the screens, if the wallpaper was locked to the default state,
3674 // then notify to indicate that it can be released and a proper wallpaper offset can be
3675 // computed before the next layout
3676 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07003677 }
3678
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003679 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003680 int count = orderedScreenIds.size();
3681 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003682 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003683 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003684 // No need to bind the first screen, as its always bound.
3685 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3686 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003687 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003688 }
3689
Winson Chungd64d1762013-08-20 14:37:16 -07003690 public void bindAppsAdded(final ArrayList<Long> newScreens,
3691 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003692 final ArrayList<ItemInfo> addAnimated,
3693 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003694 Runnable r = new Runnable() {
3695 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003696 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003697 }
3698 };
3699 if (waitUntilResume(r)) {
3700 return;
3701 }
3702
Winson Chungd64d1762013-08-20 14:37:16 -07003703 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003704 if (newScreens != null) {
3705 bindAddScreens(newScreens);
3706 }
Winson Chungd64d1762013-08-20 14:37:16 -07003707
3708 // We add the items without animation on non-visible pages, and with
3709 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003710 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003711 bindItems(addNotAnimated, 0,
3712 addNotAnimated.size(), false);
3713 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003714 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003715 bindItems(addAnimated, 0,
3716 addAnimated.size(), true);
3717 }
Winson Chungc58497e2013-09-03 17:48:37 -07003718
Winson Chung87412982013-10-03 18:34:14 -07003719 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003720 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003721
Winson Chungb745afb2015-03-02 11:51:23 -08003722 if (addedApps != null && mAppsView != null) {
3723 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003724 }
Winson Chungd64d1762013-08-20 14:37:16 -07003725 }
3726
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003727 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003728 * Bind the items start-end from the list.
3729 *
3730 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003731 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003732 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003733 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3734 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003735 Runnable r = new Runnable() {
3736 public void run() {
3737 bindItems(shortcuts, start, end, forceAnimateIcons);
3738 }
3739 };
3740 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003741 return;
3742 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003743
Winson Chungf0c6ae02012-03-21 16:10:31 -07003744 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003745 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3746 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003747 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003748 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003749 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003750 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003751 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003752
3753 // Short circuit if we are loading dock items for a configuration which has no dock
3754 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3755 mHotseat == null) {
3756 continue;
3757 }
3758
Sunny Goyal639e9062015-08-19 19:17:06 -07003759 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003760 switch (item.itemType) {
3761 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3762 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003763 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003764 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003765 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003766 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003767 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003768 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003769 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003770 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003771 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003772 default:
3773 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003774 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003775
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003776 /*
3777 * Remove colliding items.
3778 */
3779 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3780 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3781 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3782 View v = cl.getChildAt(item.cellX, item.cellY);
3783 Object tag = v.getTag();
3784 String desc = "Collision while binding workspace item: " + item
3785 + ". Collides with " + tag;
3786 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3787 throw (new RuntimeException(desc));
3788 } else {
3789 Log.d(TAG, desc);
3790 LauncherModel.deleteItemFromDatabase(this, item);
3791 continue;
3792 }
3793 }
3794 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003795 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3796 item.cellY, 1, 1);
3797 if (animateIcons) {
3798 // Animate all the applications up now
3799 view.setAlpha(0f);
3800 view.setScaleX(0f);
3801 view.setScaleY(0f);
3802 bounceAnims.add(createNewAppBounceAnimation(view, i));
3803 newShortcutsScreenId = item.screenId;
3804 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003805 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003806
Winson Chung997a9232013-07-24 15:33:46 -07003807 if (animateIcons) {
3808 // Animate to the correct page
3809 if (newShortcutsScreenId > -1) {
3810 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003811 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003812 final Runnable startBounceAnimRunnable = new Runnable() {
3813 public void run() {
3814 anim.playTogether(bounceAnims);
3815 anim.start();
3816 }
3817 };
Winson Chung997a9232013-07-24 15:33:46 -07003818 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003819 // We post the animation slightly delayed to prevent slowdowns
3820 // when we are loading right after we return to launcher.
3821 mWorkspace.postDelayed(new Runnable() {
3822 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003823 if (mWorkspace != null) {
3824 mWorkspace.snapToPage(newScreenIndex);
3825 mWorkspace.postDelayed(startBounceAnimRunnable,
3826 NEW_APPS_ANIMATION_DELAY);
3827 }
Winson Chung94d67682013-09-25 16:29:40 -07003828 }
3829 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003830 } else {
3831 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003832 }
3833 }
Winson Chung64359a52013-07-08 17:17:08 -07003834 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003835 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003836 }
3837
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003838 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3839 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3840 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003841 view.updateAppWidget(null);
3842 view.setOnClickListener(this);
3843 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003844 mWorkspace.requestLayout();
3845 }
3846
Joe Onorato9c1289c2009-08-17 11:03:03 -04003847 /**
3848 * Add the views for a widget to the workspace.
3849 *
3850 * Implementation of the method from LauncherModel.Callbacks.
3851 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003852 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003853 Runnable r = new Runnable() {
3854 public void run() {
3855 bindAppWidget(item);
3856 }
3857 };
3858 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003859 return;
3860 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003861
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003862 if (mIsSafeModeEnabled) {
3863 bindSafeModeWidget(item);
3864 return;
3865 }
3866
Daniel Sandler843e8602010-06-07 14:59:01 -04003867 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3868 if (DEBUG_WIDGETS) {
3869 Log.d(TAG, "bindAppWidget: " + item);
3870 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003871
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003872 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003873
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003874 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3875 // If the provider is not ready, bind as a pending widget.
3876 appWidgetInfo = null;
3877 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3878 // The widget id is not valid. Try to find the widget based on the provider info.
3879 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3880 } else {
3881 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3882 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003883
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003884 // If the provider is ready, but the width is not yet restored, try to restore it.
3885 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3886 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003887 if (appWidgetInfo == null) {
3888 if (DEBUG_WIDGETS) {
3889 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3890 + " belongs to component " + item.providerName
3891 + ", as the povider is null");
3892 }
3893 LauncherModel.deleteItemFromDatabase(this, item);
3894 return;
3895 }
Sunny Goyalff572272014-07-23 13:58:07 -07003896
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003897 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003898 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003899 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3900 // Id has not been allocated yet. Allocate a new id.
3901 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3902 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003903
Sunny Goyald478c832016-04-01 12:04:16 -07003904 // Also try to bind the widget. If the bind fails, the user will be shown
3905 // a click to setup UI, which will ask for the bind permission.
3906 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3907 pendingInfo.spanX = item.spanX;
3908 pendingInfo.spanY = item.spanY;
3909 pendingInfo.minSpanX = item.minSpanX;
3910 pendingInfo.minSpanY = item.minSpanY;
3911 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003912
3913 boolean isDirectConfig =
3914 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3915 if (isDirectConfig && item.bindOptions != null) {
3916 Bundle newOptions = item.bindOptions.getExtras();
3917 if (options != null) {
3918 newOptions.putAll(options);
3919 }
3920 options = newOptions;
3921 }
Sunny Goyald478c832016-04-01 12:04:16 -07003922 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3923 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003924
Sunny Goyal86df1382016-08-10 15:03:22 -07003925 // We tried to bind once. If we were not able to bind, we would need to
3926 // go through the permission dialog, which means we cannot skip the config
3927 // activity.
3928 item.bindOptions = null;
3929 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3930
Sunny Goyald478c832016-04-01 12:04:16 -07003931 // Bind succeeded
3932 if (success) {
3933 // If the widget has a configure activity, it is still needs to set it up,
3934 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003935 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003936 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3937 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003938 }
Sunny Goyald478c832016-04-01 12:04:16 -07003939
3940 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003941 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003942 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003943 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003944 // The widget was marked as UI not ready, but there is no configure activity to
3945 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003946 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3947 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003948 }
Sunny Goyalff572272014-07-23 13:58:07 -07003949 }
3950
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003951 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003952 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003953 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3954 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003955 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003956
Sunny Goyal56c73602015-09-25 12:55:01 -07003957 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003958 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003959 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003960 deleteWidgetInfo(item);
3961 return;
3962 }
3963
Sunny Goyal233ee962015-08-03 13:05:01 -07003964 item.minSpanX = appWidgetInfo.minSpanX;
3965 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003966 addAppWidgetToWorkspace(
3967 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3968 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003969 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003970 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003971 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003972 view.updateAppWidget(null);
3973 view.setOnClickListener(this);
3974 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003975 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003976 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003977
Daniel Sandler843e8602010-06-07 14:59:01 -04003978 if (DEBUG_WIDGETS) {
3979 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3980 + (SystemClock.uptimeMillis()-start) + "ms");
3981 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003982 }
3983
Sunny Goyalff572272014-07-23 13:58:07 -07003984 /**
3985 * Restores a pending widget.
3986 *
3987 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003988 */
Sunny Goyald478c832016-04-01 12:04:16 -07003989 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003990 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3991 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3992 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003993 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003994 }
3995
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003996 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003997 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003998
3999 mWorkspace.reinflateWidgetsIfNecessary();
4000 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07004001 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07004002 }
4003
Adam Cohen1462de32012-07-24 22:34:36 -07004004 public void onPageBoundSynchronously(int page) {
4005 mSynchronouslyBoundPages.add(page);
4006 }
4007
Sunny Goyal527c7d32015-08-28 15:19:36 -07004008 @Override
4009 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
4010 if (mPendingExecutor != null) {
4011 mPendingExecutor.markCompleted();
4012 }
4013 mPendingExecutor = executor;
4014 executor.attachTo(this);
4015 }
4016
4017 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4018 if (mPendingExecutor == executor) {
4019 mPendingExecutor = null;
4020 }
4021 }
4022
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07004023 @Override
4024 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
4025 Runnable r = new Runnable() {
4026 public void run() {
4027 finishFirstPageBind(executor);
4028 }
4029 };
4030 if (waitUntilResume(r)) {
4031 return;
4032 }
4033
4034 Runnable onComplete = new Runnable() {
4035 @Override
4036 public void run() {
4037 if (executor != null) {
4038 executor.onLoadAnimationCompleted();
4039 }
4040 }
4041 };
4042 if (mDragLayer.getAlpha() < 1) {
4043 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
4044 } else {
4045 onComplete.run();
4046 }
4047 }
4048
Joe Onorato9c1289c2009-08-17 11:03:03 -04004049 /**
4050 * Callback saying that there aren't any more items to bind.
4051 *
4052 * Implementation of the method from LauncherModel.Callbacks.
4053 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004054 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004055 Runnable r = new Runnable() {
4056 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004057 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004058 }
4059 };
4060 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004061 return;
4062 }
Sunny Goyale26d1002016-06-20 14:52:14 -07004063 if (LauncherAppState.PROFILE_STARTUP) {
4064 Trace.beginSection("Page bind completed");
4065 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004066 if (mSavedState != null) {
4067 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004068 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004069 }
Sunny Goyal08442b82015-10-21 17:15:08 -07004070
Joe Onorato9c1289c2009-08-17 11:03:03 -04004071 mSavedState = null;
4072 }
4073
Adam Cohen1462de32012-07-24 22:34:36 -07004074 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004075
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004076 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07004077
4078 // If we received the result of any pending adds while the loader was running (e.g. the
4079 // widget configuration forced an orientation change), process them now.
4080 if (sPendingAddItem != null) {
4081 final long screenId = completeAdd(sPendingAddItem);
4082
4083 // TODO: this moves the user to the page where the pending item was added. Ideally,
4084 // the screen would be guaranteed to exist after bind, and the page would be set through
4085 // the workspace restore process.
4086 mWorkspace.post(new Runnable() {
4087 @Override
4088 public void run() {
4089 mWorkspace.snapToScreenId(screenId);
4090 }
4091 });
4092 sPendingAddItem = null;
4093 }
4094
Sunny Goyal756adbc2015-04-16 15:20:51 -07004095 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004096
4097 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004098 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004099 }
Sunny Goyale26d1002016-06-20 14:52:14 -07004100 if (LauncherAppState.PROFILE_STARTUP) {
4101 Trace.endSection();
4102 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004103 }
4104
Winson Chunga2413752012-04-03 14:22:34 -07004105 private boolean canRunNewAppsAnimation() {
4106 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07004107 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07004108 }
4109
Winson Chungc9168342013-06-26 14:54:55 -07004110 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004111 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004112 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4113 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004114 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004115 return bounceAnim;
4116 }
4117
Adam Cohen27772732013-11-11 14:00:56 +00004118 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004119 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004120 }
4121
Tony Wickham55616cd2015-09-23 14:55:17 -07004122 public int getSearchBarHeight() {
4123 if (mLauncherCallbacks != null) {
4124 return mLauncherCallbacks.getSearchBarHeight();
4125 }
4126 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4127 }
4128
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004129 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004130 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4131 * multiple calls to bind the same list.)
4132 */
4133 @Thunk ArrayList<AppInfo> mTmpAppsList;
4134 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4135 public void run() {
4136 bindAllApplications(mTmpAppsList);
4137 mTmpAppsList = null;
4138 }
4139 };
4140
4141 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004142 * Add the icons for all apps.
4143 *
4144 * Implementation of the method from LauncherModel.Callbacks.
4145 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004146 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004147 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4148 mTmpAppsList = apps;
4149 return;
4150 }
4151
Winson Chungb745afb2015-03-02 11:51:23 -08004152 if (mAppsView != null) {
4153 mAppsView.setApps(apps);
4154 }
Adam Cohen9211d422014-10-07 18:14:53 -07004155 if (mLauncherCallbacks != null) {
4156 mLauncherCallbacks.bindAllApplications(apps);
4157 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004158 }
4159
4160 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004161 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
4162 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
4163 */
4164 @Override
4165 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
4166 mDeepShortcutMap = deepShortcutMapCopy;
4167 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
4168 }
4169
Tony Wickham1bce7fd2016-04-28 17:39:03 -07004170 public List<String> getShortcutIdsForItem(ItemInfo info) {
4171 if (!DeepShortcutManager.supportsShortcuts(info)) {
4172 return Collections.EMPTY_LIST;
4173 }
4174 ComponentName component = info.getTargetComponent();
Hyunyoung Songd3bf9802016-08-29 14:43:53 -07004175 if (component == null) {
4176 return Collections.EMPTY_LIST;
4177 }
4178
Tony Wickham1bce7fd2016-04-28 17:39:03 -07004179 List<String> ids = mDeepShortcutMap.get(new ComponentKey(component, info.user));
4180 return ids == null ? Collections.EMPTY_LIST : ids;
4181 }
4182
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004183 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004184 * A package was updated.
4185 *
4186 * Implementation of the method from LauncherModel.Callbacks.
4187 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004188 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004189 Runnable r = new Runnable() {
4190 public void run() {
4191 bindAppsUpdated(apps);
4192 }
4193 };
4194 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004195 return;
4196 }
4197
Winson Chungb745afb2015-03-02 11:51:23 -08004198 if (mAppsView != null) {
4199 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004200 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004201 }
4202
Sunny Goyal4390ace2014-10-13 11:33:11 -07004203 @Override
4204 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4205 Runnable r = new Runnable() {
4206 public void run() {
4207 bindWidgetsRestored(widgets);
4208 }
4209 };
4210 if (waitUntilResume(r)) {
4211 return;
4212 }
4213 mWorkspace.widgetsRestored(widgets);
4214 }
4215
Joe Onorato9c1289c2009-08-17 11:03:03 -04004216 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004217 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004218 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004219 *
4220 * @param updated list of shortcuts which have changed.
4221 * @param removed list of shortcuts which were deleted in the background. This can happen when
4222 * an app gets removed from the system or some of its components are no longer
4223 * available.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004224 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004225 @Override
4226 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4227 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004228 Runnable r = new Runnable() {
4229 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004230 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004231 }
4232 };
4233 if (waitUntilResume(r)) {
4234 return;
4235 }
4236
Sunny Goyal4390ace2014-10-13 11:33:11 -07004237 if (!updated.isEmpty()) {
4238 mWorkspace.updateShortcuts(updated);
4239 }
4240
4241 if (!removed.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004242 HashSet<ComponentName> removedComponents = new HashSet<>();
4243 HashSet<ShortcutKey> removedDeepShortcuts = new HashSet<>();
4244
Sunny Goyal4390ace2014-10-13 11:33:11 -07004245 for (ShortcutInfo si : removed) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004246 if (si.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07004247 removedDeepShortcuts.add(ShortcutKey.fromShortcutInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004248 } else {
4249 removedComponents.add(si.getTargetComponent());
4250 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004251 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004252
4253 if (!removedComponents.isEmpty()) {
4254 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(removedComponents, user);
4255 mWorkspace.removeItemsByMatcher(matcher);
4256 mDragController.onAppsRemoved(matcher);
4257 }
4258
4259 if (!removedDeepShortcuts.isEmpty()) {
4260 ItemInfoMatcher matcher = ItemInfoMatcher.ofShortcutKeys(removedDeepShortcuts);
4261 mWorkspace.removeItemsByMatcher(matcher);
4262 mDragController.onAppsRemoved(matcher);
4263 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004264 }
4265 }
4266
4267 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004268 * Update the state of a package, typically related to install state.
4269 *
4270 * Implementation of the method from LauncherModel.Callbacks.
4271 */
Sunny Goyale755d462014-07-22 13:48:29 -07004272 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004273 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4274 Runnable r = new Runnable() {
4275 public void run() {
4276 bindRestoreItemsChange(updates);
4277 }
4278 };
4279 if (waitUntilResume(r)) {
4280 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004281 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004282
Sunny Goyal756adbc2015-04-16 15:20:51 -07004283 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004284 }
4285
4286 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004287 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004288 * in addition to specific applications to remove, the reason being that
4289 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004290 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004291 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004292 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004293 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004294 public void bindWorkspaceComponentsRemoved(
4295 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4296 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004297 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004298 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004299 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004300 }
Winson Chungd64d1762013-08-20 14:37:16 -07004301 };
4302 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004303 return;
4304 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004305 if (!packageNames.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004306 ItemInfoMatcher matcher = ItemInfoMatcher.ofPackages(packageNames, user);
4307 mWorkspace.removeItemsByMatcher(matcher);
4308 mDragController.onAppsRemoved(matcher);
4309
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004310 }
4311 if (!components.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004312 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(components, user);
4313 mWorkspace.removeItemsByMatcher(matcher);
4314 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004315 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004316 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004317
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004318 @Override
4319 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4320 Runnable r = new Runnable() {
4321 public void run() {
4322 bindAppInfosRemoved(appInfos);
4323 }
4324 };
4325 if (waitUntilResume(r)) {
4326 return;
Joe Onorato36115782010-06-17 13:28:48 -04004327 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004328
Winson Chungdf95eb12013-10-16 14:57:07 -07004329 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004330 if (mAppsView != null) {
4331 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004332 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004333 }
Joe Onoratobe386092009-11-17 17:32:16 -08004334
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004335 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004336 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004337 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004338 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004339 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004340
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004341 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004342 public void bindWidgetsModel(WidgetsModel model) {
4343 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004344 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004345 return;
4346 }
4347
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004348 if (mWidgetsView != null && model != null) {
4349 mWidgetsView.addWidgets(model);
4350 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004351 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004352 }
4353
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004354 @Override
4355 public void notifyWidgetProvidersChanged() {
4356 if (mWorkspace.getState().shouldUpdateWidget) {
4357 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4358 }
4359 }
4360
Winson Chung400438b2011-01-16 17:53:48 -08004361 private int mapConfigurationOriActivityInfoOri(int configOri) {
4362 final Display d = getWindowManager().getDefaultDisplay();
4363 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4364 switch (d.getRotation()) {
4365 case Surface.ROTATION_0:
4366 case Surface.ROTATION_180:
4367 // We are currently in the same basic orientation as the natural orientation
4368 naturalOri = configOri;
4369 break;
4370 case Surface.ROTATION_90:
4371 case Surface.ROTATION_270:
4372 // We are currently in the other basic orientation to the natural orientation
4373 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4374 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4375 break;
4376 }
4377
4378 int[] oriMap = {
4379 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4380 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4381 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4382 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4383 };
4384 // Since the map starts at portrait, we need to offset if this device's natural orientation
4385 // is landscape.
4386 int indexOffset = 0;
4387 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4388 indexOffset = 1;
4389 }
4390 return oriMap[(d.getRotation() + indexOffset) % 4];
4391 }
Adam Cohen446e9402011-09-15 18:21:21 -07004392
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004393 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004394 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004395 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004396 if (Utilities.ATLEAST_JB_MR2) {
4397 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4398 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004399 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4400 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004401 }
Winson Chung4b919f82012-05-01 10:44:08 -07004402 }
4403 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004404
Winson Chung4b919f82012-05-01 10:44:08 -07004405 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004406 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004407 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004408 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004409 } else {
4410 mHandler.postDelayed(new Runnable() {
4411 public void run() {
4412 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4413 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004414 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004415 }
Winson Chung4b919f82012-05-01 10:44:08 -07004416 }
Winson Chung400438b2011-01-16 17:53:48 -08004417 }
4418
Hyunyoung Songc001cf52016-07-21 17:32:43 -07004419 private void markAppsViewShown() {
4420 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4421 return;
4422 }
4423 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
4424 }
4425
4426 private boolean shouldShowDiscoveryBounce() {
4427 if (mState != mState.WORKSPACE) {
4428 return false;
4429 }
4430 if (mLauncherCallbacks != null && mLauncherCallbacks.shouldShowDiscoveryBounce()) {
4431 return true;
4432 }
4433 if (!mIsResumeFromActionScreenOff) {
4434 return false;
4435 }
4436 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4437 return false;
4438 }
4439 return true;
4440 }
4441
Winson Chung5ffd51d2015-06-25 11:36:50 -07004442 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004443 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004444 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004445 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004446 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004447 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004448 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4449 if (userHandle != null) {
4450 user = UserHandleCompat.fromUser(userHandle);
4451 }
4452 }
4453 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004454 }
4455
4456 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004457 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004458 if (user == null) {
4459 user = UserHandleCompat.myUserHandle();
4460 }
4461
4462 // Called from search suggestion, add the profile extra to the intent to ensure that we
4463 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004464 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004465 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004466 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004467 return null;
4468 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004469 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004470 }
4471
Winson Chung5ffd51d2015-06-25 11:36:50 -07004472 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004473 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4474 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004475 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004476 UserHandleCompat.myUserHandle());
4477 }
4478
Winson Chung5ffd51d2015-06-25 11:36:50 -07004479 protected void moveWorkspaceToDefaultScreen() {
4480 mWorkspace.moveToDefaultScreen(false);
4481 }
4482
Winson Chung80baf5a2010-08-09 16:03:15 -07004483 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004484 * Returns a FastBitmapDrawable with the icon, accurately sized.
4485 */
4486 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4487 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4488 d.setFilterBitmap(true);
4489 resizeIconDrawable(d);
4490 return d;
4491 }
4492
4493 /**
4494 * Resizes an icon drawable to the correct icon size.
4495 */
Winson5fbe0742015-09-30 15:33:00 -07004496 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004497 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004498 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004499 }
4500
4501 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004502 * Prints out out state for debugging.
4503 */
4504 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004505 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004506 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004507 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4508 Log.d(TAG, "mRestoring=" + mRestoring);
4509 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004510 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004511 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004512
Daniel Sandler325dc232013-06-05 22:57:57 -04004513 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004514 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004515
4516 @Override
4517 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4518 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004519 // Dump workspace
4520 writer.println(prefix + "Workspace Items");
4521 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4522 writer.println(prefix + " Homescreen " + i);
4523
4524 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4525 for (int j = 0; j < layout.getChildCount(); j++) {
4526 Object tag = layout.getChildAt(j).getTag();
4527 if (tag != null) {
4528 writer.println(prefix + " " + tag.toString());
4529 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004530 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004531 }
Sunny Goyala1365452015-10-01 15:46:24 -07004532
4533 writer.println(prefix + " Hotseat");
4534 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4535 for (int j = 0; j < layout.getChildCount(); j++) {
4536 Object tag = layout.getChildAt(j).getTag();
4537 if (tag != null) {
4538 writer.println(prefix + " " + tag.toString());
4539 }
4540 }
4541
Sunny Goyal713edfc2016-05-06 09:58:34 -07004542 try {
4543 FileLog.flushAll(writer);
4544 } catch (Exception e) {
4545 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004546 }
4547
Adam Cohen9211d422014-10-07 18:14:53 -07004548 if (mLauncherCallbacks != null) {
4549 mLauncherCallbacks.dump(prefix, fd, writer, args);
4550 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004551 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004552
Adam Cohen59400422014-03-05 18:07:04 -08004553 public static CustomAppWidget getCustomAppWidget(String name) {
4554 return sCustomAppWidgets.get(name);
4555 }
4556
4557 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4558 return sCustomAppWidgets;
4559 }
4560
Sunny Goyal29538332016-02-18 09:10:19 -08004561 public static List<View> getFolderContents(View icon) {
4562 if (icon instanceof FolderIcon) {
4563 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4564 } else {
4565 return Collections.EMPTY_LIST;
4566 }
4567 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004568
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004569 public static Launcher getLauncher(Context context) {
4570 if (context instanceof Launcher) {
4571 return (Launcher) context;
4572 }
4573 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4574 }
4575
Sunny Goyal745bad92016-05-02 10:54:12 -07004576 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4577
4578 @Override
4579 public void onSharedPreferenceChanged(
4580 SharedPreferences sharedPreferences, String key) {
4581 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4582 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4583 if (!waitUntilResume(this, true)) {
4584 run();
4585 }
4586 }
4587 }
4588
4589 @Override
4590 public void run() {
4591 setOrientation();
4592 }
4593 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004594}