blob: 0444737ed9b5897258da324d0358f6f105510f63 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyal28c6b962015-10-12 11:42:05 -070019import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070025import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000026import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Winson Chungc7450e32012-04-17 17:34:08 -070028import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040029import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080031import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080036import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070039import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040040import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070042import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070043import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070045import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.pm.PackageManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070049import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070050import android.graphics.Bitmap;
51import android.graphics.Canvas;
52import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070053import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070054import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070055import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020056import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080058import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070059import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070060import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040061import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070062import android.os.Trace;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070063import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.text.Selection;
65import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070066import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070068import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070069import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070070import android.view.HapticFeedbackConstants;
71import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.MotionEvent;
74import android.view.Surface;
75import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070076import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080077import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.ViewGroup;
79import android.view.ViewTreeObserver;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080080import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070081import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070082import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080086import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070088
Sunny Goyal651077b2014-06-30 14:15:31 -070089import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070090import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyalae502842016-06-17 08:43:56 -070091import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070092import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070093import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080094import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -070095import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010096import com.android.launcher3.compat.LauncherActivityInfoCompat;
97import com.android.launcher3.compat.LauncherAppsCompat;
98import com.android.launcher3.compat.UserHandleCompat;
99import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800100import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700101import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700102import com.android.launcher3.dragndrop.DragController;
103import com.android.launcher3.dragndrop.DragLayer;
104import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -0700105import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000106import com.android.launcher3.folder.Folder;
107import com.android.launcher3.folder.FolderIcon;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700108import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700109import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700110import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700111import com.android.launcher3.model.WidgetsModel;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700112import com.android.launcher3.pageindicators.PageIndicator;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700113import com.android.launcher3.shortcuts.DeepShortcutManager;
Tony Wickham49c8d292016-07-01 11:44:34 -0700114import com.android.launcher3.shortcuts.DeepShortcutsContainer;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700115import com.android.launcher3.shortcuts.ShortcutKey;
Hyunyoung Song5aa27142016-07-21 11:48:37 -0700116import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal2100c782016-08-22 16:00:03 -0700117import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700118import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700119import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700120import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700121import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal2100c782016-08-22 16:00:03 -0700122import com.android.launcher3.util.PendingRequestArgs;
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 Goyal2100c782016-08-22 16:00:03 -0700187 // Type: PendingRequestArgs
188 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
189 // Type: ActivityResultInfo
190 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700192 static final String APPS_VIEW_SHOWN = "launcher.apps_view_shown";
Adam Cohenb54a5982014-01-08 15:21:04 -0800193
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700194 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700195 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
196 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700197
Adam Cohen091440a2015-03-18 14:16:05 -0700198 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700199 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700200
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700201 private boolean mIsSafeModeEnabled;
202
Joe Onorato9c1289c2009-08-17 11:03:03 -0400203 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800204 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700205 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700206 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207
Winson Chunga2413752012-04-03 14:22:34 -0700208 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700209 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
210 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700211 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700212
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700213 private final BroadcastReceiver mUiBroadcastReceiver = new BroadcastReceiver() {
214
215 @Override
216 public void onReceive(Context context, Intent intent) {
217 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
218 closeSystemDialogs();
219 } else if (ACTION_APPWIDGET_HOST_RESET.equals(intent.getAction())) {
220 if (mAppWidgetHost != null) {
221 mAppWidgetHost.startListening();
222 }
223 }
224 }
225 };
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800226
Adam Cohen091440a2015-03-18 14:16:05 -0700227 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700228 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700229 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800230 private DragController mDragController;
Sunny Goyal6178f132016-07-11 17:30:03 -0700231 private View mQsbContainer;
Sunny Goyal322d5562015-06-25 19:35:49 -0700232
233 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700234
Sunny Goyalffe83f12014-08-14 17:39:34 -0700235 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700236 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700237
Michael Jurka0280c3b2010-09-17 15:00:07 -0700238 private int[] mTmpAddItemCellCoordinates = new int[2];
239
Sunny Goyal316490e2015-06-02 09:38:28 -0700240 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800241 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700242
Michael Jurka838a4ca2011-02-07 13:33:06 -0800243 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700244 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700245
Sunny Goyal47328fd2016-05-25 18:56:41 -0700246 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700247
248 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700249 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700250 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700251
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700252 // Main container view and the model for the widget tray screen.
253 @Thunk WidgetsContainerView mWidgetsView;
254 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700255
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700257 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
258 // scroll issues (because the workspace may not have been measured yet) and extra work.
259 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
260 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261
262 private SpannableStringBuilder mDefaultKeySsb = null;
263
Adam Cohen091440a2015-03-18 14:16:05 -0700264 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400265
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800266 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700267 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268
Michael Jurka1e2f4652013-07-08 18:03:46 -0700269 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700270 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700271 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700272
Joe Onorato9c1289c2009-08-17 11:03:03 -0400273 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800274 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700275 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700276 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700277 private boolean mIsResumeFromActionScreenOff;
Adam Cohen091440a2015-03-18 14:16:05 -0700278 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800279 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700280 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800281 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400282
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700283 /** Maps launcher activity components to their list of shortcut ids. */
284 private MultiHashMap<ComponentKey, String> mDeepShortcutMap = new MultiHashMap<>();
285
Adam Cohen61f560d2013-09-30 15:58:20 -0700286 private View.OnTouchListener mHapticFeedbackTouchListener;
287
Adam Cohended9f8d2010-11-03 13:25:16 -0700288 // Related to the auto-advancing of widgets
289 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700290 private static final int ADVANCE_INTERVAL = 20000;
291 private static final int ADVANCE_STAGGER = 250;
292
293 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700294 private long mAutoAdvanceSentTime;
295 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700296 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700297
Winson Chung400438b2011-01-16 17:53:48 -0800298 // Determines how long to wait after a rotation before restoring the screen orientation to
299 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700300 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800301
Adam Cohen1462de32012-07-24 22:34:36 -0700302 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
303
Winson Chung46353de2012-02-16 14:05:10 -0800304 // We only want to get the SharedPreferences once since it does an FS stat each time we get
305 // it from the context.
306 private SharedPreferences mSharedPrefs;
307
Winson Chungf0c6ae02012-03-21 16:10:31 -0700308 // Holds the page that we need to animate to, and the icon views that we need to animate up
309 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700310 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700311 private Bitmap mFolderIconBitmap;
312 private Canvas mFolderIconCanvas;
313 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700314
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700315 private DeviceProfile mDeviceProfile;
316
Adam Cohen4b66bf32015-09-18 12:15:19 -0700317 private boolean mMoveToDefaultScreenFromNewIntent;
318
Winson Chung13eb5272015-05-11 16:30:13 -0700319 // This is set to the view that launched the activity that navigated the user away from
320 // launcher. Since there is no callback for when the activity has finished launching, enable
321 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800322 private BubbleTextView mWaitingForResume;
323
Adam Cohen59400422014-03-05 18:07:04 -0800324 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
325 new HashMap<String, CustomAppWidget>();
326
Adam Cohen59400422014-03-05 18:07:04 -0800327 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700328 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
329 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800330 }
331 }
332
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700333 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
334 // state transition. If another state transition happened during this delay,
335 // simply unregister this runnable.
336 private Runnable mExitSpringLoadedModeRunnable;
337
Adam Cohen091440a2015-03-18 14:16:05 -0700338 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700339 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700340 if (mWorkspace != null) {
341 mWorkspace.buildPageHardwareLayers();
342 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700343 }
344 };
345
Sunny Goyal2100c782016-08-22 16:00:03 -0700346 // Activity result which needs to be processed after workspace has loaded.
347 private ActivityResultInfo mPendingActivityResult;
348 /**
349 * Holds extra information required to handle a result from an external call, like
350 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
351 */
352 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800353
Hyunyoung Songaa953652016-04-19 18:30:24 -0700354 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800355
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700356 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700357 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400358
359 @Thunk void setOrientation() {
360 if (mRotationEnabled) {
361 unlockScreenOrientation(true);
362 } else {
363 setRequestedOrientation(
364 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700365 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400366 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700367
Sunny Goyal745bad92016-05-02 10:54:12 -0700368 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700369
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370 @Override
371 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700372 if (DEBUG_STRICT_MODE) {
373 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
374 .detectDiskReads()
375 .detectDiskWrites()
376 .detectNetwork() // or .detectAll() for all detectable problems
377 .penaltyLog()
378 .build());
379 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
380 .detectLeakedSqlLiteObjects()
381 .detectLeakedClosableObjects()
382 .penaltyLog()
383 .penaltyDeath()
384 .build());
385 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700386 if (LauncherAppState.PROFILE_STARTUP) {
387 Trace.beginSection("Launcher-onCreate");
388 }
Winson Chunga2413752012-04-03 14:22:34 -0700389
Adam Cohen9211d422014-10-07 18:14:53 -0700390 if (mLauncherCallbacks != null) {
391 mLauncherCallbacks.preOnCreate();
392 }
393
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400395
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400396 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700397
Adam Cohen2e6da152015-05-06 11:42:25 -0700398 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700399 mDeviceProfile = getResources().getConfiguration().orientation
400 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700401 app.getInvariantDeviceProfile().landscapeProfile
402 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700403
Sunny Goyalf7258242015-10-19 16:59:07 -0700404 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700405 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800406 mModel = app.setLauncher(this);
407 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700408 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700409
Joe Onorato41a12d22009-10-31 18:30:00 -0400410 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700411 mAllAppsController = new AllAppsTransitionController(this);
412 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700413
Sunny Goyalffe83f12014-08-14 17:39:34 -0700414 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700415
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700416 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
417 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700418
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700419 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
420 // this also ensures that any synchronous binding below doesn't re-trigger another
421 // LauncherModel load.
422 mPaused = false;
423
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800424 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700425
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800426 setupViews();
Winson1f064272016-07-18 17:18:02 -0700427 mDeviceProfile.layout(this, false /* notifyListeners */);
Tony Wickham827cef22016-03-17 15:39:39 -0700428 mExtractedColors = new ExtractedColors();
429 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800430
Tony Wickhame2217252016-03-22 16:34:23 -0700431 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
432 .addAccessibilityStateChangeListener(this);
433
Joe Onorato7c312c12009-08-13 21:36:53 -0700434 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700435
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436 mSavedState = savedInstanceState;
437 restoreState(mSavedState);
438
Sunny Goyale26d1002016-06-20 14:52:14 -0700439 if (LauncherAppState.PROFILE_STARTUP) {
440 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 }
442
Sunny Goyal2100c782016-08-22 16:00:03 -0700443 // We only load the page synchronously if the user rotates (or triggers a
444 // configuration change) while launcher is in the foreground
445 if (!mModel.startLoader(mWorkspace.getRestorePage())) {
446 // If we are not binding synchronously, show a fade in animation when
447 // the first page bind completes.
448 mDragLayer.setAlpha(0);
449 } else {
450 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 }
452
453 // For handling default keys
454 mDefaultKeySsb = new SpannableStringBuilder();
455 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800456
457 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700458 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
459 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800460
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800461 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700462 // In case we are on a device with locked rotation, we should look at preferences to check
463 // if the user has specifically allowed rotation.
464 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700465 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700466 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
467 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700468 }
469
470 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
471 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400472 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700473
Adam Cohen9211d422014-10-07 18:14:53 -0700474 if (mLauncherCallbacks != null) {
475 mLauncherCallbacks.onCreate(savedInstanceState);
476 }
477 }
478
Sunny Goyal7779d622015-06-11 16:18:39 -0700479 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700480 public void onExtractedColorsChanged() {
481 loadExtractedColorsAndColorItems();
482 }
483
484 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700485 // TODO: do this in pre-N as well, once the extraction part is complete.
486 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700487 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700488 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700489 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham827cef22016-03-17 15:39:39 -0700490 }
491 }
492
Adam Cohen9211d422014-10-07 18:14:53 -0700493 private LauncherCallbacks mLauncherCallbacks;
494
495 public void onPostCreate(Bundle savedInstanceState) {
496 super.onPostCreate(savedInstanceState);
497 if (mLauncherCallbacks != null) {
498 mLauncherCallbacks.onPostCreate(savedInstanceState);
499 }
500 }
501
Winson1f064272016-07-18 17:18:02 -0700502 public void onInsetsChanged(Rect insets) {
503 mDeviceProfile.updateInsets(insets);
504 mDeviceProfile.layout(this, true /* notifyListeners */);
505 }
506
Sunny Goyal32554d12015-12-03 15:31:25 -0800507 /**
508 * Call this after onCreate to set or clear overlay.
509 */
510 public void setLauncherOverlay(LauncherOverlay overlay) {
511 if (overlay != null) {
512 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
513 }
514 mWorkspace.setLauncherOverlay(overlay);
515 }
516
Adam Cohen9211d422014-10-07 18:14:53 -0700517 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
518 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700519 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700520 private boolean mWorkspaceImportanceStored = false;
521 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700522 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800523 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700524 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
525
526 @Override
527 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700528 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700529 return;
530 }
531 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700532 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700533 if (mWorkspace != null) {
534 mWorkspaceImportanceForAccessibility =
535 mWorkspace.getImportantForAccessibility();
536 mWorkspace.setImportantForAccessibility(
537 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
538 mWorkspaceImportanceStored = true;
539 }
540 if (mHotseat != null) {
541 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
542 mHotseat.setImportantForAccessibility(
543 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
544 mHotseatImportanceStored = true;
545 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700546 }
547
548 @Override
549 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700550 if (mWorkspaceImportanceStored && mWorkspace != null) {
551 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
552 }
553 if (mHotseatImportanceStored && mHotseat != null) {
554 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
555 }
556 mWorkspaceImportanceStored = false;
557 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700558 }
559 });
Adam Cohen9211d422014-10-07 18:14:53 -0700560 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700561 }
562
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700563 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700564 public void onLauncherProviderChange() {
565 if (mLauncherCallbacks != null) {
566 mLauncherCallbacks.onLauncherProviderChange();
567 }
568 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700569
Adam Cohen9211d422014-10-07 18:14:53 -0700570 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700571 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700572 if (mLauncherCallbacks != null) {
573 return mLauncherCallbacks.hasCustomContentToLeft();
574 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700575 return false;
576 }
577
Dave Hawkeya8881582013-09-17 15:55:33 -0600578 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500579 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600580 * {@link #addToCustomContentPage}. This will only be invoked if
581 * {@link #hasCustomContentToLeft()} is {@code true}.
582 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500583 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700584 if (mLauncherCallbacks != null) {
585 mLauncherCallbacks.populateCustomContentContainer();
586 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600587 }
588
589 /**
590 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
591 * ensure the custom content page is added or removed if necessary.
592 */
593 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600594 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600595 // Not bound yet, wait for bindScreens to be called.
596 return;
597 }
598
599 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
600 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500601 mWorkspace.createCustomContentContainer();
602 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600603 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
604 mWorkspace.removeCustomContentPage();
605 }
606 }
607
Hyunyoung Songaa953652016-04-19 18:30:24 -0700608 public UserEventDispatcher getUserEventDispatcher() {
609 if (mLauncherCallbacks != null) {
610 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
611 if (dispatcher != null) {
612 return dispatcher;
613 }
614 }
615
Sunny Goyal64976d52016-06-20 14:56:28 -0700616 // Logger object is a singleton and does not have to be coupled with the foreground
617 // activity. Since most user event logging is done on the UI, the object is retrieved
618 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700619 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700620 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700621 }
622 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800623 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100624
Hyunyoung Song3f471442015-04-08 19:01:34 -0700625 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700626 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
627 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700628 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700629 }
630
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000631 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700632 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
633 // This cast is safe as long as the id < 0x00FFFFFF
634 // Since we jail all the dynamically generated views, there should be no clashes
635 // with any other views.
636 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000637 }
638
639 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700640 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
641 * a configuration step, this allows the proper animations to run after other transitions.
642 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700643 private long completeAdd(
644 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
645 long screenId = info.screenId;
646 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700647 // When the screen id represents an actual screen (as opposed to a rank) we make sure
648 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700649 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700650 }
Adam Cohendb364c32014-05-20 14:23:40 -0700651
Sunny Goyal2100c782016-08-22 16:00:03 -0700652 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800653 case REQUEST_CREATE_SHORTCUT:
Sunny Goyal2100c782016-08-22 16:00:03 -0700654 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700655 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800656 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700657 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700658 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700659 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700660 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700661 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700662 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700663 int widgetId = appWidgetId;
664 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700665 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700666 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700667 // Since the view was just bound, also launch the configure activity if needed
668 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
669 .getLauncherAppWidgetInfo(widgetId);
670 if (provider != null && provider.configure != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700671 startRestoredWidgetReconfigActivity(provider, widgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -0700672 }
673 }
674 break;
675 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800676 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700677
Adam Cohendb364c32014-05-20 14:23:40 -0700678 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800679 }
680
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800681 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700682 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700683 if (isWorkspaceLoading()) {
684 // process the result once the workspace has loaded.
685 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
686 return;
687 }
688 mPendingActivityResult = null;
689
Winson Chunge341d302013-08-16 14:31:00 -0700690 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700691 final PendingRequestArgs requestArgs = mPendingRequestArgs;
692 setWaitingForResult(null);
693 if (requestArgs == null) {
694 return;
695 }
696
697 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700698
Adam Cohenad4e15c2013-10-17 16:21:35 -0700699 Runnable exitSpringLoaded = new Runnable() {
700 @Override
701 public void run() {
702 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
703 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
704 }
705 };
706
Michael Jurka8b805b12012-04-18 14:23:14 -0700707 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700708 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700709 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700710 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
711 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700712 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700713 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700714 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700715 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700716 addAppWidgetImpl(
717 appWidgetId, requestArgs, null,
718 requestArgs.getWidgetProvider(),
719 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700720 }
721 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200722 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
723 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700724 // User could have free-scrolled between pages before picking a wallpaper; make sure
725 // we move to the closest one now.
726 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700727 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200728 }
729 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700730 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200731
Adam Cohened66b2b2012-01-23 17:28:51 -0800732 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700733 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700734
Adam Cohened66b2b2012-01-23 17:28:51 -0800735 // We have special handling for widgets
736 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800737 final int appWidgetId;
738 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
739 : -1;
740 if (widgetId < 0) {
741 appWidgetId = pendingAddWidgetId;
742 } else {
743 appWidgetId = widgetId;
744 }
745
Adam Cohenad4e15c2013-10-17 16:21:35 -0700746 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800747 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700748 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
749 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700750 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700751 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700752 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700753 @Override
754 public void run() {
755 exitSpringLoadedDragModeDelayed(false, 0, null);
756 }
757 };
Adam Cohendb364c32014-05-20 14:23:40 -0700758
Sunny Goyal2100c782016-08-22 16:00:03 -0700759 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
760 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
761 } else {
762 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
763 // When the screen id represents an actual screen (as opposed to a rank)
764 // we make sure that the drop page actually exists.
765 requestArgs.screenId =
766 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700767 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700768 final CellLayout dropLayout =
769 mWorkspace.getScreenWithId(requestArgs.screenId);
770
771 dropLayout.setDropPending(true);
772 final Runnable onComplete = new Runnable() {
773 @Override
774 public void run() {
775 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
776 dropLayout.setDropPending(false);
777 }
778 };
779 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
780 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700781 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800782 return;
783 }
784
Sunny Goyald478c832016-04-01 12:04:16 -0700785 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
786 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700787 if (resultCode == RESULT_OK) {
788 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700789 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700790 }
791 // Leave the widget in the pending state if the user canceled the configure.
792 return;
793 }
794
Sunny Goyalaad90582015-07-09 14:22:50 -0700795 if (requestCode == REQUEST_CREATE_SHORTCUT) {
796 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700797 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
798 completeAdd(requestCode, data, -1, requestArgs);
799 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
800 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
801
Sunny Goyalaad90582015-07-09 14:22:50 -0700802 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700803 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
804 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800807 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800808 }
809
810 @Override
811 protected void onActivityResult(
812 final int requestCode, final int resultCode, final Intent data) {
813 handleActivityResult(requestCode, resultCode, data);
814 if (mLauncherCallbacks != null) {
815 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
816 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800817 }
818
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600819 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700820 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600821 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700822 PendingRequestArgs pendingArgs = mPendingRequestArgs;
823 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
824 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
825 setWaitingForResult(null);
826
Sunny Goyal28c6b962015-10-12 11:42:05 -0700827 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700828 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700829 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700830 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700831 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700832 Intent intent = pendingArgs.getPendingIntent();
833
Sunny Goyal28c6b962015-10-12 11:42:05 -0700834 if (grantResults.length > 0
835 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700836 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700837 } else {
838 // TODO: Show a snack bar with link to settings
839 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700840 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700841 }
842 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600843 if (mLauncherCallbacks != null) {
844 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
845 grantResults);
846 }
847 }
848
Adam Cohendb364c32014-05-20 14:23:40 -0700849 /**
850 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
851 *
852 * @param screenId the screen id to check
853 * @return the new screen, or screenId if it exists
854 */
855 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800856 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700857 if (dropLayout == null) {
858 // it's possible that the add screen was removed because it was
859 // empty and a re-bind occurred
860 mWorkspace.addExtraEmptyScreen();
861 return mWorkspace.commitExtraEmptyScreen();
862 } else {
863 return screenId;
864 }
865 }
866
Sunny Goyal2100c782016-08-22 16:00:03 -0700867 @Thunk void completeTwoStageWidgetDrop(
868 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
869 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800870 Runnable onCompleteRunnable = null;
871 int animationType = 0;
872
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700873 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800874 if (resultCode == RESULT_OK) {
875 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
876 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal2100c782016-08-22 16:00:03 -0700877 requestArgs.getWidgetProvider());
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700878 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800879 onCompleteRunnable = new Runnable() {
880 @Override
881 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700882 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700883 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
884 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800885 }
886 };
887 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800888 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800889 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800890 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700891 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700892 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700893 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
894 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700895 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700896 // The animated view may be null in the case of a rotation during widget configuration
897 onCompleteRunnable.run();
898 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899 }
900
901 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700902 protected void onStop() {
903 super.onStop();
904 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700905
906 if (mLauncherCallbacks != null) {
907 mLauncherCallbacks.onStop();
908 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700909
910 if (Utilities.isNycMR1OrAbove()) {
911 mAppWidgetHost.stopListening();
912 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700913 }
914
915 @Override
916 protected void onStart() {
917 super.onStart();
918 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700919
920 if (mLauncherCallbacks != null) {
921 mLauncherCallbacks.onStart();
922 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700923
924 if (Utilities.isNycMR1OrAbove()) {
925 mAppWidgetHost.startListening();
926 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700927 }
928
929 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800930 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200931 long startTime = 0;
932 if (DEBUG_RESUME_TIME) {
933 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400934 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200935 }
Adam Cohen9211d422014-10-07 18:14:53 -0700936
937 if (mLauncherCallbacks != null) {
938 mLauncherCallbacks.preOnResume();
939 }
940
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700942 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700943
Winson Chung4a2afa32012-07-19 14:53:05 -0700944 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700945 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700946 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800947 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700948 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700949 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700950 // view after launching an app, as they may be depending on the UI to be static to
951 // switch to another app, otherwise, if it was
Hyunyoung Songdd60ce42016-07-27 17:08:38 -0700952 showAppsView(false /* animated */, !launchedFromApp /* updatePredictedApps */,
953 false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800954 } else if (mOnResumeState == State.WIDGETS) {
955 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700956 }
957 mOnResumeState = State.NONE;
958
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800959 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700960 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700961 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700962 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700963 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700965 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200966 // We might have postponed some bind calls until onResume (see waitUntilResume) --
967 // execute them here
968 long startTimeCallbacks = 0;
969 if (DEBUG_RESUME_TIME) {
970 startTimeCallbacks = System.currentTimeMillis();
971 }
972
Michael Jurka1e2f4652013-07-08 18:03:46 -0700973 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
974 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200975 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700976 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200977 if (DEBUG_RESUME_TIME) {
978 Log.d(TAG, "Time spent processing callbacks in onResume: " +
979 (System.currentTimeMillis() - startTimeCallbacks));
980 }
Michael Jurka447bf842013-05-15 14:52:15 +0200981 }
Michael Jurka54554252013-08-01 12:52:23 +0200982 if (mOnResumeCallbacks.size() > 0) {
983 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
984 mOnResumeCallbacks.get(i).run();
985 }
986 mOnResumeCallbacks.clear();
987 }
Winson Chunge4e50662012-01-23 14:45:13 -0800988
989 // Reset the pressed state of icons that were locked in the press state while activities
990 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800991 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800992 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800993 mWaitingForResume.setStayPressed(false);
994 }
Winson Chung82963d52013-10-09 11:20:57 -0700995
Adam Cohen06dff352012-06-01 17:17:08 -0700996 // It is possible that widgets can receive updates while launcher is not in the foreground.
997 // Consequently, the widgets will be inflated in the orientation of the foreground activity
998 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
999 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001000 if (!isWorkspaceLoading()) {
1001 getWorkspace().reinflateWidgetsIfNecessary();
1002 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001003
Michael Jurka447bf842013-05-15 14:52:15 +02001004 if (DEBUG_RESUME_TIME) {
1005 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1006 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001007
Adam Cohen4b66bf32015-09-18 12:15:19 -07001008 // We want to suppress callbacks about CustomContent being shown if we have just received
1009 // onNewIntent while the user was present within launcher. In that case, we post a call
1010 // to move the user to the main screen (which will occur after onResume). We don't want to
1011 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1012 // suppress here.
1013 if (mWorkspace.getCustomContentCallbacks() != null
1014 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001015 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001016 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001017 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1018 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001019 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001020 }
1021 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001022 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001023 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001024 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001025
Sunny Goyal756adbc2015-04-16 15:20:51 -07001026 if (!isWorkspaceLoading()) {
1027 // Process any items that were added while Launcher was away.
1028 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001029
1030 // Refresh shortcuts if the permission changed.
1031 mModel.refreshShortcutsIfRequired();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001032 }
Adam Cohen9211d422014-10-07 18:14:53 -07001033
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001034 if (shouldShowDiscoveryBounce()) {
1035 mAllAppsController.showDiscoveryBounce();
1036 }
1037 mIsResumeFromActionScreenOff = false;
Adam Cohen9211d422014-10-07 18:14:53 -07001038 if (mLauncherCallbacks != null) {
1039 mLauncherCallbacks.onResume();
1040 }
Adam Cohen06dff352012-06-01 17:17:08 -07001041 }
1042
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001044 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001045 // Ensure that items added to Launcher are queued until Launcher returns
1046 InstallShortcutReceiver.enableInstallQueue();
1047
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001048 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001049 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001050 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001051 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001052
1053 // We call onHide() aggressively. The custom content callbacks should be able to
1054 // debounce excess onHide calls.
1055 if (mWorkspace.getCustomContentCallbacks() != null) {
1056 mWorkspace.getCustomContentCallbacks().onHide();
1057 }
Romain Guycbb89e42009-06-08 15:52:54 -07001058
Adam Cohen9211d422014-10-07 18:14:53 -07001059 if (mLauncherCallbacks != null) {
1060 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001061 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001062 }
1063
1064 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001065 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1066 // by a onResume or by scrolling otherwise.
1067 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001068
1069 // Custom content is completely hidden
1070 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001071
1072 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1073 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001074
1075 // Indicates whether the user is allowed to scroll away from the custom content.
1076 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001077 }
1078
Adam Cohenc2d6e892014-10-16 09:49:24 -07001079 public interface LauncherOverlay {
1080
1081 /**
1082 * Touch interaction leading to overscroll has begun
1083 */
1084 public void onScrollInteractionBegin();
1085
1086 /**
1087 * Touch interaction related to overscroll has ended
1088 */
1089 public void onScrollInteractionEnd();
1090
1091 /**
1092 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1093 * screen (or in the case of RTL, the rightmost screen).
1094 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001095 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001096
1097 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001098 * Called when the launcher is ready to use the overlay
1099 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001100 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001101 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001102 }
1103
Jun Mukai8af0cd82015-05-12 12:32:12 -07001104 public interface LauncherSearchCallbacks {
1105 /**
1106 * Called when the search overlay is shown.
1107 */
1108 public void onSearchOverlayOpened();
1109
1110 /**
1111 * Called when the search overlay is dismissed.
1112 */
1113 public void onSearchOverlayClosed();
1114 }
1115
Adam Cohenc2d6e892014-10-16 09:49:24 -07001116 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001117 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001118 }
1119
1120 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1121
Sunny Goyalc86df472016-02-25 09:19:38 -08001122 public void onScrollChanged(float progress) {
1123 if (mWorkspace != null) {
1124 mWorkspace.onOverlayScrollChanged(progress);
1125 }
1126 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001127 }
1128
Jorim Jaggid017f882014-01-14 17:08:48 -08001129 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001130 if (mLauncherCallbacks != null) {
1131 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001132 } else {
1133 // On devices with a locked orientation, we will at least have the allow rotation
1134 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001135 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001136 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001137 }
1138
Adam Cohen9211d422014-10-07 18:14:53 -07001139 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001140 CustomContentCallbacks callbacks, String description) {
1141 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001142 }
1143
Adam Cohenedb40762013-07-18 16:45:45 -07001144 // The custom content needs to offset its content to account for the QSB
1145 public int getTopOffsetForCustomContent() {
1146 return mWorkspace.getPaddingTop();
1147 }
1148
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001149 @Override
1150 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001151 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001152 if (mModel.isCurrentCallbacks(this)) {
1153 mModel.stopLoader();
1154 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001155 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1156
Romain Guy13c2e7b2010-03-10 19:45:00 -08001157 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001158 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001159
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001160 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001161 @Override
1162 public void onWindowFocusChanged(boolean hasFocus) {
1163 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001164 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001165
1166 if (mLauncherCallbacks != null) {
1167 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1168 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001169 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001170
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 private boolean acceptFilter() {
1172 final InputMethodManager inputManager = (InputMethodManager)
1173 getSystemService(Context.INPUT_METHOD_SERVICE);
1174 return !inputManager.isFullscreenMode();
1175 }
1176
1177 @Override
1178 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001179 final int uniChar = event.getUnicodeChar();
1180 final boolean handled = super.onKeyDown(keyCode, event);
1181 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1182 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1184 keyCode, event);
1185 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001186 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001187 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001188 // showSearchDialog()
1189 // If there are multiple keystrokes before the search dialog takes focus,
1190 // onSearchRequested() will be called for every keystroke,
1191 // but it is idempotent, so it's fine.
1192 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001193 }
1194 }
1195
Joe Onorato8a9625e2010-01-28 15:55:35 -08001196 // Eat the long press event so the keyboard doesn't come up.
1197 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1198 return true;
1199 }
1200
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 return handled;
1202 }
1203
Winson46163472015-09-22 17:31:56 -07001204 @Override
1205 public boolean onKeyUp(int keyCode, KeyEvent event) {
1206 if (keyCode == KeyEvent.KEYCODE_MENU) {
1207 // Ignore the menu key if we are currently dragging or are on the custom content screen
1208 if (!isOnCustomContent() && !mDragController.isDragging()) {
1209 // Close any open folders
1210 closeFolder();
1211
Tony Wickham49c8d292016-07-01 11:44:34 -07001212 // Close any shortcuts containers
1213 closeShortcutsContainer();
1214
Winson46163472015-09-22 17:31:56 -07001215 // Stop resizing any widgets
1216 mWorkspace.exitWidgetResizeMode();
1217
1218 // Show the overview mode if we are on the workspace
1219 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1220 !mWorkspace.isSwitchingState()) {
1221 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001222 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001223 }
1224 }
1225 return true;
1226 }
1227 return super.onKeyUp(keyCode, event);
1228 }
1229
Karl Rosaen138a0412009-04-23 19:00:21 -07001230 private String getTypedText() {
1231 return mDefaultKeySsb.toString();
1232 }
1233
1234 private void clearTypedText() {
1235 mDefaultKeySsb.clear();
1236 mDefaultKeySsb.clearSpans();
1237 Selection.setSelection(mDefaultKeySsb, 0);
1238 }
1239
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001241 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1242 * State
1243 */
1244 private static State intToState(int stateOrdinal) {
1245 State state = State.WORKSPACE;
1246 final State[] stateValues = State.values();
1247 for (int i = 0; i < stateValues.length; i++) {
1248 if (stateValues[i].ordinal() == stateOrdinal) {
1249 state = stateValues[i];
1250 break;
1251 }
1252 }
1253 return state;
1254 }
1255
1256 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257 * Restores the previous state, if it exists.
1258 *
1259 * @param savedState The previous state.
1260 */
1261 private void restoreState(Bundle savedState) {
1262 if (savedState == null) {
1263 return;
1264 }
1265
Michael Jurka883f55b2010-10-21 15:47:14 -07001266 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001267 if (state == State.APPS || state == State.WIDGETS) {
1268 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001269 }
1270
Adam Cohen21cd0022013-10-09 18:57:02 -07001271 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1272 PagedView.INVALID_RESTORE_PAGE);
1273 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001274 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 }
1276
Sunny Goyal2100c782016-08-22 16:00:03 -07001277 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1278 if (requestArgs != null) {
1279 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001281
1282 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 }
1284
1285 /**
1286 * Finds all the views we need and configure them properly.
1287 */
1288 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001289 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001290 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001291 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001292 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyal6178f132016-07-11 17:30:03 -07001293 mQsbContainer = mDragLayer.findViewById(mDeviceProfile.isVerticalBarLayout()
1294 ? R.id.workspace_blocked_row : R.id.qsb_container);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001295 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001296
Sunny Goyal784f9c32016-03-23 16:56:54 -07001297 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1298 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1299 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300
Winson Chung4c98d922011-05-31 16:50:48 -07001301 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001302 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001303
Winson Chung3d503fb2011-07-13 17:25:49 -07001304 // Setup the hotseat
1305 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1306 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001307 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001308 }
1309
Winsone9f27272015-10-13 10:47:51 -07001310 // Setup the overview panel
1311 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001312
Winson Chung4c98d922011-05-31 16:50:48 -07001313 // Setup the workspace
1314 mWorkspace.setHapticFeedbackEnabled(false);
1315 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001316 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001317 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1318 // default state, otherwise we will update to the wrong offsets in RTL
1319 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001320 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001321 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001322
Tony Wickham34d2c912015-09-11 09:27:58 -07001323 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001324 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001325
Winson Chungef7f8742015-06-04 17:18:17 -07001326 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001327 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001328 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001329 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1330 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1331 } else {
1332 mAppsView.setSearchBarController(new DefaultAppSearchController());
1333 }
Craig Mautner360310b2012-10-26 15:13:08 -07001334
Winson Chung3d503fb2011-07-13 17:25:49 -07001335 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001336 mDragController.setDragScoller(mWorkspace);
1337 mDragController.setScrollView(mDragLayer);
1338 mDragController.setMoveTarget(mWorkspace);
1339 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001340 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001341
Peter Schiller310a9882016-06-30 13:52:36 -07001342 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1343 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1344 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001345
Sunny Goyal322d5562015-06-25 19:35:49 -07001346 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1347 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001348 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 }
1350
Winsone9f27272015-10-13 10:47:51 -07001351 private void setupOverviewPanel() {
1352 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1353
1354 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1355 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1356 @Override
1357 public boolean onLongClick(View v) {
1358 return v.performClick();
1359 }
1360 };
1361
1362 // Bind wallpaper button actions
1363 View wallpaperButton = findViewById(R.id.wallpaper_button);
1364 wallpaperButton.setOnClickListener(new OnClickListener() {
1365 @Override
1366 public void onClick(View view) {
1367 if (!mWorkspace.isSwitchingState()) {
1368 onClickWallpaperPicker(view);
1369 }
1370 }
1371 });
1372 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1373 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1374
1375 // Bind widget button actions
1376 mWidgetsButton = findViewById(R.id.widget_button);
1377 mWidgetsButton.setOnClickListener(new OnClickListener() {
1378 @Override
1379 public void onClick(View view) {
1380 if (!mWorkspace.isSwitchingState()) {
1381 onClickAddWidgetButton(view);
1382 }
1383 }
1384 });
1385 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1386 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1387
1388 // Bind settings actions
1389 View settingsButton = findViewById(R.id.settings_button);
1390 boolean hasSettings = hasSettings();
1391 if (hasSettings) {
1392 settingsButton.setOnClickListener(new OnClickListener() {
1393 @Override
1394 public void onClick(View view) {
1395 if (!mWorkspace.isSwitchingState()) {
1396 onClickSettingsButton(view);
1397 }
1398 }
1399 });
1400 settingsButton.setOnLongClickListener(performClickOnLongClick);
1401 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1402 } else {
1403 settingsButton.setVisibility(View.GONE);
1404 }
1405
1406 mOverviewPanel.setAlpha(0f);
1407 }
1408
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001410 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001411 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001412 */
1413 public void setAllAppsButton(View allAppsButton) {
1414 mAllAppsButton = allAppsButton;
1415 }
1416
Sunny Goyalbb011da2016-06-15 15:42:29 -07001417 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001418 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001419 }
1420
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001421 public View getWidgetsButton() {
1422 return mWidgetsButton;
1423 }
1424
Anjali Koppal5ad44842014-03-10 20:34:39 -07001425 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 * Creates a view representing a shortcut.
1427 *
1428 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001430 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001431 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001432 }
1433
1434 /**
1435 * Creates a view representing a shortcut inflated from the specified resource.
1436 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001437 * @param parent The group the shortcut belongs to.
1438 * @param info The data structure describing the shortcut.
1439 *
1440 * @return A View inflated from layoutResId.
1441 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001442 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001443 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001444 parent, false);
1445 favorite.applyFromShortcutInfo(info, mIconCache);
1446 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001448 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 return favorite;
1450 }
1451
1452 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 * Add a shortcut to the workspace.
1454 *
1455 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001457 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001458 int cellY) {
1459 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001460 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001461
Sunny Goyal5c97f512015-05-19 16:03:28 -07001462 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001463 if (info == null) {
1464 return;
1465 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001466 final View view = createShortcut(info);
1467
Sunny Goyal5c97f512015-05-19 16:03:28 -07001468 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001469 // First we check if we already know the exact location where we want to add this item.
1470 if (cellX >= 0 && cellY >= 0) {
1471 cellXY[0] = cellX;
1472 cellXY[1] = cellY;
1473 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001474
1475 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001476 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001477 true, null,null)) {
1478 return;
1479 }
1480 DragObject dragObject = new DragObject();
1481 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001482 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1483 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001484 return;
1485 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001486 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001487 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001488 }
1489
1490 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001491 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001492 return;
1493 }
1494
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001495 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496
Sunny Goyal2100c782016-08-22 16:00:03 -07001497 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
1498 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 }
1500
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001502 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001504 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001506 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001507 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1508
Adam Cohened66b2b2012-01-23 17:28:51 -08001509 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001510 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001511 }
Romain Guycbb89e42009-06-08 15:52:54 -07001512
Adam Cohen59400422014-03-05 18:07:04 -08001513 if (appWidgetInfo.isCustomWidget) {
1514 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001515 }
1516
Adam Cohen59400422014-03-05 18:07:04 -08001517 LauncherAppWidgetInfo launcherInfo;
1518 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001519 launcherInfo.spanX = itemInfo.spanX;
1520 launcherInfo.spanY = itemInfo.spanY;
1521 launcherInfo.minSpanX = itemInfo.minSpanX;
1522 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001523 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001524
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001526 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527
Sunny Goyal2100c782016-08-22 16:00:03 -07001528 if (hostView == null) {
1529 // Perform actual inflation because we're live
1530 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001532 hostView.setVisibility(View.VISIBLE);
1533 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 }
Romain Guycbb89e42009-06-08 15:52:54 -07001535
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001536 private void addAppWidgetToWorkspace(
1537 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001538 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001539 hostView.setTag(item);
1540 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001541
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001542 hostView.setFocusable(true);
1543 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001544
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001545 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001546 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1547
1548 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001549 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001550 }
1551 }
1552
Adam Cohended9f8d2010-11-03 13:25:16 -07001553 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1554 @Override
1555 public void onReceive(Context context, Intent intent) {
1556 final String action = intent.getAction();
1557 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1558 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001559 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001560 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001561
Winson Chungc100e8e2011-08-09 16:02:43 -07001562 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001563 // processing a multi-step drop
Sunny Goyal2100c782016-08-22 16:00:03 -07001564 if (mAppsView != null && mWidgetsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001565 if (!showWorkspace(false)) {
1566 // If we are already on the workspace, then manually reset all apps
1567 mAppsView.reset();
1568 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001569 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001570 mIsResumeFromActionScreenOff = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001571 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1572 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001573 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001574 }
1575 }
1576 };
1577
1578 @Override
1579 public void onAttachedToWindow() {
1580 super.onAttachedToWindow();
1581
1582 // Listen for broadcasts related to user-presence
1583 final IntentFilter filter = new IntentFilter();
1584 filter.addAction(Intent.ACTION_SCREEN_OFF);
1585 filter.addAction(Intent.ACTION_USER_PRESENT);
1586 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001587 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001588 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001589 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001590
1591 if (mLauncherCallbacks != null) {
1592 mLauncherCallbacks.onAttachedToWindow();
1593 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001594 }
1595
1596 @Override
1597 public void onDetachedFromWindow() {
1598 super.onDetachedFromWindow();
1599 mVisible = false;
1600
Adam Cohend113e0c2010-11-11 10:48:05 -08001601 if (mAttached) {
1602 unregisterReceiver(mReceiver);
1603 mAttached = false;
1604 }
Winson Chungb745afb2015-03-02 11:51:23 -08001605 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001606
1607 if (mLauncherCallbacks != null) {
1608 mLauncherCallbacks.onDetachedFromWindow();
1609 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001610 }
1611
1612 public void onWindowVisibilityChanged(int visibility) {
1613 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001614 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001615 // The following code used to be in onResume, but it turns out onResume is called when
1616 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1617 // is a more appropriate event to handle
1618 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001619 if (!mWorkspaceLoading) {
1620 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001621 // We want to let Launcher draw itself at least once before we force it to build
1622 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001623 // apps is nice and speedy.
1624 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001625 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001626 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001627 if (mStarted) return;
1628 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001629 // We delay the layer building a bit in order to give
1630 // other message processing a time to run. In particular
1631 // this avoids a delay in hiding the IME if it was
1632 // currently shown, because doing that may involve
1633 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001634 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001635 final ViewTreeObserver.OnDrawListener listener = this;
1636 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001637 public void run() {
1638 if (mWorkspace != null &&
1639 mWorkspace.getViewTreeObserver() != null) {
1640 mWorkspace.getViewTreeObserver().
1641 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001642 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001643 }
1644 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001645 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001646 }
1647 });
1648 }
1649 clearTypedText();
1650 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001651 }
1652
Adam Cohen091440a2015-03-18 14:16:05 -07001653 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001654 mHandler.removeMessages(ADVANCE_MSG);
1655 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1656 mHandler.sendMessageDelayed(msg, delay);
1657 mAutoAdvanceSentTime = System.currentTimeMillis();
1658 }
1659
Adam Cohen091440a2015-03-18 14:16:05 -07001660 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001661 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1662 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1663 mAutoAdvanceRunning = autoAdvanceRunning;
1664 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001665 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001666 sendAdvanceMessage(delay);
1667 } else {
1668 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001669 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001670 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1671 }
1672 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001673 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001674 }
1675 }
1676 }
1677
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001678 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1679
Adam Cohended9f8d2010-11-03 13:25:16 -07001680 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001681 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001682 if (msg.what == ADVANCE_MSG) {
1683 int i = 0;
1684 for (View key: mWidgetsToAdvance.keySet()) {
1685 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001686 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001687 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001688 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001689 public void run() {
1690 ((Advanceable) v).advance();
1691 }
1692 }, delay);
1693 }
1694 i++;
1695 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001696 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001697 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001698 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001699 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001700 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001701
Winsonc0b52fe2015-09-09 16:38:15 -07001702 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001703 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001704 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1705 if (v instanceof Advanceable) {
1706 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001707 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001708 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001709 }
1710 }
1711
Winsonc0b52fe2015-09-09 16:38:15 -07001712 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001713 if (mWidgetsToAdvance.containsKey(hostView)) {
1714 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001715 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001716 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001717 }
1718
Hyunyoung Song3f471442015-04-08 19:01:34 -07001719 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001720 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1721 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001722 }
1723
Winson Chunga6945242014-01-08 14:04:34 -08001724 public DragLayer getDragLayer() {
1725 return mDragLayer;
1726 }
1727
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001728 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001729 return mAppsView;
1730 }
1731
Hyunyoung Song3f471442015-04-08 19:01:34 -07001732 public WidgetsContainerView getWidgetsView() {
1733 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001734 }
1735
Winson Chunga6945242014-01-08 14:04:34 -08001736 public Workspace getWorkspace() {
1737 return mWorkspace;
1738 }
1739
Sunny Goyal6178f132016-07-11 17:30:03 -07001740 public View getQsbContainer() {
1741 return mQsbContainer;
1742 }
1743
Winson Chunga6945242014-01-08 14:04:34 -08001744 public Hotseat getHotseat() {
1745 return mHotseat;
1746 }
1747
Jorim Jaggid017f882014-01-14 17:08:48 -08001748 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001749 return mOverviewPanel;
1750 }
1751
Sunny Goyal47328fd2016-05-25 18:56:41 -07001752 public DropTargetBar getDropTargetBar() {
1753 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001754 }
1755
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001756 public LauncherAppWidgetHost getAppWidgetHost() {
1757 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758 }
Romain Guycbb89e42009-06-08 15:52:54 -07001759
Winson Chunga9abd0e2010-10-27 17:18:37 -07001760 public LauncherModel getModel() {
1761 return mModel;
1762 }
1763
Adam Cohen79d90c52016-04-22 13:29:20 -07001764 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001765 return mSharedPrefs;
1766 }
1767
Adam Cohen2e6da152015-05-06 11:42:25 -07001768 public DeviceProfile getDeviceProfile() {
1769 return mDeviceProfile;
1770 }
1771
Bjorn Bringertc459e522013-06-07 19:36:01 +01001772 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001773 getWindow().closeAllPanels();
1774
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001775 // Whatever we were doing is hereby canceled.
Sunny Goyal2100c782016-08-22 16:00:03 -07001776 setWaitingForResult(null);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001777 }
1778
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779 @Override
1780 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001781 long startTime = 0;
1782 if (DEBUG_RESUME_TIME) {
1783 startTime = System.currentTimeMillis();
1784 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001785 super.onNewIntent(intent);
1786
1787 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001788 Folder openFolder = mWorkspace.getOpenFolder();
1789 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1790 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1791 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1792 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1793 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001794 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001795 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001796
Adam Cohen6fecd412013-10-02 17:41:50 -07001797 if (mWorkspace == null) {
1798 // Can be cases where mWorkspace is null, this prevents a NPE
1799 return;
1800 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001801 // In all these cases, only animate if we're already on home
1802 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001803
Sunny Goyal935fca12015-10-13 10:19:01 -07001804 closeFolder(alreadyOnHome);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001805 closeShortcutsContainer(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001806 exitSpringLoadedDragMode();
1807
1808 // If we are already on home, then just animate back to the workspace,
1809 // otherwise, just wait until onResume to set the state back to Workspace
1810 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001811 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001812 } else {
1813 mOnResumeState = State.WORKSPACE;
1814 }
1815
1816 final View v = getWindow().peekDecorView();
1817 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001818 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001819 INPUT_METHOD_SERVICE);
1820 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1821 }
1822
Winson Chungb745afb2015-03-02 11:51:23 -08001823 // Reset the apps view
1824 if (!alreadyOnHome && mAppsView != null) {
1825 mAppsView.scrollToTop();
1826 }
1827
Hyunyoung Song3f471442015-04-08 19:01:34 -07001828 // Reset the widgets view
1829 if (!alreadyOnHome && mWidgetsView != null) {
1830 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001831 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001832
Adam Cohen9211d422014-10-07 18:14:53 -07001833 if (mLauncherCallbacks != null) {
1834 mLauncherCallbacks.onHomeIntent();
1835 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 }
Adam Cohened307df2013-10-02 09:37:31 -07001837
Adam Cohen9211d422014-10-07 18:14:53 -07001838 if (mLauncherCallbacks != null) {
1839 mLauncherCallbacks.onNewIntent(intent);
1840 }
Winson15f8b172015-08-19 11:02:39 -07001841
1842 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1843 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1844 // animation.
1845 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001846 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1847 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001848 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1849 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001850
1851 // We use this flag to suppress noisy callbacks above custom content state
1852 // from onResume.
1853 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001854 mWorkspace.post(new Runnable() {
1855 @Override
1856 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001857 if (mWorkspace != null) {
1858 mWorkspace.moveToDefaultScreen(true);
1859 }
Winson15f8b172015-08-19 11:02:39 -07001860 }
1861 });
1862 }
1863 }
1864
1865 if (DEBUG_RESUME_TIME) {
1866 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1867 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001868 }
1869
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001870 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001871 public void onRestoreInstanceState(Bundle state) {
1872 super.onRestoreInstanceState(state);
1873 for (int page: mSynchronouslyBoundPages) {
1874 mWorkspace.restoreInstanceStateForChild(page);
1875 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876 }
1877
1878 @Override
1879 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001880 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001881 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1882 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001883
Adam Cohen21cd0022013-10-09 18:57:02 -07001884 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001885 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886
Michael Jurka883f55b2010-10-21 15:47:14 -07001887 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001888 // We close any open folder since it will not be re-opened, and we need to make sure
1889 // this state is reflected.
1890 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1891 closeFolder(false);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001892 closeShortcutsContainer(false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001893
Sunny Goyal2100c782016-08-22 16:00:03 -07001894 if (mPendingRequestArgs != null) {
1895 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1896 }
1897 if (mPendingActivityResult != null) {
1898 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 }
1900
Adam Cohen9211d422014-10-07 18:14:53 -07001901 if (mLauncherCallbacks != null) {
1902 mLauncherCallbacks.onSaveInstanceState(outState);
1903 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 }
1905
1906 @Override
1907 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001909
Winson Chunge7a03942011-08-05 15:05:12 -07001910 // Remove all pending runnables
1911 mHandler.removeMessages(ADVANCE_MSG);
1912 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001913 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001914 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001915
Winson Chungcd2b0142011-06-08 16:02:26 -07001916 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001917 // It's possible to receive onDestroy after a new Launcher activity has
1918 // been created. In this case, don't interfere with the new Launcher.
1919 if (mModel.isCurrentCallbacks(this)) {
1920 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001921 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001922 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001923
Sunny Goyal745bad92016-05-02 10:54:12 -07001924 if (mRotationPrefChangeHandler != null) {
1925 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1926 }
1927
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001929 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001931 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001933 mAppWidgetHost = null;
1934
1935 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936
1937 TextKeyListener.getInstance().release();
1938
Tony Wickhame2217252016-03-22 16:34:23 -07001939 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1940 .removeAccessibilityStateChangeListener(this);
1941
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001942 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001943
Michael Jurka2ecf9952012-06-18 12:52:28 -07001944 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001945
1946 if (mLauncherCallbacks != null) {
1947 mLauncherCallbacks.onDestroy();
1948 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949 }
1950
Sunny Goyalae502842016-06-17 08:43:56 -07001951 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1952 return mAccessibilityDelegate;
1953 }
1954
Adam Cohena9cf38f2011-05-02 15:36:58 -07001955 public DragController getDragController() {
1956 return mDragController;
1957 }
1958
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001960 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001961 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001962 }
1963
1964 @Override
1965 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1966 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001967 try {
1968 super.startIntentSenderForResult(intent, requestCode,
1969 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1970 } catch (IntentSender.SendIntentException e) {
1971 throw new ActivityNotFoundException();
1972 }
1973 }
1974
Winson Chung70d72102011-08-12 11:24:35 -07001975 /**
1976 * Indicates that we want global search for this activity by setting the globalSearch
1977 * argument for {@link #startSearch} to true.
1978 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001980 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001982
Karl Rosaen138a0412009-04-23 19:00:21 -07001983 if (initialQuery == null) {
1984 // Use any text typed in the launcher as the initial query
1985 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001986 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 if (appSearchData == null) {
1988 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001989 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001991
1992 // TODO send proper bounds.
1993 Rect sourceBounds = null;
Romain Guycbb89e42009-06-08 15:52:54 -07001994
Ian Parkinson047036e2014-09-01 15:40:53 +01001995 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001996 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01001997 if (clearTextImmediately) {
1998 clearTypedText();
1999 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002000
2001 // We need to show the workspace after starting the search
2002 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002003 }
2004
Ian Parkinson047036e2014-09-01 15:40:53 +01002005 /**
2006 * Start a text search.
2007 *
2008 * @return {@code true} if the search will start immediately, so any further keypresses
2009 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2010 * to buffer keypresses.
2011 */
2012 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002013 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002014 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2015 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2016 sourceBounds);
2017 }
2018
Michael Jurkaa33411c2012-06-14 16:18:21 -07002019 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002020 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002021 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002022 }
2023
2024 /**
2025 * Starts the global search activity. This code is a copied from SearchManager
2026 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002027 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002028 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002029 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002030 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2031 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2032 if (globalSearchActivity == null) {
2033 Log.w(TAG, "No global search activity found.");
2034 return;
2035 }
2036 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2037 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2038 intent.setComponent(globalSearchActivity);
2039 // Make sure that we have a Bundle to put source in
2040 if (appSearchData == null) {
2041 appSearchData = new Bundle();
2042 } else {
2043 appSearchData = new Bundle(appSearchData);
2044 }
Adam Cohen9211d422014-10-07 18:14:53 -07002045 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002046 if (!appSearchData.containsKey("source")) {
2047 appSearchData.putString("source", getPackageName());
2048 }
2049 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2050 if (!TextUtils.isEmpty(initialQuery)) {
2051 intent.putExtra(SearchManager.QUERY, initialQuery);
2052 }
2053 if (selectInitialQuery) {
2054 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2055 }
2056 intent.setSourceBounds(sourceBounds);
2057 try {
2058 startActivity(intent);
2059 } catch (ActivityNotFoundException ex) {
2060 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2061 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 }
2063
Yura4f93ec62014-02-04 14:15:21 +00002064 public boolean isOnCustomContent() {
2065 return mWorkspace.isOnOrMovingToCustomContent();
2066 }
2067
Winson Chung70d72102011-08-12 11:24:35 -07002068 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002069 public boolean onPrepareOptionsMenu(Menu menu) {
2070 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002071 if (mLauncherCallbacks != null) {
2072 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2073 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002074 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002075 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002076
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002077 @Override
2078 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002079 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002080 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002081 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002082 }
2083
Joe Onorato9c1289c2009-08-17 11:03:03 -04002084 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07002085 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002086 }
2087
Adam Cohen517a7f52014-03-01 12:12:59 -08002088 public boolean isWorkspaceLoading() {
2089 return mWorkspaceLoading;
2090 }
2091
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002092 private void setWorkspaceLoading(boolean value) {
2093 boolean isLocked = isWorkspaceLocked();
2094 mWorkspaceLoading = value;
2095 if (isLocked != isWorkspaceLocked()) {
2096 onWorkspaceLockedChanged();
2097 }
2098 }
2099
Sunny Goyal2100c782016-08-22 16:00:03 -07002100 private void setWaitingForResult(PendingRequestArgs args) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002101 boolean isLocked = isWorkspaceLocked();
Sunny Goyal2100c782016-08-22 16:00:03 -07002102 mPendingRequestArgs = args;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002103 if (isLocked != isWorkspaceLocked()) {
2104 onWorkspaceLockedChanged();
2105 }
2106 }
2107
Adam Cohen9211d422014-10-07 18:14:53 -07002108 protected void onWorkspaceLockedChanged() {
2109 if (mLauncherCallbacks != null) {
2110 mLauncherCallbacks.onWorkspaceLockedChanged();
2111 }
2112 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002113
Sunny Goyal2100c782016-08-22 16:00:03 -07002114 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
2115 LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002116 if (LOGD) {
2117 Log.d(TAG, "Adding widget from drop");
2118 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002119 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2120 }
2121
Sunny Goyal2100c782016-08-22 16:00:03 -07002122 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
2123 AppWidgetHostView boundWidget, LauncherAppWidgetProviderInfo appWidgetInfo,
Adam Cohen59400422014-03-05 18:07:04 -08002124 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002125 if (appWidgetInfo.configure != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002126 setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, appWidgetInfo, info));
Michael Jurkaaf442092010-06-10 17:01:57 -07002127
Bjorn Bringert7984c942009-12-09 15:38:25 +00002128 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002129 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2130 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002131 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002132 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002133 Runnable onComplete = new Runnable() {
2134 @Override
2135 public void run() {
2136 // Exit spring loaded mode if necessary after adding the widget
2137 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2138 null);
2139 }
2140 };
Sunny Goyal2100c782016-08-22 16:00:03 -07002141 completeAddAppWidget(appWidgetId, info, boundWidget, appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002142 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002143 }
2144 }
Romain Guycbb89e42009-06-08 15:52:54 -07002145
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002146 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002147 // Close any folders that may be open.
2148 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002149 mWorkspace.moveToCustomContentScreen(animate);
2150 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002151
2152 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2153 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002154 info.container = container;
2155 info.screenId = screenId;
2156 if (cell != null) {
2157 info.cellX = cell[0];
2158 info.cellY = cell[1];
2159 }
2160 info.spanX = spanX;
2161 info.spanY = spanY;
2162
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002163 switch (info.itemType) {
2164 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2165 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07002166 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002167 break;
2168 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal2100c782016-08-22 16:00:03 -07002169 processShortcutFromDrop(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002170 break;
2171 default:
2172 throw new IllegalStateException("Unknown item type: " + info.itemType);
2173 }
2174 }
2175
Adam Cohenfbba09b2011-07-18 21:43:05 -07002176 /**
2177 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002178 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002179 private void processShortcutFromDrop(PendingAddItemInfo info) {
2180 setWaitingForResult(new PendingRequestArgs(info));
Michael Jurka0280c3b2010-09-17 15:00:07 -07002181 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
Sunny Goyal2100c782016-08-22 16:00:03 -07002182 createShortcutIntent.setComponent(info.componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002183 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002184 }
2185
Adam Cohenfbba09b2011-07-18 21:43:05 -07002186 /**
2187 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002188 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002189 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002190 AppWidgetHostView hostView = info.boundWidget;
2191 int appWidgetId;
2192 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002193 // In the case where we've prebound the widget, we remove it from the DragLayer
2194 if (LOGD) {
2195 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2196 }
2197 getDragLayer().removeView(hostView);
2198
Adam Cohened66b2b2012-01-23 17:28:51 -08002199 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002200 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002201
2202 // Clear the boundWidget so that it doesn't get destroyed.
2203 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002204 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002205 // In this case, we either need to start an activity to get permission to bind
2206 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002207 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002208 Bundle options = info.bindOptions;
2209
Sunny Goyalffe83f12014-08-14 17:39:34 -07002210 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2211 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002212 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002213 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002214 } else {
Sunny Goyal2100c782016-08-22 16:00:03 -07002215 setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, info.info, info));
Michael Jurka8b805b12012-04-18 14:23:14 -07002216 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2217 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2218 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyal2100c782016-08-22 16:00:03 -07002219 mAppWidgetManager.getUser(info.info)
Sunny Goyalffe83f12014-08-14 17:39:34 -07002220 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002221 // TODO: we need to make sure that this accounts for the options bundle.
2222 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002223 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2224 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002225 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002226 }
2227
Adam Cohendcd297f2013-06-18 13:13:40 -07002228 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002229 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002230 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002231 folderInfo.title = getText(R.string.folder_name);
2232
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002233 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002234 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2235 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002236
2237 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002238 FolderIcon newFolder =
2239 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002240 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002241 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002242 // Force measure the new folder icon
2243 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2244 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002245 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002246 }
Romain Guycbb89e42009-06-08 15:52:54 -07002247
Winsonc0b52fe2015-09-09 16:38:15 -07002248 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002249 * Unbinds the view for the specified item, and removes the item and all its children.
2250 *
2251 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002252 * @param itemInfo the {@link ItemInfo} for this view.
2253 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002254 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002255 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002256 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002257 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002258 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2259 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002260 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002261 } else {
2262 mWorkspace.removeWorkspaceItem(v);
2263 }
Winsonc0b52fe2015-09-09 16:38:15 -07002264 if (deleteFromDb) {
2265 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2266 }
2267 } else if (itemInfo instanceof FolderInfo) {
2268 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002269 if (v instanceof FolderIcon) {
2270 ((FolderIcon) v).removeListeners();
2271 }
Winsonc0b52fe2015-09-09 16:38:15 -07002272 mWorkspace.removeWorkspaceItem(v);
2273 if (deleteFromDb) {
2274 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2275 }
2276 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2277 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002278 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002279 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002280 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002281 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002282 }
2283 } else {
2284 return false;
2285 }
2286 return true;
2287 }
2288
2289 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002290 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002291 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002292 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002293 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002294 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002295 // Deleting an app widget ID is a void call but writes to disk before returning
2296 // to the caller...
2297 new AsyncTask<Void, Void, Void>() {
2298 public Void doInBackground(Void ... args) {
2299 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2300 return null;
2301 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002302 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002303 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002304 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002305 }
2306
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002307 @Override
2308 public boolean dispatchKeyEvent(KeyEvent event) {
2309 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2310 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002311 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002312 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002313 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002314 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002315 dumpState();
2316 return true;
2317 }
2318 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002319 }
2320 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2321 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002322 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002323 return true;
2324 }
2325 }
2326
2327 return super.dispatchKeyEvent(event);
2328 }
2329
Joe Onorato88ec0992009-11-19 13:16:06 -08002330 @Override
2331 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002332 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2333 return;
2334 }
2335
Sunny Goyal45478022015-06-08 16:52:41 -07002336 if (mDragController.isDragging()) {
2337 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002338 return;
2339 }
2340
Tony Wickham49c8d292016-07-01 11:44:34 -07002341 if (getOpenShortcutsContainer() != null) {
2342 closeShortcutsContainer();
2343 } else if (isAppsViewVisible()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002344 showWorkspace(true);
2345 } else if (isWidgetsViewVisible()) {
2346 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002347 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002348 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002349 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002350 Folder openFolder = mWorkspace.getOpenFolder();
2351 if (openFolder.isEditingName()) {
2352 openFolder.dismissEditingName();
2353 } else {
2354 closeFolder();
2355 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002356 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002357 mWorkspace.exitWidgetResizeMode();
2358
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002359 // Back button is a no-op here, but give at least some feedback for the button press
2360 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002361 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002362 }
2363
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002364 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002365 * Launches the intent referred by the clicked shortcut.
2366 *
2367 * @param v The view representing the clicked shortcut.
2368 */
2369 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002370 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2371 // view has detached (it's possible for this to happen if the view is removed mid touch).
2372 if (v.getWindowToken() == null) {
2373 return;
2374 }
2375
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002376 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002377 return;
2378 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002379
Adam Cohen1697b792013-09-17 19:08:21 -07002380 if (v instanceof Workspace) {
2381 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002382 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002383 }
2384 return;
2385 }
2386
2387 if (v instanceof CellLayout) {
2388 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002389 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2390 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002391 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002392 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002393 }
2394
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002395 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002396 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002397 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002398 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002399 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002400 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002401 }
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002402 } else if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2403 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002404 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002405 } else if (tag instanceof AppInfo) {
2406 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002407 } else if (tag instanceof LauncherAppWidgetInfo) {
2408 if (v instanceof PendingAppWidgetHostView) {
2409 onClickPendingWidget((PendingAppWidgetHostView) v);
2410 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411 }
2412 }
2413
Sunny Goyal70660032015-05-14 00:07:08 -07002414 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002415 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002416 return false;
2417 }
2418
Michael Jurkaaf442092010-06-10 17:01:57 -07002419 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002420 * Event handler for the app widget view which has not fully restored.
2421 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002422 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002423 if (mIsSafeModeEnabled) {
2424 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2425 return;
2426 }
2427
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002428 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002429 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002430 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2431 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2432 // This should not happen, as we make sure that an Id is allocated during bind.
2433 return;
2434 }
2435 LauncherAppWidgetProviderInfo appWidgetInfo =
2436 mAppWidgetManager.findProvider(info.providerName, info.user);
2437 if (appWidgetInfo != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002438 setWaitingForResult(PendingRequestArgs
2439 .forWidgetInfo(info.appWidgetId, appWidgetInfo, info));
Sunny Goyalff572272014-07-23 13:58:07 -07002440
Sunny Goyald478c832016-04-01 12:04:16 -07002441 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
Sunny Goyal2100c782016-08-22 16:00:03 -07002442 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, info.appWidgetId);
Sunny Goyald478c832016-04-01 12:04:16 -07002443 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07002444 mAppWidgetManager.getUser(appWidgetInfo)
Sunny Goyald478c832016-04-01 12:04:16 -07002445 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2446 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2447 }
2448 } else {
2449 LauncherAppWidgetProviderInfo appWidgetInfo =
2450 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2451 if (appWidgetInfo != null) {
2452 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2453 }
Sunny Goyalff572272014-07-23 13:58:07 -07002454 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002455 } else if (info.installProgress < 0) {
2456 // The install has not been queued
2457 final String packageName = info.providerName.getPackageName();
2458 showBrokenAppInstallDialog(packageName,
2459 new DialogInterface.OnClickListener() {
2460 public void onClick(DialogInterface dialog, int id) {
2461 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2462 }
2463 });
2464 } else {
2465 // Download has started.
2466 final String packageName = info.providerName.getPackageName();
2467 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002468 }
2469 }
2470
Sunny Goyald478c832016-04-01 12:04:16 -07002471 private void startRestoredWidgetReconfigActivity(
2472 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002473 setWaitingForResult(PendingRequestArgs.forWidgetInfo(info.appWidgetId, provider, info));
Sunny Goyald478c832016-04-01 12:04:16 -07002474 mAppWidgetManager.startConfigActivity(provider,
2475 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2476 }
2477
Sunny Goyalff572272014-07-23 13:58:07 -07002478 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002479 * Event handler for the "grid" button that appears on the home screen, which
2480 * enters all apps mode.
2481 *
2482 * @param v The view that was clicked.
2483 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002484 protected void onClickAllAppsButton(View v) {
2485 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002486 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002487 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.TAP,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002488 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002489 showAppsView(true /* animated */, true /* updatePredictedApps */,
2490 false /* focusSearchBar */);
Winson Chung76648c52015-07-10 14:33:23 -07002491 }
2492 }
2493
2494 protected void onLongClickAllAppsButton(View v) {
2495 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2496 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002497 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.LONGPRESS,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002498 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002499 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07002500 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002501 }
2502 }
2503
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002504 private void showBrokenAppInstallDialog(final String packageName,
2505 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002506 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002507 .setTitle(R.string.abandoned_promises_title)
2508 .setMessage(R.string.abandoned_promise_explanation)
2509 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2510 .setNeutralButton(R.string.abandoned_clean_this,
2511 new DialogInterface.OnClickListener() {
2512 public void onClick(DialogInterface dialog, int id) {
2513 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2514 mWorkspace.removeAbandonedPromise(packageName, user);
2515 }
2516 })
2517 .create().show();
2518 return;
2519 }
2520
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002521 /**
2522 * Event handler for an app shortcut click.
2523 *
2524 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2525 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002526 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002527 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2528 Object tag = v.getTag();
2529 if (!(tag instanceof ShortcutInfo)) {
2530 throw new IllegalArgumentException("Input must be a Shortcut");
2531 }
2532
2533 // Open shortcut
2534 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002535
2536 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002537 if ((shortcut.isDisabled &
2538 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2539 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2540 // If the app is only disabled because of the above flags, launch activity anyway.
2541 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002542 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002543 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2544 // Use a message specific to this shortcut, if it has one.
2545 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2546 return;
2547 }
2548 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002549 int error = R.string.activity_not_available;
2550 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2551 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002552 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2553 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002554 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002555 }
2556 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2557 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002558 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002559 }
2560
Chris Wren40c5ed32014-06-24 18:24:23 -04002561 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002562 if ((v instanceof BubbleTextView)
2563 && shortcut.isPromise()
2564 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002565 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002566 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002567 new DialogInterface.OnClickListener() {
2568 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002569 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002570 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002571 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002572 return;
2573 }
2574
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002575 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002576 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002577 }
2578
Sunny Goyala7ce1662016-05-31 15:01:35 -07002579 private void startAppShortcutOrInfoActivity(View v) {
2580 ItemInfo item = (ItemInfo) v.getTag();
2581 Intent intent = item.getIntent();
2582 if (intent == null) {
2583 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002584 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002585 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002586 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002587
2588 if (success && v instanceof BubbleTextView) {
2589 mWaitingForResume = (BubbleTextView) v;
2590 mWaitingForResume.setStayPressed(true);
2591 }
2592 }
2593
2594 /**
2595 * Event handler for a folder icon click.
2596 *
2597 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2598 */
2599 protected void onClickFolderIcon(View v) {
2600 if (LOGD) Log.d(TAG, "onClickFolder");
2601 if (!(v instanceof FolderIcon)){
2602 throw new IllegalArgumentException("Input must be a FolderIcon");
2603 }
2604
2605 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002606 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002607 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002608 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002609 }
Michael Jurka5130e402011-10-13 04:55:35 -07002610 }
2611
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002612 /**
2613 * Event handler for the (Add) Widgets button that appears after a long press
2614 * on the home screen.
2615 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002616 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002617 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002618 if (mIsSafeModeEnabled) {
2619 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2620 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002621 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002622 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002623 }
2624
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002625 /**
2626 * Event handler for the wallpaper picker button that appears after a long press
2627 * on the home screen.
2628 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002629 public void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002630 if (!Utilities.isWallapaperAllowed(this)) {
2631 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2632 return;
2633 }
2634
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002635 String pickerPackage = getString(R.string.wallpaper_picker_package);
2636 if (TextUtils.isEmpty(pickerPackage)) {
2637 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2638 }
2639
Tony Wickham785f7a52015-08-31 17:28:32 -07002640 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2641 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002642
Sunny Goyal2100c782016-08-22 16:00:03 -07002643 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002644 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002645 .setPackage(pickerPackage)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002646 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
2647 intent.setSourceBounds(getViewBounds(v));
2648 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002649 }
2650
2651 /**
2652 * Event handler for a click on the settings button that appears after a long press
2653 * on the home screen.
2654 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002655 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002656 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002657 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2658 .setPackage(getPackageName());
2659 intent.setSourceBounds(getViewBounds(v));
2660 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002661 }
2662
Adam Cohen61f560d2013-09-30 15:58:20 -07002663 public View.OnTouchListener getHapticFeedbackTouchListener() {
2664 if (mHapticFeedbackTouchListener == null) {
2665 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002666 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002667 @Override
2668 public boolean onTouch(View v, MotionEvent event) {
2669 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2670 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2671 }
2672 return false;
2673 }
2674 };
2675 }
2676 return mHapticFeedbackTouchListener;
2677 }
2678
Tony Wickhame2217252016-03-22 16:34:23 -07002679 @Override
2680 public void onAccessibilityStateChanged(boolean enabled) {
2681 mDragLayer.onAccessibilityStateChanged(enabled);
2682 }
2683
Sunny Goyal06e21a22016-08-11 16:02:02 -07002684 public void onDragStarted() {
Adam Cohen9211d422014-10-07 18:14:53 -07002685 if (isOnCustomContent()) {
2686 // Custom content screen doesn't participate in drag and drop. If on custom
2687 // content screen, move to default.
2688 moveWorkspaceToDefaultScreen();
2689 }
Adam Cohen9211d422014-10-07 18:14:53 -07002690 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002691
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002692 /**
2693 * Called when the user stops interacting with the launcher.
2694 * This implies that the user is now on the homescreen and is not doing housekeeping.
2695 */
Adam Cohen9211d422014-10-07 18:14:53 -07002696 protected void onInteractionEnd() {
2697 if (mLauncherCallbacks != null) {
2698 mLauncherCallbacks.onInteractionEnd();
2699 }
2700 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002701
2702 /**
2703 * Called when the user starts interacting with the launcher.
2704 * The possible interactions are:
2705 * - open all apps
2706 * - reorder an app shortcut, or a widget
2707 * - open the overview mode.
2708 * This is a good time to stop doing things that only make sense
2709 * when the user is on the homescreen and not doing housekeeping.
2710 */
Adam Cohen9211d422014-10-07 18:14:53 -07002711 protected void onInteractionBegin() {
2712 if (mLauncherCallbacks != null) {
2713 mLauncherCallbacks.onInteractionBegin();
2714 }
2715 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002716
Winson Chungcd99cd32015-04-29 11:03:24 -07002717 /** Updates the interaction state. */
2718 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002719 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002720 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002721 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2722 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002723 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002724 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002725 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002726 onInteractionEnd();
2727 }
2728 }
2729
Sunny Goyala7ce1662016-05-31 15:01:35 -07002730 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002731 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002732 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2733 try {
2734 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2735 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2736 // is enabled by default on NYC.
2737 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2738 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002739
2740 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2741 String id = ((ShortcutInfo) info).getDeepShortcutId();
2742 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002743 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2744 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002745 } else {
2746 // Could be launching some bookkeeping activity
2747 startActivity(intent, optsBundle);
2748 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002749 } finally {
2750 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002751 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002752 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002753 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2754 // corresponding permission. Show the appropriate permission prompt if that
2755 // is the case.
2756 if (intent.getComponent() == null
2757 && Intent.ACTION_CALL.equals(intent.getAction())
2758 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2759 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002760
2761 setWaitingForResult(PendingRequestArgs
2762 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002763 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2764 REQUEST_PERMISSION_CALL_PHONE);
2765 } else {
2766 // No idea why this was thrown.
2767 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002768 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002769 }
2770 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002771
Sunny Goyala7ce1662016-05-31 15:01:35 -07002772 private Bundle getActivityLaunchOptions(View v) {
2773 if (Utilities.ATLEAST_MARSHMALLOW) {
2774 int left = 0, top = 0;
2775 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2776 if (v instanceof TextView) {
2777 // Launch from center of icon, not entire view
2778 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2779 if (icon != null) {
2780 Rect bounds = icon.getBounds();
2781 left = (width - bounds.width()) / 2;
2782 top = v.getPaddingTop();
2783 width = bounds.width();
2784 height = bounds.height();
2785 }
2786 }
2787 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2788 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2789 // On L devices, we use the device default slide-up transition.
2790 // On L MR1 devices, we use a custom version of the slide-up transition which
2791 // doesn't have the delay present in the device default.
2792 return ActivityOptions.makeCustomAnimation(
2793 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2794 }
2795 return null;
2796 }
2797
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002798 private Rect getViewBounds(View v) {
2799 int[] pos = new int[2];
2800 v.getLocationOnScreen(pos);
2801 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2802 }
2803
Sunny Goyala7ce1662016-05-31 15:01:35 -07002804 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002805 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2806 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2807 return false;
2808 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002809 // Only launch using the new animation if the shortcut has not opted out (this is a
2810 // private contract between launcher and may be ignored in the future).
2811 boolean useLaunchAnimation = (v != null) &&
2812 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2813 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2814
2815 UserHandleCompat user = null;
2816 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2817 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2818 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002819 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002820
2821 // Prepare intent
2822 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2823 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002824 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002825 }
2826 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002827 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2828 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07002829 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
2830 && ((ShortcutInfo) item).promisedIntent == null) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002831 // Shortcuts need some special checks due to legacy reasons.
2832 startShortcutIntentSafely(intent, optsBundle, item);
2833 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2834 // Could be launching some bookkeeping activity
2835 startActivity(intent, optsBundle);
2836 } else {
2837 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2838 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2839 }
2840 return true;
2841 } catch (ActivityNotFoundException|SecurityException e) {
2842 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2843 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2844 }
2845 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002846 }
2847
Adam Cohen268c4752012-06-06 17:47:33 -07002848 /**
2849 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2850 * in the DragLayer in the exact absolute location of the original FolderIcon.
2851 */
2852 private void copyFolderIconToImage(FolderIcon fi) {
2853 final int width = fi.getMeasuredWidth();
2854 final int height = fi.getMeasuredHeight();
2855
2856 // Lazy load ImageView, Bitmap and Canvas
2857 if (mFolderIconImageView == null) {
2858 mFolderIconImageView = new ImageView(this);
2859 }
2860 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2861 mFolderIconBitmap.getHeight() != height) {
2862 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2863 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2864 }
2865
2866 DragLayer.LayoutParams lp;
2867 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2868 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2869 } else {
2870 lp = new DragLayer.LayoutParams(width, height);
2871 }
2872
Adam Cohen307fe232012-08-16 17:55:58 -07002873 // The layout from which the folder is being opened may be scaled, adjust the starting
2874 // view size by this scale factor.
2875 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002876 lp.customPosition = true;
2877 lp.x = mRectForFolderAnimation.left;
2878 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002879 lp.width = (int) (scale * width);
2880 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002881
2882 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2883 fi.draw(mFolderIconCanvas);
2884 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002885 if (fi.getFolder() != null) {
2886 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2887 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002888 }
Adam Cohen268c4752012-06-06 17:47:33 -07002889 // Just in case this image view is still in the drag layer from a previous animation,
2890 // we remove it and re-add it.
2891 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2892 mDragLayer.removeView(mFolderIconImageView);
2893 }
2894 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002895 if (fi.getFolder() != null) {
2896 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002897 }
Adam Cohen268c4752012-06-06 17:47:33 -07002898 }
2899
Adam Cohen37b2a492016-04-06 18:36:06 -07002900 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002901 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002902 FolderInfo info = (FolderInfo) fi.getTag();
2903 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2904 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002905 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2906 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002907 }
2908
Adam Cohen268c4752012-06-06 17:47:33 -07002909 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2910 copyFolderIconToImage(fi);
2911 fi.setVisibility(View.INVISIBLE);
2912
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002913 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2914 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002915 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002916 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2917 }
2918 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002919 oa.start();
2920 }
2921
Sunny Goyal935fca12015-10-13 10:19:01 -07002922 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002923 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002924 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002925
Adam Cohen268c4752012-06-06 17:47:33 -07002926 // We remove and re-draw the FolderIcon in-case it has changed
2927 mDragLayer.removeView(mFolderIconImageView);
2928 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08002929
2930 if (cl != null) {
2931 cl.clearFolderLeaveBehind();
2932 }
2933
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002934 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002935 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002936 oa.addListener(new AnimatorListenerAdapter() {
2937 @Override
2938 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002939 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07002940 // Remove the ImageView copy of the FolderIcon and make the original visible.
2941 mDragLayer.removeView(mFolderIconImageView);
2942 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002943 }
2944 }
2945 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002946 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07002947 if (!animate) {
2948 oa.end();
2949 }
Adam Cohen2801caf2011-05-13 20:57:39 -07002950 }
2951
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002952 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002953 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002954 * is animated relative to the specified View. If the View is null, no animation
2955 * is played.
2956 *
Sunny Goyal08442b82015-10-21 17:15:08 -07002957 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002958 */
Adam Cohen37b2a492016-04-06 18:36:06 -07002959 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07002960
Adam Cohenfb91f302012-06-11 15:45:18 -07002961 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07002962 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
2963 if (openFolder != null && openFolder != folder) {
2964 // Close any open folder before opening a folder.
2965 closeFolder();
2966 }
2967
Adam Cohena9cf38f2011-05-02 15:36:58 -07002968 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002969
Adam Cohena9cf38f2011-05-02 15:36:58 -07002970 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002971
Sunny Goyalf4066152015-04-15 09:42:19 -07002972 // While the folder is open, the position of the icon cannot change.
2973 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
2974
Adam Cohen4554ee12011-08-03 16:13:21 -07002975 // Just verify that the folder hasn't already been added to the DragLayer.
2976 // There was a one-off crash where the folder had a parent already.
2977 if (folder.getParent() == null) {
2978 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07002979 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07002980 } else {
2981 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2982 folder.getParent() + ").");
2983 }
Adam Cohen37b2a492016-04-06 18:36:06 -07002984 folder.animateOpen();
2985
2986 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002987
2988 // Notify the accessibility manager that this folder "window" has appeared and occluded
2989 // the workspace items
2990 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2991 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002992 }
2993
2994 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07002995 closeFolder(true);
2996 }
2997
2998 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08002999 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003000 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003001 if (folder.isEditingName()) {
3002 folder.dismissEditingName();
3003 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003004 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003005 }
3006 }
3007
Sunny Goyal935fca12015-10-13 10:19:01 -07003008 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003009 animate &= !Utilities.isPowerSaverOn(this);
3010
Adam Cohen4554ee12011-08-03 16:13:21 -07003011 folder.getInfo().opened = false;
3012
3013 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3014 if (parent != null) {
3015 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003016 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003017 if (fi != null) {
3018 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3019 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003020 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003021 if (animate) {
3022 folder.animateClosed();
3023 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003024 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003025 }
Winson Chung83ca4802013-04-12 15:10:52 -07003026
Sunny Goyal935fca12015-10-13 10:19:01 -07003027 // Notify the accessibility manager that this folder "window" has disappeared and no
3028 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003029 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003030 }
3031
Tony Wickham49c8d292016-07-01 11:44:34 -07003032 public void closeShortcutsContainer() {
Sunny Goyala2454ad2016-07-22 10:50:11 -07003033 closeShortcutsContainer(true);
3034 }
3035
3036 public void closeShortcutsContainer(boolean animate) {
Tony Wickham49c8d292016-07-01 11:44:34 -07003037 DeepShortcutsContainer deepShortcutsContainer = getOpenShortcutsContainer();
3038 if (deepShortcutsContainer != null) {
Sunny Goyala2454ad2016-07-22 10:50:11 -07003039 if (animate) {
3040 deepShortcutsContainer.animateClose();
3041 } else {
3042 deepShortcutsContainer.close();
3043 }
Tony Wickham49c8d292016-07-01 11:44:34 -07003044 }
3045 }
3046
3047 /**
3048 * @return The open shortcuts container, or null if there is none
3049 */
3050 public DeepShortcutsContainer getOpenShortcutsContainer() {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07003051 // Iterate in reverse order. Shortcuts container is added later to the dragLayer,
3052 // and will be one of the last views.
3053 for (int i = mDragLayer.getChildCount() - 1; i >= 0; i--) {
3054 View child = mDragLayer.getChildAt(i);
Sunny Goyala2454ad2016-07-22 10:50:11 -07003055 if (child instanceof DeepShortcutsContainer
3056 && ((DeepShortcutsContainer) child).isOpen()) {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07003057 return (DeepShortcutsContainer) child;
3058 }
3059 }
3060 return null;
Tony Wickham49c8d292016-07-01 11:44:34 -07003061 }
3062
Tony Wickhamdadb3042016-02-24 11:07:00 -08003063 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003064 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003065 if (!isDraggingEnabled()) return false;
3066 if (isWorkspaceLocked()) return false;
3067 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003068
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07003069 if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
3070 (v == mAllAppsButton && mAllAppsButton != null)) {
Winson Chung76648c52015-07-10 14:33:23 -07003071 onLongClickAllAppsButton(v);
3072 return true;
3073 }
3074
Adam Cohen1697b792013-09-17 19:08:21 -07003075 if (v instanceof Workspace) {
3076 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003077 if (!mWorkspace.isTouchActive()) {
3078 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003079 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3080 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3081 return true;
3082 } else {
3083 return false;
3084 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003085 } else {
3086 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003087 }
Adam Cohen1697b792013-09-17 19:08:21 -07003088 }
3089
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003090 CellLayout.CellInfo longClickCellInfo = null;
3091 View itemUnderLongClick = null;
3092 if (v.getTag() instanceof ItemInfo) {
3093 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003094 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003095 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07003096 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003097 }
3098
Winson Chung3d503fb2011-07-13 17:25:49 -07003099 // The hotseat touch handling does not go through Workspace, and we always allow long press
3100 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003101 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003102 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003103 // User long pressed on empty space
3104 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3105 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003106 if (mWorkspace.isInOverviewMode()) {
3107 mWorkspace.startReordering(v);
3108 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003109 showOverviewMode(true);
Hyunyoung Song645764e2016-06-06 14:19:02 -07003110 mHotseat.requestDisallowInterceptTouchEvent(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003111 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003112 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003113 final boolean isAllAppsButton =
3114 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3115 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3116 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003117 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003118 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003119 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003120 }
3121 }
3122 }
3123 return true;
3124 }
3125
Winson Chung3d503fb2011-07-13 17:25:49 -07003126 boolean isHotseatLayout(View layout) {
3127 return mHotseat != null && layout != null &&
3128 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3129 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003130
Winson Chung3d503fb2011-07-13 17:25:49 -07003131 /**
3132 * Returns the CellLayout of the specified container at the specified screen.
3133 */
Sunny Goyal92820592015-03-02 11:31:35 -08003134 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003135 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3136 if (mHotseat != null) {
3137 return mHotseat.getLayout();
3138 } else {
3139 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003140 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003141 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003142 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003143 }
3144 }
3145
Winson Chungb745afb2015-03-02 11:51:23 -08003146 /**
3147 * For overridden classes.
3148 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003149 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003150 return isAppsViewVisible();
3151 }
3152
3153 public boolean isAppsViewVisible() {
3154 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3155 }
3156
3157 public boolean isWidgetsViewVisible() {
3158 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003159 }
3160
Michael Jurkae326f182011-11-21 14:05:46 -08003161 @Override
3162 public void onTrimMemory(int level) {
3163 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003164 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3165 // The widget preview db can result in holding onto over
3166 // 3MB of memory for caching which isn't necessary.
3167 SQLiteDatabase.releaseMemory();
3168
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003169 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003170 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003171 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003172 if (mLauncherCallbacks != null) {
3173 mLauncherCallbacks.onTrimMemory(level);
3174 }
Michael Jurkae326f182011-11-21 14:05:46 -08003175 }
3176
Winson5c6bdbb2015-09-03 11:36:19 -07003177 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003178 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003179 }
3180
Winson5c6bdbb2015-09-03 11:36:19 -07003181 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003182 boolean changed = mState != State.WORKSPACE ||
3183 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003184 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003185 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003186 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003187 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003188
Michael Jurkab3e22d92011-10-31 15:58:33 -07003189 // Set focus to the AppsCustomize button
3190 if (mAllAppsButton != null) {
3191 mAllAppsButton.requestFocus();
3192 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003193 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003194
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003195 // Change the state *after* we've called all the transition code
3196 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003197
Winson Chung5fb63472011-02-02 17:03:37 -08003198 // Resume the auto-advance of widgets
3199 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003200 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003201
Winson Chung0f785722015-04-08 10:27:49 -07003202 if (changed) {
3203 // Send an accessibility event to announce the context change
3204 getWindow().getDecorView()
3205 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003206 }
Winson5c6bdbb2015-09-03 11:36:19 -07003207 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003208 }
3209
Winsone9f27272015-10-13 10:47:51 -07003210 /**
3211 * Shows the overview button.
3212 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07003213 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003214 showOverviewMode(animated, false);
3215 }
3216
3217 /**
3218 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3219 * onto one of the overview panel buttons.
3220 */
3221 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3222 Runnable postAnimRunnable = null;
3223 if (requestButtonFocus) {
3224 postAnimRunnable = new Runnable() {
3225 @Override
3226 public void run() {
3227 // Hitting the menu button when in touch mode does not trigger touch mode to
3228 // be disabled, so if requested, force focus on one of the overview panel
3229 // buttons.
3230 mOverviewPanel.requestFocusFromTouch();
3231 }
3232 };
3233 }
Adam Cohened307df2013-10-02 09:37:31 -07003234 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003235 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003236 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003237 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003238 }
3239
Winson Chungb745afb2015-03-02 11:51:23 -08003240 /**
3241 * Shows the apps view.
3242 */
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003243 public void showAppsView(boolean animated, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003244 boolean focusSearchBar) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003245 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07003246 if (updatePredictedApps) {
3247 tryAndUpdatePredictedApps();
3248 }
Winson Chung76648c52015-07-10 14:33:23 -07003249 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003250 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003251
Winson Chungb745afb2015-03-02 11:51:23 -08003252 /**
3253 * Shows the widgets view.
3254 */
3255 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003256 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003257 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003258 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003259 }
Winson Chung76648c52015-07-10 14:33:23 -07003260 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003261
3262 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003263 @Override
3264 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003265 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003266 }
3267 });
Winson Chungb745afb2015-03-02 11:51:23 -08003268 }
3269
3270 /**
3271 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003272 *
3273 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003274 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003275 // TODO: calling method should use the return value so that when {@code false} is returned
3276 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003277 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003278 if (!(mState == State.WORKSPACE ||
3279 mState == State.APPS_SPRING_LOADED ||
3280 mState == State.WIDGETS_SPRING_LOADED ||
3281 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003282 return false;
3283 }
3284 if (toState != State.APPS && toState != State.WIDGETS) {
3285 return false;
3286 }
Winson Chungb745afb2015-03-02 11:51:23 -08003287
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003288 // This is a safe and supported transition to bypass spring_loaded mode.
3289 if (mExitSpringLoadedModeRunnable != null) {
3290 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3291 mExitSpringLoadedModeRunnable = null;
3292 }
3293
Winson Chungb745afb2015-03-02 11:51:23 -08003294 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003295 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3296 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003297 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003298 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003299 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003300
Michael Jurkab3e22d92011-10-31 15:58:33 -07003301 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003302 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003303
3304 // Pause the auto-advance of widgets until we are out of AllApps
3305 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003306 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003307 closeFolder();
Tony Wickham49c8d292016-07-01 11:44:34 -07003308 closeShortcutsContainer();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003309
3310 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003311 getWindow().getDecorView()
3312 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003313 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003314 }
3315
Winson Chungcd99cd32015-04-29 11:03:24 -07003316 /**
3317 * Updates the workspace and interaction state on state change, and return the animation to this
3318 * new state.
3319 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003320 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003321 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003322 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003323 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003324 updateInteraction(fromState, toState);
3325 return anim;
3326 }
3327
Hyunyoung Song3f471442015-04-08 19:01:34 -07003328 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003329 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003330 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003331 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003332 }
Winson Chungb745afb2015-03-02 11:51:23 -08003333
Winson Chung006ee262015-08-03 14:40:11 -07003334 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003335 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003336 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003337
3338 if (isAppsViewVisible()) {
3339 mState = State.APPS_SPRING_LOADED;
3340 } else if (isWidgetsViewVisible()) {
3341 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003342 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003343 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003344 } else {
3345 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003346 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003347 }
Adam Cohen7777d962011-08-18 18:58:38 -07003348
Hyunyoung Song3f471442015-04-08 19:01:34 -07003349 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003350 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003351 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003352
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003353 if (mExitSpringLoadedModeRunnable != null) {
3354 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3355 }
3356 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003357 @Override
3358 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003359 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003360 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3361 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003362 // Before we show workspace, hide all apps again because
3363 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3364 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003365 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003366 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003367 } else {
3368 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003369 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003370 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07003371 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003372 };
3373 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003374 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003375
Tony Wickhame0c33232016-02-08 11:37:04 -08003376 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003377 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3378 || mState == State.WIDGETS_SPRING_LOADED;
3379 }
3380
Michael Jurkad3ef3062010-11-23 16:23:58 -08003381 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003382 if (mState == State.APPS_SPRING_LOADED) {
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003383 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07003384 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003385 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003386 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003387 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3388 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003389 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003390 }
3391
Winson Chung4ac30062015-05-08 17:34:17 -07003392 /**
3393 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3394 * resumed.
3395 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003396 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003397 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003398 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003399 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003400 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003401 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003402 }
3403 }
3404 }
3405
Michael Jurkab3e22d92011-10-31 15:58:33 -07003406 void lockAllApps() {
3407 // TODO
3408 }
3409
3410 void unlockAllApps() {
3411 // TODO
3412 }
3413
Winsonf768d932015-09-25 16:12:35 -07003414 public boolean launcherCallbacksProvidesSearch() {
3415 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3416 }
3417
Michael Jurkad7c28052012-04-27 15:43:36 -07003418 @Override
3419 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003420 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003421 final List<CharSequence> text = event.getText();
3422 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003423 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003424 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003425 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003426 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003427 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003428 } else if (mWorkspace != null) {
3429 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003430 } else {
3431 text.add(getString(R.string.all_apps_home_button_label));
3432 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003433 return result;
3434 }
3435
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003436 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003437 * If the activity is currently paused, signal that we need to run the passed Runnable
3438 * in onResume.
3439 *
3440 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003441 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3442 * wrong when we're not running, and if the activity comes back to what the configuration was
3443 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003444 *
3445 * Implementation of the method from LauncherModel.Callbacks.
3446 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003447 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003448 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003449 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003450 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003451 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003452 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003453 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003454 }
3455 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003456 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003457 return true;
3458 } else {
3459 return false;
3460 }
3461 }
3462
Michael Jurkac402cd92013-05-20 15:49:32 +02003463 private boolean waitUntilResume(Runnable run) {
3464 return waitUntilResume(run, false);
3465 }
3466
Michael Jurka1e2f4652013-07-08 18:03:46 -07003467 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003468 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003469 }
3470
Michael Jurka7607c2f2013-04-03 14:33:19 -07003471 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003472 * If the activity is currently paused, signal that we need to re-run the loader
3473 * in onResume.
3474 *
3475 * This needs to be called from incoming places where resources might have been loaded
3476 * while we are paused. That is becaues the Configuration might be wrong
3477 * when we're not running, and if it comes back to what it was when we
3478 * were paused, we are not restarted.
3479 *
3480 * Implementation of the method from LauncherModel.Callbacks.
3481 *
3482 * @return true if we are currently paused. The caller might be able to
3483 * skip some work in that case since we will come back again.
3484 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003485 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003486 public boolean setLoadOnResume() {
3487 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003488 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003489 mOnResumeNeedsLoad = true;
3490 return true;
3491 } else {
3492 return false;
3493 }
3494 }
3495
3496 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003497 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003498 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003499 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003500 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003501 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003502 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003503 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003504 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003505 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003506 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003507
Joe Onorato9c1289c2009-08-17 11:03:03 -04003508 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003509 * Clear any pending bind callbacks. This is called when is loader is planning to
3510 * perform a full rebind from scratch.
3511 */
3512 @Override
3513 public void clearPendingBinds() {
3514 mBindOnResumeCallbacks.clear();
3515 if (mPendingExecutor != null) {
3516 mPendingExecutor.markCompleted();
3517 mPendingExecutor = null;
3518 }
3519 }
3520
3521 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003522 * Refreshes the shortcuts shown on the workspace.
3523 *
3524 * Implementation of the method from LauncherModel.Callbacks.
3525 */
3526 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003527 if (LauncherAppState.PROFILE_STARTUP) {
3528 Trace.beginSection("Starting page bind");
3529 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003530 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003531
Winson Chungd64d1762013-08-20 14:37:16 -07003532 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003533 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003534 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003535
Michael Jurka05bf644e2011-11-30 20:28:53 -08003536 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003537 if (mHotseat != null) {
3538 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003539 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003540 if (LauncherAppState.PROFILE_STARTUP) {
3541 Trace.endSection();
3542 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003543 }
3544
Adam Cohendcd297f2013-06-18 13:13:40 -07003545 @Override
3546 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003547 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003548 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3549 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003550 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3551 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3552 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003553 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3554 // If there are no screens, we need to have an empty screen
3555 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003556 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003557 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003558
3559 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003560 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003561 if (hasCustomContentToLeft()) {
3562 mWorkspace.createCustomContentContainer();
3563 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003564 }
Winsonc7d2e832016-07-28 12:24:55 -07003565
3566 // After we have added all the screens, if the wallpaper was locked to the default state,
3567 // then notify to indicate that it can be released and a proper wallpaper offset can be
3568 // computed before the next layout
3569 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07003570 }
3571
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003572 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003573 int count = orderedScreenIds.size();
3574 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003575 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003576 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003577 // No need to bind the first screen, as its always bound.
3578 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3579 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003580 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003581 }
3582
Winson Chungd64d1762013-08-20 14:37:16 -07003583 public void bindAppsAdded(final ArrayList<Long> newScreens,
3584 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003585 final ArrayList<ItemInfo> addAnimated,
3586 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003587 Runnable r = new Runnable() {
3588 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003589 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003590 }
3591 };
3592 if (waitUntilResume(r)) {
3593 return;
3594 }
3595
Winson Chungd64d1762013-08-20 14:37:16 -07003596 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003597 if (newScreens != null) {
3598 bindAddScreens(newScreens);
3599 }
Winson Chungd64d1762013-08-20 14:37:16 -07003600
3601 // We add the items without animation on non-visible pages, and with
3602 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003603 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003604 bindItems(addNotAnimated, 0,
3605 addNotAnimated.size(), false);
3606 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003607 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003608 bindItems(addAnimated, 0,
3609 addAnimated.size(), true);
3610 }
Winson Chungc58497e2013-09-03 17:48:37 -07003611
Winson Chung87412982013-10-03 18:34:14 -07003612 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003613 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003614
Winson Chungb745afb2015-03-02 11:51:23 -08003615 if (addedApps != null && mAppsView != null) {
3616 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003617 }
Winson Chungd64d1762013-08-20 14:37:16 -07003618 }
3619
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003620 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003621 * Bind the items start-end from the list.
3622 *
3623 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003624 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003625 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003626 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3627 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003628 Runnable r = new Runnable() {
3629 public void run() {
3630 bindItems(shortcuts, start, end, forceAnimateIcons);
3631 }
3632 };
3633 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003634 return;
3635 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003636
Winson Chungf0c6ae02012-03-21 16:10:31 -07003637 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003638 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3639 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003640 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003641 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003642 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003643 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003644 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003645
3646 // Short circuit if we are loading dock items for a configuration which has no dock
3647 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3648 mHotseat == null) {
3649 continue;
3650 }
3651
Sunny Goyal639e9062015-08-19 19:17:06 -07003652 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003653 switch (item.itemType) {
3654 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3655 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003656 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003657 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003658 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003659 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003660 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003661 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003662 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003663 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003664 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003665 default:
3666 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003667 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003668
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003669 /*
3670 * Remove colliding items.
3671 */
3672 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3673 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3674 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3675 View v = cl.getChildAt(item.cellX, item.cellY);
3676 Object tag = v.getTag();
3677 String desc = "Collision while binding workspace item: " + item
3678 + ". Collides with " + tag;
3679 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3680 throw (new RuntimeException(desc));
3681 } else {
3682 Log.d(TAG, desc);
3683 LauncherModel.deleteItemFromDatabase(this, item);
3684 continue;
3685 }
3686 }
3687 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003688 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3689 item.cellY, 1, 1);
3690 if (animateIcons) {
3691 // Animate all the applications up now
3692 view.setAlpha(0f);
3693 view.setScaleX(0f);
3694 view.setScaleY(0f);
3695 bounceAnims.add(createNewAppBounceAnimation(view, i));
3696 newShortcutsScreenId = item.screenId;
3697 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003698 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003699
Winson Chung997a9232013-07-24 15:33:46 -07003700 if (animateIcons) {
3701 // Animate to the correct page
3702 if (newShortcutsScreenId > -1) {
3703 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003704 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003705 final Runnable startBounceAnimRunnable = new Runnable() {
3706 public void run() {
3707 anim.playTogether(bounceAnims);
3708 anim.start();
3709 }
3710 };
Winson Chung997a9232013-07-24 15:33:46 -07003711 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003712 // We post the animation slightly delayed to prevent slowdowns
3713 // when we are loading right after we return to launcher.
3714 mWorkspace.postDelayed(new Runnable() {
3715 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003716 if (mWorkspace != null) {
3717 mWorkspace.snapToPage(newScreenIndex);
3718 mWorkspace.postDelayed(startBounceAnimRunnable,
3719 NEW_APPS_ANIMATION_DELAY);
3720 }
Winson Chung94d67682013-09-25 16:29:40 -07003721 }
3722 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003723 } else {
3724 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003725 }
3726 }
Winson Chung64359a52013-07-08 17:17:08 -07003727 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003728 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003729 }
3730
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003731 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3732 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3733 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003734 view.updateAppWidget(null);
3735 view.setOnClickListener(this);
3736 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003737 mWorkspace.requestLayout();
3738 }
3739
Joe Onorato9c1289c2009-08-17 11:03:03 -04003740 /**
3741 * Add the views for a widget to the workspace.
3742 *
3743 * Implementation of the method from LauncherModel.Callbacks.
3744 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003745 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003746 Runnable r = new Runnable() {
3747 public void run() {
3748 bindAppWidget(item);
3749 }
3750 };
3751 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003752 return;
3753 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003754
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003755 if (mIsSafeModeEnabled) {
3756 bindSafeModeWidget(item);
3757 return;
3758 }
3759
Daniel Sandler843e8602010-06-07 14:59:01 -04003760 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3761 if (DEBUG_WIDGETS) {
3762 Log.d(TAG, "bindAppWidget: " + item);
3763 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003764
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003765 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003766
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003767 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3768 // If the provider is not ready, bind as a pending widget.
3769 appWidgetInfo = null;
3770 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3771 // The widget id is not valid. Try to find the widget based on the provider info.
3772 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3773 } else {
3774 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3775 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003776
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003777 // If the provider is ready, but the width is not yet restored, try to restore it.
3778 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3779 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003780 if (appWidgetInfo == null) {
3781 if (DEBUG_WIDGETS) {
3782 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3783 + " belongs to component " + item.providerName
3784 + ", as the povider is null");
3785 }
3786 LauncherModel.deleteItemFromDatabase(this, item);
3787 return;
3788 }
Sunny Goyalff572272014-07-23 13:58:07 -07003789
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003790 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003791 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003792 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3793 // Id has not been allocated yet. Allocate a new id.
3794 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3795 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003796
Sunny Goyald478c832016-04-01 12:04:16 -07003797 // Also try to bind the widget. If the bind fails, the user will be shown
3798 // a click to setup UI, which will ask for the bind permission.
3799 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3800 pendingInfo.spanX = item.spanX;
3801 pendingInfo.spanY = item.spanY;
3802 pendingInfo.minSpanX = item.minSpanX;
3803 pendingInfo.minSpanY = item.minSpanY;
3804 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003805
3806 boolean isDirectConfig =
3807 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3808 if (isDirectConfig && item.bindOptions != null) {
3809 Bundle newOptions = item.bindOptions.getExtras();
3810 if (options != null) {
3811 newOptions.putAll(options);
3812 }
3813 options = newOptions;
3814 }
Sunny Goyald478c832016-04-01 12:04:16 -07003815 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3816 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003817
Sunny Goyal86df1382016-08-10 15:03:22 -07003818 // We tried to bind once. If we were not able to bind, we would need to
3819 // go through the permission dialog, which means we cannot skip the config
3820 // activity.
3821 item.bindOptions = null;
3822 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3823
Sunny Goyald478c832016-04-01 12:04:16 -07003824 // Bind succeeded
3825 if (success) {
3826 // If the widget has a configure activity, it is still needs to set it up,
3827 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003828 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003829 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3830 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003831 }
Sunny Goyald478c832016-04-01 12:04:16 -07003832
3833 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003834 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003835 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003836 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003837 // The widget was marked as UI not ready, but there is no configure activity to
3838 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003839 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3840 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003841 }
Sunny Goyalff572272014-07-23 13:58:07 -07003842 }
3843
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003844 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003845 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003846 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3847 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003848 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003849
Sunny Goyal56c73602015-09-25 12:55:01 -07003850 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003851 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003852 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003853 deleteWidgetInfo(item);
3854 return;
3855 }
3856
Sunny Goyal233ee962015-08-03 13:05:01 -07003857 item.minSpanX = appWidgetInfo.minSpanX;
3858 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003859 addAppWidgetToWorkspace(
3860 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3861 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003862 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003863 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003864 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003865 view.updateAppWidget(null);
3866 view.setOnClickListener(this);
3867 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003868 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003869 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003870
Daniel Sandler843e8602010-06-07 14:59:01 -04003871 if (DEBUG_WIDGETS) {
3872 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3873 + (SystemClock.uptimeMillis()-start) + "ms");
3874 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003875 }
3876
Sunny Goyalff572272014-07-23 13:58:07 -07003877 /**
3878 * Restores a pending widget.
3879 *
3880 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003881 */
Sunny Goyald478c832016-04-01 12:04:16 -07003882 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003883 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3884 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3885 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003886 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003887 }
3888
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003889 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003890 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003891
3892 mWorkspace.reinflateWidgetsIfNecessary();
3893 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003894 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003895 }
3896
Adam Cohen1462de32012-07-24 22:34:36 -07003897 public void onPageBoundSynchronously(int page) {
3898 mSynchronouslyBoundPages.add(page);
3899 }
3900
Sunny Goyal527c7d32015-08-28 15:19:36 -07003901 @Override
3902 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3903 if (mPendingExecutor != null) {
3904 mPendingExecutor.markCompleted();
3905 }
3906 mPendingExecutor = executor;
3907 executor.attachTo(this);
3908 }
3909
3910 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3911 if (mPendingExecutor == executor) {
3912 mPendingExecutor = null;
3913 }
3914 }
3915
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003916 @Override
3917 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3918 Runnable r = new Runnable() {
3919 public void run() {
3920 finishFirstPageBind(executor);
3921 }
3922 };
3923 if (waitUntilResume(r)) {
3924 return;
3925 }
3926
3927 Runnable onComplete = new Runnable() {
3928 @Override
3929 public void run() {
3930 if (executor != null) {
3931 executor.onLoadAnimationCompleted();
3932 }
3933 }
3934 };
3935 if (mDragLayer.getAlpha() < 1) {
3936 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3937 } else {
3938 onComplete.run();
3939 }
3940 }
3941
Joe Onorato9c1289c2009-08-17 11:03:03 -04003942 /**
3943 * Callback saying that there aren't any more items to bind.
3944 *
3945 * Implementation of the method from LauncherModel.Callbacks.
3946 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003947 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003948 Runnable r = new Runnable() {
3949 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003950 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003951 }
3952 };
3953 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003954 return;
3955 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003956 if (LauncherAppState.PROFILE_STARTUP) {
3957 Trace.beginSection("Page bind completed");
3958 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003959 if (mSavedState != null) {
3960 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003961 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003962 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003963
Joe Onorato9c1289c2009-08-17 11:03:03 -04003964 mSavedState = null;
3965 }
3966
Adam Cohen1462de32012-07-24 22:34:36 -07003967 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003968
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003969 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003970
Sunny Goyal2100c782016-08-22 16:00:03 -07003971 if (mPendingActivityResult != null) {
3972 handleActivityResult(mPendingActivityResult.requestCode,
3973 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3974 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003975 }
3976
Sunny Goyal756adbc2015-04-16 15:20:51 -07003977 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003978
3979 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003980 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003981 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003982 if (LauncherAppState.PROFILE_STARTUP) {
3983 Trace.endSection();
3984 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003985 }
3986
Winson Chunga2413752012-04-03 14:22:34 -07003987 private boolean canRunNewAppsAnimation() {
3988 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003989 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003990 }
3991
Winson Chungc9168342013-06-26 14:54:55 -07003992 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003993 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003994 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3995 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003996 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003997 return bounceAnim;
3998 }
3999
Adam Cohen27772732013-11-11 14:00:56 +00004000 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004001 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004002 }
4003
Tony Wickham55616cd2015-09-23 14:55:17 -07004004 public int getSearchBarHeight() {
4005 if (mLauncherCallbacks != null) {
4006 return mLauncherCallbacks.getSearchBarHeight();
4007 }
4008 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4009 }
4010
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004011 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004012 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4013 * multiple calls to bind the same list.)
4014 */
4015 @Thunk ArrayList<AppInfo> mTmpAppsList;
4016 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4017 public void run() {
4018 bindAllApplications(mTmpAppsList);
4019 mTmpAppsList = null;
4020 }
4021 };
4022
4023 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004024 * Add the icons for all apps.
4025 *
4026 * Implementation of the method from LauncherModel.Callbacks.
4027 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004028 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004029 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4030 mTmpAppsList = apps;
4031 return;
4032 }
4033
Winson Chungb745afb2015-03-02 11:51:23 -08004034 if (mAppsView != null) {
4035 mAppsView.setApps(apps);
4036 }
Adam Cohen9211d422014-10-07 18:14:53 -07004037 if (mLauncherCallbacks != null) {
4038 mLauncherCallbacks.bindAllApplications(apps);
4039 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004040 }
4041
4042 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004043 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
4044 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
4045 */
4046 @Override
4047 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
4048 mDeepShortcutMap = deepShortcutMapCopy;
4049 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
4050 }
4051
Tony Wickham1bce7fd2016-04-28 17:39:03 -07004052 public List<String> getShortcutIdsForItem(ItemInfo info) {
4053 if (!DeepShortcutManager.supportsShortcuts(info)) {
4054 return Collections.EMPTY_LIST;
4055 }
4056 ComponentName component = info.getTargetComponent();
Hyunyoung Songd3bf9802016-08-29 14:43:53 -07004057 if (component == null) {
4058 return Collections.EMPTY_LIST;
4059 }
4060
Tony Wickham1bce7fd2016-04-28 17:39:03 -07004061 List<String> ids = mDeepShortcutMap.get(new ComponentKey(component, info.user));
4062 return ids == null ? Collections.EMPTY_LIST : ids;
4063 }
4064
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004065 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004066 * A package was updated.
4067 *
4068 * Implementation of the method from LauncherModel.Callbacks.
4069 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004070 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004071 Runnable r = new Runnable() {
4072 public void run() {
4073 bindAppsUpdated(apps);
4074 }
4075 };
4076 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004077 return;
4078 }
4079
Winson Chungb745afb2015-03-02 11:51:23 -08004080 if (mAppsView != null) {
4081 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004082 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004083 }
4084
Sunny Goyal4390ace2014-10-13 11:33:11 -07004085 @Override
4086 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4087 Runnable r = new Runnable() {
4088 public void run() {
4089 bindWidgetsRestored(widgets);
4090 }
4091 };
4092 if (waitUntilResume(r)) {
4093 return;
4094 }
4095 mWorkspace.widgetsRestored(widgets);
4096 }
4097
Joe Onorato9c1289c2009-08-17 11:03:03 -04004098 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004099 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004100 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004101 *
4102 * @param updated list of shortcuts which have changed.
4103 * @param removed list of shortcuts which were deleted in the background. This can happen when
4104 * an app gets removed from the system or some of its components are no longer
4105 * available.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004106 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004107 @Override
4108 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4109 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004110 Runnable r = new Runnable() {
4111 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004112 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004113 }
4114 };
4115 if (waitUntilResume(r)) {
4116 return;
4117 }
4118
Sunny Goyal4390ace2014-10-13 11:33:11 -07004119 if (!updated.isEmpty()) {
4120 mWorkspace.updateShortcuts(updated);
4121 }
4122
4123 if (!removed.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004124 HashSet<ComponentName> removedComponents = new HashSet<>();
4125 HashSet<ShortcutKey> removedDeepShortcuts = new HashSet<>();
4126
Sunny Goyal4390ace2014-10-13 11:33:11 -07004127 for (ShortcutInfo si : removed) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004128 if (si.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07004129 removedDeepShortcuts.add(ShortcutKey.fromShortcutInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004130 } else {
4131 removedComponents.add(si.getTargetComponent());
4132 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004133 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004134
4135 if (!removedComponents.isEmpty()) {
4136 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(removedComponents, user);
4137 mWorkspace.removeItemsByMatcher(matcher);
4138 mDragController.onAppsRemoved(matcher);
4139 }
4140
4141 if (!removedDeepShortcuts.isEmpty()) {
4142 ItemInfoMatcher matcher = ItemInfoMatcher.ofShortcutKeys(removedDeepShortcuts);
4143 mWorkspace.removeItemsByMatcher(matcher);
4144 mDragController.onAppsRemoved(matcher);
4145 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004146 }
4147 }
4148
4149 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004150 * Update the state of a package, typically related to install state.
4151 *
4152 * Implementation of the method from LauncherModel.Callbacks.
4153 */
Sunny Goyale755d462014-07-22 13:48:29 -07004154 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004155 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4156 Runnable r = new Runnable() {
4157 public void run() {
4158 bindRestoreItemsChange(updates);
4159 }
4160 };
4161 if (waitUntilResume(r)) {
4162 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004163 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004164
Sunny Goyal756adbc2015-04-16 15:20:51 -07004165 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004166 }
4167
4168 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004169 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004170 * in addition to specific applications to remove, the reason being that
4171 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004172 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004173 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004174 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004175 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004176 public void bindWorkspaceComponentsRemoved(
4177 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4178 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004179 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004180 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004181 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004182 }
Winson Chungd64d1762013-08-20 14:37:16 -07004183 };
4184 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004185 return;
4186 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004187 if (!packageNames.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004188 ItemInfoMatcher matcher = ItemInfoMatcher.ofPackages(packageNames, user);
4189 mWorkspace.removeItemsByMatcher(matcher);
4190 mDragController.onAppsRemoved(matcher);
4191
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004192 }
4193 if (!components.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004194 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(components, user);
4195 mWorkspace.removeItemsByMatcher(matcher);
4196 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004197 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004198 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004199
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004200 @Override
4201 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4202 Runnable r = new Runnable() {
4203 public void run() {
4204 bindAppInfosRemoved(appInfos);
4205 }
4206 };
4207 if (waitUntilResume(r)) {
4208 return;
Joe Onorato36115782010-06-17 13:28:48 -04004209 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004210
Winson Chungdf95eb12013-10-16 14:57:07 -07004211 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004212 if (mAppsView != null) {
4213 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004214 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004215 }
Joe Onoratobe386092009-11-17 17:32:16 -08004216
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004217 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004218 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004219 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004220 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004221 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004222
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004223 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004224 public void bindWidgetsModel(WidgetsModel model) {
4225 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004226 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004227 return;
4228 }
4229
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004230 if (mWidgetsView != null && model != null) {
4231 mWidgetsView.addWidgets(model);
4232 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004233 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004234 }
4235
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004236 @Override
4237 public void notifyWidgetProvidersChanged() {
4238 if (mWorkspace.getState().shouldUpdateWidget) {
4239 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4240 }
4241 }
4242
Winson Chung400438b2011-01-16 17:53:48 -08004243 private int mapConfigurationOriActivityInfoOri(int configOri) {
4244 final Display d = getWindowManager().getDefaultDisplay();
4245 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4246 switch (d.getRotation()) {
4247 case Surface.ROTATION_0:
4248 case Surface.ROTATION_180:
4249 // We are currently in the same basic orientation as the natural orientation
4250 naturalOri = configOri;
4251 break;
4252 case Surface.ROTATION_90:
4253 case Surface.ROTATION_270:
4254 // We are currently in the other basic orientation to the natural orientation
4255 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4256 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4257 break;
4258 }
4259
4260 int[] oriMap = {
4261 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4262 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4263 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4264 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4265 };
4266 // Since the map starts at portrait, we need to offset if this device's natural orientation
4267 // is landscape.
4268 int indexOffset = 0;
4269 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4270 indexOffset = 1;
4271 }
4272 return oriMap[(d.getRotation() + indexOffset) % 4];
4273 }
Adam Cohen446e9402011-09-15 18:21:21 -07004274
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004275 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004276 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004277 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004278 if (Utilities.ATLEAST_JB_MR2) {
4279 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4280 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004281 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4282 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004283 }
Winson Chung4b919f82012-05-01 10:44:08 -07004284 }
4285 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004286
Winson Chung4b919f82012-05-01 10:44:08 -07004287 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004288 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004289 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004290 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004291 } else {
4292 mHandler.postDelayed(new Runnable() {
4293 public void run() {
4294 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4295 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004296 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004297 }
Winson Chung4b919f82012-05-01 10:44:08 -07004298 }
Winson Chung400438b2011-01-16 17:53:48 -08004299 }
4300
Hyunyoung Songc001cf52016-07-21 17:32:43 -07004301 private void markAppsViewShown() {
4302 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4303 return;
4304 }
4305 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
4306 }
4307
4308 private boolean shouldShowDiscoveryBounce() {
4309 if (mState != mState.WORKSPACE) {
4310 return false;
4311 }
4312 if (mLauncherCallbacks != null && mLauncherCallbacks.shouldShowDiscoveryBounce()) {
4313 return true;
4314 }
4315 if (!mIsResumeFromActionScreenOff) {
4316 return false;
4317 }
4318 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4319 return false;
4320 }
4321 return true;
4322 }
4323
Winson Chung5ffd51d2015-06-25 11:36:50 -07004324 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004325 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004326 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004327 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004328 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004329 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004330 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4331 if (userHandle != null) {
4332 user = UserHandleCompat.fromUser(userHandle);
4333 }
4334 }
4335 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004336 }
4337
4338 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004339 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004340 if (user == null) {
4341 user = UserHandleCompat.myUserHandle();
4342 }
4343
4344 // Called from search suggestion, add the profile extra to the intent to ensure that we
4345 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004346 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004347 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004348 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004349 return null;
4350 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004351 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004352 }
4353
Winson Chung5ffd51d2015-06-25 11:36:50 -07004354 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004355 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4356 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004357 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004358 UserHandleCompat.myUserHandle());
4359 }
4360
Winson Chung5ffd51d2015-06-25 11:36:50 -07004361 protected void moveWorkspaceToDefaultScreen() {
4362 mWorkspace.moveToDefaultScreen(false);
4363 }
4364
Winson Chung80baf5a2010-08-09 16:03:15 -07004365 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004366 * Returns a FastBitmapDrawable with the icon, accurately sized.
4367 */
4368 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4369 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4370 d.setFilterBitmap(true);
4371 resizeIconDrawable(d);
4372 return d;
4373 }
4374
4375 /**
4376 * Resizes an icon drawable to the correct icon size.
4377 */
Winson5fbe0742015-09-30 15:33:00 -07004378 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004379 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004380 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004381 }
4382
4383 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004384 * Prints out out state for debugging.
4385 */
4386 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004387 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004388 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004389 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
Sunny Goyal2100c782016-08-22 16:00:03 -07004390 Log.d(TAG, "mPendingRequestArgs=" + mPendingRequestArgs);
4391 Log.d(TAG, "mPendingActivityResult=" + mPendingActivityResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004392 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004393 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004394
Daniel Sandler325dc232013-06-05 22:57:57 -04004395 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004396 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004397
4398 @Override
4399 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4400 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004401 // Dump workspace
4402 writer.println(prefix + "Workspace Items");
4403 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4404 writer.println(prefix + " Homescreen " + i);
4405
4406 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4407 for (int j = 0; j < layout.getChildCount(); j++) {
4408 Object tag = layout.getChildAt(j).getTag();
4409 if (tag != null) {
4410 writer.println(prefix + " " + tag.toString());
4411 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004412 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004413 }
Sunny Goyala1365452015-10-01 15:46:24 -07004414
4415 writer.println(prefix + " Hotseat");
4416 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4417 for (int j = 0; j < layout.getChildCount(); j++) {
4418 Object tag = layout.getChildAt(j).getTag();
4419 if (tag != null) {
4420 writer.println(prefix + " " + tag.toString());
4421 }
4422 }
4423
Sunny Goyal713edfc2016-05-06 09:58:34 -07004424 try {
4425 FileLog.flushAll(writer);
4426 } catch (Exception e) {
4427 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004428 }
4429
Adam Cohen9211d422014-10-07 18:14:53 -07004430 if (mLauncherCallbacks != null) {
4431 mLauncherCallbacks.dump(prefix, fd, writer, args);
4432 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004433 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004434
Adam Cohen59400422014-03-05 18:07:04 -08004435 public static CustomAppWidget getCustomAppWidget(String name) {
4436 return sCustomAppWidgets.get(name);
4437 }
4438
4439 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4440 return sCustomAppWidgets;
4441 }
4442
Sunny Goyal29538332016-02-18 09:10:19 -08004443 public static List<View> getFolderContents(View icon) {
4444 if (icon instanceof FolderIcon) {
4445 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4446 } else {
4447 return Collections.EMPTY_LIST;
4448 }
4449 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004450
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004451 public static Launcher getLauncher(Context context) {
4452 if (context instanceof Launcher) {
4453 return (Launcher) context;
4454 }
4455 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4456 }
4457
Sunny Goyal745bad92016-05-02 10:54:12 -07004458 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4459
4460 @Override
4461 public void onSharedPreferenceChanged(
4462 SharedPreferences sharedPreferences, String key) {
4463 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4464 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4465 if (!waitUntilResume(this, true)) {
4466 run();
4467 }
4468 }
4469 }
4470
4471 @Override
4472 public void run() {
4473 setOrientation();
4474 }
4475 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004476}