blob: 0a14c421d512cfc84c3133c6cdfa64085297f528 [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
Winson Chung4a2afa32012-07-19 14:53:05 -0700257 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
258 // scroll issues (because the workspace may not have been measured yet) and extra work.
259 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
260 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261
262 private SpannableStringBuilder mDefaultKeySsb = null;
263
Adam Cohen091440a2015-03-18 14:16:05 -0700264 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400265
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800266 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700267 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268
Michael Jurka1e2f4652013-07-08 18:03:46 -0700269 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700270 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700271 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700272
Joe Onorato9c1289c2009-08-17 11:03:03 -0400273 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800274 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700275 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700276 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700277 private boolean mIsResumeFromActionScreenOff;
Adam Cohen091440a2015-03-18 14:16:05 -0700278 @Thunk boolean mUserPresent = true;
Tony Wickham93113872016-09-14 16:46:32 -0700279 private boolean mVisible;
280 private boolean mHasFocus;
281 private boolean mAttached;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400282
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700283 /** Maps launcher activity components to their list of shortcut ids. */
284 private MultiHashMap<ComponentKey, String> mDeepShortcutMap = new MultiHashMap<>();
285
Adam Cohen61f560d2013-09-30 15:58:20 -0700286 private View.OnTouchListener mHapticFeedbackTouchListener;
287
Adam Cohended9f8d2010-11-03 13:25:16 -0700288 // Related to the auto-advancing of widgets
289 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700290 private static final int ADVANCE_INTERVAL = 20000;
291 private static final int ADVANCE_STAGGER = 250;
292
293 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700294 private long mAutoAdvanceSentTime;
295 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700296 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700297
Winson Chung400438b2011-01-16 17:53:48 -0800298 // Determines how long to wait after a rotation before restoring the screen orientation to
299 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700300 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800301
Adam Cohen1462de32012-07-24 22:34:36 -0700302 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
303
Winson Chung46353de2012-02-16 14:05:10 -0800304 // We only want to get the SharedPreferences once since it does an FS stat each time we get
305 // it from the context.
306 private SharedPreferences mSharedPrefs;
307
Winson Chungf0c6ae02012-03-21 16:10:31 -0700308 // Holds the page that we need to animate to, and the icon views that we need to animate up
309 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700310 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700311 private Bitmap mFolderIconBitmap;
312 private Canvas mFolderIconCanvas;
313 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700314
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700315 private DeviceProfile mDeviceProfile;
316
Adam Cohen4b66bf32015-09-18 12:15:19 -0700317 private boolean mMoveToDefaultScreenFromNewIntent;
318
Winson Chung13eb5272015-05-11 16:30:13 -0700319 // This is set to the view that launched the activity that navigated the user away from
320 // launcher. Since there is no callback for when the activity has finished launching, enable
321 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800322 private BubbleTextView mWaitingForResume;
323
Adam Cohen59400422014-03-05 18:07:04 -0800324 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
325 new HashMap<String, CustomAppWidget>();
326
Adam Cohen59400422014-03-05 18:07:04 -0800327 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700328 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
329 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800330 }
331 }
332
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700333 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
334 // state transition. If another state transition happened during this delay,
335 // simply unregister this runnable.
336 private Runnable mExitSpringLoadedModeRunnable;
337
Adam Cohen091440a2015-03-18 14:16:05 -0700338 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700339 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700340 if (mWorkspace != null) {
341 mWorkspace.buildPageHardwareLayers();
342 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700343 }
344 };
345
Sunny Goyal2100c782016-08-22 16:00:03 -0700346 // Activity result which needs to be processed after workspace has loaded.
347 private ActivityResultInfo mPendingActivityResult;
348 /**
349 * Holds extra information required to handle a result from an external call, like
350 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
351 */
352 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800353
Hyunyoung Songaa953652016-04-19 18:30:24 -0700354 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800355
Jon Miranda379198e2016-09-23 08:54:40 -0700356 private float mLastDispatchTouchEventX = 0.0f;
357 private float mEdgeOfScreenThresholdPx = 0.0f;
358
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700359 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700360 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400361
362 @Thunk void setOrientation() {
363 if (mRotationEnabled) {
364 unlockScreenOrientation(true);
365 } else {
366 setRequestedOrientation(
367 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700368 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400369 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700370
Sunny Goyal745bad92016-05-02 10:54:12 -0700371 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700372
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 @Override
374 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700375 if (DEBUG_STRICT_MODE) {
376 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
377 .detectDiskReads()
378 .detectDiskWrites()
379 .detectNetwork() // or .detectAll() for all detectable problems
380 .penaltyLog()
381 .build());
382 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
383 .detectLeakedSqlLiteObjects()
384 .detectLeakedClosableObjects()
385 .penaltyLog()
386 .penaltyDeath()
387 .build());
388 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700389 if (LauncherAppState.PROFILE_STARTUP) {
390 Trace.beginSection("Launcher-onCreate");
391 }
Winson Chunga2413752012-04-03 14:22:34 -0700392
Adam Cohen9211d422014-10-07 18:14:53 -0700393 if (mLauncherCallbacks != null) {
394 mLauncherCallbacks.preOnCreate();
395 }
396
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400398
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400399 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700400
Adam Cohen2e6da152015-05-06 11:42:25 -0700401 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700402 mDeviceProfile = getResources().getConfiguration().orientation
403 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700404 app.getInvariantDeviceProfile().landscapeProfile
405 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700406
Sunny Goyalf7258242015-10-19 16:59:07 -0700407 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700408 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800409 mModel = app.setLauncher(this);
410 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700411 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700412
Joe Onorato41a12d22009-10-31 18:30:00 -0400413 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700414 mAllAppsController = new AllAppsTransitionController(this);
415 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700416
Sunny Goyalffe83f12014-08-14 17:39:34 -0700417 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700418
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700419 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
420 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700421
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700422 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
423 // this also ensures that any synchronous binding below doesn't re-trigger another
424 // LauncherModel load.
425 mPaused = false;
426
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800427 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700428
Jon Miranda379198e2016-09-23 08:54:40 -0700429 mEdgeOfScreenThresholdPx = getResources()
430 .getDimensionPixelSize(R.dimen.edge_of_screen_threshold);
431
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432 setupViews();
Winson1f064272016-07-18 17:18:02 -0700433 mDeviceProfile.layout(this, false /* notifyListeners */);
Tony Wickham827cef22016-03-17 15:39:39 -0700434 mExtractedColors = new ExtractedColors();
435 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436
Tony Wickhame2217252016-03-22 16:34:23 -0700437 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
438 .addAccessibilityStateChangeListener(this);
439
Joe Onorato7c312c12009-08-13 21:36:53 -0700440 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700441
Sunny Goyalfe770c92016-09-27 14:38:58 -0700442 restoreState(savedInstanceState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443
Sunny Goyale26d1002016-06-20 14:52:14 -0700444 if (LauncherAppState.PROFILE_STARTUP) {
445 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 }
447
Sunny Goyal2100c782016-08-22 16:00:03 -0700448 // We only load the page synchronously if the user rotates (or triggers a
449 // configuration change) while launcher is in the foreground
Sunny Goyalfe770c92016-09-27 14:38:58 -0700450 int currentScreen = PagedView.INVALID_RESTORE_PAGE;
451 if (savedInstanceState != null) {
452 currentScreen = savedInstanceState.getInt(RUNTIME_STATE_CURRENT_SCREEN, currentScreen);
453 }
454 if (!mModel.startLoader(currentScreen)) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700455 // If we are not binding synchronously, show a fade in animation when
456 // the first page bind completes.
457 mDragLayer.setAlpha(0);
458 } else {
Sunny Goyalfe770c92016-09-27 14:38:58 -0700459 // Pages bound synchronously.
460 mWorkspace.setCurrentPage(currentScreen);
461
Sunny Goyal2100c782016-08-22 16:00:03 -0700462 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800463 }
464
465 // For handling default keys
466 mDefaultKeySsb = new SpannableStringBuilder();
467 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800468
469 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700470 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
471 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800472
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800473 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700474 // In case we are on a device with locked rotation, we should look at preferences to check
475 // if the user has specifically allowed rotation.
476 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700477 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700478 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
479 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700480 }
481
482 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
483 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400484 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700485
Adam Cohen9211d422014-10-07 18:14:53 -0700486 if (mLauncherCallbacks != null) {
487 mLauncherCallbacks.onCreate(savedInstanceState);
488 }
489 }
490
Sunny Goyal7779d622015-06-11 16:18:39 -0700491 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700492 public void onExtractedColorsChanged() {
493 loadExtractedColorsAndColorItems();
494 }
495
496 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700497 // TODO: do this in pre-N as well, once the extraction part is complete.
Tony Wickham345bff32016-09-28 15:34:51 -0700498 if (Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700499 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700500 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700501 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham345bff32016-09-28 15:34:51 -0700502 // It's possible that All Apps is visible when this is run,
503 // so always use light status bar in that case.
504 activateLightStatusBar(isAllAppsVisible());
Tony Wickham827cef22016-03-17 15:39:39 -0700505 }
506 }
507
Tony Wickham345bff32016-09-28 15:34:51 -0700508 /**
509 * Sets the status bar to be light or not. Light status bar means dark icons.
510 * @param activate if true, make sure the status bar is light, otherwise base on wallpaper.
511 */
512 public void activateLightStatusBar(boolean activate) {
513 boolean lightStatusBar = activate
514 || mExtractedColors.getColor(ExtractedColors.STATUS_BAR_INDEX,
515 ExtractedColors.DEFAULT_DARK) == ExtractedColors.DEFAULT_LIGHT;
516 int oldSystemUiFlags = getWindow().getDecorView().getSystemUiVisibility();
517 int newSystemUiFlags = oldSystemUiFlags;
Tony Wickham93113872016-09-14 16:46:32 -0700518 if (lightStatusBar) {
Tony Wickham345bff32016-09-28 15:34:51 -0700519 newSystemUiFlags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
Tony Wickham93113872016-09-14 16:46:32 -0700520 } else {
Tony Wickham345bff32016-09-28 15:34:51 -0700521 newSystemUiFlags &= ~(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Tony Wickham93113872016-09-14 16:46:32 -0700522 }
Tony Wickham345bff32016-09-28 15:34:51 -0700523 if (newSystemUiFlags != oldSystemUiFlags) {
524 getWindow().getDecorView().setSystemUiVisibility(newSystemUiFlags);
525 }
Tony Wickham93113872016-09-14 16:46:32 -0700526 }
527
Adam Cohen9211d422014-10-07 18:14:53 -0700528 private LauncherCallbacks mLauncherCallbacks;
529
530 public void onPostCreate(Bundle savedInstanceState) {
531 super.onPostCreate(savedInstanceState);
532 if (mLauncherCallbacks != null) {
533 mLauncherCallbacks.onPostCreate(savedInstanceState);
534 }
535 }
536
Winson1f064272016-07-18 17:18:02 -0700537 public void onInsetsChanged(Rect insets) {
538 mDeviceProfile.updateInsets(insets);
539 mDeviceProfile.layout(this, true /* notifyListeners */);
540 }
541
Sunny Goyal32554d12015-12-03 15:31:25 -0800542 /**
543 * Call this after onCreate to set or clear overlay.
544 */
545 public void setLauncherOverlay(LauncherOverlay overlay) {
546 if (overlay != null) {
547 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
548 }
549 mWorkspace.setLauncherOverlay(overlay);
550 }
551
Adam Cohen9211d422014-10-07 18:14:53 -0700552 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
553 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700554 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700555 private boolean mWorkspaceImportanceStored = false;
556 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700557 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800558 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700559 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
560
561 @Override
562 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700563 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700564 return;
565 }
566 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700567 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700568 if (mWorkspace != null) {
569 mWorkspaceImportanceForAccessibility =
570 mWorkspace.getImportantForAccessibility();
571 mWorkspace.setImportantForAccessibility(
572 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
573 mWorkspaceImportanceStored = true;
574 }
575 if (mHotseat != null) {
576 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
577 mHotseat.setImportantForAccessibility(
578 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
579 mHotseatImportanceStored = true;
580 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700581 }
582
583 @Override
584 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700585 if (mWorkspaceImportanceStored && mWorkspace != null) {
586 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
587 }
588 if (mHotseatImportanceStored && mHotseat != null) {
589 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
590 }
591 mWorkspaceImportanceStored = false;
592 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700593 }
594 });
Adam Cohen9211d422014-10-07 18:14:53 -0700595 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700596 }
597
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700598 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700599 public void onLauncherProviderChange() {
600 if (mLauncherCallbacks != null) {
601 mLauncherCallbacks.onLauncherProviderChange();
602 }
603 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700604
Adam Cohen9211d422014-10-07 18:14:53 -0700605 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700606 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700607 if (mLauncherCallbacks != null) {
608 return mLauncherCallbacks.hasCustomContentToLeft();
609 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700610 return false;
611 }
612
Dave Hawkeya8881582013-09-17 15:55:33 -0600613 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500614 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600615 * {@link #addToCustomContentPage}. This will only be invoked if
616 * {@link #hasCustomContentToLeft()} is {@code true}.
617 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500618 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700619 if (mLauncherCallbacks != null) {
620 mLauncherCallbacks.populateCustomContentContainer();
621 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600622 }
623
624 /**
Sunny Goyalfe770c92016-09-27 14:38:58 -0700625 * Invoked by subclasses to signal a change to the {@link #addToCustomContentPage} value to
Dave Hawkeya8881582013-09-17 15:55:33 -0600626 * ensure the custom content page is added or removed if necessary.
627 */
628 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600629 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600630 // Not bound yet, wait for bindScreens to be called.
631 return;
632 }
633
634 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
635 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500636 mWorkspace.createCustomContentContainer();
637 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600638 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
639 mWorkspace.removeCustomContentPage();
640 }
641 }
642
Hyunyoung Songaa953652016-04-19 18:30:24 -0700643 public UserEventDispatcher getUserEventDispatcher() {
644 if (mLauncherCallbacks != null) {
645 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
646 if (dispatcher != null) {
647 return dispatcher;
648 }
649 }
650
Sunny Goyal64976d52016-06-20 14:56:28 -0700651 // Logger object is a singleton and does not have to be coupled with the foreground
652 // activity. Since most user event logging is done on the UI, the object is retrieved
653 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700654 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700655 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700656 }
657 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800658 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100659
Hyunyoung Song3f471442015-04-08 19:01:34 -0700660 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700661 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
662 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700663 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700664 }
665
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000666 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700667 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
668 // This cast is safe as long as the id < 0x00FFFFFF
669 // Since we jail all the dynamically generated views, there should be no clashes
670 // with any other views.
671 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000672 }
673
674 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700675 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
676 * a configuration step, this allows the proper animations to run after other transitions.
677 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700678 private long completeAdd(
679 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
680 long screenId = info.screenId;
681 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700682 // When the screen id represents an actual screen (as opposed to a rank) we make sure
683 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700684 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700685 }
Adam Cohendb364c32014-05-20 14:23:40 -0700686
Sunny Goyal2100c782016-08-22 16:00:03 -0700687 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800688 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700689 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700690 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800691 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700692 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700693 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700694 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700695 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700696 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700697 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700698 int widgetId = appWidgetId;
699 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700700 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700701 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700702 // Since the view was just bound, also launch the configure activity if needed
703 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
704 .getLauncherAppWidgetInfo(widgetId);
705 if (provider != null && provider.configure != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700706 startRestoredWidgetReconfigActivity(provider, widgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -0700707 }
708 }
709 break;
710 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800711 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700712
Adam Cohendb364c32014-05-20 14:23:40 -0700713 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800714 }
715
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800716 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700717 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700718 if (isWorkspaceLoading()) {
719 // process the result once the workspace has loaded.
720 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
721 return;
722 }
723 mPendingActivityResult = null;
724
Winson Chunge341d302013-08-16 14:31:00 -0700725 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700726 final PendingRequestArgs requestArgs = mPendingRequestArgs;
727 setWaitingForResult(null);
728 if (requestArgs == null) {
729 return;
730 }
731
732 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700733
Adam Cohenad4e15c2013-10-17 16:21:35 -0700734 Runnable exitSpringLoaded = new Runnable() {
735 @Override
736 public void run() {
737 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
738 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
739 }
740 };
741
Michael Jurka8b805b12012-04-18 14:23:14 -0700742 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700743 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700744 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700745 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
746 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700747 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700748 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700749 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700750 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700751 addAppWidgetImpl(
752 appWidgetId, requestArgs, null,
753 requestArgs.getWidgetProvider(),
754 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700755 }
756 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200757 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
758 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700759 // User could have free-scrolled between pages before picking a wallpaper; make sure
760 // we move to the closest one now.
761 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700762 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200763 }
764 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700765 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200766
Adam Cohened66b2b2012-01-23 17:28:51 -0800767 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700768 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700769
Adam Cohened66b2b2012-01-23 17:28:51 -0800770 // We have special handling for widgets
771 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800772 final int appWidgetId;
773 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
774 : -1;
775 if (widgetId < 0) {
776 appWidgetId = pendingAddWidgetId;
777 } else {
778 appWidgetId = widgetId;
779 }
780
Adam Cohenad4e15c2013-10-17 16:21:35 -0700781 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800782 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700783 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
784 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700785 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700786 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700787 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700788 @Override
789 public void run() {
790 exitSpringLoadedDragModeDelayed(false, 0, null);
791 }
792 };
Adam Cohendb364c32014-05-20 14:23:40 -0700793
Sunny Goyal2100c782016-08-22 16:00:03 -0700794 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
795 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
796 } else {
797 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
798 // When the screen id represents an actual screen (as opposed to a rank)
799 // we make sure that the drop page actually exists.
800 requestArgs.screenId =
801 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700802 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700803 final CellLayout dropLayout =
804 mWorkspace.getScreenWithId(requestArgs.screenId);
805
806 dropLayout.setDropPending(true);
807 final Runnable onComplete = new Runnable() {
808 @Override
809 public void run() {
810 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
811 dropLayout.setDropPending(false);
812 }
813 };
814 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
815 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700816 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800817 return;
818 }
819
Sunny Goyald478c832016-04-01 12:04:16 -0700820 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
821 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700822 if (resultCode == RESULT_OK) {
823 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700824 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700825 }
826 // Leave the widget in the pending state if the user canceled the configure.
827 return;
828 }
829
Sunny Goyalaad90582015-07-09 14:22:50 -0700830 if (requestCode == REQUEST_CREATE_SHORTCUT) {
831 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700832 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
833 completeAdd(requestCode, data, -1, requestArgs);
834 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
835 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
836
Sunny Goyalaad90582015-07-09 14:22:50 -0700837 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700838 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
839 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800842 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800843 }
844
845 @Override
846 protected void onActivityResult(
847 final int requestCode, final int resultCode, final Intent data) {
848 handleActivityResult(requestCode, resultCode, data);
849 if (mLauncherCallbacks != null) {
850 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
851 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800852 }
853
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600854 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700855 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600856 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700857 PendingRequestArgs pendingArgs = mPendingRequestArgs;
858 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
859 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
860 setWaitingForResult(null);
861
Sunny Goyal28c6b962015-10-12 11:42:05 -0700862 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700863 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700864 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700865 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700866 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700867 Intent intent = pendingArgs.getPendingIntent();
868
Sunny Goyal28c6b962015-10-12 11:42:05 -0700869 if (grantResults.length > 0
870 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700871 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700872 } else {
873 // TODO: Show a snack bar with link to settings
874 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700875 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700876 }
877 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600878 if (mLauncherCallbacks != null) {
879 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
880 grantResults);
881 }
882 }
883
Adam Cohendb364c32014-05-20 14:23:40 -0700884 /**
885 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
886 *
887 * @param screenId the screen id to check
888 * @return the new screen, or screenId if it exists
889 */
890 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800891 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700892 if (dropLayout == null) {
893 // it's possible that the add screen was removed because it was
894 // empty and a re-bind occurred
895 mWorkspace.addExtraEmptyScreen();
896 return mWorkspace.commitExtraEmptyScreen();
897 } else {
898 return screenId;
899 }
900 }
901
Sunny Goyal2100c782016-08-22 16:00:03 -0700902 @Thunk void completeTwoStageWidgetDrop(
903 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
904 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800905 Runnable onCompleteRunnable = null;
906 int animationType = 0;
907
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700908 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800909 if (resultCode == RESULT_OK) {
910 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
911 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal2100c782016-08-22 16:00:03 -0700912 requestArgs.getWidgetProvider());
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700913 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800914 onCompleteRunnable = new Runnable() {
915 @Override
916 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700917 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700918 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
919 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800920 }
921 };
922 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800923 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800924 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800925 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700926 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700927 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700928 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
929 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700930 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700931 // The animated view may be null in the case of a rotation during widget configuration
932 onCompleteRunnable.run();
933 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800934 }
935
936 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700937 protected void onStop() {
938 super.onStop();
939 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700940
941 if (mLauncherCallbacks != null) {
942 mLauncherCallbacks.onStop();
943 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700944
945 if (Utilities.isNycMR1OrAbove()) {
946 mAppWidgetHost.stopListening();
947 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700948 }
949
950 @Override
951 protected void onStart() {
952 super.onStart();
953 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700954
955 if (mLauncherCallbacks != null) {
956 mLauncherCallbacks.onStart();
957 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700958
959 if (Utilities.isNycMR1OrAbove()) {
960 mAppWidgetHost.startListening();
961 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700962 }
963
964 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200966 long startTime = 0;
967 if (DEBUG_RESUME_TIME) {
968 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400969 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200970 }
Adam Cohen9211d422014-10-07 18:14:53 -0700971
972 if (mLauncherCallbacks != null) {
973 mLauncherCallbacks.preOnResume();
974 }
975
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700977 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700978
Winson Chung4a2afa32012-07-19 14:53:05 -0700979 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700980 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700981 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800982 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700983 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700984 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700985 // view after launching an app, as they may be depending on the UI to be static to
986 // switch to another app, otherwise, if it was
Hyunyoung Songdd60ce42016-07-27 17:08:38 -0700987 showAppsView(false /* animated */, !launchedFromApp /* updatePredictedApps */,
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700988 mAppsView.shouldRestoreImeState() /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800989 } else if (mOnResumeState == State.WIDGETS) {
990 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700991 }
992 mOnResumeState = State.NONE;
993
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800994 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700995 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700996 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700997 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700998 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001000 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001001 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1002 // execute them here
1003 long startTimeCallbacks = 0;
1004 if (DEBUG_RESUME_TIME) {
1005 startTimeCallbacks = System.currentTimeMillis();
1006 }
1007
Michael Jurka1e2f4652013-07-08 18:03:46 -07001008 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1009 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001010 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001011 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001012 if (DEBUG_RESUME_TIME) {
1013 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1014 (System.currentTimeMillis() - startTimeCallbacks));
1015 }
Michael Jurka447bf842013-05-15 14:52:15 +02001016 }
Michael Jurka54554252013-08-01 12:52:23 +02001017 if (mOnResumeCallbacks.size() > 0) {
1018 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1019 mOnResumeCallbacks.get(i).run();
1020 }
1021 mOnResumeCallbacks.clear();
1022 }
Winson Chunge4e50662012-01-23 14:45:13 -08001023
1024 // Reset the pressed state of icons that were locked in the press state while activities
1025 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001026 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001027 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001028 mWaitingForResume.setStayPressed(false);
1029 }
Winson Chung82963d52013-10-09 11:20:57 -07001030
Adam Cohen06dff352012-06-01 17:17:08 -07001031 // It is possible that widgets can receive updates while launcher is not in the foreground.
1032 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1033 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1034 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001035 if (!isWorkspaceLoading()) {
1036 getWorkspace().reinflateWidgetsIfNecessary();
1037 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001038
Michael Jurka447bf842013-05-15 14:52:15 +02001039 if (DEBUG_RESUME_TIME) {
1040 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1041 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001042
Adam Cohen4b66bf32015-09-18 12:15:19 -07001043 // We want to suppress callbacks about CustomContent being shown if we have just received
1044 // onNewIntent while the user was present within launcher. In that case, we post a call
1045 // to move the user to the main screen (which will occur after onResume). We don't want to
1046 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1047 // suppress here.
1048 if (mWorkspace.getCustomContentCallbacks() != null
1049 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001050 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001051 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001052 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1053 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001054 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001055 }
1056 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001057 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001058 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001059 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001060
Sunny Goyal756adbc2015-04-16 15:20:51 -07001061 if (!isWorkspaceLoading()) {
1062 // Process any items that were added while Launcher was away.
1063 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001064
1065 // Refresh shortcuts if the permission changed.
1066 mModel.refreshShortcutsIfRequired();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001067 }
Adam Cohen9211d422014-10-07 18:14:53 -07001068
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001069 if (shouldShowDiscoveryBounce()) {
1070 mAllAppsController.showDiscoveryBounce();
1071 }
1072 mIsResumeFromActionScreenOff = false;
Adam Cohen9211d422014-10-07 18:14:53 -07001073 if (mLauncherCallbacks != null) {
1074 mLauncherCallbacks.onResume();
1075 }
Adam Cohen06dff352012-06-01 17:17:08 -07001076 }
1077
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001079 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001080 // Ensure that items added to Launcher are queued until Launcher returns
1081 InstallShortcutReceiver.enableInstallQueue();
1082
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001083 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001084 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001085 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001086 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001087
1088 // We call onHide() aggressively. The custom content callbacks should be able to
1089 // debounce excess onHide calls.
1090 if (mWorkspace.getCustomContentCallbacks() != null) {
1091 mWorkspace.getCustomContentCallbacks().onHide();
1092 }
Romain Guycbb89e42009-06-08 15:52:54 -07001093
Adam Cohen9211d422014-10-07 18:14:53 -07001094 if (mLauncherCallbacks != null) {
1095 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001096 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001097 }
1098
1099 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001100 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1101 // by a onResume or by scrolling otherwise.
1102 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001103
1104 // Custom content is completely hidden
1105 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001106
1107 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1108 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001109
1110 // Indicates whether the user is allowed to scroll away from the custom content.
1111 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001112 }
1113
Adam Cohenc2d6e892014-10-16 09:49:24 -07001114 public interface LauncherOverlay {
1115
1116 /**
1117 * Touch interaction leading to overscroll has begun
1118 */
1119 public void onScrollInteractionBegin();
1120
1121 /**
1122 * Touch interaction related to overscroll has ended
1123 */
1124 public void onScrollInteractionEnd();
1125
1126 /**
1127 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1128 * screen (or in the case of RTL, the rightmost screen).
1129 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001130 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001131
1132 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001133 * Called when the launcher is ready to use the overlay
1134 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001135 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001136 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001137 }
1138
Jun Mukai8af0cd82015-05-12 12:32:12 -07001139 public interface LauncherSearchCallbacks {
1140 /**
1141 * Called when the search overlay is shown.
1142 */
1143 public void onSearchOverlayOpened();
1144
1145 /**
1146 * Called when the search overlay is dismissed.
1147 */
1148 public void onSearchOverlayClosed();
1149 }
1150
Adam Cohenc2d6e892014-10-16 09:49:24 -07001151 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001152 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001153 }
1154
1155 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1156
Sunny Goyalc86df472016-02-25 09:19:38 -08001157 public void onScrollChanged(float progress) {
1158 if (mWorkspace != null) {
1159 mWorkspace.onOverlayScrollChanged(progress);
1160 }
1161 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001162 }
1163
Jorim Jaggid017f882014-01-14 17:08:48 -08001164 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001165 if (mLauncherCallbacks != null) {
1166 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001167 } else {
1168 // On devices with a locked orientation, we will at least have the allow rotation
1169 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001170 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001171 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001172 }
1173
Adam Cohen9211d422014-10-07 18:14:53 -07001174 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001175 CustomContentCallbacks callbacks, String description) {
1176 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001177 }
1178
Adam Cohenedb40762013-07-18 16:45:45 -07001179 // The custom content needs to offset its content to account for the QSB
1180 public int getTopOffsetForCustomContent() {
1181 return mWorkspace.getPaddingTop();
1182 }
1183
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001184 @Override
1185 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001186 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001187 if (mModel.isCurrentCallbacks(this)) {
1188 mModel.stopLoader();
1189 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001190 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1191
Romain Guy13c2e7b2010-03-10 19:45:00 -08001192 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001193 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001194
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001195 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001196 @Override
1197 public void onWindowFocusChanged(boolean hasFocus) {
1198 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001199 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001200
1201 if (mLauncherCallbacks != null) {
1202 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1203 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001204 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001205
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 private boolean acceptFilter() {
1207 final InputMethodManager inputManager = (InputMethodManager)
1208 getSystemService(Context.INPUT_METHOD_SERVICE);
1209 return !inputManager.isFullscreenMode();
1210 }
1211
1212 @Override
1213 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001214 final int uniChar = event.getUnicodeChar();
1215 final boolean handled = super.onKeyDown(keyCode, event);
1216 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1217 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1219 keyCode, event);
1220 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001221 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001222 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001223 // showSearchDialog()
1224 // If there are multiple keystrokes before the search dialog takes focus,
1225 // onSearchRequested() will be called for every keystroke,
1226 // but it is idempotent, so it's fine.
1227 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 }
1229 }
1230
Joe Onorato8a9625e2010-01-28 15:55:35 -08001231 // Eat the long press event so the keyboard doesn't come up.
1232 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1233 return true;
1234 }
1235
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 return handled;
1237 }
1238
Winson46163472015-09-22 17:31:56 -07001239 @Override
1240 public boolean onKeyUp(int keyCode, KeyEvent event) {
1241 if (keyCode == KeyEvent.KEYCODE_MENU) {
1242 // Ignore the menu key if we are currently dragging or are on the custom content screen
1243 if (!isOnCustomContent() && !mDragController.isDragging()) {
1244 // Close any open folders
1245 closeFolder();
1246
Tony Wickham49c8d292016-07-01 11:44:34 -07001247 // Close any shortcuts containers
1248 closeShortcutsContainer();
1249
Winson46163472015-09-22 17:31:56 -07001250 // Stop resizing any widgets
1251 mWorkspace.exitWidgetResizeMode();
1252
1253 // Show the overview mode if we are on the workspace
1254 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1255 !mWorkspace.isSwitchingState()) {
1256 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001257 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001258 }
1259 }
1260 return true;
1261 }
1262 return super.onKeyUp(keyCode, event);
1263 }
1264
Karl Rosaen138a0412009-04-23 19:00:21 -07001265 private String getTypedText() {
1266 return mDefaultKeySsb.toString();
1267 }
1268
Sunny Goyal6f28e712016-09-13 14:19:29 -07001269 @Override
1270 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001271 mDefaultKeySsb.clear();
1272 mDefaultKeySsb.clearSpans();
1273 Selection.setSelection(mDefaultKeySsb, 0);
1274 }
1275
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001276 /**
1277 * Restores the previous state, if it exists.
1278 *
1279 * @param savedState The previous state.
1280 */
1281 private void restoreState(Bundle savedState) {
1282 if (savedState == null) {
1283 return;
1284 }
1285
Sunny Goyalfe770c92016-09-27 14:38:58 -07001286 int stateOrdinal = savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal());
1287 State[] stateValues = State.values();
1288 State state = (stateOrdinal >= 0 && stateOrdinal < stateValues.length)
1289 ? stateValues[stateOrdinal] : State.WORKSPACE;
Winson Chungb745afb2015-03-02 11:51:23 -08001290 if (state == State.APPS || state == State.WIDGETS) {
1291 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001292 }
1293
Sunny Goyal2100c782016-08-22 16:00:03 -07001294 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1295 if (requestArgs != null) {
1296 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001298
1299 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 }
1301
1302 /**
1303 * Finds all the views we need and configure them properly.
1304 */
1305 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001306 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001307 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001308 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001309 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyal6178f132016-07-11 17:30:03 -07001310 mQsbContainer = mDragLayer.findViewById(mDeviceProfile.isVerticalBarLayout()
1311 ? R.id.workspace_blocked_row : R.id.qsb_container);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001312 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001313
Sunny Goyal784f9c32016-03-23 16:56:54 -07001314 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1315 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1316 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317
Winson Chung4c98d922011-05-31 16:50:48 -07001318 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001319 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001320
Winson Chung3d503fb2011-07-13 17:25:49 -07001321 // Setup the hotseat
1322 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1323 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001324 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001325 }
1326
Winsone9f27272015-10-13 10:47:51 -07001327 // Setup the overview panel
1328 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001329
Winson Chung4c98d922011-05-31 16:50:48 -07001330 // Setup the workspace
1331 mWorkspace.setHapticFeedbackEnabled(false);
1332 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001333 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001334 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1335 // default state, otherwise we will update to the wrong offsets in RTL
1336 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001337 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001338 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001339
Tony Wickham34d2c912015-09-11 09:27:58 -07001340 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001341 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001342
Winson Chungef7f8742015-06-04 17:18:17 -07001343 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001344 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001345 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001346 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1347 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1348 } else {
1349 mAppsView.setSearchBarController(new DefaultAppSearchController());
1350 }
Craig Mautner360310b2012-10-26 15:13:08 -07001351
Winson Chung3d503fb2011-07-13 17:25:49 -07001352 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001353 mDragController.setDragScoller(mWorkspace);
1354 mDragController.setScrollView(mDragLayer);
1355 mDragController.setMoveTarget(mWorkspace);
1356 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001357 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001358
Peter Schiller310a9882016-06-30 13:52:36 -07001359 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1360 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1361 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001362
Sunny Goyal322d5562015-06-25 19:35:49 -07001363 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1364 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001365 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 }
1367
Winsone9f27272015-10-13 10:47:51 -07001368 private void setupOverviewPanel() {
1369 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1370
1371 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1372 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1373 @Override
1374 public boolean onLongClick(View v) {
1375 return v.performClick();
1376 }
1377 };
1378
1379 // Bind wallpaper button actions
1380 View wallpaperButton = findViewById(R.id.wallpaper_button);
1381 wallpaperButton.setOnClickListener(new OnClickListener() {
1382 @Override
1383 public void onClick(View view) {
1384 if (!mWorkspace.isSwitchingState()) {
1385 onClickWallpaperPicker(view);
1386 }
1387 }
1388 });
1389 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1390 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1391
1392 // Bind widget button actions
1393 mWidgetsButton = findViewById(R.id.widget_button);
1394 mWidgetsButton.setOnClickListener(new OnClickListener() {
1395 @Override
1396 public void onClick(View view) {
1397 if (!mWorkspace.isSwitchingState()) {
1398 onClickAddWidgetButton(view);
1399 }
1400 }
1401 });
1402 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1403 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1404
1405 // Bind settings actions
1406 View settingsButton = findViewById(R.id.settings_button);
1407 boolean hasSettings = hasSettings();
1408 if (hasSettings) {
1409 settingsButton.setOnClickListener(new OnClickListener() {
1410 @Override
1411 public void onClick(View view) {
1412 if (!mWorkspace.isSwitchingState()) {
1413 onClickSettingsButton(view);
1414 }
1415 }
1416 });
1417 settingsButton.setOnLongClickListener(performClickOnLongClick);
1418 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1419 } else {
1420 settingsButton.setVisibility(View.GONE);
1421 }
1422
1423 mOverviewPanel.setAlpha(0f);
1424 }
1425
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001427 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001428 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001429 */
1430 public void setAllAppsButton(View allAppsButton) {
1431 mAllAppsButton = allAppsButton;
1432 }
1433
Sunny Goyalbb011da2016-06-15 15:42:29 -07001434 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001435 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001436 }
1437
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001438 public View getWidgetsButton() {
1439 return mWidgetsButton;
1440 }
1441
Anjali Koppal5ad44842014-03-10 20:34:39 -07001442 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 * Creates a view representing a shortcut.
1444 *
1445 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001447 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001448 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 }
1450
1451 /**
1452 * Creates a view representing a shortcut inflated from the specified resource.
1453 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 * @param parent The group the shortcut belongs to.
1455 * @param info The data structure describing the shortcut.
1456 *
1457 * @return A View inflated from layoutResId.
1458 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001459 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001460 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001461 parent, false);
1462 favorite.applyFromShortcutInfo(info, mIconCache);
1463 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001465 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 return favorite;
1467 }
1468
1469 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 * Add a shortcut to the workspace.
1471 *
1472 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001474 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001475 int cellY, PendingRequestArgs args) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001476 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001477 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001478
Sunny Goyal5c97f512015-05-19 16:03:28 -07001479 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001480 if (info == null || args.getRequestCode() != REQUEST_CREATE_SHORTCUT ||
1481 args.getPendingIntent().getComponent() == null) {
1482 return;
1483 }
1484 if (!PackageManagerHelper.hasPermissionForActivity(
1485 this, info.intent, args.getPendingIntent().getComponent().getPackageName())) {
1486 // The app is trying to add a shortcut without sufficient permissions
1487 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
Adam Cohend9198822011-11-22 16:42:47 -08001488 return;
1489 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001490 final View view = createShortcut(info);
1491
Sunny Goyal5c97f512015-05-19 16:03:28 -07001492 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001493 // First we check if we already know the exact location where we want to add this item.
1494 if (cellX >= 0 && cellY >= 0) {
1495 cellXY[0] = cellX;
1496 cellXY[1] = cellY;
1497 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001498
1499 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001500 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001501 true, null,null)) {
1502 return;
1503 }
1504 DragObject dragObject = new DragObject();
1505 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001506 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1507 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001508 return;
1509 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001510 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001511 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001512 }
1513
1514 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001515 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001516 return;
1517 }
1518
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001519 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520
Sunny Goyal2100c782016-08-22 16:00:03 -07001521 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
1522 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 }
1524
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001526 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001528 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001530 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001531 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1532
Adam Cohened66b2b2012-01-23 17:28:51 -08001533 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001534 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001535 }
Romain Guycbb89e42009-06-08 15:52:54 -07001536
Adam Cohen59400422014-03-05 18:07:04 -08001537 if (appWidgetInfo.isCustomWidget) {
1538 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001539 }
1540
Adam Cohen59400422014-03-05 18:07:04 -08001541 LauncherAppWidgetInfo launcherInfo;
1542 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001543 launcherInfo.spanX = itemInfo.spanX;
1544 launcherInfo.spanY = itemInfo.spanY;
1545 launcherInfo.minSpanX = itemInfo.minSpanX;
1546 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001547 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001548
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001550 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551
Sunny Goyal2100c782016-08-22 16:00:03 -07001552 if (hostView == null) {
1553 // Perform actual inflation because we're live
1554 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001556 hostView.setVisibility(View.VISIBLE);
1557 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 }
Romain Guycbb89e42009-06-08 15:52:54 -07001559
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001560 private void addAppWidgetToWorkspace(
1561 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001562 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001563 hostView.setTag(item);
1564 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001565
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001566 hostView.setFocusable(true);
1567 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001568
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001569 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001570 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1571
1572 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001573 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001574 }
1575 }
1576
Adam Cohended9f8d2010-11-03 13:25:16 -07001577 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1578 @Override
1579 public void onReceive(Context context, Intent intent) {
1580 final String action = intent.getAction();
1581 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1582 mUserPresent = false;
Sunny Goyal6ad72f02016-09-23 11:01:10 -07001583 mDragLayer.clearResizeFrame();
Winson Chungb745afb2015-03-02 11:51:23 -08001584 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001585
Winson Chungc100e8e2011-08-09 16:02:43 -07001586 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001587 // processing a multi-step drop
Sunny Goyal2100c782016-08-22 16:00:03 -07001588 if (mAppsView != null && mWidgetsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001589 if (!showWorkspace(false)) {
1590 // If we are already on the workspace, then manually reset all apps
1591 mAppsView.reset();
1592 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001593 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001594 mIsResumeFromActionScreenOff = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001595 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1596 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001597 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001598 }
1599 }
1600 };
1601
1602 @Override
1603 public void onAttachedToWindow() {
1604 super.onAttachedToWindow();
1605
1606 // Listen for broadcasts related to user-presence
1607 final IntentFilter filter = new IntentFilter();
1608 filter.addAction(Intent.ACTION_SCREEN_OFF);
1609 filter.addAction(Intent.ACTION_USER_PRESENT);
1610 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001611 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001612 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001613 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001614
1615 if (mLauncherCallbacks != null) {
1616 mLauncherCallbacks.onAttachedToWindow();
1617 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001618 }
1619
1620 @Override
1621 public void onDetachedFromWindow() {
1622 super.onDetachedFromWindow();
1623 mVisible = false;
1624
Adam Cohend113e0c2010-11-11 10:48:05 -08001625 if (mAttached) {
1626 unregisterReceiver(mReceiver);
1627 mAttached = false;
1628 }
Winson Chungb745afb2015-03-02 11:51:23 -08001629 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001630
1631 if (mLauncherCallbacks != null) {
1632 mLauncherCallbacks.onDetachedFromWindow();
1633 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001634 }
1635
1636 public void onWindowVisibilityChanged(int visibility) {
1637 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001638 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001639 // The following code used to be in onResume, but it turns out onResume is called when
1640 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1641 // is a more appropriate event to handle
1642 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001643 if (!mWorkspaceLoading) {
1644 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001645 // We want to let Launcher draw itself at least once before we force it to build
1646 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001647 // apps is nice and speedy.
1648 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001649 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001650 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001651 if (mStarted) return;
1652 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001653 // We delay the layer building a bit in order to give
1654 // other message processing a time to run. In particular
1655 // this avoids a delay in hiding the IME if it was
1656 // currently shown, because doing that may involve
1657 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001658 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001659 final ViewTreeObserver.OnDrawListener listener = this;
1660 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001661 public void run() {
1662 if (mWorkspace != null &&
1663 mWorkspace.getViewTreeObserver() != null) {
1664 mWorkspace.getViewTreeObserver().
1665 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001666 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001667 }
1668 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001669 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001670 }
1671 });
1672 }
1673 clearTypedText();
1674 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001675 }
1676
Adam Cohen091440a2015-03-18 14:16:05 -07001677 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001678 mHandler.removeMessages(ADVANCE_MSG);
1679 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1680 mHandler.sendMessageDelayed(msg, delay);
1681 mAutoAdvanceSentTime = System.currentTimeMillis();
1682 }
1683
Adam Cohen091440a2015-03-18 14:16:05 -07001684 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001685 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1686 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1687 mAutoAdvanceRunning = autoAdvanceRunning;
1688 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001689 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001690 sendAdvanceMessage(delay);
1691 } else {
1692 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001693 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001694 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1695 }
1696 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001697 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001698 }
1699 }
1700 }
1701
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001702 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1703
Adam Cohended9f8d2010-11-03 13:25:16 -07001704 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001705 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001706 if (msg.what == ADVANCE_MSG) {
1707 int i = 0;
1708 for (View key: mWidgetsToAdvance.keySet()) {
1709 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001710 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001711 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001712 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001713 public void run() {
1714 ((Advanceable) v).advance();
1715 }
1716 }, delay);
1717 }
1718 i++;
1719 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001720 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001721 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001722 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001723 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001724 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001725
Winsonc0b52fe2015-09-09 16:38:15 -07001726 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001727 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001728 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1729 if (v instanceof Advanceable) {
1730 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001731 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001732 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001733 }
1734 }
1735
Winsonc0b52fe2015-09-09 16:38:15 -07001736 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001737 if (mWidgetsToAdvance.containsKey(hostView)) {
1738 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001739 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001740 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001741 }
1742
Hyunyoung Song3f471442015-04-08 19:01:34 -07001743 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001744 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1745 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001746 }
1747
Winson Chunga6945242014-01-08 14:04:34 -08001748 public DragLayer getDragLayer() {
1749 return mDragLayer;
1750 }
1751
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001752 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001753 return mAppsView;
1754 }
1755
Hyunyoung Song3f471442015-04-08 19:01:34 -07001756 public WidgetsContainerView getWidgetsView() {
1757 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001758 }
1759
Winson Chunga6945242014-01-08 14:04:34 -08001760 public Workspace getWorkspace() {
1761 return mWorkspace;
1762 }
1763
Sunny Goyal6178f132016-07-11 17:30:03 -07001764 public View getQsbContainer() {
1765 return mQsbContainer;
1766 }
1767
Winson Chunga6945242014-01-08 14:04:34 -08001768 public Hotseat getHotseat() {
1769 return mHotseat;
1770 }
1771
Jorim Jaggid017f882014-01-14 17:08:48 -08001772 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001773 return mOverviewPanel;
1774 }
1775
Sunny Goyal47328fd2016-05-25 18:56:41 -07001776 public DropTargetBar getDropTargetBar() {
1777 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001778 }
1779
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001780 public LauncherAppWidgetHost getAppWidgetHost() {
1781 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001782 }
Romain Guycbb89e42009-06-08 15:52:54 -07001783
Winson Chunga9abd0e2010-10-27 17:18:37 -07001784 public LauncherModel getModel() {
1785 return mModel;
1786 }
1787
Adam Cohen79d90c52016-04-22 13:29:20 -07001788 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001789 return mSharedPrefs;
1790 }
1791
Adam Cohen2e6da152015-05-06 11:42:25 -07001792 public DeviceProfile getDeviceProfile() {
1793 return mDeviceProfile;
1794 }
1795
Bjorn Bringertc459e522013-06-07 19:36:01 +01001796 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001797 getWindow().closeAllPanels();
1798
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001799 // Whatever we were doing is hereby canceled.
Sunny Goyal2100c782016-08-22 16:00:03 -07001800 setWaitingForResult(null);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001801 }
1802
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803 @Override
1804 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001805 long startTime = 0;
1806 if (DEBUG_RESUME_TIME) {
1807 startTime = System.currentTimeMillis();
1808 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 super.onNewIntent(intent);
1810
Winson15f8b172015-08-19 11:02:39 -07001811 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1812 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1813 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Sunny Goyal85313732016-09-28 12:00:07 -07001814
1815 // Check this condition before handling isActionMain, as this will get reset.
1816 boolean shouldMoveToDefaultScreen = alreadyOnHome &&
1817 mState == State.WORKSPACE && getTopFloatingView() == null;
1818
Winson15f8b172015-08-19 11:02:39 -07001819 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1820 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001821 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001822 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001823
Adam Cohen6fecd412013-10-02 17:41:50 -07001824 if (mWorkspace == null) {
1825 // Can be cases where mWorkspace is null, this prevents a NPE
1826 return;
1827 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001828 // In all these cases, only animate if we're already on home
1829 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001830
Sunny Goyal935fca12015-10-13 10:19:01 -07001831 closeFolder(alreadyOnHome);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001832 closeShortcutsContainer(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001833 exitSpringLoadedDragMode();
1834
1835 // If we are already on home, then just animate back to the workspace,
1836 // otherwise, just wait until onResume to set the state back to Workspace
1837 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001838 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001839 } else {
1840 mOnResumeState = State.WORKSPACE;
1841 }
1842
1843 final View v = getWindow().peekDecorView();
1844 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001845 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001846 INPUT_METHOD_SERVICE);
1847 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1848 }
1849
Winson Chungb745afb2015-03-02 11:51:23 -08001850 // Reset the apps view
1851 if (!alreadyOnHome && mAppsView != null) {
1852 mAppsView.scrollToTop();
1853 }
1854
Hyunyoung Song3f471442015-04-08 19:01:34 -07001855 // Reset the widgets view
1856 if (!alreadyOnHome && mWidgetsView != null) {
1857 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001858 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001859
Adam Cohen9211d422014-10-07 18:14:53 -07001860 if (mLauncherCallbacks != null) {
1861 mLauncherCallbacks.onHomeIntent();
1862 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 }
Adam Cohened307df2013-10-02 09:37:31 -07001864
Adam Cohen9211d422014-10-07 18:14:53 -07001865 if (mLauncherCallbacks != null) {
1866 mLauncherCallbacks.onNewIntent(intent);
1867 }
Winson15f8b172015-08-19 11:02:39 -07001868
1869 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1870 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1871 // animation.
1872 if (isActionMain) {
Sunny Goyal85313732016-09-28 12:00:07 -07001873 boolean callbackAllowsMoveToDefaultScreen = mLauncherCallbacks != null ?
Ivan Lee667d6882015-09-03 10:16:07 -06001874 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Sunny Goyal85313732016-09-28 12:00:07 -07001875 if (shouldMoveToDefaultScreen && !mWorkspace.isTouchActive()
1876 && callbackAllowsMoveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001877
1878 // We use this flag to suppress noisy callbacks above custom content state
1879 // from onResume.
1880 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001881 mWorkspace.post(new Runnable() {
1882 @Override
1883 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001884 if (mWorkspace != null) {
1885 mWorkspace.moveToDefaultScreen(true);
1886 }
Winson15f8b172015-08-19 11:02:39 -07001887 }
1888 });
1889 }
1890 }
1891
1892 if (DEBUG_RESUME_TIME) {
1893 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1894 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001895 }
1896
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001898 public void onRestoreInstanceState(Bundle state) {
1899 super.onRestoreInstanceState(state);
1900 for (int page: mSynchronouslyBoundPages) {
1901 mWorkspace.restoreInstanceStateForChild(page);
1902 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 }
1904
1905 @Override
1906 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001907 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001908 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1909 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001910
Adam Cohen21cd0022013-10-09 18:57:02 -07001911 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001912 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913
Michael Jurka883f55b2010-10-21 15:47:14 -07001914 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001915 // We close any open folder since it will not be re-opened, and we need to make sure
1916 // this state is reflected.
1917 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1918 closeFolder(false);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001919 closeShortcutsContainer(false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001920
Sunny Goyal2100c782016-08-22 16:00:03 -07001921 if (mPendingRequestArgs != null) {
1922 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1923 }
1924 if (mPendingActivityResult != null) {
1925 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926 }
1927
Adam Cohen9211d422014-10-07 18:14:53 -07001928 if (mLauncherCallbacks != null) {
1929 mLauncherCallbacks.onSaveInstanceState(outState);
1930 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 }
1932
1933 @Override
1934 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001936
Winson Chunge7a03942011-08-05 15:05:12 -07001937 // Remove all pending runnables
1938 mHandler.removeMessages(ADVANCE_MSG);
1939 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001940 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001941 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001942
Winson Chungcd2b0142011-06-08 16:02:26 -07001943 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001944 // It's possible to receive onDestroy after a new Launcher activity has
1945 // been created. In this case, don't interfere with the new Launcher.
1946 if (mModel.isCurrentCallbacks(this)) {
1947 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001948 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001949 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001950
Sunny Goyal745bad92016-05-02 10:54:12 -07001951 if (mRotationPrefChangeHandler != null) {
1952 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1953 }
1954
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001956 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001958 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001960 mAppWidgetHost = null;
1961
1962 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963
1964 TextKeyListener.getInstance().release();
1965
Tony Wickhame2217252016-03-22 16:34:23 -07001966 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1967 .removeAccessibilityStateChangeListener(this);
1968
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001969 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001970
Michael Jurka2ecf9952012-06-18 12:52:28 -07001971 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001972
1973 if (mLauncherCallbacks != null) {
1974 mLauncherCallbacks.onDestroy();
1975 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 }
1977
Sunny Goyalae502842016-06-17 08:43:56 -07001978 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1979 return mAccessibilityDelegate;
1980 }
1981
Adam Cohena9cf38f2011-05-02 15:36:58 -07001982 public DragController getDragController() {
1983 return mDragController;
1984 }
1985
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001987 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001988 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001989 }
1990
1991 @Override
1992 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1993 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001994 try {
1995 super.startIntentSenderForResult(intent, requestCode,
1996 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1997 } catch (IntentSender.SendIntentException e) {
1998 throw new ActivityNotFoundException();
1999 }
2000 }
2001
Winson Chung70d72102011-08-12 11:24:35 -07002002 /**
2003 * Indicates that we want global search for this activity by setting the globalSearch
2004 * argument for {@link #startSearch} to true.
2005 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002006 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002007 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002009
Karl Rosaen138a0412009-04-23 19:00:21 -07002010 if (initialQuery == null) {
2011 // Use any text typed in the launcher as the initial query
2012 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002013 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 if (appSearchData == null) {
2015 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002016 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002018
Sunny Goyal6f28e712016-09-13 14:19:29 -07002019 if (mLauncherCallbacks == null ||
2020 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
2021 // Starting search from the callbacks failed. Start the default global search.
2022 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01002023 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002024
2025 // We need to show the workspace after starting the search
2026 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002027 }
2028
Ian Parkinson047036e2014-09-01 15:40:53 +01002029 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07002030 * Starts the global search activity. This code is a copied from SearchManager
2031 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07002032 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002033 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002034 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002035 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2036 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2037 if (globalSearchActivity == null) {
2038 Log.w(TAG, "No global search activity found.");
2039 return;
2040 }
2041 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2042 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2043 intent.setComponent(globalSearchActivity);
2044 // Make sure that we have a Bundle to put source in
2045 if (appSearchData == null) {
2046 appSearchData = new Bundle();
2047 } else {
2048 appSearchData = new Bundle(appSearchData);
2049 }
Adam Cohen9211d422014-10-07 18:14:53 -07002050 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002051 if (!appSearchData.containsKey("source")) {
2052 appSearchData.putString("source", getPackageName());
2053 }
2054 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2055 if (!TextUtils.isEmpty(initialQuery)) {
2056 intent.putExtra(SearchManager.QUERY, initialQuery);
2057 }
2058 if (selectInitialQuery) {
2059 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2060 }
2061 intent.setSourceBounds(sourceBounds);
2062 try {
2063 startActivity(intent);
2064 } catch (ActivityNotFoundException ex) {
2065 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2066 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067 }
2068
Yura4f93ec62014-02-04 14:15:21 +00002069 public boolean isOnCustomContent() {
2070 return mWorkspace.isOnOrMovingToCustomContent();
2071 }
2072
Winson Chung70d72102011-08-12 11:24:35 -07002073 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002074 public boolean onPrepareOptionsMenu(Menu menu) {
2075 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002076 if (mLauncherCallbacks != null) {
2077 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2078 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002079 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002080 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002081
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002082 @Override
2083 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002084 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002085 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002086 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002087 }
2088
Joe Onorato9c1289c2009-08-17 11:03:03 -04002089 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07002090 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002091 }
2092
Adam Cohen517a7f52014-03-01 12:12:59 -08002093 public boolean isWorkspaceLoading() {
2094 return mWorkspaceLoading;
2095 }
2096
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002097 private void setWorkspaceLoading(boolean value) {
2098 boolean isLocked = isWorkspaceLocked();
2099 mWorkspaceLoading = value;
2100 if (isLocked != isWorkspaceLocked()) {
2101 onWorkspaceLockedChanged();
2102 }
2103 }
2104
Sunny Goyal2100c782016-08-22 16:00:03 -07002105 private void setWaitingForResult(PendingRequestArgs args) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002106 boolean isLocked = isWorkspaceLocked();
Sunny Goyal2100c782016-08-22 16:00:03 -07002107 mPendingRequestArgs = args;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002108 if (isLocked != isWorkspaceLocked()) {
2109 onWorkspaceLockedChanged();
2110 }
2111 }
2112
Adam Cohen9211d422014-10-07 18:14:53 -07002113 protected void onWorkspaceLockedChanged() {
2114 if (mLauncherCallbacks != null) {
2115 mLauncherCallbacks.onWorkspaceLockedChanged();
2116 }
2117 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002118
Sunny Goyal2100c782016-08-22 16:00:03 -07002119 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
2120 LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002121 if (LOGD) {
2122 Log.d(TAG, "Adding widget from drop");
2123 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002124 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2125 }
2126
Sunny Goyal2100c782016-08-22 16:00:03 -07002127 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
2128 AppWidgetHostView boundWidget, LauncherAppWidgetProviderInfo appWidgetInfo,
Adam Cohen59400422014-03-05 18:07:04 -08002129 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002130 if (appWidgetInfo.configure != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002131 setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, appWidgetInfo, info));
Michael Jurkaaf442092010-06-10 17:01:57 -07002132
Bjorn Bringert7984c942009-12-09 15:38:25 +00002133 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002134 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2135 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002136 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002137 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002138 Runnable onComplete = new Runnable() {
2139 @Override
2140 public void run() {
2141 // Exit spring loaded mode if necessary after adding the widget
2142 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2143 null);
2144 }
2145 };
Sunny Goyal2100c782016-08-22 16:00:03 -07002146 completeAddAppWidget(appWidgetId, info, boundWidget, appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002147 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002148 }
2149 }
Romain Guycbb89e42009-06-08 15:52:54 -07002150
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002151 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002152 // Close any folders that may be open.
2153 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002154 mWorkspace.moveToCustomContentScreen(animate);
2155 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002156
2157 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2158 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002159 info.container = container;
2160 info.screenId = screenId;
2161 if (cell != null) {
2162 info.cellX = cell[0];
2163 info.cellY = cell[1];
2164 }
2165 info.spanX = spanX;
2166 info.spanY = spanY;
2167
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002168 switch (info.itemType) {
2169 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2170 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07002171 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002172 break;
2173 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal2100c782016-08-22 16:00:03 -07002174 processShortcutFromDrop(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002175 break;
2176 default:
2177 throw new IllegalStateException("Unknown item type: " + info.itemType);
2178 }
2179 }
2180
Adam Cohenfbba09b2011-07-18 21:43:05 -07002181 /**
2182 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002183 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002184 private void processShortcutFromDrop(PendingAddItemInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07002185 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
2186 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
2187 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002188 }
2189
Adam Cohenfbba09b2011-07-18 21:43:05 -07002190 /**
2191 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002192 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002193 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002194 AppWidgetHostView hostView = info.boundWidget;
2195 int appWidgetId;
2196 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002197 // In the case where we've prebound the widget, we remove it from the DragLayer
2198 if (LOGD) {
2199 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2200 }
2201 getDragLayer().removeView(hostView);
2202
Adam Cohened66b2b2012-01-23 17:28:51 -08002203 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002204 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002205
2206 // Clear the boundWidget so that it doesn't get destroyed.
2207 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002208 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002209 // In this case, we either need to start an activity to get permission to bind
2210 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002211 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002212 Bundle options = info.bindOptions;
2213
Sunny Goyalffe83f12014-08-14 17:39:34 -07002214 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2215 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002216 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002217 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002218 } else {
Sunny Goyal2100c782016-08-22 16:00:03 -07002219 setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, info.info, info));
Michael Jurka8b805b12012-04-18 14:23:14 -07002220 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2221 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2222 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyal2100c782016-08-22 16:00:03 -07002223 mAppWidgetManager.getUser(info.info)
Sunny Goyalffe83f12014-08-14 17:39:34 -07002224 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002225 // TODO: we need to make sure that this accounts for the options bundle.
2226 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002227 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2228 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002229 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002230 }
2231
Adam Cohendcd297f2013-06-18 13:13:40 -07002232 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002233 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002234 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002235 folderInfo.title = getText(R.string.folder_name);
2236
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002237 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002238 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2239 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002240
2241 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002242 FolderIcon newFolder =
2243 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002244 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002245 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002246 // Force measure the new folder icon
2247 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2248 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002249 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002250 }
Romain Guycbb89e42009-06-08 15:52:54 -07002251
Winsonc0b52fe2015-09-09 16:38:15 -07002252 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002253 * Unbinds the view for the specified item, and removes the item and all its children.
2254 *
2255 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002256 * @param itemInfo the {@link ItemInfo} for this view.
2257 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002258 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002259 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002260 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002261 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002262 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2263 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002264 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002265 } else {
2266 mWorkspace.removeWorkspaceItem(v);
2267 }
Winsonc0b52fe2015-09-09 16:38:15 -07002268 if (deleteFromDb) {
2269 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2270 }
2271 } else if (itemInfo instanceof FolderInfo) {
2272 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002273 if (v instanceof FolderIcon) {
2274 ((FolderIcon) v).removeListeners();
2275 }
Winsonc0b52fe2015-09-09 16:38:15 -07002276 mWorkspace.removeWorkspaceItem(v);
2277 if (deleteFromDb) {
2278 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2279 }
2280 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2281 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002282 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002283 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002284 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002285 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002286 }
2287 } else {
2288 return false;
2289 }
2290 return true;
2291 }
2292
2293 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002294 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002295 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002296 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002297 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002298 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002299 // Deleting an app widget ID is a void call but writes to disk before returning
2300 // to the caller...
2301 new AsyncTask<Void, Void, Void>() {
2302 public Void doInBackground(Void ... args) {
2303 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2304 return null;
2305 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002306 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002307 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002308 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002309 }
2310
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002311 @Override
2312 public boolean dispatchKeyEvent(KeyEvent event) {
2313 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2314 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002315 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002316 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002317 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002318 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002319 dumpState();
2320 return true;
2321 }
2322 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002323 }
2324 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2325 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002326 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002327 return true;
2328 }
2329 }
2330
2331 return super.dispatchKeyEvent(event);
2332 }
2333
Joe Onorato88ec0992009-11-19 13:16:06 -08002334 @Override
2335 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002336 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2337 return;
2338 }
2339
Sunny Goyal45478022015-06-08 16:52:41 -07002340 if (mDragController.isDragging()) {
2341 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002342 return;
2343 }
2344
Tony Wickham49c8d292016-07-01 11:44:34 -07002345 if (getOpenShortcutsContainer() != null) {
2346 closeShortcutsContainer();
2347 } else if (isAppsViewVisible()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002348 showWorkspace(true);
2349 } else if (isWidgetsViewVisible()) {
2350 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002351 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002352 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002353 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002354 Folder openFolder = mWorkspace.getOpenFolder();
2355 if (openFolder.isEditingName()) {
2356 openFolder.dismissEditingName();
2357 } else {
2358 closeFolder();
2359 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002360 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002361 mWorkspace.exitWidgetResizeMode();
2362
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002363 // Back button is a no-op here, but give at least some feedback for the button press
2364 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002365 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002366 }
2367
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002368 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002369 * Launches the intent referred by the clicked shortcut.
2370 *
2371 * @param v The view representing the clicked shortcut.
2372 */
2373 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002374 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2375 // view has detached (it's possible for this to happen if the view is removed mid touch).
2376 if (v.getWindowToken() == null) {
2377 return;
2378 }
2379
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002380 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002381 return;
2382 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002383
Adam Cohen1697b792013-09-17 19:08:21 -07002384 if (v instanceof Workspace) {
2385 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002386 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002387 }
2388 return;
2389 }
2390
2391 if (v instanceof CellLayout) {
2392 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002393 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2394 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002395 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002396 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002397 }
2398
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002399 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002400 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002401 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002402 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002403 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002404 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002405 }
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002406 } else if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2407 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002408 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002409 } else if (tag instanceof AppInfo) {
2410 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002411 } else if (tag instanceof LauncherAppWidgetInfo) {
2412 if (v instanceof PendingAppWidgetHostView) {
2413 onClickPendingWidget((PendingAppWidgetHostView) v);
2414 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002415 }
2416 }
2417
Sunny Goyal70660032015-05-14 00:07:08 -07002418 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002419 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002420 return false;
2421 }
2422
Michael Jurkaaf442092010-06-10 17:01:57 -07002423 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002424 * Event handler for the app widget view which has not fully restored.
2425 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002426 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002427 if (mIsSafeModeEnabled) {
2428 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2429 return;
2430 }
2431
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002432 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002433 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002434 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2435 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2436 // This should not happen, as we make sure that an Id is allocated during bind.
2437 return;
2438 }
2439 LauncherAppWidgetProviderInfo appWidgetInfo =
2440 mAppWidgetManager.findProvider(info.providerName, info.user);
2441 if (appWidgetInfo != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002442 setWaitingForResult(PendingRequestArgs
2443 .forWidgetInfo(info.appWidgetId, appWidgetInfo, info));
Sunny Goyalff572272014-07-23 13:58:07 -07002444
Sunny Goyald478c832016-04-01 12:04:16 -07002445 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
Sunny Goyal2100c782016-08-22 16:00:03 -07002446 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, info.appWidgetId);
Sunny Goyald478c832016-04-01 12:04:16 -07002447 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07002448 mAppWidgetManager.getUser(appWidgetInfo)
Sunny Goyald478c832016-04-01 12:04:16 -07002449 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2450 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2451 }
2452 } else {
2453 LauncherAppWidgetProviderInfo appWidgetInfo =
2454 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2455 if (appWidgetInfo != null) {
2456 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2457 }
Sunny Goyalff572272014-07-23 13:58:07 -07002458 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002459 } else if (info.installProgress < 0) {
2460 // The install has not been queued
2461 final String packageName = info.providerName.getPackageName();
2462 showBrokenAppInstallDialog(packageName,
2463 new DialogInterface.OnClickListener() {
2464 public void onClick(DialogInterface dialog, int id) {
2465 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2466 }
2467 });
2468 } else {
2469 // Download has started.
2470 final String packageName = info.providerName.getPackageName();
2471 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002472 }
2473 }
2474
Sunny Goyald478c832016-04-01 12:04:16 -07002475 private void startRestoredWidgetReconfigActivity(
2476 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002477 setWaitingForResult(PendingRequestArgs.forWidgetInfo(info.appWidgetId, provider, info));
Sunny Goyald478c832016-04-01 12:04:16 -07002478 mAppWidgetManager.startConfigActivity(provider,
2479 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2480 }
2481
Sunny Goyalff572272014-07-23 13:58:07 -07002482 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002483 * Event handler for the "grid" button that appears on the home screen, which
2484 * enters all apps mode.
2485 *
2486 * @param v The view that was clicked.
2487 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002488 protected void onClickAllAppsButton(View v) {
2489 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002490 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002491 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.TAP,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002492 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002493 showAppsView(true /* animated */, true /* updatePredictedApps */,
2494 false /* focusSearchBar */);
Winson Chung76648c52015-07-10 14:33:23 -07002495 }
2496 }
2497
2498 protected void onLongClickAllAppsButton(View v) {
2499 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2500 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002501 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.LONGPRESS,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002502 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002503 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07002504 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002505 }
2506 }
2507
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002508 private void showBrokenAppInstallDialog(final String packageName,
2509 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002510 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002511 .setTitle(R.string.abandoned_promises_title)
2512 .setMessage(R.string.abandoned_promise_explanation)
2513 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2514 .setNeutralButton(R.string.abandoned_clean_this,
2515 new DialogInterface.OnClickListener() {
2516 public void onClick(DialogInterface dialog, int id) {
2517 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2518 mWorkspace.removeAbandonedPromise(packageName, user);
2519 }
2520 })
2521 .create().show();
2522 return;
2523 }
2524
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002525 /**
2526 * Event handler for an app shortcut click.
2527 *
2528 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2529 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002530 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002531 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2532 Object tag = v.getTag();
2533 if (!(tag instanceof ShortcutInfo)) {
2534 throw new IllegalArgumentException("Input must be a Shortcut");
2535 }
2536
2537 // Open shortcut
2538 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002539
2540 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002541 if ((shortcut.isDisabled &
2542 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2543 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2544 // If the app is only disabled because of the above flags, launch activity anyway.
2545 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002546 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002547 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2548 // Use a message specific to this shortcut, if it has one.
2549 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2550 return;
2551 }
2552 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002553 int error = R.string.activity_not_available;
2554 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2555 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002556 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2557 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002558 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002559 }
2560 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2561 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002562 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002563 }
2564
Chris Wren40c5ed32014-06-24 18:24:23 -04002565 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002566 if ((v instanceof BubbleTextView)
2567 && shortcut.isPromise()
2568 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002569 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002570 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002571 new DialogInterface.OnClickListener() {
2572 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002573 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002574 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002575 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002576 return;
2577 }
2578
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002579 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002580 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002581 }
2582
Sunny Goyala7ce1662016-05-31 15:01:35 -07002583 private void startAppShortcutOrInfoActivity(View v) {
2584 ItemInfo item = (ItemInfo) v.getTag();
2585 Intent intent = item.getIntent();
2586 if (intent == null) {
2587 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002588 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002589 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002590 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002591
2592 if (success && v instanceof BubbleTextView) {
2593 mWaitingForResume = (BubbleTextView) v;
2594 mWaitingForResume.setStayPressed(true);
2595 }
2596 }
2597
2598 /**
2599 * Event handler for a folder icon click.
2600 *
2601 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2602 */
2603 protected void onClickFolderIcon(View v) {
2604 if (LOGD) Log.d(TAG, "onClickFolder");
2605 if (!(v instanceof FolderIcon)){
2606 throw new IllegalArgumentException("Input must be a FolderIcon");
2607 }
2608
2609 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002610 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002611 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002612 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002613 }
Michael Jurka5130e402011-10-13 04:55:35 -07002614 }
2615
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002616 /**
2617 * Event handler for the (Add) Widgets button that appears after a long press
2618 * on the home screen.
2619 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002620 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002621 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002622 if (mIsSafeModeEnabled) {
2623 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2624 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002625 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002626 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002627 }
2628
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002629 /**
2630 * Event handler for the wallpaper picker button that appears after a long press
2631 * on the home screen.
2632 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002633 public void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002634 if (!Utilities.isWallapaperAllowed(this)) {
2635 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2636 return;
2637 }
2638
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002639 String pickerPackage = getString(R.string.wallpaper_picker_package);
2640 if (TextUtils.isEmpty(pickerPackage)) {
2641 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2642 }
2643
Tony Wickham785f7a52015-08-31 17:28:32 -07002644 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2645 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002646
Sunny Goyal2100c782016-08-22 16:00:03 -07002647 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002648 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002649 .setPackage(pickerPackage)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002650 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
2651 intent.setSourceBounds(getViewBounds(v));
2652 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002653 }
2654
2655 /**
2656 * Event handler for a click on the settings button that appears after a long press
2657 * on the home screen.
2658 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002659 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002660 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002661 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2662 .setPackage(getPackageName());
2663 intent.setSourceBounds(getViewBounds(v));
2664 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002665 }
2666
Adam Cohen61f560d2013-09-30 15:58:20 -07002667 public View.OnTouchListener getHapticFeedbackTouchListener() {
2668 if (mHapticFeedbackTouchListener == null) {
2669 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002670 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002671 @Override
2672 public boolean onTouch(View v, MotionEvent event) {
2673 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2674 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2675 }
2676 return false;
2677 }
2678 };
2679 }
2680 return mHapticFeedbackTouchListener;
2681 }
2682
Tony Wickhame2217252016-03-22 16:34:23 -07002683 @Override
2684 public void onAccessibilityStateChanged(boolean enabled) {
2685 mDragLayer.onAccessibilityStateChanged(enabled);
2686 }
2687
Sunny Goyal06e21a22016-08-11 16:02:02 -07002688 public void onDragStarted() {
Adam Cohen9211d422014-10-07 18:14:53 -07002689 if (isOnCustomContent()) {
2690 // Custom content screen doesn't participate in drag and drop. If on custom
2691 // content screen, move to default.
2692 moveWorkspaceToDefaultScreen();
2693 }
Adam Cohen9211d422014-10-07 18:14:53 -07002694 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002695
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002696 /**
2697 * Called when the user stops interacting with the launcher.
2698 * This implies that the user is now on the homescreen and is not doing housekeeping.
2699 */
Adam Cohen9211d422014-10-07 18:14:53 -07002700 protected void onInteractionEnd() {
2701 if (mLauncherCallbacks != null) {
2702 mLauncherCallbacks.onInteractionEnd();
2703 }
2704 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002705
2706 /**
2707 * Called when the user starts interacting with the launcher.
2708 * The possible interactions are:
2709 * - open all apps
2710 * - reorder an app shortcut, or a widget
2711 * - open the overview mode.
2712 * This is a good time to stop doing things that only make sense
2713 * when the user is on the homescreen and not doing housekeeping.
2714 */
Adam Cohen9211d422014-10-07 18:14:53 -07002715 protected void onInteractionBegin() {
2716 if (mLauncherCallbacks != null) {
2717 mLauncherCallbacks.onInteractionBegin();
2718 }
2719 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002720
Winson Chungcd99cd32015-04-29 11:03:24 -07002721 /** Updates the interaction state. */
2722 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002723 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002724 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002725 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2726 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002727 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002728 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002729 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002730 onInteractionEnd();
2731 }
2732 }
2733
Sunny Goyala7ce1662016-05-31 15:01:35 -07002734 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002735 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002736 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2737 try {
2738 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2739 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2740 // is enabled by default on NYC.
2741 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2742 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002743
2744 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2745 String id = ((ShortcutInfo) info).getDeepShortcutId();
2746 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002747 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2748 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002749 } else {
2750 // Could be launching some bookkeeping activity
2751 startActivity(intent, optsBundle);
2752 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002753 } finally {
2754 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002755 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002756 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002757 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2758 // corresponding permission. Show the appropriate permission prompt if that
2759 // is the case.
2760 if (intent.getComponent() == null
2761 && Intent.ACTION_CALL.equals(intent.getAction())
2762 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2763 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002764
2765 setWaitingForResult(PendingRequestArgs
2766 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002767 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2768 REQUEST_PERMISSION_CALL_PHONE);
2769 } else {
2770 // No idea why this was thrown.
2771 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002772 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002773 }
2774 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002775
Sunny Goyalfe770c92016-09-27 14:38:58 -07002776 @TargetApi(Build.VERSION_CODES.M)
Sunny Goyala7ce1662016-05-31 15:01:35 -07002777 private Bundle getActivityLaunchOptions(View v) {
2778 if (Utilities.ATLEAST_MARSHMALLOW) {
2779 int left = 0, top = 0;
2780 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2781 if (v instanceof TextView) {
2782 // Launch from center of icon, not entire view
2783 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2784 if (icon != null) {
2785 Rect bounds = icon.getBounds();
2786 left = (width - bounds.width()) / 2;
2787 top = v.getPaddingTop();
2788 width = bounds.width();
2789 height = bounds.height();
2790 }
2791 }
2792 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2793 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2794 // On L devices, we use the device default slide-up transition.
2795 // On L MR1 devices, we use a custom version of the slide-up transition which
2796 // doesn't have the delay present in the device default.
2797 return ActivityOptions.makeCustomAnimation(
2798 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2799 }
2800 return null;
2801 }
2802
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002803 private Rect getViewBounds(View v) {
2804 int[] pos = new int[2];
2805 v.getLocationOnScreen(pos);
2806 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2807 }
2808
Sunny Goyala7ce1662016-05-31 15:01:35 -07002809 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002810 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2811 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2812 return false;
2813 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002814 // Only launch using the new animation if the shortcut has not opted out (this is a
2815 // private contract between launcher and may be ignored in the future).
2816 boolean useLaunchAnimation = (v != null) &&
2817 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2818 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2819
2820 UserHandleCompat user = null;
2821 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2822 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2823 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002824 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002825
2826 // Prepare intent
2827 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2828 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002829 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002830 }
2831 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002832 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2833 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07002834 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
2835 && ((ShortcutInfo) item).promisedIntent == null) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002836 // Shortcuts need some special checks due to legacy reasons.
2837 startShortcutIntentSafely(intent, optsBundle, item);
2838 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2839 // Could be launching some bookkeeping activity
2840 startActivity(intent, optsBundle);
2841 } else {
2842 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2843 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2844 }
2845 return true;
2846 } catch (ActivityNotFoundException|SecurityException e) {
2847 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2848 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2849 }
2850 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002851 }
2852
Adam Cohen268c4752012-06-06 17:47:33 -07002853 /**
2854 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2855 * in the DragLayer in the exact absolute location of the original FolderIcon.
2856 */
2857 private void copyFolderIconToImage(FolderIcon fi) {
2858 final int width = fi.getMeasuredWidth();
2859 final int height = fi.getMeasuredHeight();
2860
2861 // Lazy load ImageView, Bitmap and Canvas
2862 if (mFolderIconImageView == null) {
2863 mFolderIconImageView = new ImageView(this);
2864 }
2865 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2866 mFolderIconBitmap.getHeight() != height) {
2867 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2868 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2869 }
2870
2871 DragLayer.LayoutParams lp;
2872 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2873 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2874 } else {
2875 lp = new DragLayer.LayoutParams(width, height);
2876 }
2877
Adam Cohen307fe232012-08-16 17:55:58 -07002878 // The layout from which the folder is being opened may be scaled, adjust the starting
2879 // view size by this scale factor.
2880 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002881 lp.customPosition = true;
2882 lp.x = mRectForFolderAnimation.left;
2883 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002884 lp.width = (int) (scale * width);
2885 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002886
2887 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2888 fi.draw(mFolderIconCanvas);
2889 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002890 if (fi.getFolder() != null) {
2891 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2892 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002893 }
Adam Cohen268c4752012-06-06 17:47:33 -07002894 // Just in case this image view is still in the drag layer from a previous animation,
2895 // we remove it and re-add it.
2896 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2897 mDragLayer.removeView(mFolderIconImageView);
2898 }
2899 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002900 if (fi.getFolder() != null) {
2901 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002902 }
Adam Cohen268c4752012-06-06 17:47:33 -07002903 }
2904
Adam Cohen37b2a492016-04-06 18:36:06 -07002905 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002906 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002907 FolderInfo info = (FolderInfo) fi.getTag();
2908 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2909 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002910 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2911 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002912 }
2913
Adam Cohen268c4752012-06-06 17:47:33 -07002914 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2915 copyFolderIconToImage(fi);
2916 fi.setVisibility(View.INVISIBLE);
2917
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002918 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2919 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002920 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002921 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2922 }
2923 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002924 oa.start();
2925 }
2926
Sunny Goyal935fca12015-10-13 10:19:01 -07002927 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002928 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002929 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002930
Adam Cohen268c4752012-06-06 17:47:33 -07002931 // We remove and re-draw the FolderIcon in-case it has changed
2932 mDragLayer.removeView(mFolderIconImageView);
2933 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08002934
2935 if (cl != null) {
2936 cl.clearFolderLeaveBehind();
2937 }
2938
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002939 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002940 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002941 oa.addListener(new AnimatorListenerAdapter() {
2942 @Override
2943 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002944 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07002945 // Remove the ImageView copy of the FolderIcon and make the original visible.
2946 mDragLayer.removeView(mFolderIconImageView);
2947 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002948 }
2949 }
2950 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002951 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07002952 if (!animate) {
2953 oa.end();
2954 }
Adam Cohen2801caf2011-05-13 20:57:39 -07002955 }
2956
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002957 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002958 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002959 * is animated relative to the specified View. If the View is null, no animation
2960 * is played.
2961 *
Sunny Goyal08442b82015-10-21 17:15:08 -07002962 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002963 */
Adam Cohen37b2a492016-04-06 18:36:06 -07002964 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07002965
Adam Cohenfb91f302012-06-11 15:45:18 -07002966 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07002967 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
2968 if (openFolder != null && openFolder != folder) {
2969 // Close any open folder before opening a folder.
2970 closeFolder();
2971 }
2972
Adam Cohena9cf38f2011-05-02 15:36:58 -07002973 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002974
Adam Cohena9cf38f2011-05-02 15:36:58 -07002975 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002976
Sunny Goyalf4066152015-04-15 09:42:19 -07002977 // While the folder is open, the position of the icon cannot change.
2978 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
2979
Adam Cohen4554ee12011-08-03 16:13:21 -07002980 // Just verify that the folder hasn't already been added to the DragLayer.
2981 // There was a one-off crash where the folder had a parent already.
2982 if (folder.getParent() == null) {
2983 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07002984 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07002985 } else {
2986 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2987 folder.getParent() + ").");
2988 }
Adam Cohen37b2a492016-04-06 18:36:06 -07002989 folder.animateOpen();
2990
2991 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002992
2993 // Notify the accessibility manager that this folder "window" has appeared and occluded
2994 // the workspace items
2995 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2996 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002997 }
2998
2999 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003000 closeFolder(true);
3001 }
3002
3003 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003004 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003005 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003006 if (folder.isEditingName()) {
3007 folder.dismissEditingName();
3008 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003009 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003010 }
3011 }
3012
Sunny Goyal935fca12015-10-13 10:19:01 -07003013 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003014 animate &= !Utilities.isPowerSaverOn(this);
3015
Adam Cohen4554ee12011-08-03 16:13:21 -07003016 folder.getInfo().opened = false;
3017
3018 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3019 if (parent != null) {
3020 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003021 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003022 if (fi != null) {
3023 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3024 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003025 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003026 if (animate) {
3027 folder.animateClosed();
3028 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003029 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003030 }
Winson Chung83ca4802013-04-12 15:10:52 -07003031
Sunny Goyal935fca12015-10-13 10:19:01 -07003032 // Notify the accessibility manager that this folder "window" has disappeared and no
3033 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003034 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003035 }
3036
Tony Wickham49c8d292016-07-01 11:44:34 -07003037 public void closeShortcutsContainer() {
Sunny Goyala2454ad2016-07-22 10:50:11 -07003038 closeShortcutsContainer(true);
3039 }
3040
3041 public void closeShortcutsContainer(boolean animate) {
Tony Wickham49c8d292016-07-01 11:44:34 -07003042 DeepShortcutsContainer deepShortcutsContainer = getOpenShortcutsContainer();
3043 if (deepShortcutsContainer != null) {
Sunny Goyala2454ad2016-07-22 10:50:11 -07003044 if (animate) {
3045 deepShortcutsContainer.animateClose();
3046 } else {
3047 deepShortcutsContainer.close();
3048 }
Tony Wickham49c8d292016-07-01 11:44:34 -07003049 }
3050 }
3051
Sunny Goyal52851aa2016-09-02 10:41:43 -07003052 public View getTopFloatingView() {
3053 View topView = getOpenShortcutsContainer();
3054 if (topView == null) {
3055 topView = getWorkspace().getOpenFolder();
3056 }
3057 return topView;
3058 }
3059
Tony Wickham49c8d292016-07-01 11:44:34 -07003060 /**
3061 * @return The open shortcuts container, or null if there is none
3062 */
3063 public DeepShortcutsContainer getOpenShortcutsContainer() {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07003064 // Iterate in reverse order. Shortcuts container is added later to the dragLayer,
3065 // and will be one of the last views.
3066 for (int i = mDragLayer.getChildCount() - 1; i >= 0; i--) {
3067 View child = mDragLayer.getChildAt(i);
Sunny Goyala2454ad2016-07-22 10:50:11 -07003068 if (child instanceof DeepShortcutsContainer
3069 && ((DeepShortcutsContainer) child).isOpen()) {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07003070 return (DeepShortcutsContainer) child;
3071 }
3072 }
3073 return null;
Tony Wickham49c8d292016-07-01 11:44:34 -07003074 }
3075
Tony Wickhamdadb3042016-02-24 11:07:00 -08003076 @Override
Jon Miranda379198e2016-09-23 08:54:40 -07003077 public boolean dispatchTouchEvent(MotionEvent ev) {
3078 mLastDispatchTouchEventX = ev.getX();
3079 return super.dispatchTouchEvent(ev);
3080 }
3081
3082 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003083 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003084 if (!isDraggingEnabled()) return false;
3085 if (isWorkspaceLocked()) return false;
3086 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003087
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07003088 if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
3089 (v == mAllAppsButton && mAllAppsButton != null)) {
Winson Chung76648c52015-07-10 14:33:23 -07003090 onLongClickAllAppsButton(v);
3091 return true;
3092 }
3093
Jon Miranda379198e2016-09-23 08:54:40 -07003094 boolean fromEdgeOfScreen = mLastDispatchTouchEventX < mEdgeOfScreenThresholdPx
3095 || mLastDispatchTouchEventX > (mDeviceProfile.widthPx - mEdgeOfScreenThresholdPx);
3096
Adam Cohen1697b792013-09-17 19:08:21 -07003097 if (v instanceof Workspace) {
3098 if (!mWorkspace.isInOverviewMode()) {
Jon Miranda379198e2016-09-23 08:54:40 -07003099 if (!mWorkspace.isTouchActive() && !fromEdgeOfScreen) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003100 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003101 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3102 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3103 return true;
3104 } else {
3105 return false;
3106 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003107 } else {
3108 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003109 }
Adam Cohen1697b792013-09-17 19:08:21 -07003110 }
3111
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003112 CellLayout.CellInfo longClickCellInfo = null;
3113 View itemUnderLongClick = null;
3114 if (v.getTag() instanceof ItemInfo) {
3115 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003116 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003117 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07003118 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003119 }
3120
Winson Chung3d503fb2011-07-13 17:25:49 -07003121 // The hotseat touch handling does not go through Workspace, and we always allow long press
3122 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003123 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003124 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003125 // User long pressed on empty space
Adam Cohend3ecce92013-09-16 14:58:00 -07003126 if (mWorkspace.isInOverviewMode()) {
3127 mWorkspace.startReordering(v);
3128 } else {
Jon Miranda379198e2016-09-23 08:54:40 -07003129 if (fromEdgeOfScreen) {
3130 return false;
3131 }
Winson Chungdc61c4d2015-04-20 18:26:57 -07003132 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003133 }
Jon Miranda379198e2016-09-23 08:54:40 -07003134 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3135 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003136 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003137 final boolean isAllAppsButton =
3138 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3139 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3140 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003141 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003142 // User long pressed on an item
Tony Wickham6e74e892016-09-19 14:06:31 -07003143 DragOptions dragOptions = new DragOptions();
3144 if (itemUnderLongClick instanceof BubbleTextView) {
3145 BubbleTextView icon = (BubbleTextView) itemUnderLongClick;
3146 if (icon.hasDeepShortcuts()) {
3147 DeepShortcutsContainer dsc = DeepShortcutsContainer.showForIcon(icon);
3148 if (dsc != null) {
3149 dragOptions.deferDragCondition = dsc.createDeferDragCondition(null);
3150 }
3151 }
3152 }
3153 mWorkspace.startDrag(longClickCellInfo, dragOptions);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003154 }
3155 }
3156 }
3157 return true;
3158 }
3159
Winson Chung3d503fb2011-07-13 17:25:49 -07003160 boolean isHotseatLayout(View layout) {
3161 return mHotseat != null && layout != null &&
3162 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3163 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003164
Winson Chung3d503fb2011-07-13 17:25:49 -07003165 /**
3166 * Returns the CellLayout of the specified container at the specified screen.
3167 */
Sunny Goyal92820592015-03-02 11:31:35 -08003168 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003169 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3170 if (mHotseat != null) {
3171 return mHotseat.getLayout();
3172 } else {
3173 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003174 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003175 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003176 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003177 }
3178 }
3179
Winson Chungb745afb2015-03-02 11:51:23 -08003180 /**
3181 * For overridden classes.
3182 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003183 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003184 return isAppsViewVisible();
3185 }
3186
3187 public boolean isAppsViewVisible() {
3188 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3189 }
3190
3191 public boolean isWidgetsViewVisible() {
3192 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003193 }
3194
Michael Jurkae326f182011-11-21 14:05:46 -08003195 @Override
3196 public void onTrimMemory(int level) {
3197 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003198 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3199 // The widget preview db can result in holding onto over
3200 // 3MB of memory for caching which isn't necessary.
3201 SQLiteDatabase.releaseMemory();
3202
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003203 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003204 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003205 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003206 if (mLauncherCallbacks != null) {
3207 mLauncherCallbacks.onTrimMemory(level);
3208 }
Michael Jurkae326f182011-11-21 14:05:46 -08003209 }
3210
Winson5c6bdbb2015-09-03 11:36:19 -07003211 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003212 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003213 }
3214
Winson5c6bdbb2015-09-03 11:36:19 -07003215 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003216 boolean changed = mState != State.WORKSPACE ||
3217 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003218 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003219 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003220 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003221 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003222
Michael Jurkab3e22d92011-10-31 15:58:33 -07003223 // Set focus to the AppsCustomize button
3224 if (mAllAppsButton != null) {
3225 mAllAppsButton.requestFocus();
3226 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003227 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003228
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003229 // Change the state *after* we've called all the transition code
3230 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003231
Winson Chung5fb63472011-02-02 17:03:37 -08003232 // Resume the auto-advance of widgets
3233 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003234 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003235
Winson Chung0f785722015-04-08 10:27:49 -07003236 if (changed) {
3237 // Send an accessibility event to announce the context change
3238 getWindow().getDecorView()
3239 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003240 }
Winson5c6bdbb2015-09-03 11:36:19 -07003241 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003242 }
3243
Winsone9f27272015-10-13 10:47:51 -07003244 /**
3245 * Shows the overview button.
3246 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07003247 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003248 showOverviewMode(animated, false);
3249 }
3250
3251 /**
3252 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3253 * onto one of the overview panel buttons.
3254 */
3255 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3256 Runnable postAnimRunnable = null;
3257 if (requestButtonFocus) {
3258 postAnimRunnable = new Runnable() {
3259 @Override
3260 public void run() {
3261 // Hitting the menu button when in touch mode does not trigger touch mode to
3262 // be disabled, so if requested, force focus on one of the overview panel
3263 // buttons.
3264 mOverviewPanel.requestFocusFromTouch();
3265 }
3266 };
3267 }
Adam Cohened307df2013-10-02 09:37:31 -07003268 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003269 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003270 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003271 mState = State.WORKSPACE;
Hyunyoung Song0a9c0922016-09-20 22:50:06 -07003272 // If animated from long press, then don't allow any of the controller in the drag
3273 // layer to intercept any remaining touch.
3274 mWorkspace.requestDisallowInterceptTouchEvent(animated);
Joe Onorato00acb122009-08-04 16:04:30 -04003275 }
3276
Winson Chungb745afb2015-03-02 11:51:23 -08003277 /**
3278 * Shows the apps view.
3279 */
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003280 public void showAppsView(boolean animated, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003281 boolean focusSearchBar) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003282 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07003283 if (updatePredictedApps) {
3284 tryAndUpdatePredictedApps();
3285 }
Winson Chung76648c52015-07-10 14:33:23 -07003286 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003287 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003288
Winson Chungb745afb2015-03-02 11:51:23 -08003289 /**
3290 * Shows the widgets view.
3291 */
3292 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003293 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003294 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003295 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003296 }
Winson Chung76648c52015-07-10 14:33:23 -07003297 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003298
3299 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003300 @Override
3301 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003302 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003303 }
3304 });
Winson Chungb745afb2015-03-02 11:51:23 -08003305 }
3306
3307 /**
3308 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003309 *
3310 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003311 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003312 // TODO: calling method should use the return value so that when {@code false} is returned
3313 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003314 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003315 if (!(mState == State.WORKSPACE ||
3316 mState == State.APPS_SPRING_LOADED ||
3317 mState == State.WIDGETS_SPRING_LOADED ||
3318 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003319 return false;
3320 }
3321 if (toState != State.APPS && toState != State.WIDGETS) {
3322 return false;
3323 }
Winson Chungb745afb2015-03-02 11:51:23 -08003324
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003325 // This is a safe and supported transition to bypass spring_loaded mode.
3326 if (mExitSpringLoadedModeRunnable != null) {
3327 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3328 mExitSpringLoadedModeRunnable = null;
3329 }
3330
Winson Chungb745afb2015-03-02 11:51:23 -08003331 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003332 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3333 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003334 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003335 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003336 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003337
Michael Jurkab3e22d92011-10-31 15:58:33 -07003338 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003339 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003340
3341 // Pause the auto-advance of widgets until we are out of AllApps
3342 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003343 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003344 closeFolder();
Tony Wickham49c8d292016-07-01 11:44:34 -07003345 closeShortcutsContainer();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003346
3347 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003348 getWindow().getDecorView()
3349 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003350 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003351 }
3352
Winson Chungcd99cd32015-04-29 11:03:24 -07003353 /**
3354 * Updates the workspace and interaction state on state change, and return the animation to this
3355 * new state.
3356 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003357 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003358 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003359 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003360 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003361 updateInteraction(fromState, toState);
3362 return anim;
3363 }
3364
Hyunyoung Song3f471442015-04-08 19:01:34 -07003365 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003366 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003367 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003368 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003369 }
Winson Chungb745afb2015-03-02 11:51:23 -08003370
Winson Chung006ee262015-08-03 14:40:11 -07003371 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003372 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003373 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003374
3375 if (isAppsViewVisible()) {
3376 mState = State.APPS_SPRING_LOADED;
3377 } else if (isWidgetsViewVisible()) {
3378 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003379 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003380 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003381 } else {
3382 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003383 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003384 }
Adam Cohen7777d962011-08-18 18:58:38 -07003385
Hyunyoung Song3f471442015-04-08 19:01:34 -07003386 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003387 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003388 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003389
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003390 if (mExitSpringLoadedModeRunnable != null) {
3391 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3392 }
3393 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003394 @Override
3395 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003396 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003397 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3398 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003399 // Before we show workspace, hide all apps again because
3400 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3401 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003402 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003403 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003404 } else {
3405 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003406 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003407 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07003408 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003409 };
3410 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003411 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003412
Tony Wickhame0c33232016-02-08 11:37:04 -08003413 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003414 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3415 || mState == State.WIDGETS_SPRING_LOADED;
3416 }
3417
Michael Jurkad3ef3062010-11-23 16:23:58 -08003418 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003419 if (mState == State.APPS_SPRING_LOADED) {
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003420 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07003421 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003422 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003423 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003424 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3425 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003426 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003427 }
3428
Winson Chung4ac30062015-05-08 17:34:17 -07003429 /**
3430 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3431 * resumed.
3432 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003433 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003434 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003435 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003436 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003437 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003438 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003439 }
3440 }
3441 }
3442
Michael Jurkab3e22d92011-10-31 15:58:33 -07003443 void lockAllApps() {
3444 // TODO
3445 }
3446
3447 void unlockAllApps() {
3448 // TODO
3449 }
3450
Michael Jurkad7c28052012-04-27 15:43:36 -07003451 @Override
3452 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003453 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003454 final List<CharSequence> text = event.getText();
3455 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003456 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003457 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003458 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003459 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003460 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003461 } else if (mWorkspace != null) {
3462 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003463 } else {
3464 text.add(getString(R.string.all_apps_home_button_label));
3465 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003466 return result;
3467 }
3468
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003469 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003470 * If the activity is currently paused, signal that we need to run the passed Runnable
3471 * in onResume.
3472 *
3473 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003474 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3475 * wrong when we're not running, and if the activity comes back to what the configuration was
3476 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003477 *
3478 * Implementation of the method from LauncherModel.Callbacks.
3479 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003480 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003481 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003482 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003483 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003484 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003485 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003486 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003487 }
3488 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003489 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003490 return true;
3491 } else {
3492 return false;
3493 }
3494 }
3495
Michael Jurkac402cd92013-05-20 15:49:32 +02003496 private boolean waitUntilResume(Runnable run) {
3497 return waitUntilResume(run, false);
3498 }
3499
Michael Jurka1e2f4652013-07-08 18:03:46 -07003500 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003501 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003502 }
3503
Michael Jurka7607c2f2013-04-03 14:33:19 -07003504 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003505 * If the activity is currently paused, signal that we need to re-run the loader
3506 * in onResume.
3507 *
3508 * This needs to be called from incoming places where resources might have been loaded
3509 * while we are paused. That is becaues the Configuration might be wrong
3510 * when we're not running, and if it comes back to what it was when we
3511 * were paused, we are not restarted.
3512 *
3513 * Implementation of the method from LauncherModel.Callbacks.
3514 *
3515 * @return true if we are currently paused. The caller might be able to
3516 * skip some work in that case since we will come back again.
3517 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003518 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003519 public boolean setLoadOnResume() {
3520 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003521 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003522 mOnResumeNeedsLoad = true;
3523 return true;
3524 } else {
3525 return false;
3526 }
3527 }
3528
3529 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003530 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003531 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003532 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003533 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003534 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003535 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003536 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003537 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003538 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003539 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003540
Joe Onorato9c1289c2009-08-17 11:03:03 -04003541 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003542 * Clear any pending bind callbacks. This is called when is loader is planning to
3543 * perform a full rebind from scratch.
3544 */
3545 @Override
3546 public void clearPendingBinds() {
3547 mBindOnResumeCallbacks.clear();
3548 if (mPendingExecutor != null) {
3549 mPendingExecutor.markCompleted();
3550 mPendingExecutor = null;
3551 }
3552 }
3553
3554 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003555 * Refreshes the shortcuts shown on the workspace.
3556 *
3557 * Implementation of the method from LauncherModel.Callbacks.
3558 */
3559 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003560 if (LauncherAppState.PROFILE_STARTUP) {
3561 Trace.beginSection("Starting page bind");
3562 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003563 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003564
Winson Chungd64d1762013-08-20 14:37:16 -07003565 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003566 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003567 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003568
Michael Jurka05bf644e2011-11-30 20:28:53 -08003569 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003570 if (mHotseat != null) {
3571 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003572 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003573 if (LauncherAppState.PROFILE_STARTUP) {
3574 Trace.endSection();
3575 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003576 }
3577
Adam Cohendcd297f2013-06-18 13:13:40 -07003578 @Override
3579 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003580 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003581 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3582 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003583 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3584 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3585 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003586 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3587 // If there are no screens, we need to have an empty screen
3588 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003589 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003590 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003591
3592 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003593 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003594 if (hasCustomContentToLeft()) {
3595 mWorkspace.createCustomContentContainer();
3596 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003597 }
Winsonc7d2e832016-07-28 12:24:55 -07003598
3599 // After we have added all the screens, if the wallpaper was locked to the default state,
3600 // then notify to indicate that it can be released and a proper wallpaper offset can be
3601 // computed before the next layout
3602 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07003603 }
3604
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003605 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003606 int count = orderedScreenIds.size();
3607 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003608 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003609 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003610 // No need to bind the first screen, as its always bound.
3611 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3612 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003613 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003614 }
3615
Winson Chungd64d1762013-08-20 14:37:16 -07003616 public void bindAppsAdded(final ArrayList<Long> newScreens,
3617 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003618 final ArrayList<ItemInfo> addAnimated,
3619 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003620 Runnable r = new Runnable() {
3621 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003622 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003623 }
3624 };
3625 if (waitUntilResume(r)) {
3626 return;
3627 }
3628
Winson Chungd64d1762013-08-20 14:37:16 -07003629 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003630 if (newScreens != null) {
3631 bindAddScreens(newScreens);
3632 }
Winson Chungd64d1762013-08-20 14:37:16 -07003633
3634 // We add the items without animation on non-visible pages, and with
3635 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003636 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003637 bindItems(addNotAnimated, 0,
3638 addNotAnimated.size(), false);
3639 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003640 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003641 bindItems(addAnimated, 0,
3642 addAnimated.size(), true);
3643 }
Winson Chungc58497e2013-09-03 17:48:37 -07003644
Winson Chung87412982013-10-03 18:34:14 -07003645 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003646 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003647
Winson Chungb745afb2015-03-02 11:51:23 -08003648 if (addedApps != null && mAppsView != null) {
3649 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003650 }
Winson Chungd64d1762013-08-20 14:37:16 -07003651 }
3652
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003653 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003654 * Bind the items start-end from the list.
3655 *
3656 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003657 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003658 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003659 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3660 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003661 Runnable r = new Runnable() {
3662 public void run() {
3663 bindItems(shortcuts, start, end, forceAnimateIcons);
3664 }
3665 };
3666 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003667 return;
3668 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003669
Winson Chungf0c6ae02012-03-21 16:10:31 -07003670 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003671 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3672 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003673 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003674 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003675 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003676 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003677 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003678
3679 // Short circuit if we are loading dock items for a configuration which has no dock
3680 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3681 mHotseat == null) {
3682 continue;
3683 }
3684
Sunny Goyal639e9062015-08-19 19:17:06 -07003685 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003686 switch (item.itemType) {
3687 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3688 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003689 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003690 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003691 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003692 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003693 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003694 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003695 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003696 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003697 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003698 default:
3699 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003700 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003701
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003702 /*
3703 * Remove colliding items.
3704 */
3705 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3706 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3707 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3708 View v = cl.getChildAt(item.cellX, item.cellY);
3709 Object tag = v.getTag();
3710 String desc = "Collision while binding workspace item: " + item
3711 + ". Collides with " + tag;
3712 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3713 throw (new RuntimeException(desc));
3714 } else {
3715 Log.d(TAG, desc);
3716 LauncherModel.deleteItemFromDatabase(this, item);
3717 continue;
3718 }
3719 }
3720 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003721 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3722 item.cellY, 1, 1);
3723 if (animateIcons) {
3724 // Animate all the applications up now
3725 view.setAlpha(0f);
3726 view.setScaleX(0f);
3727 view.setScaleY(0f);
3728 bounceAnims.add(createNewAppBounceAnimation(view, i));
3729 newShortcutsScreenId = item.screenId;
3730 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003731 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003732
Winson Chung997a9232013-07-24 15:33:46 -07003733 if (animateIcons) {
3734 // Animate to the correct page
3735 if (newShortcutsScreenId > -1) {
3736 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003737 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003738 final Runnable startBounceAnimRunnable = new Runnable() {
3739 public void run() {
3740 anim.playTogether(bounceAnims);
3741 anim.start();
3742 }
3743 };
Winson Chung997a9232013-07-24 15:33:46 -07003744 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003745 // We post the animation slightly delayed to prevent slowdowns
3746 // when we are loading right after we return to launcher.
3747 mWorkspace.postDelayed(new Runnable() {
3748 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003749 if (mWorkspace != null) {
3750 mWorkspace.snapToPage(newScreenIndex);
3751 mWorkspace.postDelayed(startBounceAnimRunnable,
3752 NEW_APPS_ANIMATION_DELAY);
3753 }
Winson Chung94d67682013-09-25 16:29:40 -07003754 }
3755 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003756 } else {
3757 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003758 }
3759 }
Winson Chung64359a52013-07-08 17:17:08 -07003760 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003761 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003762 }
3763
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003764 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3765 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3766 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003767 view.updateAppWidget(null);
3768 view.setOnClickListener(this);
3769 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003770 mWorkspace.requestLayout();
3771 }
3772
Joe Onorato9c1289c2009-08-17 11:03:03 -04003773 /**
3774 * Add the views for a widget to the workspace.
3775 *
3776 * Implementation of the method from LauncherModel.Callbacks.
3777 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003778 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003779 Runnable r = new Runnable() {
3780 public void run() {
3781 bindAppWidget(item);
3782 }
3783 };
3784 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003785 return;
3786 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003787
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003788 if (mIsSafeModeEnabled) {
3789 bindSafeModeWidget(item);
3790 return;
3791 }
3792
Daniel Sandler843e8602010-06-07 14:59:01 -04003793 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3794 if (DEBUG_WIDGETS) {
3795 Log.d(TAG, "bindAppWidget: " + item);
3796 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003797
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003798 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003799
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003800 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3801 // If the provider is not ready, bind as a pending widget.
3802 appWidgetInfo = null;
3803 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3804 // The widget id is not valid. Try to find the widget based on the provider info.
3805 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3806 } else {
3807 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3808 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003809
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003810 // If the provider is ready, but the width is not yet restored, try to restore it.
3811 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3812 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003813 if (appWidgetInfo == null) {
3814 if (DEBUG_WIDGETS) {
3815 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3816 + " belongs to component " + item.providerName
3817 + ", as the povider is null");
3818 }
3819 LauncherModel.deleteItemFromDatabase(this, item);
3820 return;
3821 }
Sunny Goyalff572272014-07-23 13:58:07 -07003822
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003823 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003824 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003825 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3826 // Id has not been allocated yet. Allocate a new id.
3827 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3828 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003829
Sunny Goyald478c832016-04-01 12:04:16 -07003830 // Also try to bind the widget. If the bind fails, the user will be shown
3831 // a click to setup UI, which will ask for the bind permission.
3832 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3833 pendingInfo.spanX = item.spanX;
3834 pendingInfo.spanY = item.spanY;
3835 pendingInfo.minSpanX = item.minSpanX;
3836 pendingInfo.minSpanY = item.minSpanY;
3837 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003838
3839 boolean isDirectConfig =
3840 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3841 if (isDirectConfig && item.bindOptions != null) {
3842 Bundle newOptions = item.bindOptions.getExtras();
3843 if (options != null) {
3844 newOptions.putAll(options);
3845 }
3846 options = newOptions;
3847 }
Sunny Goyald478c832016-04-01 12:04:16 -07003848 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3849 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003850
Sunny Goyal86df1382016-08-10 15:03:22 -07003851 // We tried to bind once. If we were not able to bind, we would need to
3852 // go through the permission dialog, which means we cannot skip the config
3853 // activity.
3854 item.bindOptions = null;
3855 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3856
Sunny Goyald478c832016-04-01 12:04:16 -07003857 // Bind succeeded
3858 if (success) {
3859 // If the widget has a configure activity, it is still needs to set it up,
3860 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003861 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003862 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3863 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003864 }
Sunny Goyald478c832016-04-01 12:04:16 -07003865
3866 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003867 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003868 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003869 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003870 // The widget was marked as UI not ready, but there is no configure activity to
3871 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003872 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3873 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003874 }
Sunny Goyalff572272014-07-23 13:58:07 -07003875 }
3876
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003877 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003878 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003879 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3880 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003881 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003882
Sunny Goyal56c73602015-09-25 12:55:01 -07003883 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003884 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003885 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003886 deleteWidgetInfo(item);
3887 return;
3888 }
3889
Sunny Goyal233ee962015-08-03 13:05:01 -07003890 item.minSpanX = appWidgetInfo.minSpanX;
3891 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003892 addAppWidgetToWorkspace(
3893 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3894 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003895 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003896 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003897 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003898 view.updateAppWidget(null);
3899 view.setOnClickListener(this);
3900 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003901 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003902 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003903
Daniel Sandler843e8602010-06-07 14:59:01 -04003904 if (DEBUG_WIDGETS) {
3905 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3906 + (SystemClock.uptimeMillis()-start) + "ms");
3907 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003908 }
3909
Sunny Goyalff572272014-07-23 13:58:07 -07003910 /**
3911 * Restores a pending widget.
3912 *
3913 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003914 */
Sunny Goyald478c832016-04-01 12:04:16 -07003915 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003916 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3917 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3918 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003919 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003920 }
3921
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003922 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003923 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003924
3925 mWorkspace.reinflateWidgetsIfNecessary();
3926 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003927 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003928 }
3929
Adam Cohen1462de32012-07-24 22:34:36 -07003930 public void onPageBoundSynchronously(int page) {
3931 mSynchronouslyBoundPages.add(page);
3932 }
3933
Sunny Goyal527c7d32015-08-28 15:19:36 -07003934 @Override
3935 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3936 if (mPendingExecutor != null) {
3937 mPendingExecutor.markCompleted();
3938 }
3939 mPendingExecutor = executor;
3940 executor.attachTo(this);
3941 }
3942
3943 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3944 if (mPendingExecutor == executor) {
3945 mPendingExecutor = null;
3946 }
3947 }
3948
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003949 @Override
3950 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3951 Runnable r = new Runnable() {
3952 public void run() {
3953 finishFirstPageBind(executor);
3954 }
3955 };
3956 if (waitUntilResume(r)) {
3957 return;
3958 }
3959
3960 Runnable onComplete = new Runnable() {
3961 @Override
3962 public void run() {
3963 if (executor != null) {
3964 executor.onLoadAnimationCompleted();
3965 }
3966 }
3967 };
3968 if (mDragLayer.getAlpha() < 1) {
3969 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3970 } else {
3971 onComplete.run();
3972 }
3973 }
3974
Joe Onorato9c1289c2009-08-17 11:03:03 -04003975 /**
3976 * Callback saying that there aren't any more items to bind.
3977 *
3978 * Implementation of the method from LauncherModel.Callbacks.
3979 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003980 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003981 Runnable r = new Runnable() {
3982 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003983 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003984 }
3985 };
3986 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003987 return;
3988 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003989 if (LauncherAppState.PROFILE_STARTUP) {
3990 Trace.beginSection("Page bind completed");
3991 }
Adam Cohen1462de32012-07-24 22:34:36 -07003992 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003993
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003994 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003995
Sunny Goyal2100c782016-08-22 16:00:03 -07003996 if (mPendingActivityResult != null) {
3997 handleActivityResult(mPendingActivityResult.requestCode,
3998 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3999 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07004000 }
4001
Sunny Goyal756adbc2015-04-16 15:20:51 -07004002 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004003
4004 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004005 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004006 }
Sunny Goyale26d1002016-06-20 14:52:14 -07004007 if (LauncherAppState.PROFILE_STARTUP) {
4008 Trace.endSection();
4009 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004010 }
4011
Winson Chunga2413752012-04-03 14:22:34 -07004012 private boolean canRunNewAppsAnimation() {
4013 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07004014 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07004015 }
4016
Winson Chungc9168342013-06-26 14:54:55 -07004017 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004018 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004019 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4020 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004021 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004022 return bounceAnim;
4023 }
4024
Adam Cohen27772732013-11-11 14:00:56 +00004025 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004026 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004027 }
4028
Tony Wickham55616cd2015-09-23 14:55:17 -07004029 public int getSearchBarHeight() {
4030 if (mLauncherCallbacks != null) {
4031 return mLauncherCallbacks.getSearchBarHeight();
4032 }
4033 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4034 }
4035
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004036 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004037 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4038 * multiple calls to bind the same list.)
4039 */
4040 @Thunk ArrayList<AppInfo> mTmpAppsList;
4041 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4042 public void run() {
4043 bindAllApplications(mTmpAppsList);
4044 mTmpAppsList = null;
4045 }
4046 };
4047
4048 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004049 * Add the icons for all apps.
4050 *
4051 * Implementation of the method from LauncherModel.Callbacks.
4052 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004053 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004054 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4055 mTmpAppsList = apps;
4056 return;
4057 }
4058
Winson Chungb745afb2015-03-02 11:51:23 -08004059 if (mAppsView != null) {
4060 mAppsView.setApps(apps);
4061 }
Adam Cohen9211d422014-10-07 18:14:53 -07004062 if (mLauncherCallbacks != null) {
4063 mLauncherCallbacks.bindAllApplications(apps);
4064 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004065 }
4066
4067 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004068 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
4069 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
4070 */
4071 @Override
4072 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
4073 mDeepShortcutMap = deepShortcutMapCopy;
4074 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
4075 }
4076
Tony Wickham1bce7fd2016-04-28 17:39:03 -07004077 public List<String> getShortcutIdsForItem(ItemInfo info) {
4078 if (!DeepShortcutManager.supportsShortcuts(info)) {
4079 return Collections.EMPTY_LIST;
4080 }
4081 ComponentName component = info.getTargetComponent();
Hyunyoung Songd3bf9802016-08-29 14:43:53 -07004082 if (component == null) {
4083 return Collections.EMPTY_LIST;
4084 }
4085
Tony Wickham1bce7fd2016-04-28 17:39:03 -07004086 List<String> ids = mDeepShortcutMap.get(new ComponentKey(component, info.user));
4087 return ids == null ? Collections.EMPTY_LIST : ids;
4088 }
4089
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004090 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004091 * A package was updated.
4092 *
4093 * Implementation of the method from LauncherModel.Callbacks.
4094 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004095 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004096 Runnable r = new Runnable() {
4097 public void run() {
4098 bindAppsUpdated(apps);
4099 }
4100 };
4101 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004102 return;
4103 }
4104
Winson Chungb745afb2015-03-02 11:51:23 -08004105 if (mAppsView != null) {
4106 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004107 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004108 }
4109
Sunny Goyal4390ace2014-10-13 11:33:11 -07004110 @Override
4111 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4112 Runnable r = new Runnable() {
4113 public void run() {
4114 bindWidgetsRestored(widgets);
4115 }
4116 };
4117 if (waitUntilResume(r)) {
4118 return;
4119 }
4120 mWorkspace.widgetsRestored(widgets);
4121 }
4122
Joe Onorato9c1289c2009-08-17 11:03:03 -04004123 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004124 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004125 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004126 *
4127 * @param updated list of shortcuts which have changed.
4128 * @param removed list of shortcuts which were deleted in the background. This can happen when
4129 * an app gets removed from the system or some of its components are no longer
4130 * available.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004131 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004132 @Override
4133 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4134 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004135 Runnable r = new Runnable() {
4136 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004137 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004138 }
4139 };
4140 if (waitUntilResume(r)) {
4141 return;
4142 }
4143
Sunny Goyal4390ace2014-10-13 11:33:11 -07004144 if (!updated.isEmpty()) {
4145 mWorkspace.updateShortcuts(updated);
4146 }
4147
4148 if (!removed.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004149 HashSet<ComponentName> removedComponents = new HashSet<>();
4150 HashSet<ShortcutKey> removedDeepShortcuts = new HashSet<>();
4151
Sunny Goyal4390ace2014-10-13 11:33:11 -07004152 for (ShortcutInfo si : removed) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004153 if (si.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07004154 removedDeepShortcuts.add(ShortcutKey.fromShortcutInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004155 } else {
4156 removedComponents.add(si.getTargetComponent());
4157 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004158 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004159
4160 if (!removedComponents.isEmpty()) {
4161 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(removedComponents, user);
4162 mWorkspace.removeItemsByMatcher(matcher);
4163 mDragController.onAppsRemoved(matcher);
4164 }
4165
4166 if (!removedDeepShortcuts.isEmpty()) {
4167 ItemInfoMatcher matcher = ItemInfoMatcher.ofShortcutKeys(removedDeepShortcuts);
4168 mWorkspace.removeItemsByMatcher(matcher);
4169 mDragController.onAppsRemoved(matcher);
4170 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004171 }
4172 }
4173
4174 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004175 * Update the state of a package, typically related to install state.
4176 *
4177 * Implementation of the method from LauncherModel.Callbacks.
4178 */
Sunny Goyale755d462014-07-22 13:48:29 -07004179 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004180 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4181 Runnable r = new Runnable() {
4182 public void run() {
4183 bindRestoreItemsChange(updates);
4184 }
4185 };
4186 if (waitUntilResume(r)) {
4187 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004188 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004189
Sunny Goyal756adbc2015-04-16 15:20:51 -07004190 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004191 }
4192
4193 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004194 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004195 * in addition to specific applications to remove, the reason being that
4196 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004197 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004198 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004199 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004200 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004201 public void bindWorkspaceComponentsRemoved(
4202 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4203 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004204 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004205 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004206 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004207 }
Winson Chungd64d1762013-08-20 14:37:16 -07004208 };
4209 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004210 return;
4211 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004212 if (!packageNames.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004213 ItemInfoMatcher matcher = ItemInfoMatcher.ofPackages(packageNames, user);
4214 mWorkspace.removeItemsByMatcher(matcher);
4215 mDragController.onAppsRemoved(matcher);
4216
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004217 }
4218 if (!components.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004219 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(components, user);
4220 mWorkspace.removeItemsByMatcher(matcher);
4221 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004222 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004223 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004224
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004225 @Override
4226 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4227 Runnable r = new Runnable() {
4228 public void run() {
4229 bindAppInfosRemoved(appInfos);
4230 }
4231 };
4232 if (waitUntilResume(r)) {
4233 return;
Joe Onorato36115782010-06-17 13:28:48 -04004234 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004235
Winson Chungdf95eb12013-10-16 14:57:07 -07004236 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004237 if (mAppsView != null) {
4238 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004239 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004240 }
Joe Onoratobe386092009-11-17 17:32:16 -08004241
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004242 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004243 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004244 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004245 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004246 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004247
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004248 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004249 public void bindWidgetsModel(WidgetsModel model) {
4250 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004251 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004252 return;
4253 }
4254
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004255 if (mWidgetsView != null && model != null) {
4256 mWidgetsView.addWidgets(model);
4257 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004258 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004259 }
4260
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004261 @Override
4262 public void notifyWidgetProvidersChanged() {
4263 if (mWorkspace.getState().shouldUpdateWidget) {
4264 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4265 }
4266 }
4267
Winson Chung400438b2011-01-16 17:53:48 -08004268 private int mapConfigurationOriActivityInfoOri(int configOri) {
4269 final Display d = getWindowManager().getDefaultDisplay();
4270 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4271 switch (d.getRotation()) {
4272 case Surface.ROTATION_0:
4273 case Surface.ROTATION_180:
4274 // We are currently in the same basic orientation as the natural orientation
4275 naturalOri = configOri;
4276 break;
4277 case Surface.ROTATION_90:
4278 case Surface.ROTATION_270:
4279 // We are currently in the other basic orientation to the natural orientation
4280 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4281 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4282 break;
4283 }
4284
4285 int[] oriMap = {
4286 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4287 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4288 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4289 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4290 };
4291 // Since the map starts at portrait, we need to offset if this device's natural orientation
4292 // is landscape.
4293 int indexOffset = 0;
4294 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4295 indexOffset = 1;
4296 }
4297 return oriMap[(d.getRotation() + indexOffset) % 4];
4298 }
Adam Cohen446e9402011-09-15 18:21:21 -07004299
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004300 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004301 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004302 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004303 if (Utilities.ATLEAST_JB_MR2) {
4304 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4305 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004306 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4307 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004308 }
Winson Chung4b919f82012-05-01 10:44:08 -07004309 }
4310 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004311
Winson Chung4b919f82012-05-01 10:44:08 -07004312 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004313 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004314 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004315 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004316 } else {
4317 mHandler.postDelayed(new Runnable() {
4318 public void run() {
4319 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4320 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004321 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004322 }
Winson Chung4b919f82012-05-01 10:44:08 -07004323 }
Winson Chung400438b2011-01-16 17:53:48 -08004324 }
4325
Hyunyoung Songc001cf52016-07-21 17:32:43 -07004326 private void markAppsViewShown() {
4327 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4328 return;
4329 }
4330 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
4331 }
4332
4333 private boolean shouldShowDiscoveryBounce() {
4334 if (mState != mState.WORKSPACE) {
4335 return false;
4336 }
4337 if (mLauncherCallbacks != null && mLauncherCallbacks.shouldShowDiscoveryBounce()) {
4338 return true;
4339 }
4340 if (!mIsResumeFromActionScreenOff) {
4341 return false;
4342 }
4343 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4344 return false;
4345 }
4346 return true;
4347 }
4348
Winson Chung5ffd51d2015-06-25 11:36:50 -07004349 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004350 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004351 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004352 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004353 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004354 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004355 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4356 if (userHandle != null) {
4357 user = UserHandleCompat.fromUser(userHandle);
4358 }
4359 }
4360 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004361 }
4362
4363 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004364 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004365 if (user == null) {
4366 user = UserHandleCompat.myUserHandle();
4367 }
4368
4369 // Called from search suggestion, add the profile extra to the intent to ensure that we
4370 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004371 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004372 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004373 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004374 return null;
4375 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004376 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004377 }
4378
Winson Chung5ffd51d2015-06-25 11:36:50 -07004379 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004380 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4381 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004382 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004383 UserHandleCompat.myUserHandle());
4384 }
4385
Winson Chung5ffd51d2015-06-25 11:36:50 -07004386 protected void moveWorkspaceToDefaultScreen() {
4387 mWorkspace.moveToDefaultScreen(false);
4388 }
4389
Winson Chung80baf5a2010-08-09 16:03:15 -07004390 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004391 * Returns a FastBitmapDrawable with the icon, accurately sized.
4392 */
4393 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4394 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4395 d.setFilterBitmap(true);
4396 resizeIconDrawable(d);
4397 return d;
4398 }
4399
4400 /**
4401 * Resizes an icon drawable to the correct icon size.
4402 */
Winson5fbe0742015-09-30 15:33:00 -07004403 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004404 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004405 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004406 }
4407
4408 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004409 * Prints out out state for debugging.
4410 */
4411 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004412 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004413 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
Sunny Goyal2100c782016-08-22 16:00:03 -07004414 Log.d(TAG, "mPendingRequestArgs=" + mPendingRequestArgs);
4415 Log.d(TAG, "mPendingActivityResult=" + mPendingActivityResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004416 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004417 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004418
Daniel Sandler325dc232013-06-05 22:57:57 -04004419 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004420 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004421
4422 @Override
4423 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4424 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004425 // Dump workspace
4426 writer.println(prefix + "Workspace Items");
4427 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4428 writer.println(prefix + " Homescreen " + i);
4429
4430 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4431 for (int j = 0; j < layout.getChildCount(); j++) {
4432 Object tag = layout.getChildAt(j).getTag();
4433 if (tag != null) {
4434 writer.println(prefix + " " + tag.toString());
4435 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004436 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004437 }
Sunny Goyala1365452015-10-01 15:46:24 -07004438
4439 writer.println(prefix + " Hotseat");
4440 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4441 for (int j = 0; j < layout.getChildCount(); j++) {
4442 Object tag = layout.getChildAt(j).getTag();
4443 if (tag != null) {
4444 writer.println(prefix + " " + tag.toString());
4445 }
4446 }
4447
Sunny Goyal713edfc2016-05-06 09:58:34 -07004448 try {
4449 FileLog.flushAll(writer);
4450 } catch (Exception e) {
4451 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004452 }
4453
Adam Cohen9211d422014-10-07 18:14:53 -07004454 if (mLauncherCallbacks != null) {
4455 mLauncherCallbacks.dump(prefix, fd, writer, args);
4456 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004457 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004458
Adam Cohen59400422014-03-05 18:07:04 -08004459 public static CustomAppWidget getCustomAppWidget(String name) {
4460 return sCustomAppWidgets.get(name);
4461 }
4462
4463 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4464 return sCustomAppWidgets;
4465 }
4466
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004467 public static Launcher getLauncher(Context context) {
4468 if (context instanceof Launcher) {
4469 return (Launcher) context;
4470 }
4471 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4472 }
4473
Sunny Goyal745bad92016-05-02 10:54:12 -07004474 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4475
4476 @Override
4477 public void onSharedPreferenceChanged(
4478 SharedPreferences sharedPreferences, String key) {
4479 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4480 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4481 if (!waitUntilResume(this, true)) {
4482 run();
4483 }
4484 }
4485 }
4486
4487 @Override
4488 public void run() {
4489 setOrientation();
4490 }
4491 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004492}