blob: 544aef2b0b02f6f8f18ef6d5585778deb35d33d6 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyal28c6b962015-10-12 11:42:05 -070019import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070025import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000026import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Winson Chungc7450e32012-04-17 17:34:08 -070028import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040029import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080031import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080036import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070039import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040040import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070042import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070043import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070045import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.pm.PackageManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070049import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070050import android.graphics.Bitmap;
51import android.graphics.Canvas;
52import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070053import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070054import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070055import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020056import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080058import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070059import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070060import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040061import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070062import android.os.Trace;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070063import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.text.Selection;
65import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070066import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070068import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070069import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070070import android.view.HapticFeedbackConstants;
71import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.MotionEvent;
74import android.view.Surface;
75import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070076import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080077import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.ViewGroup;
79import android.view.ViewTreeObserver;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080080import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070081import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070082import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080086import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070088
Sunny Goyal651077b2014-06-30 14:15:31 -070089import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070090import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyalae502842016-06-17 08:43:56 -070091import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070092import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070093import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080094import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -070095import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010096import com.android.launcher3.compat.LauncherActivityInfoCompat;
97import com.android.launcher3.compat.LauncherAppsCompat;
98import com.android.launcher3.compat.UserHandleCompat;
99import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800100import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700101import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700102import com.android.launcher3.dragndrop.DragController;
103import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700104import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700105import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -0700106import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000107import com.android.launcher3.folder.Folder;
108import com.android.launcher3.folder.FolderIcon;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700109import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700110import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700111import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700112import com.android.launcher3.model.WidgetsModel;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700113import com.android.launcher3.pageindicators.PageIndicator;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700114import com.android.launcher3.shortcuts.DeepShortcutManager;
Tony Wickham49c8d292016-07-01 11:44:34 -0700115import com.android.launcher3.shortcuts.DeepShortcutsContainer;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700116import com.android.launcher3.shortcuts.ShortcutKey;
Hyunyoung Song5aa27142016-07-21 11:48:37 -0700117import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal2100c782016-08-22 16:00:03 -0700118import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700119import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700120import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700121import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700122import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal2100c782016-08-22 16:00:03 -0700123import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal322d5562015-06-25 19:35:49 -0700124import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700125import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700126import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700127import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700128import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700129import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700130
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700131import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700132import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700133import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700134import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800135import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700136import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700137import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700138import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700139
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140/**
141 * Default launcher application.
142 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100143public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700144 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
145 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700146 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700147 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700148 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700150 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700151 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400152 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700153
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700155 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700156 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700157 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158
Michael Jurka8b805b12012-04-18 14:23:14 -0700159 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700160 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700161 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700162
Sunny Goyal28c6b962015-10-12 11:42:05 -0700163 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
164
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700165 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
166
Mathew Inwood876a8462013-06-14 14:12:41 +0100167 /**
168 * IntentStarter uses request codes starting with this. This must be greater than all activity
169 * request codes used internally.
170 */
171 protected static final int REQUEST_LAST = 100;
172
Michael Jurka0a457bf2012-11-19 14:05:05 -0800173 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700174 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800175 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
Winson Chung2672ff92012-05-04 16:22:30 -0700177 // The Intent extra that defines whether to ignore the launch animation
178 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400179 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700180
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700181 public static final String ACTION_APPWIDGET_HOST_RESET =
182 "com.android.launcher3.intent.ACTION_APPWIDGET_HOST_RESET";
183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 // Type: int
185 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700186 // Type: int
187 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700188 // Type: PendingRequestArgs
189 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
190 // Type: ActivityResultInfo
191 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700193 static final String APPS_VIEW_SHOWN = "launcher.apps_view_shown";
Adam Cohenb54a5982014-01-08 15:21:04 -0800194
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700195 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700196 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
197 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700198
Adam Cohen091440a2015-03-18 14:16:05 -0700199 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700200 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700201
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700202 private boolean mIsSafeModeEnabled;
203
Joe Onorato9c1289c2009-08-17 11:03:03 -0400204 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800205 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700206 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700207 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208
Winson Chunga2413752012-04-03 14:22:34 -0700209 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700210 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
211 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700212 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700213
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700214 private final BroadcastReceiver mUiBroadcastReceiver = new BroadcastReceiver() {
215
216 @Override
217 public void onReceive(Context context, Intent intent) {
218 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
219 closeSystemDialogs();
220 } else if (ACTION_APPWIDGET_HOST_RESET.equals(intent.getAction())) {
221 if (mAppWidgetHost != null) {
222 mAppWidgetHost.startListening();
223 }
224 }
225 }
226 };
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800227
Adam Cohen091440a2015-03-18 14:16:05 -0700228 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700229 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700230 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800231 private DragController mDragController;
Sunny Goyal6178f132016-07-11 17:30:03 -0700232 private View mQsbContainer;
Sunny Goyal322d5562015-06-25 19:35:49 -0700233
234 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700235
Sunny Goyalffe83f12014-08-14 17:39:34 -0700236 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700237 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700238
Michael Jurka0280c3b2010-09-17 15:00:07 -0700239 private int[] mTmpAddItemCellCoordinates = new int[2];
240
Sunny Goyal316490e2015-06-02 09:38:28 -0700241 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800242 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700243
Michael Jurka838a4ca2011-02-07 13:33:06 -0800244 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700245 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700246
Sunny Goyal47328fd2016-05-25 18:56:41 -0700247 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700248
249 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700250 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700251 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700252
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700253 // Main container view and the model for the widget tray screen.
254 @Thunk WidgetsContainerView mWidgetsView;
255 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700256
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700258 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
259 // scroll issues (because the workspace may not have been measured yet) and extra work.
260 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
261 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262
263 private SpannableStringBuilder mDefaultKeySsb = null;
264
Adam Cohen091440a2015-03-18 14:16:05 -0700265 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400266
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800267 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700268 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269
Michael Jurka1e2f4652013-07-08 18:03:46 -0700270 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700271 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700272 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700273
Joe Onorato9c1289c2009-08-17 11:03:03 -0400274 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800275 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700276 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700277 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700278 private boolean mIsResumeFromActionScreenOff;
Adam Cohen091440a2015-03-18 14:16:05 -0700279 @Thunk boolean mUserPresent = true;
Tony Wickham93113872016-09-14 16:46:32 -0700280 private boolean mVisible;
281 private boolean mHasFocus;
282 private boolean mAttached;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400283
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700284 /** Maps launcher activity components to their list of shortcut ids. */
285 private MultiHashMap<ComponentKey, String> mDeepShortcutMap = new MultiHashMap<>();
286
Adam Cohen61f560d2013-09-30 15:58:20 -0700287 private View.OnTouchListener mHapticFeedbackTouchListener;
288
Adam Cohended9f8d2010-11-03 13:25:16 -0700289 // Related to the auto-advancing of widgets
290 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700291 private static final int ADVANCE_INTERVAL = 20000;
292 private static final int ADVANCE_STAGGER = 250;
293
294 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700295 private long mAutoAdvanceSentTime;
296 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700297 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700298
Winson Chung400438b2011-01-16 17:53:48 -0800299 // Determines how long to wait after a rotation before restoring the screen orientation to
300 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700301 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800302
Adam Cohen1462de32012-07-24 22:34:36 -0700303 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
304
Winson Chung46353de2012-02-16 14:05:10 -0800305 // We only want to get the SharedPreferences once since it does an FS stat each time we get
306 // it from the context.
307 private SharedPreferences mSharedPrefs;
308
Winson Chungf0c6ae02012-03-21 16:10:31 -0700309 // Holds the page that we need to animate to, and the icon views that we need to animate up
310 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700311 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700312 private Bitmap mFolderIconBitmap;
313 private Canvas mFolderIconCanvas;
314 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700315
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700316 private DeviceProfile mDeviceProfile;
317
Adam Cohen4b66bf32015-09-18 12:15:19 -0700318 private boolean mMoveToDefaultScreenFromNewIntent;
319
Winson Chung13eb5272015-05-11 16:30:13 -0700320 // This is set to the view that launched the activity that navigated the user away from
321 // launcher. Since there is no callback for when the activity has finished launching, enable
322 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800323 private BubbleTextView mWaitingForResume;
324
Adam Cohen59400422014-03-05 18:07:04 -0800325 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
326 new HashMap<String, CustomAppWidget>();
327
Adam Cohen59400422014-03-05 18:07:04 -0800328 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700329 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
330 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800331 }
332 }
333
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700334 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
335 // state transition. If another state transition happened during this delay,
336 // simply unregister this runnable.
337 private Runnable mExitSpringLoadedModeRunnable;
338
Adam Cohen091440a2015-03-18 14:16:05 -0700339 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700340 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700341 if (mWorkspace != null) {
342 mWorkspace.buildPageHardwareLayers();
343 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700344 }
345 };
346
Sunny Goyal2100c782016-08-22 16:00:03 -0700347 // Activity result which needs to be processed after workspace has loaded.
348 private ActivityResultInfo mPendingActivityResult;
349 /**
350 * Holds extra information required to handle a result from an external call, like
351 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
352 */
353 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800354
Hyunyoung Songaa953652016-04-19 18:30:24 -0700355 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800356
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700357 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700358 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400359
360 @Thunk void setOrientation() {
361 if (mRotationEnabled) {
362 unlockScreenOrientation(true);
363 } else {
364 setRequestedOrientation(
365 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700366 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400367 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700368
Sunny Goyal745bad92016-05-02 10:54:12 -0700369 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700370
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371 @Override
372 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700373 if (DEBUG_STRICT_MODE) {
374 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
375 .detectDiskReads()
376 .detectDiskWrites()
377 .detectNetwork() // or .detectAll() for all detectable problems
378 .penaltyLog()
379 .build());
380 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
381 .detectLeakedSqlLiteObjects()
382 .detectLeakedClosableObjects()
383 .penaltyLog()
384 .penaltyDeath()
385 .build());
386 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700387 if (LauncherAppState.PROFILE_STARTUP) {
388 Trace.beginSection("Launcher-onCreate");
389 }
Winson Chunga2413752012-04-03 14:22:34 -0700390
Adam Cohen9211d422014-10-07 18:14:53 -0700391 if (mLauncherCallbacks != null) {
392 mLauncherCallbacks.preOnCreate();
393 }
394
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400396
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400397 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700398
Adam Cohen2e6da152015-05-06 11:42:25 -0700399 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700400 mDeviceProfile = getResources().getConfiguration().orientation
401 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700402 app.getInvariantDeviceProfile().landscapeProfile
403 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700404
Sunny Goyalf7258242015-10-19 16:59:07 -0700405 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700406 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800407 mModel = app.setLauncher(this);
408 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700409 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700410
Joe Onorato41a12d22009-10-31 18:30:00 -0400411 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700412 mAllAppsController = new AllAppsTransitionController(this);
413 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700414
Sunny Goyalffe83f12014-08-14 17:39:34 -0700415 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700416
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700417 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
418 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700419
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700420 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
421 // this also ensures that any synchronous binding below doesn't re-trigger another
422 // LauncherModel load.
423 mPaused = false;
424
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700426
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800427 setupViews();
Winson1f064272016-07-18 17:18:02 -0700428 mDeviceProfile.layout(this, false /* notifyListeners */);
Tony Wickham827cef22016-03-17 15:39:39 -0700429 mExtractedColors = new ExtractedColors();
430 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431
Tony Wickhame2217252016-03-22 16:34:23 -0700432 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
433 .addAccessibilityStateChangeListener(this);
434
Joe Onorato7c312c12009-08-13 21:36:53 -0700435 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700436
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 mSavedState = savedInstanceState;
438 restoreState(mSavedState);
439
Sunny Goyale26d1002016-06-20 14:52:14 -0700440 if (LauncherAppState.PROFILE_STARTUP) {
441 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442 }
443
Sunny Goyal2100c782016-08-22 16:00:03 -0700444 // We only load the page synchronously if the user rotates (or triggers a
445 // configuration change) while launcher is in the foreground
446 if (!mModel.startLoader(mWorkspace.getRestorePage())) {
447 // If we are not binding synchronously, show a fade in animation when
448 // the first page bind completes.
449 mDragLayer.setAlpha(0);
450 } else {
451 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 }
453
454 // For handling default keys
455 mDefaultKeySsb = new SpannableStringBuilder();
456 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800457
458 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700459 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
460 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800461
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800462 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700463 // In case we are on a device with locked rotation, we should look at preferences to check
464 // if the user has specifically allowed rotation.
465 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700466 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700467 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
468 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700469 }
470
471 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
472 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400473 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700474
Adam Cohen9211d422014-10-07 18:14:53 -0700475 if (mLauncherCallbacks != null) {
476 mLauncherCallbacks.onCreate(savedInstanceState);
477 }
478 }
479
Sunny Goyal7779d622015-06-11 16:18:39 -0700480 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700481 public void onExtractedColorsChanged() {
482 loadExtractedColorsAndColorItems();
483 }
484
485 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700486 // TODO: do this in pre-N as well, once the extraction part is complete.
Tony Wickham345bff32016-09-28 15:34:51 -0700487 if (Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700488 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700489 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700490 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham345bff32016-09-28 15:34:51 -0700491 // It's possible that All Apps is visible when this is run,
492 // so always use light status bar in that case.
493 activateLightStatusBar(isAllAppsVisible());
Tony Wickham827cef22016-03-17 15:39:39 -0700494 }
495 }
496
Tony Wickham345bff32016-09-28 15:34:51 -0700497 /**
498 * Sets the status bar to be light or not. Light status bar means dark icons.
499 * @param activate if true, make sure the status bar is light, otherwise base on wallpaper.
500 */
501 public void activateLightStatusBar(boolean activate) {
Tony Wickham0fed55b2016-10-12 14:55:46 -0700502 boolean lightStatusBar = activate || (FeatureFlags.LIGHT_STATUS_BAR
503 && mExtractedColors.getColor(ExtractedColors.STATUS_BAR_INDEX,
504 ExtractedColors.DEFAULT_DARK) == ExtractedColors.DEFAULT_LIGHT);
Tony Wickham345bff32016-09-28 15:34:51 -0700505 int oldSystemUiFlags = getWindow().getDecorView().getSystemUiVisibility();
506 int newSystemUiFlags = oldSystemUiFlags;
Tony Wickham93113872016-09-14 16:46:32 -0700507 if (lightStatusBar) {
Tony Wickham345bff32016-09-28 15:34:51 -0700508 newSystemUiFlags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
Tony Wickham93113872016-09-14 16:46:32 -0700509 } else {
Tony Wickham345bff32016-09-28 15:34:51 -0700510 newSystemUiFlags &= ~(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Tony Wickham93113872016-09-14 16:46:32 -0700511 }
Tony Wickham345bff32016-09-28 15:34:51 -0700512 if (newSystemUiFlags != oldSystemUiFlags) {
513 getWindow().getDecorView().setSystemUiVisibility(newSystemUiFlags);
514 }
Tony Wickham93113872016-09-14 16:46:32 -0700515 }
516
Adam Cohen9211d422014-10-07 18:14:53 -0700517 private LauncherCallbacks mLauncherCallbacks;
518
519 public void onPostCreate(Bundle savedInstanceState) {
520 super.onPostCreate(savedInstanceState);
521 if (mLauncherCallbacks != null) {
522 mLauncherCallbacks.onPostCreate(savedInstanceState);
523 }
524 }
525
Winson1f064272016-07-18 17:18:02 -0700526 public void onInsetsChanged(Rect insets) {
527 mDeviceProfile.updateInsets(insets);
528 mDeviceProfile.layout(this, true /* notifyListeners */);
529 }
530
Sunny Goyal32554d12015-12-03 15:31:25 -0800531 /**
532 * Call this after onCreate to set or clear overlay.
533 */
534 public void setLauncherOverlay(LauncherOverlay overlay) {
535 if (overlay != null) {
536 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
537 }
538 mWorkspace.setLauncherOverlay(overlay);
539 }
540
Adam Cohen9211d422014-10-07 18:14:53 -0700541 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
542 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700543 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700544 private boolean mWorkspaceImportanceStored = false;
545 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700546 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800547 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700548 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
549
550 @Override
551 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700552 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700553 return;
554 }
555 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700556 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700557 if (mWorkspace != null) {
558 mWorkspaceImportanceForAccessibility =
559 mWorkspace.getImportantForAccessibility();
560 mWorkspace.setImportantForAccessibility(
561 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
562 mWorkspaceImportanceStored = true;
563 }
564 if (mHotseat != null) {
565 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
566 mHotseat.setImportantForAccessibility(
567 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
568 mHotseatImportanceStored = true;
569 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700570 }
571
572 @Override
573 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700574 if (mWorkspaceImportanceStored && mWorkspace != null) {
575 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
576 }
577 if (mHotseatImportanceStored && mHotseat != null) {
578 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
579 }
580 mWorkspaceImportanceStored = false;
581 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700582 }
583 });
Adam Cohen9211d422014-10-07 18:14:53 -0700584 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700585 }
586
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700587 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700588 public void onLauncherProviderChange() {
589 if (mLauncherCallbacks != null) {
590 mLauncherCallbacks.onLauncherProviderChange();
591 }
592 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700593
Adam Cohen9211d422014-10-07 18:14:53 -0700594 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700595 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700596 if (mLauncherCallbacks != null) {
597 return mLauncherCallbacks.hasCustomContentToLeft();
598 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700599 return false;
600 }
601
Dave Hawkeya8881582013-09-17 15:55:33 -0600602 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500603 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600604 * {@link #addToCustomContentPage}. This will only be invoked if
605 * {@link #hasCustomContentToLeft()} is {@code true}.
606 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500607 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700608 if (mLauncherCallbacks != null) {
609 mLauncherCallbacks.populateCustomContentContainer();
610 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600611 }
612
613 /**
614 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
615 * ensure the custom content page is added or removed if necessary.
616 */
617 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600618 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600619 // Not bound yet, wait for bindScreens to be called.
620 return;
621 }
622
623 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
624 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500625 mWorkspace.createCustomContentContainer();
626 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600627 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
628 mWorkspace.removeCustomContentPage();
629 }
630 }
631
Hyunyoung Songaa953652016-04-19 18:30:24 -0700632 public UserEventDispatcher getUserEventDispatcher() {
633 if (mLauncherCallbacks != null) {
634 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
635 if (dispatcher != null) {
636 return dispatcher;
637 }
638 }
639
Sunny Goyal64976d52016-06-20 14:56:28 -0700640 // Logger object is a singleton and does not have to be coupled with the foreground
641 // activity. Since most user event logging is done on the UI, the object is retrieved
642 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700643 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700644 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700645 }
646 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800647 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100648
Hyunyoung Song3f471442015-04-08 19:01:34 -0700649 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700650 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
651 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700652 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700653 }
654
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000655 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700656 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
657 // This cast is safe as long as the id < 0x00FFFFFF
658 // Since we jail all the dynamically generated views, there should be no clashes
659 // with any other views.
660 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000661 }
662
663 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700664 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
665 * a configuration step, this allows the proper animations to run after other transitions.
666 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700667 private long completeAdd(
668 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
669 long screenId = info.screenId;
670 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700671 // When the screen id represents an actual screen (as opposed to a rank) we make sure
672 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700673 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700674 }
Adam Cohendb364c32014-05-20 14:23:40 -0700675
Sunny Goyal2100c782016-08-22 16:00:03 -0700676 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800677 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700678 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700679 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800680 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700681 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700682 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700683 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700684 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700685 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700686 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700687 int widgetId = appWidgetId;
688 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700689 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700690 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700691 // Since the view was just bound, also launch the configure activity if needed
692 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
693 .getLauncherAppWidgetInfo(widgetId);
694 if (provider != null && provider.configure != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700695 startRestoredWidgetReconfigActivity(provider, widgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -0700696 }
697 }
698 break;
699 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800700 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700701
Adam Cohendb364c32014-05-20 14:23:40 -0700702 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800703 }
704
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800705 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700706 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700707 if (isWorkspaceLoading()) {
708 // process the result once the workspace has loaded.
709 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
710 return;
711 }
712 mPendingActivityResult = null;
713
Winson Chunge341d302013-08-16 14:31:00 -0700714 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700715 final PendingRequestArgs requestArgs = mPendingRequestArgs;
716 setWaitingForResult(null);
717 if (requestArgs == null) {
718 return;
719 }
720
721 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700722
Adam Cohenad4e15c2013-10-17 16:21:35 -0700723 Runnable exitSpringLoaded = new Runnable() {
724 @Override
725 public void run() {
726 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
727 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
728 }
729 };
730
Michael Jurka8b805b12012-04-18 14:23:14 -0700731 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700732 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700733 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700734 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
735 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700736 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700737 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700738 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700739 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700740 addAppWidgetImpl(
741 appWidgetId, requestArgs, null,
742 requestArgs.getWidgetProvider(),
743 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700744 }
745 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200746 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
747 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700748 // User could have free-scrolled between pages before picking a wallpaper; make sure
749 // we move to the closest one now.
750 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700751 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200752 }
753 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700754 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200755
Adam Cohened66b2b2012-01-23 17:28:51 -0800756 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700757 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700758
Adam Cohened66b2b2012-01-23 17:28:51 -0800759 // We have special handling for widgets
760 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800761 final int appWidgetId;
762 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
763 : -1;
764 if (widgetId < 0) {
765 appWidgetId = pendingAddWidgetId;
766 } else {
767 appWidgetId = widgetId;
768 }
769
Adam Cohenad4e15c2013-10-17 16:21:35 -0700770 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800771 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700772 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
773 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700774 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700775 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700776 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700777 @Override
778 public void run() {
779 exitSpringLoadedDragModeDelayed(false, 0, null);
780 }
781 };
Adam Cohendb364c32014-05-20 14:23:40 -0700782
Sunny Goyal2100c782016-08-22 16:00:03 -0700783 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
784 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
785 } else {
786 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
787 // When the screen id represents an actual screen (as opposed to a rank)
788 // we make sure that the drop page actually exists.
789 requestArgs.screenId =
790 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700791 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700792 final CellLayout dropLayout =
793 mWorkspace.getScreenWithId(requestArgs.screenId);
794
795 dropLayout.setDropPending(true);
796 final Runnable onComplete = new Runnable() {
797 @Override
798 public void run() {
799 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
800 dropLayout.setDropPending(false);
801 }
802 };
803 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
804 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700805 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800806 return;
807 }
808
Sunny Goyald478c832016-04-01 12:04:16 -0700809 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
810 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700811 if (resultCode == RESULT_OK) {
812 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700813 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700814 }
815 // Leave the widget in the pending state if the user canceled the configure.
816 return;
817 }
818
Sunny Goyalaad90582015-07-09 14:22:50 -0700819 if (requestCode == REQUEST_CREATE_SHORTCUT) {
820 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700821 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
822 completeAdd(requestCode, data, -1, requestArgs);
823 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
824 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
825
Sunny Goyalaad90582015-07-09 14:22:50 -0700826 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700827 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
828 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800831 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800832 }
833
834 @Override
835 protected void onActivityResult(
836 final int requestCode, final int resultCode, final Intent data) {
837 handleActivityResult(requestCode, resultCode, data);
838 if (mLauncherCallbacks != null) {
839 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
840 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800841 }
842
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600843 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700844 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600845 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700846 PendingRequestArgs pendingArgs = mPendingRequestArgs;
847 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
848 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
849 setWaitingForResult(null);
850
Sunny Goyal28c6b962015-10-12 11:42:05 -0700851 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700852 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700853 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700854 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700855 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700856 Intent intent = pendingArgs.getPendingIntent();
857
Sunny Goyal28c6b962015-10-12 11:42:05 -0700858 if (grantResults.length > 0
859 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700860 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700861 } else {
862 // TODO: Show a snack bar with link to settings
863 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700864 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700865 }
866 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600867 if (mLauncherCallbacks != null) {
868 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
869 grantResults);
870 }
871 }
872
Adam Cohendb364c32014-05-20 14:23:40 -0700873 /**
874 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
875 *
876 * @param screenId the screen id to check
877 * @return the new screen, or screenId if it exists
878 */
879 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800880 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700881 if (dropLayout == null) {
882 // it's possible that the add screen was removed because it was
883 // empty and a re-bind occurred
884 mWorkspace.addExtraEmptyScreen();
885 return mWorkspace.commitExtraEmptyScreen();
886 } else {
887 return screenId;
888 }
889 }
890
Sunny Goyal2100c782016-08-22 16:00:03 -0700891 @Thunk void completeTwoStageWidgetDrop(
892 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
893 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800894 Runnable onCompleteRunnable = null;
895 int animationType = 0;
896
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700897 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800898 if (resultCode == RESULT_OK) {
899 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
900 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal2100c782016-08-22 16:00:03 -0700901 requestArgs.getWidgetProvider());
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700902 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800903 onCompleteRunnable = new Runnable() {
904 @Override
905 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700906 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700907 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
908 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800909 }
910 };
911 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800912 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800913 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800914 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700915 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700916 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700917 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
918 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700919 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700920 // The animated view may be null in the case of a rotation during widget configuration
921 onCompleteRunnable.run();
922 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 }
924
925 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700926 protected void onStop() {
927 super.onStop();
928 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700929
930 if (mLauncherCallbacks != null) {
931 mLauncherCallbacks.onStop();
932 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700933
934 if (Utilities.isNycMR1OrAbove()) {
935 mAppWidgetHost.stopListening();
936 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700937 }
938
939 @Override
940 protected void onStart() {
941 super.onStart();
942 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700943
944 if (mLauncherCallbacks != null) {
945 mLauncherCallbacks.onStart();
946 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700947
948 if (Utilities.isNycMR1OrAbove()) {
949 mAppWidgetHost.startListening();
950 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700951 }
952
953 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200955 long startTime = 0;
956 if (DEBUG_RESUME_TIME) {
957 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400958 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200959 }
Adam Cohen9211d422014-10-07 18:14:53 -0700960
961 if (mLauncherCallbacks != null) {
962 mLauncherCallbacks.preOnResume();
963 }
964
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700966 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700967
Winson Chung4a2afa32012-07-19 14:53:05 -0700968 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700969 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700970 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800971 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700972 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700973 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700974 // view after launching an app, as they may be depending on the UI to be static to
975 // switch to another app, otherwise, if it was
Hyunyoung Songdd60ce42016-07-27 17:08:38 -0700976 showAppsView(false /* animated */, !launchedFromApp /* updatePredictedApps */,
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700977 mAppsView.shouldRestoreImeState() /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800978 } else if (mOnResumeState == State.WIDGETS) {
979 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700980 }
981 mOnResumeState = State.NONE;
982
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800983 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700984 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700985 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700986 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700987 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700989 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200990 // We might have postponed some bind calls until onResume (see waitUntilResume) --
991 // execute them here
992 long startTimeCallbacks = 0;
993 if (DEBUG_RESUME_TIME) {
994 startTimeCallbacks = System.currentTimeMillis();
995 }
996
Michael Jurka1e2f4652013-07-08 18:03:46 -0700997 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
998 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200999 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001000 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001001 if (DEBUG_RESUME_TIME) {
1002 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1003 (System.currentTimeMillis() - startTimeCallbacks));
1004 }
Michael Jurka447bf842013-05-15 14:52:15 +02001005 }
Michael Jurka54554252013-08-01 12:52:23 +02001006 if (mOnResumeCallbacks.size() > 0) {
1007 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1008 mOnResumeCallbacks.get(i).run();
1009 }
1010 mOnResumeCallbacks.clear();
1011 }
Winson Chunge4e50662012-01-23 14:45:13 -08001012
1013 // Reset the pressed state of icons that were locked in the press state while activities
1014 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001015 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001016 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001017 mWaitingForResume.setStayPressed(false);
1018 }
Winson Chung82963d52013-10-09 11:20:57 -07001019
Adam Cohen06dff352012-06-01 17:17:08 -07001020 // It is possible that widgets can receive updates while launcher is not in the foreground.
1021 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1022 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1023 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001024 if (!isWorkspaceLoading()) {
1025 getWorkspace().reinflateWidgetsIfNecessary();
1026 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001027
Michael Jurka447bf842013-05-15 14:52:15 +02001028 if (DEBUG_RESUME_TIME) {
1029 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1030 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001031
Adam Cohen4b66bf32015-09-18 12:15:19 -07001032 // We want to suppress callbacks about CustomContent being shown if we have just received
1033 // onNewIntent while the user was present within launcher. In that case, we post a call
1034 // to move the user to the main screen (which will occur after onResume). We don't want to
1035 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1036 // suppress here.
1037 if (mWorkspace.getCustomContentCallbacks() != null
1038 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001039 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001040 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001041 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1042 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001043 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001044 }
1045 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001046 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001047 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001048 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001049
Sunny Goyal756adbc2015-04-16 15:20:51 -07001050 if (!isWorkspaceLoading()) {
1051 // Process any items that were added while Launcher was away.
1052 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001053
1054 // Refresh shortcuts if the permission changed.
1055 mModel.refreshShortcutsIfRequired();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001056 }
Adam Cohen9211d422014-10-07 18:14:53 -07001057
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001058 if (shouldShowDiscoveryBounce()) {
1059 mAllAppsController.showDiscoveryBounce();
1060 }
1061 mIsResumeFromActionScreenOff = false;
Adam Cohen9211d422014-10-07 18:14:53 -07001062 if (mLauncherCallbacks != null) {
1063 mLauncherCallbacks.onResume();
1064 }
Adam Cohen06dff352012-06-01 17:17:08 -07001065 }
1066
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001068 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001069 // Ensure that items added to Launcher are queued until Launcher returns
1070 InstallShortcutReceiver.enableInstallQueue();
1071
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001072 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001073 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001074 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001075 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001076
1077 // We call onHide() aggressively. The custom content callbacks should be able to
1078 // debounce excess onHide calls.
1079 if (mWorkspace.getCustomContentCallbacks() != null) {
1080 mWorkspace.getCustomContentCallbacks().onHide();
1081 }
Romain Guycbb89e42009-06-08 15:52:54 -07001082
Adam Cohen9211d422014-10-07 18:14:53 -07001083 if (mLauncherCallbacks != null) {
1084 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001085 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001086 }
1087
1088 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001089 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1090 // by a onResume or by scrolling otherwise.
1091 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001092
1093 // Custom content is completely hidden
1094 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001095
1096 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1097 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001098
1099 // Indicates whether the user is allowed to scroll away from the custom content.
1100 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001101 }
1102
Adam Cohenc2d6e892014-10-16 09:49:24 -07001103 public interface LauncherOverlay {
1104
1105 /**
1106 * Touch interaction leading to overscroll has begun
1107 */
1108 public void onScrollInteractionBegin();
1109
1110 /**
1111 * Touch interaction related to overscroll has ended
1112 */
1113 public void onScrollInteractionEnd();
1114
1115 /**
1116 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1117 * screen (or in the case of RTL, the rightmost screen).
1118 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001119 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001120
1121 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001122 * Called when the launcher is ready to use the overlay
1123 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001124 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001125 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001126 }
1127
Jun Mukai8af0cd82015-05-12 12:32:12 -07001128 public interface LauncherSearchCallbacks {
1129 /**
1130 * Called when the search overlay is shown.
1131 */
1132 public void onSearchOverlayOpened();
1133
1134 /**
1135 * Called when the search overlay is dismissed.
1136 */
1137 public void onSearchOverlayClosed();
1138 }
1139
Adam Cohenc2d6e892014-10-16 09:49:24 -07001140 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001141 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001142 }
1143
1144 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1145
Sunny Goyalc86df472016-02-25 09:19:38 -08001146 public void onScrollChanged(float progress) {
1147 if (mWorkspace != null) {
1148 mWorkspace.onOverlayScrollChanged(progress);
1149 }
1150 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001151 }
1152
Jorim Jaggid017f882014-01-14 17:08:48 -08001153 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001154 if (mLauncherCallbacks != null) {
1155 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001156 } else {
1157 // On devices with a locked orientation, we will at least have the allow rotation
1158 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001159 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001160 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001161 }
1162
Adam Cohen9211d422014-10-07 18:14:53 -07001163 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001164 CustomContentCallbacks callbacks, String description) {
1165 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001166 }
1167
Adam Cohenedb40762013-07-18 16:45:45 -07001168 // The custom content needs to offset its content to account for the QSB
1169 public int getTopOffsetForCustomContent() {
1170 return mWorkspace.getPaddingTop();
1171 }
1172
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001173 @Override
1174 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001175 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001176 if (mModel.isCurrentCallbacks(this)) {
1177 mModel.stopLoader();
1178 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001179 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1180
Romain Guy13c2e7b2010-03-10 19:45:00 -08001181 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001182 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001183
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001184 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001185 @Override
1186 public void onWindowFocusChanged(boolean hasFocus) {
1187 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001188 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001189
1190 if (mLauncherCallbacks != null) {
1191 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1192 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001193 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001194
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195 private boolean acceptFilter() {
1196 final InputMethodManager inputManager = (InputMethodManager)
1197 getSystemService(Context.INPUT_METHOD_SERVICE);
1198 return !inputManager.isFullscreenMode();
1199 }
1200
1201 @Override
1202 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001203 final int uniChar = event.getUnicodeChar();
1204 final boolean handled = super.onKeyDown(keyCode, event);
1205 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1206 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1208 keyCode, event);
1209 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001210 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001211 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001212 // showSearchDialog()
1213 // If there are multiple keystrokes before the search dialog takes focus,
1214 // onSearchRequested() will be called for every keystroke,
1215 // but it is idempotent, so it's fine.
1216 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217 }
1218 }
1219
Joe Onorato8a9625e2010-01-28 15:55:35 -08001220 // Eat the long press event so the keyboard doesn't come up.
1221 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1222 return true;
1223 }
1224
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 return handled;
1226 }
1227
Winson46163472015-09-22 17:31:56 -07001228 @Override
1229 public boolean onKeyUp(int keyCode, KeyEvent event) {
1230 if (keyCode == KeyEvent.KEYCODE_MENU) {
1231 // Ignore the menu key if we are currently dragging or are on the custom content screen
1232 if (!isOnCustomContent() && !mDragController.isDragging()) {
1233 // Close any open folders
1234 closeFolder();
1235
Tony Wickham49c8d292016-07-01 11:44:34 -07001236 // Close any shortcuts containers
1237 closeShortcutsContainer();
1238
Winson46163472015-09-22 17:31:56 -07001239 // Stop resizing any widgets
1240 mWorkspace.exitWidgetResizeMode();
1241
1242 // Show the overview mode if we are on the workspace
1243 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1244 !mWorkspace.isSwitchingState()) {
1245 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001246 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001247 }
1248 }
1249 return true;
1250 }
1251 return super.onKeyUp(keyCode, event);
1252 }
1253
Karl Rosaen138a0412009-04-23 19:00:21 -07001254 private String getTypedText() {
1255 return mDefaultKeySsb.toString();
1256 }
1257
Sunny Goyal6f28e712016-09-13 14:19:29 -07001258 @Override
1259 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001260 mDefaultKeySsb.clear();
1261 mDefaultKeySsb.clearSpans();
1262 Selection.setSelection(mDefaultKeySsb, 0);
1263 }
1264
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001266 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1267 * State
1268 */
1269 private static State intToState(int stateOrdinal) {
1270 State state = State.WORKSPACE;
1271 final State[] stateValues = State.values();
1272 for (int i = 0; i < stateValues.length; i++) {
1273 if (stateValues[i].ordinal() == stateOrdinal) {
1274 state = stateValues[i];
1275 break;
1276 }
1277 }
1278 return state;
1279 }
1280
1281 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 * Restores the previous state, if it exists.
1283 *
1284 * @param savedState The previous state.
1285 */
1286 private void restoreState(Bundle savedState) {
1287 if (savedState == null) {
1288 return;
1289 }
1290
Michael Jurka883f55b2010-10-21 15:47:14 -07001291 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001292 if (state == State.APPS || state == State.WIDGETS) {
1293 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001294 }
1295
Adam Cohen21cd0022013-10-09 18:57:02 -07001296 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1297 PagedView.INVALID_RESTORE_PAGE);
1298 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001299 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 }
1301
Sunny Goyal2100c782016-08-22 16:00:03 -07001302 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1303 if (requestArgs != null) {
1304 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001306
1307 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 }
1309
1310 /**
1311 * Finds all the views we need and configure them properly.
1312 */
1313 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001314 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001315 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001316 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001317 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyal6178f132016-07-11 17:30:03 -07001318 mQsbContainer = mDragLayer.findViewById(mDeviceProfile.isVerticalBarLayout()
1319 ? R.id.workspace_blocked_row : R.id.qsb_container);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001320 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001321
Sunny Goyal784f9c32016-03-23 16:56:54 -07001322 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1323 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1324 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325
Winson Chung4c98d922011-05-31 16:50:48 -07001326 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001327 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001328
Winson Chung3d503fb2011-07-13 17:25:49 -07001329 // Setup the hotseat
1330 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1331 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001332 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001333 }
1334
Winsone9f27272015-10-13 10:47:51 -07001335 // Setup the overview panel
1336 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001337
Winson Chung4c98d922011-05-31 16:50:48 -07001338 // Setup the workspace
1339 mWorkspace.setHapticFeedbackEnabled(false);
1340 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001341 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001342 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1343 // default state, otherwise we will update to the wrong offsets in RTL
1344 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001345 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001346 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001347
Tony Wickham34d2c912015-09-11 09:27:58 -07001348 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001349 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001350
Winson Chungef7f8742015-06-04 17:18:17 -07001351 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001352 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001353 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001354 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1355 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1356 } else {
1357 mAppsView.setSearchBarController(new DefaultAppSearchController());
1358 }
Craig Mautner360310b2012-10-26 15:13:08 -07001359
Winson Chung3d503fb2011-07-13 17:25:49 -07001360 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001361 mDragController.setDragScoller(mWorkspace);
1362 mDragController.setScrollView(mDragLayer);
1363 mDragController.setMoveTarget(mWorkspace);
1364 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001365 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001366
Peter Schiller310a9882016-06-30 13:52:36 -07001367 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1368 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1369 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001370
Sunny Goyal322d5562015-06-25 19:35:49 -07001371 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1372 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001373 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 }
1375
Winsone9f27272015-10-13 10:47:51 -07001376 private void setupOverviewPanel() {
1377 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1378
1379 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1380 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1381 @Override
1382 public boolean onLongClick(View v) {
1383 return v.performClick();
1384 }
1385 };
1386
1387 // Bind wallpaper button actions
1388 View wallpaperButton = findViewById(R.id.wallpaper_button);
1389 wallpaperButton.setOnClickListener(new OnClickListener() {
1390 @Override
1391 public void onClick(View view) {
1392 if (!mWorkspace.isSwitchingState()) {
1393 onClickWallpaperPicker(view);
1394 }
1395 }
1396 });
1397 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1398 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1399
1400 // Bind widget button actions
1401 mWidgetsButton = findViewById(R.id.widget_button);
1402 mWidgetsButton.setOnClickListener(new OnClickListener() {
1403 @Override
1404 public void onClick(View view) {
1405 if (!mWorkspace.isSwitchingState()) {
1406 onClickAddWidgetButton(view);
1407 }
1408 }
1409 });
1410 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1411 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1412
1413 // Bind settings actions
1414 View settingsButton = findViewById(R.id.settings_button);
1415 boolean hasSettings = hasSettings();
1416 if (hasSettings) {
1417 settingsButton.setOnClickListener(new OnClickListener() {
1418 @Override
1419 public void onClick(View view) {
1420 if (!mWorkspace.isSwitchingState()) {
1421 onClickSettingsButton(view);
1422 }
1423 }
1424 });
1425 settingsButton.setOnLongClickListener(performClickOnLongClick);
1426 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1427 } else {
1428 settingsButton.setVisibility(View.GONE);
1429 }
1430
1431 mOverviewPanel.setAlpha(0f);
1432 }
1433
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001435 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001436 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001437 */
1438 public void setAllAppsButton(View allAppsButton) {
1439 mAllAppsButton = allAppsButton;
1440 }
1441
Sunny Goyalbb011da2016-06-15 15:42:29 -07001442 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001443 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001444 }
1445
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001446 public View getWidgetsButton() {
1447 return mWidgetsButton;
1448 }
1449
Anjali Koppal5ad44842014-03-10 20:34:39 -07001450 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 * Creates a view representing a shortcut.
1452 *
1453 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001455 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001456 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 }
1458
1459 /**
1460 * Creates a view representing a shortcut inflated from the specified resource.
1461 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 * @param parent The group the shortcut belongs to.
1463 * @param info The data structure describing the shortcut.
1464 *
1465 * @return A View inflated from layoutResId.
1466 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001467 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001468 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001469 parent, false);
1470 favorite.applyFromShortcutInfo(info, mIconCache);
1471 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001473 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 return favorite;
1475 }
1476
1477 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 * Add a shortcut to the workspace.
1479 *
1480 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001482 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001483 int cellY, PendingRequestArgs args) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001484 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001485 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001486
Sunny Goyal5c97f512015-05-19 16:03:28 -07001487 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001488 if (info == null || args.getRequestCode() != REQUEST_CREATE_SHORTCUT ||
1489 args.getPendingIntent().getComponent() == null) {
1490 return;
1491 }
1492 if (!PackageManagerHelper.hasPermissionForActivity(
1493 this, info.intent, args.getPendingIntent().getComponent().getPackageName())) {
1494 // The app is trying to add a shortcut without sufficient permissions
1495 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
Adam Cohend9198822011-11-22 16:42:47 -08001496 return;
1497 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001498 final View view = createShortcut(info);
1499
Sunny Goyal5c97f512015-05-19 16:03:28 -07001500 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001501 // First we check if we already know the exact location where we want to add this item.
1502 if (cellX >= 0 && cellY >= 0) {
1503 cellXY[0] = cellX;
1504 cellXY[1] = cellY;
1505 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001506
1507 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001508 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001509 true, null,null)) {
1510 return;
1511 }
1512 DragObject dragObject = new DragObject();
1513 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001514 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1515 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001516 return;
1517 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001518 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001519 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001520 }
1521
1522 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001523 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001524 return;
1525 }
1526
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001527 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528
Sunny Goyal2100c782016-08-22 16:00:03 -07001529 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
1530 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 }
1532
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001534 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001536 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001538 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001539 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1540
Adam Cohened66b2b2012-01-23 17:28:51 -08001541 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001542 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001543 }
Romain Guycbb89e42009-06-08 15:52:54 -07001544
Adam Cohen59400422014-03-05 18:07:04 -08001545 if (appWidgetInfo.isCustomWidget) {
1546 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001547 }
1548
Adam Cohen59400422014-03-05 18:07:04 -08001549 LauncherAppWidgetInfo launcherInfo;
1550 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001551 launcherInfo.spanX = itemInfo.spanX;
1552 launcherInfo.spanY = itemInfo.spanY;
1553 launcherInfo.minSpanX = itemInfo.minSpanX;
1554 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001555 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001556
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001558 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559
Sunny Goyal2100c782016-08-22 16:00:03 -07001560 if (hostView == null) {
1561 // Perform actual inflation because we're live
1562 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001564 hostView.setVisibility(View.VISIBLE);
1565 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566 }
Romain Guycbb89e42009-06-08 15:52:54 -07001567
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001568 private void addAppWidgetToWorkspace(
1569 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001570 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001571 hostView.setTag(item);
1572 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001573
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001574 hostView.setFocusable(true);
1575 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001576
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001577 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001578 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1579
1580 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001581 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001582 }
1583 }
1584
Adam Cohended9f8d2010-11-03 13:25:16 -07001585 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1586 @Override
1587 public void onReceive(Context context, Intent intent) {
1588 final String action = intent.getAction();
1589 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1590 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001591 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001592 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001593
Winson Chungc100e8e2011-08-09 16:02:43 -07001594 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001595 // processing a multi-step drop
Sunny Goyal2100c782016-08-22 16:00:03 -07001596 if (mAppsView != null && mWidgetsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001597 if (!showWorkspace(false)) {
1598 // If we are already on the workspace, then manually reset all apps
1599 mAppsView.reset();
1600 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001601 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001602 mIsResumeFromActionScreenOff = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001603 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1604 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001605 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001606 }
1607 }
1608 };
1609
1610 @Override
1611 public void onAttachedToWindow() {
1612 super.onAttachedToWindow();
1613
1614 // Listen for broadcasts related to user-presence
1615 final IntentFilter filter = new IntentFilter();
1616 filter.addAction(Intent.ACTION_SCREEN_OFF);
1617 filter.addAction(Intent.ACTION_USER_PRESENT);
1618 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001619 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001620 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001621 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001622
1623 if (mLauncherCallbacks != null) {
1624 mLauncherCallbacks.onAttachedToWindow();
1625 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001626 }
1627
1628 @Override
1629 public void onDetachedFromWindow() {
1630 super.onDetachedFromWindow();
1631 mVisible = false;
1632
Adam Cohend113e0c2010-11-11 10:48:05 -08001633 if (mAttached) {
1634 unregisterReceiver(mReceiver);
1635 mAttached = false;
1636 }
Winson Chungb745afb2015-03-02 11:51:23 -08001637 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001638
1639 if (mLauncherCallbacks != null) {
1640 mLauncherCallbacks.onDetachedFromWindow();
1641 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001642 }
1643
1644 public void onWindowVisibilityChanged(int visibility) {
1645 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001646 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001647 // The following code used to be in onResume, but it turns out onResume is called when
1648 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1649 // is a more appropriate event to handle
1650 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001651 if (!mWorkspaceLoading) {
1652 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001653 // We want to let Launcher draw itself at least once before we force it to build
1654 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001655 // apps is nice and speedy.
1656 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001657 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001658 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001659 if (mStarted) return;
1660 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001661 // We delay the layer building a bit in order to give
1662 // other message processing a time to run. In particular
1663 // this avoids a delay in hiding the IME if it was
1664 // currently shown, because doing that may involve
1665 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001666 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001667 final ViewTreeObserver.OnDrawListener listener = this;
1668 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001669 public void run() {
1670 if (mWorkspace != null &&
1671 mWorkspace.getViewTreeObserver() != null) {
1672 mWorkspace.getViewTreeObserver().
1673 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001674 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001675 }
1676 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001677 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001678 }
1679 });
1680 }
1681 clearTypedText();
1682 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001683 }
1684
Adam Cohen091440a2015-03-18 14:16:05 -07001685 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001686 mHandler.removeMessages(ADVANCE_MSG);
1687 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1688 mHandler.sendMessageDelayed(msg, delay);
1689 mAutoAdvanceSentTime = System.currentTimeMillis();
1690 }
1691
Adam Cohen091440a2015-03-18 14:16:05 -07001692 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001693 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1694 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1695 mAutoAdvanceRunning = autoAdvanceRunning;
1696 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001697 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001698 sendAdvanceMessage(delay);
1699 } else {
1700 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001701 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001702 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1703 }
1704 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001705 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001706 }
1707 }
1708 }
1709
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001710 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1711
Adam Cohended9f8d2010-11-03 13:25:16 -07001712 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001713 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001714 if (msg.what == ADVANCE_MSG) {
1715 int i = 0;
1716 for (View key: mWidgetsToAdvance.keySet()) {
1717 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001718 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001719 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001720 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001721 public void run() {
1722 ((Advanceable) v).advance();
1723 }
1724 }, delay);
1725 }
1726 i++;
1727 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001728 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001729 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001730 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001731 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001732 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001733
Winsonc0b52fe2015-09-09 16:38:15 -07001734 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001735 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001736 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1737 if (v instanceof Advanceable) {
1738 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001739 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001740 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001741 }
1742 }
1743
Winsonc0b52fe2015-09-09 16:38:15 -07001744 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001745 if (mWidgetsToAdvance.containsKey(hostView)) {
1746 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001747 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001748 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001749 }
1750
Hyunyoung Song3f471442015-04-08 19:01:34 -07001751 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001752 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1753 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001754 }
1755
Winson Chunga6945242014-01-08 14:04:34 -08001756 public DragLayer getDragLayer() {
1757 return mDragLayer;
1758 }
1759
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001760 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001761 return mAppsView;
1762 }
1763
Hyunyoung Song3f471442015-04-08 19:01:34 -07001764 public WidgetsContainerView getWidgetsView() {
1765 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001766 }
1767
Winson Chunga6945242014-01-08 14:04:34 -08001768 public Workspace getWorkspace() {
1769 return mWorkspace;
1770 }
1771
Sunny Goyal6178f132016-07-11 17:30:03 -07001772 public View getQsbContainer() {
1773 return mQsbContainer;
1774 }
1775
Winson Chunga6945242014-01-08 14:04:34 -08001776 public Hotseat getHotseat() {
1777 return mHotseat;
1778 }
1779
Jorim Jaggid017f882014-01-14 17:08:48 -08001780 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001781 return mOverviewPanel;
1782 }
1783
Sunny Goyal47328fd2016-05-25 18:56:41 -07001784 public DropTargetBar getDropTargetBar() {
1785 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001786 }
1787
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001788 public LauncherAppWidgetHost getAppWidgetHost() {
1789 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 }
Romain Guycbb89e42009-06-08 15:52:54 -07001791
Winson Chunga9abd0e2010-10-27 17:18:37 -07001792 public LauncherModel getModel() {
1793 return mModel;
1794 }
1795
Adam Cohen79d90c52016-04-22 13:29:20 -07001796 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001797 return mSharedPrefs;
1798 }
1799
Adam Cohen2e6da152015-05-06 11:42:25 -07001800 public DeviceProfile getDeviceProfile() {
1801 return mDeviceProfile;
1802 }
1803
Bjorn Bringertc459e522013-06-07 19:36:01 +01001804 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001805 getWindow().closeAllPanels();
1806
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001807 // Whatever we were doing is hereby canceled.
Sunny Goyal2100c782016-08-22 16:00:03 -07001808 setWaitingForResult(null);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001809 }
1810
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811 @Override
1812 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001813 long startTime = 0;
1814 if (DEBUG_RESUME_TIME) {
1815 startTime = System.currentTimeMillis();
1816 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 super.onNewIntent(intent);
1818
Winson15f8b172015-08-19 11:02:39 -07001819 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1820 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1821 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001822
1823 // Check this condition before handling isActionMain, as this will get reset.
1824 boolean shouldMoveToDefaultScreen = alreadyOnHome &&
1825 mState == State.WORKSPACE && getTopFloatingView() == null;
1826
Winson15f8b172015-08-19 11:02:39 -07001827 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1828 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001829 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001830 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001831
Adam Cohen6fecd412013-10-02 17:41:50 -07001832 if (mWorkspace == null) {
1833 // Can be cases where mWorkspace is null, this prevents a NPE
1834 return;
1835 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001836 // In all these cases, only animate if we're already on home
1837 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001838
Sunny Goyal935fca12015-10-13 10:19:01 -07001839 closeFolder(alreadyOnHome);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001840 closeShortcutsContainer(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001841 exitSpringLoadedDragMode();
1842
1843 // If we are already on home, then just animate back to the workspace,
1844 // otherwise, just wait until onResume to set the state back to Workspace
1845 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001846 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001847 } else {
1848 mOnResumeState = State.WORKSPACE;
1849 }
1850
1851 final View v = getWindow().peekDecorView();
1852 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001853 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001854 INPUT_METHOD_SERVICE);
1855 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1856 }
1857
Winson Chungb745afb2015-03-02 11:51:23 -08001858 // Reset the apps view
1859 if (!alreadyOnHome && mAppsView != null) {
1860 mAppsView.scrollToTop();
1861 }
1862
Hyunyoung Song3f471442015-04-08 19:01:34 -07001863 // Reset the widgets view
1864 if (!alreadyOnHome && mWidgetsView != null) {
1865 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001866 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001867
Adam Cohen9211d422014-10-07 18:14:53 -07001868 if (mLauncherCallbacks != null) {
1869 mLauncherCallbacks.onHomeIntent();
1870 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871 }
Adam Cohened307df2013-10-02 09:37:31 -07001872
Adam Cohen9211d422014-10-07 18:14:53 -07001873 if (mLauncherCallbacks != null) {
1874 mLauncherCallbacks.onNewIntent(intent);
1875 }
Winson15f8b172015-08-19 11:02:39 -07001876
1877 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1878 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1879 // animation.
1880 if (isActionMain) {
Sunny Goyal85313732016-09-28 12:00:07 -07001881 boolean callbackAllowsMoveToDefaultScreen = mLauncherCallbacks != null ?
Ivan Lee667d6882015-09-03 10:16:07 -06001882 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Sunny Goyal85313732016-09-28 12:00:07 -07001883 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()
1884 && callbackAllowsMoveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001885
1886 // We use this flag to suppress noisy callbacks above custom content state
1887 // from onResume.
1888 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001889 mWorkspace.post(new Runnable() {
1890 @Override
1891 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001892 if (mWorkspace != null) {
1893 mWorkspace.moveToDefaultScreen(true);
1894 }
Winson15f8b172015-08-19 11:02:39 -07001895 }
1896 });
1897 }
1898 }
1899
1900 if (DEBUG_RESUME_TIME) {
1901 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1902 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001903 }
1904
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001906 public void onRestoreInstanceState(Bundle state) {
1907 super.onRestoreInstanceState(state);
1908 for (int page: mSynchronouslyBoundPages) {
1909 mWorkspace.restoreInstanceStateForChild(page);
1910 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 }
1912
1913 @Override
1914 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001915 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001916 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1917 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001918
Adam Cohen21cd0022013-10-09 18:57:02 -07001919 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001920 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921
Michael Jurka883f55b2010-10-21 15:47:14 -07001922 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001923 // We close any open folder since it will not be re-opened, and we need to make sure
1924 // this state is reflected.
1925 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1926 closeFolder(false);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001927 closeShortcutsContainer(false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001928
Sunny Goyal2100c782016-08-22 16:00:03 -07001929 if (mPendingRequestArgs != null) {
1930 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1931 }
1932 if (mPendingActivityResult != null) {
1933 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934 }
1935
Adam Cohen9211d422014-10-07 18:14:53 -07001936 if (mLauncherCallbacks != null) {
1937 mLauncherCallbacks.onSaveInstanceState(outState);
1938 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001939 }
1940
1941 @Override
1942 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001944
Winson Chunge7a03942011-08-05 15:05:12 -07001945 // Remove all pending runnables
1946 mHandler.removeMessages(ADVANCE_MSG);
1947 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001948 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001949 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001950
Winson Chungcd2b0142011-06-08 16:02:26 -07001951 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001952 // It's possible to receive onDestroy after a new Launcher activity has
1953 // been created. In this case, don't interfere with the new Launcher.
1954 if (mModel.isCurrentCallbacks(this)) {
1955 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001956 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001957 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001958
Sunny Goyal745bad92016-05-02 10:54:12 -07001959 if (mRotationPrefChangeHandler != null) {
1960 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1961 }
1962
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001964 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001966 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001968 mAppWidgetHost = null;
1969
1970 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971
1972 TextKeyListener.getInstance().release();
1973
Tony Wickhame2217252016-03-22 16:34:23 -07001974 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1975 .removeAccessibilityStateChangeListener(this);
1976
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001977 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001978
Michael Jurka2ecf9952012-06-18 12:52:28 -07001979 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001980
1981 if (mLauncherCallbacks != null) {
1982 mLauncherCallbacks.onDestroy();
1983 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984 }
1985
Sunny Goyalae502842016-06-17 08:43:56 -07001986 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1987 return mAccessibilityDelegate;
1988 }
1989
Adam Cohena9cf38f2011-05-02 15:36:58 -07001990 public DragController getDragController() {
1991 return mDragController;
1992 }
1993
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001995 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001996 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001997 }
1998
1999 @Override
2000 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2001 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07002002 try {
2003 super.startIntentSenderForResult(intent, requestCode,
2004 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2005 } catch (IntentSender.SendIntentException e) {
2006 throw new ActivityNotFoundException();
2007 }
2008 }
2009
Winson Chung70d72102011-08-12 11:24:35 -07002010 /**
2011 * Indicates that we want global search for this activity by setting the globalSearch
2012 * argument for {@link #startSearch} to true.
2013 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002015 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002016 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002017
Karl Rosaen138a0412009-04-23 19:00:21 -07002018 if (initialQuery == null) {
2019 // Use any text typed in the launcher as the initial query
2020 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002021 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 if (appSearchData == null) {
2023 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002024 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002026
Sunny Goyal6f28e712016-09-13 14:19:29 -07002027 if (mLauncherCallbacks == null ||
2028 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
2029 // Starting search from the callbacks failed. Start the default global search.
2030 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01002031 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002032
2033 // We need to show the workspace after starting the search
2034 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002035 }
2036
Ian Parkinson047036e2014-09-01 15:40:53 +01002037 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07002038 * Starts the global search activity. This code is a copied from SearchManager
2039 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07002040 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002041 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002042 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002043 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2044 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2045 if (globalSearchActivity == null) {
2046 Log.w(TAG, "No global search activity found.");
2047 return;
2048 }
2049 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2050 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2051 intent.setComponent(globalSearchActivity);
2052 // Make sure that we have a Bundle to put source in
2053 if (appSearchData == null) {
2054 appSearchData = new Bundle();
2055 } else {
2056 appSearchData = new Bundle(appSearchData);
2057 }
Adam Cohen9211d422014-10-07 18:14:53 -07002058 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002059 if (!appSearchData.containsKey("source")) {
2060 appSearchData.putString("source", getPackageName());
2061 }
2062 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2063 if (!TextUtils.isEmpty(initialQuery)) {
2064 intent.putExtra(SearchManager.QUERY, initialQuery);
2065 }
2066 if (selectInitialQuery) {
2067 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2068 }
2069 intent.setSourceBounds(sourceBounds);
2070 try {
2071 startActivity(intent);
2072 } catch (ActivityNotFoundException ex) {
2073 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2074 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002075 }
2076
Yura4f93ec62014-02-04 14:15:21 +00002077 public boolean isOnCustomContent() {
2078 return mWorkspace.isOnOrMovingToCustomContent();
2079 }
2080
Winson Chung70d72102011-08-12 11:24:35 -07002081 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002082 public boolean onPrepareOptionsMenu(Menu menu) {
2083 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002084 if (mLauncherCallbacks != null) {
2085 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2086 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002087 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002088 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002090 @Override
2091 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002092 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002093 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002094 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002095 }
2096
Joe Onorato9c1289c2009-08-17 11:03:03 -04002097 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07002098 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002099 }
2100
Adam Cohen517a7f52014-03-01 12:12:59 -08002101 public boolean isWorkspaceLoading() {
2102 return mWorkspaceLoading;
2103 }
2104
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002105 private void setWorkspaceLoading(boolean value) {
2106 boolean isLocked = isWorkspaceLocked();
2107 mWorkspaceLoading = value;
2108 if (isLocked != isWorkspaceLocked()) {
2109 onWorkspaceLockedChanged();
2110 }
2111 }
2112
Sunny Goyal2100c782016-08-22 16:00:03 -07002113 private void setWaitingForResult(PendingRequestArgs args) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002114 boolean isLocked = isWorkspaceLocked();
Sunny Goyal2100c782016-08-22 16:00:03 -07002115 mPendingRequestArgs = args;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002116 if (isLocked != isWorkspaceLocked()) {
2117 onWorkspaceLockedChanged();
2118 }
2119 }
2120
Adam Cohen9211d422014-10-07 18:14:53 -07002121 protected void onWorkspaceLockedChanged() {
2122 if (mLauncherCallbacks != null) {
2123 mLauncherCallbacks.onWorkspaceLockedChanged();
2124 }
2125 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002126
Sunny Goyal2100c782016-08-22 16:00:03 -07002127 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
2128 LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002129 if (LOGD) {
2130 Log.d(TAG, "Adding widget from drop");
2131 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002132 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2133 }
2134
Sunny Goyal2100c782016-08-22 16:00:03 -07002135 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
2136 AppWidgetHostView boundWidget, LauncherAppWidgetProviderInfo appWidgetInfo,
Adam Cohen59400422014-03-05 18:07:04 -08002137 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002138 if (appWidgetInfo.configure != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002139 setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, appWidgetInfo, info));
Michael Jurkaaf442092010-06-10 17:01:57 -07002140
Bjorn Bringert7984c942009-12-09 15:38:25 +00002141 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002142 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2143 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002144 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002145 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002146 Runnable onComplete = new Runnable() {
2147 @Override
2148 public void run() {
2149 // Exit spring loaded mode if necessary after adding the widget
2150 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2151 null);
2152 }
2153 };
Sunny Goyal2100c782016-08-22 16:00:03 -07002154 completeAddAppWidget(appWidgetId, info, boundWidget, appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002155 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002156 }
2157 }
Romain Guycbb89e42009-06-08 15:52:54 -07002158
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002159 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002160 // Close any folders that may be open.
2161 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002162 mWorkspace.moveToCustomContentScreen(animate);
2163 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002164
2165 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2166 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002167 info.container = container;
2168 info.screenId = screenId;
2169 if (cell != null) {
2170 info.cellX = cell[0];
2171 info.cellY = cell[1];
2172 }
2173 info.spanX = spanX;
2174 info.spanY = spanY;
2175
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002176 switch (info.itemType) {
2177 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2178 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07002179 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002180 break;
2181 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal2100c782016-08-22 16:00:03 -07002182 processShortcutFromDrop(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002183 break;
2184 default:
2185 throw new IllegalStateException("Unknown item type: " + info.itemType);
2186 }
2187 }
2188
Adam Cohenfbba09b2011-07-18 21:43:05 -07002189 /**
2190 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002191 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002192 private void processShortcutFromDrop(PendingAddItemInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07002193 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
2194 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
2195 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002196 }
2197
Adam Cohenfbba09b2011-07-18 21:43:05 -07002198 /**
2199 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002200 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002201 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002202 AppWidgetHostView hostView = info.boundWidget;
2203 int appWidgetId;
2204 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002205 // In the case where we've prebound the widget, we remove it from the DragLayer
2206 if (LOGD) {
2207 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2208 }
2209 getDragLayer().removeView(hostView);
2210
Adam Cohened66b2b2012-01-23 17:28:51 -08002211 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002212 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002213
2214 // Clear the boundWidget so that it doesn't get destroyed.
2215 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002216 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002217 // In this case, we either need to start an activity to get permission to bind
2218 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002219 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002220 Bundle options = info.bindOptions;
2221
Sunny Goyalffe83f12014-08-14 17:39:34 -07002222 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2223 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002224 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002225 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002226 } else {
Sunny Goyal2100c782016-08-22 16:00:03 -07002227 setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, info.info, info));
Michael Jurka8b805b12012-04-18 14:23:14 -07002228 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2229 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2230 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyal2100c782016-08-22 16:00:03 -07002231 mAppWidgetManager.getUser(info.info)
Sunny Goyalffe83f12014-08-14 17:39:34 -07002232 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002233 // TODO: we need to make sure that this accounts for the options bundle.
2234 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002235 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2236 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002237 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002238 }
2239
Adam Cohendcd297f2013-06-18 13:13:40 -07002240 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002241 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002242 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002243 folderInfo.title = getText(R.string.folder_name);
2244
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002245 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002246 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2247 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248
2249 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002250 FolderIcon newFolder =
2251 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002252 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002253 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002254 // Force measure the new folder icon
2255 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2256 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002257 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002258 }
Romain Guycbb89e42009-06-08 15:52:54 -07002259
Winsonc0b52fe2015-09-09 16:38:15 -07002260 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002261 * Unbinds the view for the specified item, and removes the item and all its children.
2262 *
2263 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002264 * @param itemInfo the {@link ItemInfo} for this view.
2265 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002266 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002267 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002268 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002269 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002270 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2271 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002272 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002273 } else {
2274 mWorkspace.removeWorkspaceItem(v);
2275 }
Winsonc0b52fe2015-09-09 16:38:15 -07002276 if (deleteFromDb) {
2277 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2278 }
2279 } else if (itemInfo instanceof FolderInfo) {
2280 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002281 if (v instanceof FolderIcon) {
2282 ((FolderIcon) v).removeListeners();
2283 }
Winsonc0b52fe2015-09-09 16:38:15 -07002284 mWorkspace.removeWorkspaceItem(v);
2285 if (deleteFromDb) {
2286 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2287 }
2288 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2289 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002290 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002291 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002292 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002293 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002294 }
2295 } else {
2296 return false;
2297 }
2298 return true;
2299 }
2300
2301 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002302 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002303 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002304 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002305 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002306 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002307 // Deleting an app widget ID is a void call but writes to disk before returning
2308 // to the caller...
2309 new AsyncTask<Void, Void, Void>() {
2310 public Void doInBackground(Void ... args) {
2311 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2312 return null;
2313 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002314 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002315 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002316 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002317 }
2318
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002319 @Override
2320 public boolean dispatchKeyEvent(KeyEvent event) {
2321 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2322 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002323 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002324 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002325 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002326 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002327 dumpState();
2328 return true;
2329 }
2330 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002331 }
2332 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2333 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002334 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002335 return true;
2336 }
2337 }
2338
2339 return super.dispatchKeyEvent(event);
2340 }
2341
Joe Onorato88ec0992009-11-19 13:16:06 -08002342 @Override
2343 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002344 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2345 return;
2346 }
2347
Sunny Goyal45478022015-06-08 16:52:41 -07002348 if (mDragController.isDragging()) {
2349 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002350 return;
2351 }
2352
Tony Wickham49c8d292016-07-01 11:44:34 -07002353 if (getOpenShortcutsContainer() != null) {
2354 closeShortcutsContainer();
2355 } else if (isAppsViewVisible()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002356 showWorkspace(true);
2357 } else if (isWidgetsViewVisible()) {
2358 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002359 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002360 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002361 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002362 Folder openFolder = mWorkspace.getOpenFolder();
2363 if (openFolder.isEditingName()) {
2364 openFolder.dismissEditingName();
2365 } else {
2366 closeFolder();
2367 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002368 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002369 mWorkspace.exitWidgetResizeMode();
2370
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002371 // Back button is a no-op here, but give at least some feedback for the button press
2372 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002373 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002374 }
2375
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002376 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002377 * Launches the intent referred by the clicked shortcut.
2378 *
2379 * @param v The view representing the clicked shortcut.
2380 */
2381 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002382 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2383 // view has detached (it's possible for this to happen if the view is removed mid touch).
2384 if (v.getWindowToken() == null) {
2385 return;
2386 }
2387
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002388 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002389 return;
2390 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002391
Adam Cohen1697b792013-09-17 19:08:21 -07002392 if (v instanceof Workspace) {
2393 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002394 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002395 }
2396 return;
2397 }
2398
2399 if (v instanceof CellLayout) {
2400 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002401 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2402 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002403 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002404 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002405 }
2406
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002407 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002408 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002409 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002410 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002411 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002412 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002413 }
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002414 } else if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2415 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002416 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002417 } else if (tag instanceof AppInfo) {
2418 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002419 } else if (tag instanceof LauncherAppWidgetInfo) {
2420 if (v instanceof PendingAppWidgetHostView) {
2421 onClickPendingWidget((PendingAppWidgetHostView) v);
2422 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002423 }
2424 }
2425
Sunny Goyal70660032015-05-14 00:07:08 -07002426 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002427 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002428 return false;
2429 }
2430
Michael Jurkaaf442092010-06-10 17:01:57 -07002431 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002432 * Event handler for the app widget view which has not fully restored.
2433 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002434 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002435 if (mIsSafeModeEnabled) {
2436 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2437 return;
2438 }
2439
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002440 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002441 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002442 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2443 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2444 // This should not happen, as we make sure that an Id is allocated during bind.
2445 return;
2446 }
2447 LauncherAppWidgetProviderInfo appWidgetInfo =
2448 mAppWidgetManager.findProvider(info.providerName, info.user);
2449 if (appWidgetInfo != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002450 setWaitingForResult(PendingRequestArgs
2451 .forWidgetInfo(info.appWidgetId, appWidgetInfo, info));
Sunny Goyalff572272014-07-23 13:58:07 -07002452
Sunny Goyald478c832016-04-01 12:04:16 -07002453 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
Sunny Goyal2100c782016-08-22 16:00:03 -07002454 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, info.appWidgetId);
Sunny Goyald478c832016-04-01 12:04:16 -07002455 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07002456 mAppWidgetManager.getUser(appWidgetInfo)
Sunny Goyald478c832016-04-01 12:04:16 -07002457 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2458 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2459 }
2460 } else {
2461 LauncherAppWidgetProviderInfo appWidgetInfo =
2462 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2463 if (appWidgetInfo != null) {
2464 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2465 }
Sunny Goyalff572272014-07-23 13:58:07 -07002466 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002467 } else if (info.installProgress < 0) {
2468 // The install has not been queued
2469 final String packageName = info.providerName.getPackageName();
2470 showBrokenAppInstallDialog(packageName,
2471 new DialogInterface.OnClickListener() {
2472 public void onClick(DialogInterface dialog, int id) {
2473 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2474 }
2475 });
2476 } else {
2477 // Download has started.
2478 final String packageName = info.providerName.getPackageName();
2479 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002480 }
2481 }
2482
Sunny Goyald478c832016-04-01 12:04:16 -07002483 private void startRestoredWidgetReconfigActivity(
2484 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002485 setWaitingForResult(PendingRequestArgs.forWidgetInfo(info.appWidgetId, provider, info));
Sunny Goyald478c832016-04-01 12:04:16 -07002486 mAppWidgetManager.startConfigActivity(provider,
2487 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2488 }
2489
Sunny Goyalff572272014-07-23 13:58:07 -07002490 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002491 * Event handler for the "grid" button that appears on the home screen, which
2492 * enters all apps mode.
2493 *
2494 * @param v The view that was clicked.
2495 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002496 protected void onClickAllAppsButton(View v) {
2497 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002498 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002499 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.TAP,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002500 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002501 showAppsView(true /* animated */, true /* updatePredictedApps */,
2502 false /* focusSearchBar */);
Winson Chung76648c52015-07-10 14:33:23 -07002503 }
2504 }
2505
2506 protected void onLongClickAllAppsButton(View v) {
2507 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2508 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002509 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.LONGPRESS,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002510 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002511 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07002512 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002513 }
2514 }
2515
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002516 private void showBrokenAppInstallDialog(final String packageName,
2517 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002518 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002519 .setTitle(R.string.abandoned_promises_title)
2520 .setMessage(R.string.abandoned_promise_explanation)
2521 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2522 .setNeutralButton(R.string.abandoned_clean_this,
2523 new DialogInterface.OnClickListener() {
2524 public void onClick(DialogInterface dialog, int id) {
2525 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2526 mWorkspace.removeAbandonedPromise(packageName, user);
2527 }
2528 })
2529 .create().show();
2530 return;
2531 }
2532
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002533 /**
2534 * Event handler for an app shortcut click.
2535 *
2536 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2537 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002538 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002539 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2540 Object tag = v.getTag();
2541 if (!(tag instanceof ShortcutInfo)) {
2542 throw new IllegalArgumentException("Input must be a Shortcut");
2543 }
2544
2545 // Open shortcut
2546 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002547
2548 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002549 if ((shortcut.isDisabled &
2550 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2551 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2552 // If the app is only disabled because of the above flags, launch activity anyway.
2553 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002554 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002555 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2556 // Use a message specific to this shortcut, if it has one.
2557 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2558 return;
2559 }
2560 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002561 int error = R.string.activity_not_available;
2562 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2563 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002564 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2565 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002566 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002567 }
2568 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2569 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002570 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002571 }
2572
Chris Wren40c5ed32014-06-24 18:24:23 -04002573 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002574 if ((v instanceof BubbleTextView)
2575 && shortcut.isPromise()
2576 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002577 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002578 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002579 new DialogInterface.OnClickListener() {
2580 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002581 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002582 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002583 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002584 return;
2585 }
2586
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002587 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002588 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002589 }
2590
Sunny Goyala7ce1662016-05-31 15:01:35 -07002591 private void startAppShortcutOrInfoActivity(View v) {
2592 ItemInfo item = (ItemInfo) v.getTag();
2593 Intent intent = item.getIntent();
2594 if (intent == null) {
2595 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002596 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002597 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002598 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002599
2600 if (success && v instanceof BubbleTextView) {
2601 mWaitingForResume = (BubbleTextView) v;
2602 mWaitingForResume.setStayPressed(true);
2603 }
2604 }
2605
2606 /**
2607 * Event handler for a folder icon click.
2608 *
2609 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2610 */
2611 protected void onClickFolderIcon(View v) {
2612 if (LOGD) Log.d(TAG, "onClickFolder");
2613 if (!(v instanceof FolderIcon)){
2614 throw new IllegalArgumentException("Input must be a FolderIcon");
2615 }
2616
2617 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002618 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002619 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002620 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002621 }
Michael Jurka5130e402011-10-13 04:55:35 -07002622 }
2623
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002624 /**
2625 * Event handler for the (Add) Widgets button that appears after a long press
2626 * on the home screen.
2627 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002628 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002629 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002630 if (mIsSafeModeEnabled) {
2631 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2632 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002633 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002634 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002635 }
2636
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002637 /**
2638 * Event handler for the wallpaper picker button that appears after a long press
2639 * on the home screen.
2640 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002641 public void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002642 if (!Utilities.isWallapaperAllowed(this)) {
2643 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2644 return;
2645 }
2646
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002647 String pickerPackage = getString(R.string.wallpaper_picker_package);
2648 if (TextUtils.isEmpty(pickerPackage)) {
2649 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2650 }
2651
Tony Wickham785f7a52015-08-31 17:28:32 -07002652 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2653 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002654
Sunny Goyal2100c782016-08-22 16:00:03 -07002655 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002656 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002657 .setPackage(pickerPackage)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002658 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
2659 intent.setSourceBounds(getViewBounds(v));
2660 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002661 }
2662
2663 /**
2664 * Event handler for a click on the settings button that appears after a long press
2665 * on the home screen.
2666 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002667 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002668 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002669 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2670 .setPackage(getPackageName());
2671 intent.setSourceBounds(getViewBounds(v));
2672 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002673 }
2674
Adam Cohen61f560d2013-09-30 15:58:20 -07002675 public View.OnTouchListener getHapticFeedbackTouchListener() {
2676 if (mHapticFeedbackTouchListener == null) {
2677 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002678 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002679 @Override
2680 public boolean onTouch(View v, MotionEvent event) {
2681 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2682 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2683 }
2684 return false;
2685 }
2686 };
2687 }
2688 return mHapticFeedbackTouchListener;
2689 }
2690
Tony Wickhame2217252016-03-22 16:34:23 -07002691 @Override
2692 public void onAccessibilityStateChanged(boolean enabled) {
2693 mDragLayer.onAccessibilityStateChanged(enabled);
2694 }
2695
Sunny Goyal06e21a22016-08-11 16:02:02 -07002696 public void onDragStarted() {
Adam Cohen9211d422014-10-07 18:14:53 -07002697 if (isOnCustomContent()) {
2698 // Custom content screen doesn't participate in drag and drop. If on custom
2699 // content screen, move to default.
2700 moveWorkspaceToDefaultScreen();
2701 }
Adam Cohen9211d422014-10-07 18:14:53 -07002702 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002703
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002704 /**
2705 * Called when the user stops interacting with the launcher.
2706 * This implies that the user is now on the homescreen and is not doing housekeeping.
2707 */
Adam Cohen9211d422014-10-07 18:14:53 -07002708 protected void onInteractionEnd() {
2709 if (mLauncherCallbacks != null) {
2710 mLauncherCallbacks.onInteractionEnd();
2711 }
2712 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002713
2714 /**
2715 * Called when the user starts interacting with the launcher.
2716 * The possible interactions are:
2717 * - open all apps
2718 * - reorder an app shortcut, or a widget
2719 * - open the overview mode.
2720 * This is a good time to stop doing things that only make sense
2721 * when the user is on the homescreen and not doing housekeeping.
2722 */
Adam Cohen9211d422014-10-07 18:14:53 -07002723 protected void onInteractionBegin() {
2724 if (mLauncherCallbacks != null) {
2725 mLauncherCallbacks.onInteractionBegin();
2726 }
2727 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002728
Winson Chungcd99cd32015-04-29 11:03:24 -07002729 /** Updates the interaction state. */
2730 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002731 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002732 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002733 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2734 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002735 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002736 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002737 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002738 onInteractionEnd();
2739 }
2740 }
2741
Sunny Goyala7ce1662016-05-31 15:01:35 -07002742 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002743 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002744 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2745 try {
2746 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2747 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2748 // is enabled by default on NYC.
2749 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2750 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002751
2752 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2753 String id = ((ShortcutInfo) info).getDeepShortcutId();
2754 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002755 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2756 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002757 } else {
2758 // Could be launching some bookkeeping activity
2759 startActivity(intent, optsBundle);
2760 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002761 } finally {
2762 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002763 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002764 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002765 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2766 // corresponding permission. Show the appropriate permission prompt if that
2767 // is the case.
2768 if (intent.getComponent() == null
2769 && Intent.ACTION_CALL.equals(intent.getAction())
2770 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2771 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002772
2773 setWaitingForResult(PendingRequestArgs
2774 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002775 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2776 REQUEST_PERMISSION_CALL_PHONE);
2777 } else {
2778 // No idea why this was thrown.
2779 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002780 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002781 }
2782 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002783
Sunny Goyala7ce1662016-05-31 15:01:35 -07002784 private Bundle getActivityLaunchOptions(View v) {
2785 if (Utilities.ATLEAST_MARSHMALLOW) {
2786 int left = 0, top = 0;
2787 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2788 if (v instanceof TextView) {
2789 // Launch from center of icon, not entire view
2790 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2791 if (icon != null) {
2792 Rect bounds = icon.getBounds();
2793 left = (width - bounds.width()) / 2;
2794 top = v.getPaddingTop();
2795 width = bounds.width();
2796 height = bounds.height();
2797 }
2798 }
2799 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2800 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2801 // On L devices, we use the device default slide-up transition.
2802 // On L MR1 devices, we use a custom version of the slide-up transition which
2803 // doesn't have the delay present in the device default.
2804 return ActivityOptions.makeCustomAnimation(
2805 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2806 }
2807 return null;
2808 }
2809
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002810 private Rect getViewBounds(View v) {
2811 int[] pos = new int[2];
2812 v.getLocationOnScreen(pos);
2813 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2814 }
2815
Sunny Goyala7ce1662016-05-31 15:01:35 -07002816 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002817 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2818 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2819 return false;
2820 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002821 // Only launch using the new animation if the shortcut has not opted out (this is a
2822 // private contract between launcher and may be ignored in the future).
2823 boolean useLaunchAnimation = (v != null) &&
2824 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2825 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2826
2827 UserHandleCompat user = null;
2828 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2829 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2830 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002831 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002832
2833 // Prepare intent
2834 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2835 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002836 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002837 }
2838 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002839 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2840 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07002841 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
2842 && ((ShortcutInfo) item).promisedIntent == null) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002843 // Shortcuts need some special checks due to legacy reasons.
2844 startShortcutIntentSafely(intent, optsBundle, item);
2845 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2846 // Could be launching some bookkeeping activity
2847 startActivity(intent, optsBundle);
2848 } else {
2849 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2850 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2851 }
2852 return true;
2853 } catch (ActivityNotFoundException|SecurityException e) {
2854 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2855 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2856 }
2857 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002858 }
2859
Adam Cohen268c4752012-06-06 17:47:33 -07002860 /**
2861 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2862 * in the DragLayer in the exact absolute location of the original FolderIcon.
2863 */
2864 private void copyFolderIconToImage(FolderIcon fi) {
2865 final int width = fi.getMeasuredWidth();
2866 final int height = fi.getMeasuredHeight();
2867
2868 // Lazy load ImageView, Bitmap and Canvas
2869 if (mFolderIconImageView == null) {
2870 mFolderIconImageView = new ImageView(this);
2871 }
2872 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2873 mFolderIconBitmap.getHeight() != height) {
2874 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2875 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2876 }
2877
2878 DragLayer.LayoutParams lp;
2879 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2880 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2881 } else {
2882 lp = new DragLayer.LayoutParams(width, height);
2883 }
2884
Adam Cohen307fe232012-08-16 17:55:58 -07002885 // The layout from which the folder is being opened may be scaled, adjust the starting
2886 // view size by this scale factor.
2887 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002888 lp.customPosition = true;
2889 lp.x = mRectForFolderAnimation.left;
2890 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002891 lp.width = (int) (scale * width);
2892 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002893
2894 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2895 fi.draw(mFolderIconCanvas);
2896 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002897 if (fi.getFolder() != null) {
2898 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2899 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002900 }
Adam Cohen268c4752012-06-06 17:47:33 -07002901 // Just in case this image view is still in the drag layer from a previous animation,
2902 // we remove it and re-add it.
2903 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2904 mDragLayer.removeView(mFolderIconImageView);
2905 }
2906 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002907 if (fi.getFolder() != null) {
2908 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002909 }
Adam Cohen268c4752012-06-06 17:47:33 -07002910 }
2911
Adam Cohen37b2a492016-04-06 18:36:06 -07002912 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002913 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002914 FolderInfo info = (FolderInfo) fi.getTag();
2915 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2916 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002917 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2918 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002919 }
2920
Adam Cohen268c4752012-06-06 17:47:33 -07002921 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2922 copyFolderIconToImage(fi);
2923 fi.setVisibility(View.INVISIBLE);
2924
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002925 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2926 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002927 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002928 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2929 }
2930 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002931 oa.start();
2932 }
2933
Sunny Goyal935fca12015-10-13 10:19:01 -07002934 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002935 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002936 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002937
Adam Cohen268c4752012-06-06 17:47:33 -07002938 // We remove and re-draw the FolderIcon in-case it has changed
2939 mDragLayer.removeView(mFolderIconImageView);
2940 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08002941
2942 if (cl != null) {
2943 cl.clearFolderLeaveBehind();
2944 }
2945
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002946 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002947 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002948 oa.addListener(new AnimatorListenerAdapter() {
2949 @Override
2950 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002951 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07002952 // Remove the ImageView copy of the FolderIcon and make the original visible.
2953 mDragLayer.removeView(mFolderIconImageView);
2954 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002955 }
2956 }
2957 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002958 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07002959 if (!animate) {
2960 oa.end();
2961 }
Adam Cohen2801caf2011-05-13 20:57:39 -07002962 }
2963
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002964 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002965 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002966 * is animated relative to the specified View. If the View is null, no animation
2967 * is played.
2968 *
Sunny Goyal08442b82015-10-21 17:15:08 -07002969 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002970 */
Adam Cohen37b2a492016-04-06 18:36:06 -07002971 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07002972
Adam Cohenfb91f302012-06-11 15:45:18 -07002973 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07002974 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
2975 if (openFolder != null && openFolder != folder) {
2976 // Close any open folder before opening a folder.
2977 closeFolder();
2978 }
2979
Adam Cohena9cf38f2011-05-02 15:36:58 -07002980 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002981
Adam Cohena9cf38f2011-05-02 15:36:58 -07002982 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002983
Sunny Goyalf4066152015-04-15 09:42:19 -07002984 // While the folder is open, the position of the icon cannot change.
2985 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
2986
Adam Cohen4554ee12011-08-03 16:13:21 -07002987 // Just verify that the folder hasn't already been added to the DragLayer.
2988 // There was a one-off crash where the folder had a parent already.
2989 if (folder.getParent() == null) {
2990 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07002991 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07002992 } else {
2993 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2994 folder.getParent() + ").");
2995 }
Adam Cohen37b2a492016-04-06 18:36:06 -07002996 folder.animateOpen();
2997
2998 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002999
3000 // Notify the accessibility manager that this folder "window" has appeared and occluded
3001 // the workspace items
3002 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3003 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003004 }
3005
3006 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003007 closeFolder(true);
3008 }
3009
3010 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003011 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003012 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003013 if (folder.isEditingName()) {
3014 folder.dismissEditingName();
3015 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003016 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003017 }
3018 }
3019
Sunny Goyal935fca12015-10-13 10:19:01 -07003020 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003021 animate &= !Utilities.isPowerSaverOn(this);
3022
Adam Cohen4554ee12011-08-03 16:13:21 -07003023 folder.getInfo().opened = false;
3024
3025 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3026 if (parent != null) {
3027 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003028 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003029 if (fi != null) {
3030 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3031 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003032 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003033 if (animate) {
3034 folder.animateClosed();
3035 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003036 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003037 }
Winson Chung83ca4802013-04-12 15:10:52 -07003038
Sunny Goyal935fca12015-10-13 10:19:01 -07003039 // Notify the accessibility manager that this folder "window" has disappeared and no
3040 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003041 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003042 }
3043
Tony Wickham49c8d292016-07-01 11:44:34 -07003044 public void closeShortcutsContainer() {
Sunny Goyala2454ad2016-07-22 10:50:11 -07003045 closeShortcutsContainer(true);
3046 }
3047
3048 public void closeShortcutsContainer(boolean animate) {
Tony Wickham49c8d292016-07-01 11:44:34 -07003049 DeepShortcutsContainer deepShortcutsContainer = getOpenShortcutsContainer();
3050 if (deepShortcutsContainer != null) {
Sunny Goyala2454ad2016-07-22 10:50:11 -07003051 if (animate) {
3052 deepShortcutsContainer.animateClose();
3053 } else {
3054 deepShortcutsContainer.close();
3055 }
Tony Wickham49c8d292016-07-01 11:44:34 -07003056 }
3057 }
3058
Sunny Goyal52851aa2016-09-02 10:41:43 -07003059 public View getTopFloatingView() {
3060 View topView = getOpenShortcutsContainer();
3061 if (topView == null) {
3062 topView = getWorkspace().getOpenFolder();
3063 }
3064 return topView;
3065 }
3066
Tony Wickham49c8d292016-07-01 11:44:34 -07003067 /**
3068 * @return The open shortcuts container, or null if there is none
3069 */
3070 public DeepShortcutsContainer getOpenShortcutsContainer() {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07003071 // Iterate in reverse order. Shortcuts container is added later to the dragLayer,
3072 // and will be one of the last views.
3073 for (int i = mDragLayer.getChildCount() - 1; i >= 0; i--) {
3074 View child = mDragLayer.getChildAt(i);
Sunny Goyala2454ad2016-07-22 10:50:11 -07003075 if (child instanceof DeepShortcutsContainer
3076 && ((DeepShortcutsContainer) child).isOpen()) {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07003077 return (DeepShortcutsContainer) child;
3078 }
3079 }
3080 return null;
Tony Wickham49c8d292016-07-01 11:44:34 -07003081 }
3082
Tony Wickhamdadb3042016-02-24 11:07:00 -08003083 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003084 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003085 if (!isDraggingEnabled()) return false;
3086 if (isWorkspaceLocked()) return false;
3087 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003088
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07003089 if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
3090 (v == mAllAppsButton && mAllAppsButton != null)) {
Winson Chung76648c52015-07-10 14:33:23 -07003091 onLongClickAllAppsButton(v);
3092 return true;
3093 }
3094
Adam Cohen1697b792013-09-17 19:08:21 -07003095 if (v instanceof Workspace) {
3096 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003097 if (!mWorkspace.isTouchActive()) {
3098 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003099 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3100 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3101 return true;
3102 } else {
3103 return false;
3104 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003105 } else {
3106 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003107 }
Adam Cohen1697b792013-09-17 19:08:21 -07003108 }
3109
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003110 CellLayout.CellInfo longClickCellInfo = null;
3111 View itemUnderLongClick = null;
3112 if (v.getTag() instanceof ItemInfo) {
3113 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003114 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003115 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07003116 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003117 }
3118
Winson Chung3d503fb2011-07-13 17:25:49 -07003119 // The hotseat touch handling does not go through Workspace, and we always allow long press
3120 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003121 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003122 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003123 // User long pressed on empty space
3124 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3125 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003126 if (mWorkspace.isInOverviewMode()) {
3127 mWorkspace.startReordering(v);
3128 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003129 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003130 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003131 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003132 final boolean isAllAppsButton =
3133 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3134 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3135 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003136 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003137 // User long pressed on an item
Tony Wickham6e74e892016-09-19 14:06:31 -07003138 DragOptions dragOptions = new DragOptions();
3139 if (itemUnderLongClick instanceof BubbleTextView) {
3140 BubbleTextView icon = (BubbleTextView) itemUnderLongClick;
3141 if (icon.hasDeepShortcuts()) {
3142 DeepShortcutsContainer dsc = DeepShortcutsContainer.showForIcon(icon);
3143 if (dsc != null) {
3144 dragOptions.deferDragCondition = dsc.createDeferDragCondition(null);
3145 }
3146 }
3147 }
3148 mWorkspace.startDrag(longClickCellInfo, dragOptions);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003149 }
3150 }
3151 }
3152 return true;
3153 }
3154
Winson Chung3d503fb2011-07-13 17:25:49 -07003155 boolean isHotseatLayout(View layout) {
3156 return mHotseat != null && layout != null &&
3157 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3158 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003159
Winson Chung3d503fb2011-07-13 17:25:49 -07003160 /**
3161 * Returns the CellLayout of the specified container at the specified screen.
3162 */
Sunny Goyal92820592015-03-02 11:31:35 -08003163 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003164 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3165 if (mHotseat != null) {
3166 return mHotseat.getLayout();
3167 } else {
3168 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003169 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003170 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003171 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003172 }
3173 }
3174
Winson Chungb745afb2015-03-02 11:51:23 -08003175 /**
3176 * For overridden classes.
3177 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003178 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003179 return isAppsViewVisible();
3180 }
3181
3182 public boolean isAppsViewVisible() {
3183 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3184 }
3185
3186 public boolean isWidgetsViewVisible() {
3187 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003188 }
3189
Michael Jurkae326f182011-11-21 14:05:46 -08003190 @Override
3191 public void onTrimMemory(int level) {
3192 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003193 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3194 // The widget preview db can result in holding onto over
3195 // 3MB of memory for caching which isn't necessary.
3196 SQLiteDatabase.releaseMemory();
3197
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003198 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003199 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003200 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003201 if (mLauncherCallbacks != null) {
3202 mLauncherCallbacks.onTrimMemory(level);
3203 }
Michael Jurkae326f182011-11-21 14:05:46 -08003204 }
3205
Winson5c6bdbb2015-09-03 11:36:19 -07003206 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003207 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003208 }
3209
Winson5c6bdbb2015-09-03 11:36:19 -07003210 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003211 boolean changed = mState != State.WORKSPACE ||
3212 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003213 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003214 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003215 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003216 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003217
Michael Jurkab3e22d92011-10-31 15:58:33 -07003218 // Set focus to the AppsCustomize button
3219 if (mAllAppsButton != null) {
3220 mAllAppsButton.requestFocus();
3221 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003222 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003223
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003224 // Change the state *after* we've called all the transition code
3225 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003226
Winson Chung5fb63472011-02-02 17:03:37 -08003227 // Resume the auto-advance of widgets
3228 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003229 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003230
Winson Chung0f785722015-04-08 10:27:49 -07003231 if (changed) {
3232 // Send an accessibility event to announce the context change
3233 getWindow().getDecorView()
3234 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003235 }
Winson5c6bdbb2015-09-03 11:36:19 -07003236 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003237 }
3238
Winsone9f27272015-10-13 10:47:51 -07003239 /**
3240 * Shows the overview button.
3241 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07003242 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003243 showOverviewMode(animated, false);
3244 }
3245
3246 /**
3247 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3248 * onto one of the overview panel buttons.
3249 */
3250 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3251 Runnable postAnimRunnable = null;
3252 if (requestButtonFocus) {
3253 postAnimRunnable = new Runnable() {
3254 @Override
3255 public void run() {
3256 // Hitting the menu button when in touch mode does not trigger touch mode to
3257 // be disabled, so if requested, force focus on one of the overview panel
3258 // buttons.
3259 mOverviewPanel.requestFocusFromTouch();
3260 }
3261 };
3262 }
Adam Cohened307df2013-10-02 09:37:31 -07003263 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003264 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003265 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003266 mState = State.WORKSPACE;
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07003267 // If animated from long press, then don't allow any of the controller in the drag
3268 // layer to intercept any remaining touch.
3269 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04003270 }
3271
Winson Chungb745afb2015-03-02 11:51:23 -08003272 /**
3273 * Shows the apps view.
3274 */
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003275 public void showAppsView(boolean animated, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003276 boolean focusSearchBar) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003277 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07003278 if (updatePredictedApps) {
3279 tryAndUpdatePredictedApps();
3280 }
Winson Chung76648c52015-07-10 14:33:23 -07003281 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003282 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003283
Winson Chungb745afb2015-03-02 11:51:23 -08003284 /**
3285 * Shows the widgets view.
3286 */
3287 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003288 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003289 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003290 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003291 }
Winson Chung76648c52015-07-10 14:33:23 -07003292 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003293
3294 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003295 @Override
3296 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003297 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003298 }
3299 });
Winson Chungb745afb2015-03-02 11:51:23 -08003300 }
3301
3302 /**
3303 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003304 *
3305 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003306 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003307 // TODO: calling method should use the return value so that when {@code false} is returned
3308 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003309 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003310 if (!(mState == State.WORKSPACE ||
3311 mState == State.APPS_SPRING_LOADED ||
3312 mState == State.WIDGETS_SPRING_LOADED ||
3313 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003314 return false;
3315 }
3316 if (toState != State.APPS && toState != State.WIDGETS) {
3317 return false;
3318 }
Winson Chungb745afb2015-03-02 11:51:23 -08003319
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003320 // This is a safe and supported transition to bypass spring_loaded mode.
3321 if (mExitSpringLoadedModeRunnable != null) {
3322 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3323 mExitSpringLoadedModeRunnable = null;
3324 }
3325
Winson Chungb745afb2015-03-02 11:51:23 -08003326 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003327 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3328 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003329 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003330 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003331 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003332
Michael Jurkab3e22d92011-10-31 15:58:33 -07003333 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003334 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003335
3336 // Pause the auto-advance of widgets until we are out of AllApps
3337 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003338 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003339 closeFolder();
Tony Wickham49c8d292016-07-01 11:44:34 -07003340 closeShortcutsContainer();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003341
3342 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003343 getWindow().getDecorView()
3344 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003345 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003346 }
3347
Winson Chungcd99cd32015-04-29 11:03:24 -07003348 /**
3349 * Updates the workspace and interaction state on state change, and return the animation to this
3350 * new state.
3351 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003352 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003353 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003354 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003355 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003356 updateInteraction(fromState, toState);
3357 return anim;
3358 }
3359
Hyunyoung Song3f471442015-04-08 19:01:34 -07003360 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003361 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003362 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003363 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003364 }
Winson Chungb745afb2015-03-02 11:51:23 -08003365
Winson Chung006ee262015-08-03 14:40:11 -07003366 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003367 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003368 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003369
3370 if (isAppsViewVisible()) {
3371 mState = State.APPS_SPRING_LOADED;
3372 } else if (isWidgetsViewVisible()) {
3373 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003374 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003375 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003376 } else {
3377 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003378 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003379 }
Adam Cohen7777d962011-08-18 18:58:38 -07003380
Hyunyoung Song3f471442015-04-08 19:01:34 -07003381 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003382 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003383 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003384
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003385 if (mExitSpringLoadedModeRunnable != null) {
3386 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3387 }
3388 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003389 @Override
3390 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003391 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003392 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3393 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003394 // Before we show workspace, hide all apps again because
3395 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3396 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003397 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003398 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003399 } else {
3400 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003401 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003402 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07003403 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003404 };
3405 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003406 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003407
Tony Wickhame0c33232016-02-08 11:37:04 -08003408 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003409 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3410 || mState == State.WIDGETS_SPRING_LOADED;
3411 }
3412
Michael Jurkad3ef3062010-11-23 16:23:58 -08003413 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003414 if (mState == State.APPS_SPRING_LOADED) {
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003415 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07003416 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003417 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003418 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003419 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3420 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003421 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003422 }
3423
Winson Chung4ac30062015-05-08 17:34:17 -07003424 /**
3425 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3426 * resumed.
3427 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003428 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003429 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003430 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003431 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003432 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003433 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003434 }
3435 }
3436 }
3437
Michael Jurkab3e22d92011-10-31 15:58:33 -07003438 void lockAllApps() {
3439 // TODO
3440 }
3441
3442 void unlockAllApps() {
3443 // TODO
3444 }
3445
Michael Jurkad7c28052012-04-27 15:43:36 -07003446 @Override
3447 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003448 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003449 final List<CharSequence> text = event.getText();
3450 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003451 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003452 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003453 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003454 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003455 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003456 } else if (mWorkspace != null) {
3457 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003458 } else {
3459 text.add(getString(R.string.all_apps_home_button_label));
3460 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003461 return result;
3462 }
3463
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003464 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003465 * If the activity is currently paused, signal that we need to run the passed Runnable
3466 * in onResume.
3467 *
3468 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003469 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3470 * wrong when we're not running, and if the activity comes back to what the configuration was
3471 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003472 *
3473 * Implementation of the method from LauncherModel.Callbacks.
3474 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003475 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003476 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003477 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003478 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003479 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003480 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003481 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003482 }
3483 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003484 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003485 return true;
3486 } else {
3487 return false;
3488 }
3489 }
3490
Michael Jurkac402cd92013-05-20 15:49:32 +02003491 private boolean waitUntilResume(Runnable run) {
3492 return waitUntilResume(run, false);
3493 }
3494
Michael Jurka1e2f4652013-07-08 18:03:46 -07003495 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003496 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003497 }
3498
Michael Jurka7607c2f2013-04-03 14:33:19 -07003499 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003500 * If the activity is currently paused, signal that we need to re-run the loader
3501 * in onResume.
3502 *
3503 * This needs to be called from incoming places where resources might have been loaded
3504 * while we are paused. That is becaues the Configuration might be wrong
3505 * when we're not running, and if it comes back to what it was when we
3506 * were paused, we are not restarted.
3507 *
3508 * Implementation of the method from LauncherModel.Callbacks.
3509 *
3510 * @return true if we are currently paused. The caller might be able to
3511 * skip some work in that case since we will come back again.
3512 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003513 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003514 public boolean setLoadOnResume() {
3515 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003516 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003517 mOnResumeNeedsLoad = true;
3518 return true;
3519 } else {
3520 return false;
3521 }
3522 }
3523
3524 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003525 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003526 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003527 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003528 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003529 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003530 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003531 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003532 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003533 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003534 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003535
Joe Onorato9c1289c2009-08-17 11:03:03 -04003536 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003537 * Clear any pending bind callbacks. This is called when is loader is planning to
3538 * perform a full rebind from scratch.
3539 */
3540 @Override
3541 public void clearPendingBinds() {
3542 mBindOnResumeCallbacks.clear();
3543 if (mPendingExecutor != null) {
3544 mPendingExecutor.markCompleted();
3545 mPendingExecutor = null;
3546 }
3547 }
3548
3549 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003550 * Refreshes the shortcuts shown on the workspace.
3551 *
3552 * Implementation of the method from LauncherModel.Callbacks.
3553 */
3554 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003555 if (LauncherAppState.PROFILE_STARTUP) {
3556 Trace.beginSection("Starting page bind");
3557 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003558 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003559
Winson Chungd64d1762013-08-20 14:37:16 -07003560 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003561 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003562 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003563
Michael Jurka05bf644e2011-11-30 20:28:53 -08003564 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003565 if (mHotseat != null) {
3566 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003567 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003568 if (LauncherAppState.PROFILE_STARTUP) {
3569 Trace.endSection();
3570 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003571 }
3572
Adam Cohendcd297f2013-06-18 13:13:40 -07003573 @Override
3574 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003575 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003576 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3577 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003578 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3579 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3580 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003581 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3582 // If there are no screens, we need to have an empty screen
3583 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003584 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003585 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003586
3587 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003588 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003589 if (hasCustomContentToLeft()) {
3590 mWorkspace.createCustomContentContainer();
3591 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003592 }
Winsonc7d2e832016-07-28 12:24:55 -07003593
3594 // After we have added all the screens, if the wallpaper was locked to the default state,
3595 // then notify to indicate that it can be released and a proper wallpaper offset can be
3596 // computed before the next layout
3597 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07003598 }
3599
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003600 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003601 int count = orderedScreenIds.size();
3602 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003603 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003604 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003605 // No need to bind the first screen, as its always bound.
3606 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3607 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003608 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003609 }
3610
Winson Chungd64d1762013-08-20 14:37:16 -07003611 public void bindAppsAdded(final ArrayList<Long> newScreens,
3612 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003613 final ArrayList<ItemInfo> addAnimated,
3614 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003615 Runnable r = new Runnable() {
3616 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003617 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003618 }
3619 };
3620 if (waitUntilResume(r)) {
3621 return;
3622 }
3623
Winson Chungd64d1762013-08-20 14:37:16 -07003624 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003625 if (newScreens != null) {
3626 bindAddScreens(newScreens);
3627 }
Winson Chungd64d1762013-08-20 14:37:16 -07003628
3629 // We add the items without animation on non-visible pages, and with
3630 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003631 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003632 bindItems(addNotAnimated, 0,
3633 addNotAnimated.size(), false);
3634 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003635 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003636 bindItems(addAnimated, 0,
3637 addAnimated.size(), true);
3638 }
Winson Chungc58497e2013-09-03 17:48:37 -07003639
Winson Chung87412982013-10-03 18:34:14 -07003640 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003641 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003642
Winson Chungb745afb2015-03-02 11:51:23 -08003643 if (addedApps != null && mAppsView != null) {
3644 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003645 }
Winson Chungd64d1762013-08-20 14:37:16 -07003646 }
3647
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003648 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003649 * Bind the items start-end from the list.
3650 *
3651 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003652 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003653 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003654 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3655 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003656 Runnable r = new Runnable() {
3657 public void run() {
3658 bindItems(shortcuts, start, end, forceAnimateIcons);
3659 }
3660 };
3661 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003662 return;
3663 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003664
Winson Chungf0c6ae02012-03-21 16:10:31 -07003665 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003666 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3667 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003668 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003669 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003670 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003671 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003672 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003673
3674 // Short circuit if we are loading dock items for a configuration which has no dock
3675 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3676 mHotseat == null) {
3677 continue;
3678 }
3679
Sunny Goyal639e9062015-08-19 19:17:06 -07003680 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003681 switch (item.itemType) {
3682 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3683 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003684 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003685 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003686 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003687 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003688 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003689 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003690 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003691 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003692 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003693 default:
3694 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003695 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003696
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003697 /*
3698 * Remove colliding items.
3699 */
3700 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3701 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3702 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3703 View v = cl.getChildAt(item.cellX, item.cellY);
3704 Object tag = v.getTag();
3705 String desc = "Collision while binding workspace item: " + item
3706 + ". Collides with " + tag;
3707 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3708 throw (new RuntimeException(desc));
3709 } else {
3710 Log.d(TAG, desc);
3711 LauncherModel.deleteItemFromDatabase(this, item);
3712 continue;
3713 }
3714 }
3715 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003716 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3717 item.cellY, 1, 1);
3718 if (animateIcons) {
3719 // Animate all the applications up now
3720 view.setAlpha(0f);
3721 view.setScaleX(0f);
3722 view.setScaleY(0f);
3723 bounceAnims.add(createNewAppBounceAnimation(view, i));
3724 newShortcutsScreenId = item.screenId;
3725 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003726 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003727
Winson Chung997a9232013-07-24 15:33:46 -07003728 if (animateIcons) {
3729 // Animate to the correct page
3730 if (newShortcutsScreenId > -1) {
3731 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003732 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003733 final Runnable startBounceAnimRunnable = new Runnable() {
3734 public void run() {
3735 anim.playTogether(bounceAnims);
3736 anim.start();
3737 }
3738 };
Winson Chung997a9232013-07-24 15:33:46 -07003739 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003740 // We post the animation slightly delayed to prevent slowdowns
3741 // when we are loading right after we return to launcher.
3742 mWorkspace.postDelayed(new Runnable() {
3743 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003744 if (mWorkspace != null) {
3745 mWorkspace.snapToPage(newScreenIndex);
3746 mWorkspace.postDelayed(startBounceAnimRunnable,
3747 NEW_APPS_ANIMATION_DELAY);
3748 }
Winson Chung94d67682013-09-25 16:29:40 -07003749 }
3750 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003751 } else {
3752 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003753 }
3754 }
Winson Chung64359a52013-07-08 17:17:08 -07003755 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003756 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003757 }
3758
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003759 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3760 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3761 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003762 view.updateAppWidget(null);
3763 view.setOnClickListener(this);
3764 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003765 mWorkspace.requestLayout();
3766 }
3767
Joe Onorato9c1289c2009-08-17 11:03:03 -04003768 /**
3769 * Add the views for a widget to the workspace.
3770 *
3771 * Implementation of the method from LauncherModel.Callbacks.
3772 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003773 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003774 Runnable r = new Runnable() {
3775 public void run() {
3776 bindAppWidget(item);
3777 }
3778 };
3779 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003780 return;
3781 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003782
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003783 if (mIsSafeModeEnabled) {
3784 bindSafeModeWidget(item);
3785 return;
3786 }
3787
Daniel Sandler843e8602010-06-07 14:59:01 -04003788 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3789 if (DEBUG_WIDGETS) {
3790 Log.d(TAG, "bindAppWidget: " + item);
3791 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003792
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003793 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003794
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003795 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3796 // If the provider is not ready, bind as a pending widget.
3797 appWidgetInfo = null;
3798 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3799 // The widget id is not valid. Try to find the widget based on the provider info.
3800 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3801 } else {
3802 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3803 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003804
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003805 // If the provider is ready, but the width is not yet restored, try to restore it.
3806 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3807 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003808 if (appWidgetInfo == null) {
3809 if (DEBUG_WIDGETS) {
3810 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3811 + " belongs to component " + item.providerName
3812 + ", as the povider is null");
3813 }
3814 LauncherModel.deleteItemFromDatabase(this, item);
3815 return;
3816 }
Sunny Goyalff572272014-07-23 13:58:07 -07003817
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003818 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003819 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003820 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3821 // Id has not been allocated yet. Allocate a new id.
3822 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3823 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003824
Sunny Goyald478c832016-04-01 12:04:16 -07003825 // Also try to bind the widget. If the bind fails, the user will be shown
3826 // a click to setup UI, which will ask for the bind permission.
3827 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3828 pendingInfo.spanX = item.spanX;
3829 pendingInfo.spanY = item.spanY;
3830 pendingInfo.minSpanX = item.minSpanX;
3831 pendingInfo.minSpanY = item.minSpanY;
3832 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003833
3834 boolean isDirectConfig =
3835 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3836 if (isDirectConfig && item.bindOptions != null) {
3837 Bundle newOptions = item.bindOptions.getExtras();
3838 if (options != null) {
3839 newOptions.putAll(options);
3840 }
3841 options = newOptions;
3842 }
Sunny Goyald478c832016-04-01 12:04:16 -07003843 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3844 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003845
Sunny Goyal86df1382016-08-10 15:03:22 -07003846 // We tried to bind once. If we were not able to bind, we would need to
3847 // go through the permission dialog, which means we cannot skip the config
3848 // activity.
3849 item.bindOptions = null;
3850 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3851
Sunny Goyald478c832016-04-01 12:04:16 -07003852 // Bind succeeded
3853 if (success) {
3854 // If the widget has a configure activity, it is still needs to set it up,
3855 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003856 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003857 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3858 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003859 }
Sunny Goyald478c832016-04-01 12:04:16 -07003860
3861 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003862 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003863 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003864 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003865 // The widget was marked as UI not ready, but there is no configure activity to
3866 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003867 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3868 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003869 }
Sunny Goyalff572272014-07-23 13:58:07 -07003870 }
3871
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003872 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003873 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003874 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3875 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003876 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003877
Sunny Goyal56c73602015-09-25 12:55:01 -07003878 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003879 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003880 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003881 deleteWidgetInfo(item);
3882 return;
3883 }
3884
Sunny Goyal233ee962015-08-03 13:05:01 -07003885 item.minSpanX = appWidgetInfo.minSpanX;
3886 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003887 addAppWidgetToWorkspace(
3888 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3889 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003890 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003891 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003892 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003893 view.updateAppWidget(null);
3894 view.setOnClickListener(this);
3895 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003896 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003897 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003898
Daniel Sandler843e8602010-06-07 14:59:01 -04003899 if (DEBUG_WIDGETS) {
3900 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3901 + (SystemClock.uptimeMillis()-start) + "ms");
3902 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003903 }
3904
Sunny Goyalff572272014-07-23 13:58:07 -07003905 /**
3906 * Restores a pending widget.
3907 *
3908 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003909 */
Sunny Goyald478c832016-04-01 12:04:16 -07003910 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003911 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3912 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3913 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003914 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003915 }
3916
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003917 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003918 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003919
3920 mWorkspace.reinflateWidgetsIfNecessary();
3921 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003922 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003923 }
3924
Adam Cohen1462de32012-07-24 22:34:36 -07003925 public void onPageBoundSynchronously(int page) {
3926 mSynchronouslyBoundPages.add(page);
3927 }
3928
Sunny Goyal527c7d32015-08-28 15:19:36 -07003929 @Override
3930 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3931 if (mPendingExecutor != null) {
3932 mPendingExecutor.markCompleted();
3933 }
3934 mPendingExecutor = executor;
3935 executor.attachTo(this);
3936 }
3937
3938 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3939 if (mPendingExecutor == executor) {
3940 mPendingExecutor = null;
3941 }
3942 }
3943
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003944 @Override
3945 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3946 Runnable r = new Runnable() {
3947 public void run() {
3948 finishFirstPageBind(executor);
3949 }
3950 };
3951 if (waitUntilResume(r)) {
3952 return;
3953 }
3954
3955 Runnable onComplete = new Runnable() {
3956 @Override
3957 public void run() {
3958 if (executor != null) {
3959 executor.onLoadAnimationCompleted();
3960 }
3961 }
3962 };
3963 if (mDragLayer.getAlpha() < 1) {
3964 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3965 } else {
3966 onComplete.run();
3967 }
3968 }
3969
Joe Onorato9c1289c2009-08-17 11:03:03 -04003970 /**
3971 * Callback saying that there aren't any more items to bind.
3972 *
3973 * Implementation of the method from LauncherModel.Callbacks.
3974 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003975 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003976 Runnable r = new Runnable() {
3977 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003978 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003979 }
3980 };
3981 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003982 return;
3983 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003984 if (LauncherAppState.PROFILE_STARTUP) {
3985 Trace.beginSection("Page bind completed");
3986 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003987 if (mSavedState != null) {
3988 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003989 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003990 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003991
Joe Onorato9c1289c2009-08-17 11:03:03 -04003992 mSavedState = null;
3993 }
3994
Adam Cohen1462de32012-07-24 22:34:36 -07003995 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003996
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003997 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003998
Sunny Goyal2100c782016-08-22 16:00:03 -07003999 if (mPendingActivityResult != null) {
4000 handleActivityResult(mPendingActivityResult.requestCode,
4001 mPendingActivityResult.resultCode, mPendingActivityResult.data);
4002 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07004003 }
4004
Sunny Goyal756adbc2015-04-16 15:20:51 -07004005 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004006
4007 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004008 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004009 }
Sunny Goyale26d1002016-06-20 14:52:14 -07004010 if (LauncherAppState.PROFILE_STARTUP) {
4011 Trace.endSection();
4012 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004013 }
4014
Winson Chunga2413752012-04-03 14:22:34 -07004015 private boolean canRunNewAppsAnimation() {
4016 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07004017 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07004018 }
4019
Winson Chungc9168342013-06-26 14:54:55 -07004020 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004021 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004022 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4023 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004024 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004025 return bounceAnim;
4026 }
4027
Adam Cohen27772732013-11-11 14:00:56 +00004028 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004029 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004030 }
4031
Tony Wickham55616cd2015-09-23 14:55:17 -07004032 public int getSearchBarHeight() {
4033 if (mLauncherCallbacks != null) {
4034 return mLauncherCallbacks.getSearchBarHeight();
4035 }
4036 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4037 }
4038
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004039 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004040 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4041 * multiple calls to bind the same list.)
4042 */
4043 @Thunk ArrayList<AppInfo> mTmpAppsList;
4044 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4045 public void run() {
4046 bindAllApplications(mTmpAppsList);
4047 mTmpAppsList = null;
4048 }
4049 };
4050
4051 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004052 * Add the icons for all apps.
4053 *
4054 * Implementation of the method from LauncherModel.Callbacks.
4055 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004056 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004057 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4058 mTmpAppsList = apps;
4059 return;
4060 }
4061
Winson Chungb745afb2015-03-02 11:51:23 -08004062 if (mAppsView != null) {
4063 mAppsView.setApps(apps);
4064 }
Adam Cohen9211d422014-10-07 18:14:53 -07004065 if (mLauncherCallbacks != null) {
4066 mLauncherCallbacks.bindAllApplications(apps);
4067 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004068 }
4069
4070 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004071 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
4072 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
4073 */
4074 @Override
4075 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
4076 mDeepShortcutMap = deepShortcutMapCopy;
4077 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
4078 }
4079
Tony Wickham1bce7fd2016-04-28 17:39:03 -07004080 public List<String> getShortcutIdsForItem(ItemInfo info) {
4081 if (!DeepShortcutManager.supportsShortcuts(info)) {
4082 return Collections.EMPTY_LIST;
4083 }
4084 ComponentName component = info.getTargetComponent();
Hyunyoung Songd3bf9802016-08-29 14:43:53 -07004085 if (component == null) {
4086 return Collections.EMPTY_LIST;
4087 }
4088
Tony Wickham1bce7fd2016-04-28 17:39:03 -07004089 List<String> ids = mDeepShortcutMap.get(new ComponentKey(component, info.user));
4090 return ids == null ? Collections.EMPTY_LIST : ids;
4091 }
4092
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004093 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004094 * A package was updated.
4095 *
4096 * Implementation of the method from LauncherModel.Callbacks.
4097 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004098 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004099 Runnable r = new Runnable() {
4100 public void run() {
4101 bindAppsUpdated(apps);
4102 }
4103 };
4104 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004105 return;
4106 }
4107
Winson Chungb745afb2015-03-02 11:51:23 -08004108 if (mAppsView != null) {
4109 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004110 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004111 }
4112
Sunny Goyal4390ace2014-10-13 11:33:11 -07004113 @Override
4114 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4115 Runnable r = new Runnable() {
4116 public void run() {
4117 bindWidgetsRestored(widgets);
4118 }
4119 };
4120 if (waitUntilResume(r)) {
4121 return;
4122 }
4123 mWorkspace.widgetsRestored(widgets);
4124 }
4125
Joe Onorato9c1289c2009-08-17 11:03:03 -04004126 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004127 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004128 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004129 *
4130 * @param updated list of shortcuts which have changed.
4131 * @param removed list of shortcuts which were deleted in the background. This can happen when
4132 * an app gets removed from the system or some of its components are no longer
4133 * available.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004134 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004135 @Override
4136 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4137 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004138 Runnable r = new Runnable() {
4139 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004140 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004141 }
4142 };
4143 if (waitUntilResume(r)) {
4144 return;
4145 }
4146
Sunny Goyal4390ace2014-10-13 11:33:11 -07004147 if (!updated.isEmpty()) {
4148 mWorkspace.updateShortcuts(updated);
4149 }
4150
4151 if (!removed.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004152 HashSet<ComponentName> removedComponents = new HashSet<>();
4153 HashSet<ShortcutKey> removedDeepShortcuts = new HashSet<>();
4154
Sunny Goyal4390ace2014-10-13 11:33:11 -07004155 for (ShortcutInfo si : removed) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004156 if (si.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07004157 removedDeepShortcuts.add(ShortcutKey.fromShortcutInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004158 } else {
4159 removedComponents.add(si.getTargetComponent());
4160 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004161 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004162
4163 if (!removedComponents.isEmpty()) {
4164 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(removedComponents, user);
4165 mWorkspace.removeItemsByMatcher(matcher);
4166 mDragController.onAppsRemoved(matcher);
4167 }
4168
4169 if (!removedDeepShortcuts.isEmpty()) {
4170 ItemInfoMatcher matcher = ItemInfoMatcher.ofShortcutKeys(removedDeepShortcuts);
4171 mWorkspace.removeItemsByMatcher(matcher);
4172 mDragController.onAppsRemoved(matcher);
4173 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004174 }
4175 }
4176
4177 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004178 * Update the state of a package, typically related to install state.
4179 *
4180 * Implementation of the method from LauncherModel.Callbacks.
4181 */
Sunny Goyale755d462014-07-22 13:48:29 -07004182 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004183 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4184 Runnable r = new Runnable() {
4185 public void run() {
4186 bindRestoreItemsChange(updates);
4187 }
4188 };
4189 if (waitUntilResume(r)) {
4190 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004191 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004192
Sunny Goyal756adbc2015-04-16 15:20:51 -07004193 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004194 }
4195
4196 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004197 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004198 * in addition to specific applications to remove, the reason being that
4199 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004200 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004201 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004202 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004203 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004204 public void bindWorkspaceComponentsRemoved(
4205 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4206 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004207 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004208 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004209 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004210 }
Winson Chungd64d1762013-08-20 14:37:16 -07004211 };
4212 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004213 return;
4214 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004215 if (!packageNames.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004216 ItemInfoMatcher matcher = ItemInfoMatcher.ofPackages(packageNames, user);
4217 mWorkspace.removeItemsByMatcher(matcher);
4218 mDragController.onAppsRemoved(matcher);
4219
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004220 }
4221 if (!components.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004222 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(components, user);
4223 mWorkspace.removeItemsByMatcher(matcher);
4224 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004225 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004226 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004227
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004228 @Override
4229 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4230 Runnable r = new Runnable() {
4231 public void run() {
4232 bindAppInfosRemoved(appInfos);
4233 }
4234 };
4235 if (waitUntilResume(r)) {
4236 return;
Joe Onorato36115782010-06-17 13:28:48 -04004237 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004238
Winson Chungdf95eb12013-10-16 14:57:07 -07004239 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004240 if (mAppsView != null) {
4241 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004242 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004243 }
Joe Onoratobe386092009-11-17 17:32:16 -08004244
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004245 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004246 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004247 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004248 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004249 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004250
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004251 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004252 public void bindWidgetsModel(WidgetsModel model) {
4253 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004254 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004255 return;
4256 }
4257
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004258 if (mWidgetsView != null && model != null) {
4259 mWidgetsView.addWidgets(model);
4260 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004261 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004262 }
4263
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004264 @Override
4265 public void notifyWidgetProvidersChanged() {
4266 if (mWorkspace.getState().shouldUpdateWidget) {
4267 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4268 }
4269 }
4270
Winson Chung400438b2011-01-16 17:53:48 -08004271 private int mapConfigurationOriActivityInfoOri(int configOri) {
4272 final Display d = getWindowManager().getDefaultDisplay();
4273 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4274 switch (d.getRotation()) {
4275 case Surface.ROTATION_0:
4276 case Surface.ROTATION_180:
4277 // We are currently in the same basic orientation as the natural orientation
4278 naturalOri = configOri;
4279 break;
4280 case Surface.ROTATION_90:
4281 case Surface.ROTATION_270:
4282 // We are currently in the other basic orientation to the natural orientation
4283 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4284 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4285 break;
4286 }
4287
4288 int[] oriMap = {
4289 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4290 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4291 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4292 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4293 };
4294 // Since the map starts at portrait, we need to offset if this device's natural orientation
4295 // is landscape.
4296 int indexOffset = 0;
4297 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4298 indexOffset = 1;
4299 }
4300 return oriMap[(d.getRotation() + indexOffset) % 4];
4301 }
Adam Cohen446e9402011-09-15 18:21:21 -07004302
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004303 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004304 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004305 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004306 if (Utilities.ATLEAST_JB_MR2) {
4307 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4308 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004309 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4310 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004311 }
Winson Chung4b919f82012-05-01 10:44:08 -07004312 }
4313 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004314
Winson Chung4b919f82012-05-01 10:44:08 -07004315 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004316 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004317 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004318 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004319 } else {
4320 mHandler.postDelayed(new Runnable() {
4321 public void run() {
4322 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4323 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004324 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004325 }
Winson Chung4b919f82012-05-01 10:44:08 -07004326 }
Winson Chung400438b2011-01-16 17:53:48 -08004327 }
4328
Hyunyoung Songc001cf52016-07-21 17:32:43 -07004329 private void markAppsViewShown() {
4330 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4331 return;
4332 }
4333 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
4334 }
4335
4336 private boolean shouldShowDiscoveryBounce() {
4337 if (mState != mState.WORKSPACE) {
4338 return false;
4339 }
4340 if (mLauncherCallbacks != null && mLauncherCallbacks.shouldShowDiscoveryBounce()) {
4341 return true;
4342 }
4343 if (!mIsResumeFromActionScreenOff) {
4344 return false;
4345 }
4346 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4347 return false;
4348 }
4349 return true;
4350 }
4351
Winson Chung5ffd51d2015-06-25 11:36:50 -07004352 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004353 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004354 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004355 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004356 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004357 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004358 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4359 if (userHandle != null) {
4360 user = UserHandleCompat.fromUser(userHandle);
4361 }
4362 }
4363 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004364 }
4365
4366 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004367 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004368 if (user == null) {
4369 user = UserHandleCompat.myUserHandle();
4370 }
4371
4372 // Called from search suggestion, add the profile extra to the intent to ensure that we
4373 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004374 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004375 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004376 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004377 return null;
4378 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004379 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004380 }
4381
Winson Chung5ffd51d2015-06-25 11:36:50 -07004382 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004383 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4384 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004385 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004386 UserHandleCompat.myUserHandle());
4387 }
4388
Winson Chung5ffd51d2015-06-25 11:36:50 -07004389 protected void moveWorkspaceToDefaultScreen() {
4390 mWorkspace.moveToDefaultScreen(false);
4391 }
4392
Winson Chung80baf5a2010-08-09 16:03:15 -07004393 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004394 * Returns a FastBitmapDrawable with the icon, accurately sized.
4395 */
4396 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4397 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4398 d.setFilterBitmap(true);
4399 resizeIconDrawable(d);
4400 return d;
4401 }
4402
4403 /**
4404 * Resizes an icon drawable to the correct icon size.
4405 */
Winson5fbe0742015-09-30 15:33:00 -07004406 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004407 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004408 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004409 }
4410
4411 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004412 * Prints out out state for debugging.
4413 */
4414 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004415 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004416 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004417 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
Sunny Goyal2100c782016-08-22 16:00:03 -07004418 Log.d(TAG, "mPendingRequestArgs=" + mPendingRequestArgs);
4419 Log.d(TAG, "mPendingActivityResult=" + mPendingActivityResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004420 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004421 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004422
Daniel Sandler325dc232013-06-05 22:57:57 -04004423 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004424 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004425
4426 @Override
4427 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4428 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004429 // Dump workspace
4430 writer.println(prefix + "Workspace Items");
4431 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4432 writer.println(prefix + " Homescreen " + i);
4433
4434 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4435 for (int j = 0; j < layout.getChildCount(); j++) {
4436 Object tag = layout.getChildAt(j).getTag();
4437 if (tag != null) {
4438 writer.println(prefix + " " + tag.toString());
4439 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004440 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004441 }
Sunny Goyala1365452015-10-01 15:46:24 -07004442
4443 writer.println(prefix + " Hotseat");
4444 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4445 for (int j = 0; j < layout.getChildCount(); j++) {
4446 Object tag = layout.getChildAt(j).getTag();
4447 if (tag != null) {
4448 writer.println(prefix + " " + tag.toString());
4449 }
4450 }
4451
Sunny Goyal713edfc2016-05-06 09:58:34 -07004452 try {
4453 FileLog.flushAll(writer);
4454 } catch (Exception e) {
4455 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004456 }
4457
Adam Cohen9211d422014-10-07 18:14:53 -07004458 if (mLauncherCallbacks != null) {
4459 mLauncherCallbacks.dump(prefix, fd, writer, args);
4460 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004461 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004462
Adam Cohen59400422014-03-05 18:07:04 -08004463 public static CustomAppWidget getCustomAppWidget(String name) {
4464 return sCustomAppWidgets.get(name);
4465 }
4466
4467 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4468 return sCustomAppWidgets;
4469 }
4470
Sunny Goyal29538332016-02-18 09:10:19 -08004471 public static List<View> getFolderContents(View icon) {
4472 if (icon instanceof FolderIcon) {
4473 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4474 } else {
4475 return Collections.EMPTY_LIST;
4476 }
4477 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004478
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004479 public static Launcher getLauncher(Context context) {
4480 if (context instanceof Launcher) {
4481 return (Launcher) context;
4482 }
4483 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4484 }
4485
Sunny Goyal745bad92016-05-02 10:54:12 -07004486 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4487
4488 @Override
4489 public void onSharedPreferenceChanged(
4490 SharedPreferences sharedPreferences, String key) {
4491 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4492 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4493 if (!waitUntilResume(this, true)) {
4494 run();
4495 }
4496 }
4497 }
4498
4499 @Override
4500 public void run() {
4501 setOrientation();
4502 }
4503 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004504}