blob: d768ea87ce9d9cf9b46a075e274a68d550270a49 [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;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080037import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Sunny Goyal756cd262015-08-20 12:33:21 -070039import android.content.ContentValues;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040041import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070044import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070046import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070050import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070053import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070056import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020059import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020061import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080062import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070063import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070064import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070066import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.HapticFeedbackConstants;
74import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.MotionEvent;
77import android.view.Surface;
78import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070079import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080080import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070081import android.view.ViewGroup;
82import android.view.ViewTreeObserver;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080084import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070085import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070086import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080090import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070092
Sunny Goyal651077b2014-06-30 14:15:31 -070093import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070094import com.android.launcher3.LauncherSettings.Favorites;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070095import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070096import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080097import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -070098import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010099import com.android.launcher3.compat.LauncherActivityInfoCompat;
100import com.android.launcher3.compat.LauncherAppsCompat;
101import com.android.launcher3.compat.UserHandleCompat;
102import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800103import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700104import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700105import com.android.launcher3.dragndrop.DragController;
106import com.android.launcher3.dragndrop.DragLayer;
107import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -0700108import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000109import com.android.launcher3.folder.Folder;
110import com.android.launcher3.folder.FolderIcon;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700111import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700112import com.android.launcher3.logging.FileLog;
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800113import com.android.launcher3.logging.LoggerUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700114import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700115import com.android.launcher3.model.WidgetsModel;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700116import com.android.launcher3.pageindicators.PageIndicator;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800117import com.android.launcher3.userevent.nano.LauncherLogProto;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700118import com.android.launcher3.util.ComponentKey;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700119import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal322d5562015-06-25 19:35:49 -0700120import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700121import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700122import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700123import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700124import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700125import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700126
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700127import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700128import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700129import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700130import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800131import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700132import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700133import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700134import java.util.List;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800135import java.util.Locale;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700136
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137/**
138 * Default launcher application.
139 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100140public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700141 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
142 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700143 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700144 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700145 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
Daniel Sandlerf061f822013-06-27 13:59:36 -0400147 static final boolean PROFILE_STARTUP = false;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700148 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700149 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400150 static final boolean DEBUG_RESUME_TIME = false;
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700151 static final boolean DEBUG_LOGGING = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700154 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700155 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700156 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
Michael Jurka8b805b12012-04-18 14:23:14 -0700158 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700159 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700160 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700161
Sunny Goyal28c6b962015-10-12 11:42:05 -0700162 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
163
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700164 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
165 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
166 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
167
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700168 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
169
Mathew Inwood876a8462013-06-14 14:12:41 +0100170 /**
171 * IntentStarter uses request codes starting with this. This must be greater than all activity
172 * request codes used internally.
173 */
174 protected static final int REQUEST_LAST = 100;
175
Michael Jurka0a457bf2012-11-19 14:05:05 -0800176 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700177 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800178 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
Winson Chung2672ff92012-05-04 16:22:30 -0700180 // The Intent extra that defines whether to ignore the launch animation
181 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400182 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700183
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700184 public static final String ACTION_APPWIDGET_HOST_RESET =
185 "com.android.launcher3.intent.ACTION_APPWIDGET_HOST_RESET";
186
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 // Type: int
188 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700189 // Type: int
190 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700191 // Type: Content Values / parcelable
192 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700193 // Type: parcelable
194 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000195 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800196 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197
Adam Cohen432609a2014-03-13 17:03:22 -0700198 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800199 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
200
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700201 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700202 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
203 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700204
Adam Cohen091440a2015-03-18 14:16:05 -0700205 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700206 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700207
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700208 private boolean mIsSafeModeEnabled;
209
Joe Onorato9c1289c2009-08-17 11:03:03 -0400210 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800211 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700212 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700213 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214
Winson Chunga2413752012-04-03 14:22:34 -0700215 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700216 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
217 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700218 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700219
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700220 private final BroadcastReceiver mUiBroadcastReceiver = new BroadcastReceiver() {
221
222 @Override
223 public void onReceive(Context context, Intent intent) {
224 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
225 closeSystemDialogs();
226 } else if (ACTION_APPWIDGET_HOST_RESET.equals(intent.getAction())) {
227 if (mAppWidgetHost != null) {
228 mAppWidgetHost.startListening();
229 }
230 }
231 }
232 };
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800233
Adam Cohen091440a2015-03-18 14:16:05 -0700234 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700235 private View mLauncherView;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700236 private PageIndicator mPageIndicator;
Adam Cohen091440a2015-03-18 14:16:05 -0700237 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800238 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700239
240 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700241
Sunny Goyalffe83f12014-08-14 17:39:34 -0700242 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700243 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700244
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700245 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800246 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800247 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700248
Michael Jurka0280c3b2010-09-17 15:00:07 -0700249 private int[] mTmpAddItemCellCoordinates = new int[2];
250
Sunny Goyal316490e2015-06-02 09:38:28 -0700251 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800252 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700253
Michael Jurka838a4ca2011-02-07 13:33:06 -0800254 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700255 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700256
Sunny Goyal47328fd2016-05-25 18:56:41 -0700257 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700258
259 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700260 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700261 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700262
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700263 // Main container view and the model for the widget tray screen.
264 @Thunk WidgetsContainerView mWidgetsView;
265 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700266
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700268 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
269 // scroll issues (because the workspace may not have been measured yet) and extra work.
270 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
271 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272
273 private SpannableStringBuilder mDefaultKeySsb = null;
274
Adam Cohen091440a2015-03-18 14:16:05 -0700275 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400276
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800277 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278 private boolean mRestoring;
279 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700280 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281
Michael Jurka1e2f4652013-07-08 18:03:46 -0700282 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700283 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700284 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700285
Joe Onorato9c1289c2009-08-17 11:03:03 -0400286 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800287 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700288 private ExtractedColors mExtractedColors;
Adam Cohen091440a2015-03-18 14:16:05 -0700289 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800290 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700291 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800292 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400293
Sunny Goyal639e9062015-08-19 19:17:06 -0700294 private LauncherClings mClings;
295
Adam Cohen61f560d2013-09-30 15:58:20 -0700296 private View.OnTouchListener mHapticFeedbackTouchListener;
297
Adam Cohended9f8d2010-11-03 13:25:16 -0700298 // Related to the auto-advancing of widgets
299 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700300 private static final int ADVANCE_INTERVAL = 20000;
301 private static final int ADVANCE_STAGGER = 250;
302
303 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700304 private long mAutoAdvanceSentTime;
305 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700306 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700307
Winson Chung400438b2011-01-16 17:53:48 -0800308 // Determines how long to wait after a rotation before restoring the screen orientation to
309 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700310 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800311
Adam Cohen091440a2015-03-18 14:16:05 -0700312 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700313
Adam Cohen1462de32012-07-24 22:34:36 -0700314 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700315 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700316
Winson Chung46353de2012-02-16 14:05:10 -0800317 // We only want to get the SharedPreferences once since it does an FS stat each time we get
318 // it from the context.
319 private SharedPreferences mSharedPrefs;
320
Winson Chungf0c6ae02012-03-21 16:10:31 -0700321 // Holds the page that we need to animate to, and the icon views that we need to animate up
322 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700323 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700324 private Bitmap mFolderIconBitmap;
325 private Canvas mFolderIconCanvas;
326 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700327
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700328 private DeviceProfile mDeviceProfile;
329
Adam Cohen4b66bf32015-09-18 12:15:19 -0700330 private boolean mMoveToDefaultScreenFromNewIntent;
331
Winson Chung13eb5272015-05-11 16:30:13 -0700332 // This is set to the view that launched the activity that navigated the user away from
333 // launcher. Since there is no callback for when the activity has finished launching, enable
334 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800335 private BubbleTextView mWaitingForResume;
336
Adam Cohen59400422014-03-05 18:07:04 -0800337 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
338 new HashMap<String, CustomAppWidget>();
339
Adam Cohen59400422014-03-05 18:07:04 -0800340 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700341 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
342 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800343 }
344 }
345
Adam Cohen091440a2015-03-18 14:16:05 -0700346 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700347 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700348 if (mWorkspace != null) {
349 mWorkspace.buildPageHardwareLayers();
350 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700351 }
352 };
353
Adam Cohendb364c32014-05-20 14:23:40 -0700354 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800355
Adam Cohen091440a2015-03-18 14:16:05 -0700356 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800357 int requestCode;
358 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700359 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700360 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800361 int cellX;
362 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700363 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800364 }
365
Hyunyoung Songaa953652016-04-19 18:30:24 -0700366 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800367
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700368 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700369 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400370
371 @Thunk void setOrientation() {
372 if (mRotationEnabled) {
373 unlockScreenOrientation(true);
374 } else {
375 setRequestedOrientation(
376 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700377 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400378 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700379
Sunny Goyal745bad92016-05-02 10:54:12 -0700380 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700381
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 @Override
383 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700384 if (DEBUG_STRICT_MODE) {
385 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
386 .detectDiskReads()
387 .detectDiskWrites()
388 .detectNetwork() // or .detectAll() for all detectable problems
389 .penaltyLog()
390 .build());
391 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
392 .detectLeakedSqlLiteObjects()
393 .detectLeakedClosableObjects()
394 .penaltyLog()
395 .penaltyDeath()
396 .build());
397 }
398
Adam Cohen9211d422014-10-07 18:14:53 -0700399 if (mLauncherCallbacks != null) {
400 mLauncherCallbacks.preOnCreate();
401 }
402
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400404
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400405 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700406
Adam Cohen2e6da152015-05-06 11:42:25 -0700407 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700408 mDeviceProfile = getResources().getConfiguration().orientation
409 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700410 app.getInvariantDeviceProfile().landscapeProfile
411 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700412
Sunny Goyalf7258242015-10-19 16:59:07 -0700413 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700414 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800415 mModel = app.setLauncher(this);
416 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700417
Joe Onorato41a12d22009-10-31 18:30:00 -0400418 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700419 mAllAppsController = new AllAppsTransitionController(this);
420 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700421
Sunny Goyalffe83f12014-08-14 17:39:34 -0700422 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700423
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700424 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
425 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700426
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700427 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
428 // this also ensures that any synchronous binding below doesn't re-trigger another
429 // LauncherModel load.
430 mPaused = false;
431
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200433 android.os.Debug.startMethodTracing(
434 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800435 }
436
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700438
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700440 mDeviceProfile.layout(this);
Tony Wickham827cef22016-03-17 15:39:39 -0700441 mExtractedColors = new ExtractedColors();
442 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443
Tony Wickhame2217252016-03-22 16:34:23 -0700444 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
445 .addAccessibilityStateChangeListener(this);
446
Joe Onorato7c312c12009-08-13 21:36:53 -0700447 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700448
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 mSavedState = savedInstanceState;
450 restoreState(mSavedState);
451
452 if (PROFILE_STARTUP) {
453 android.os.Debug.stopMethodTracing();
454 }
455
456 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700457 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700458 // If the user leaves launcher, then we should just load items asynchronously when
459 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700460 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700461 } else {
462 // We only load the page synchronously if the user rotates (or triggers a
463 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700464 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700465 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 }
467
468 // For handling default keys
469 mDefaultKeySsb = new SpannableStringBuilder();
470 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800471
472 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700473 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
474 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800475
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800476 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700477 // In case we are on a device with locked rotation, we should look at preferences to check
478 // if the user has specifically allowed rotation.
479 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700480 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700481 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
482 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700483 }
484
485 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
486 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400487 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700488
Adam Cohen9211d422014-10-07 18:14:53 -0700489 if (mLauncherCallbacks != null) {
490 mLauncherCallbacks.onCreate(savedInstanceState);
491 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700492
493 if (shouldShowIntroScreen()) {
494 showIntroScreen();
495 } else {
496 showFirstRunActivity();
497 showFirstRunClings();
498 }
Adam Cohen9211d422014-10-07 18:14:53 -0700499 }
500
Sunny Goyal7779d622015-06-11 16:18:39 -0700501 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700502 public void onExtractedColorsChanged() {
503 loadExtractedColorsAndColorItems();
504 }
505
506 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700507 // TODO: do this in pre-N as well, once the extraction part is complete.
508 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700509 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700510 mHotseat.updateColor(mExtractedColors, !mPaused);
Tony Wickham770217c2016-05-18 15:16:40 -0700511 if (mPageIndicator != null) {
512 mPageIndicator.updateColor(mExtractedColors);
513 }
Tony Wickham827cef22016-03-17 15:39:39 -0700514 }
515 }
516
Adam Cohen9211d422014-10-07 18:14:53 -0700517 private LauncherCallbacks mLauncherCallbacks;
518
519 public void onPostCreate(Bundle savedInstanceState) {
520 super.onPostCreate(savedInstanceState);
521 if (mLauncherCallbacks != null) {
522 mLauncherCallbacks.onPostCreate(savedInstanceState);
523 }
524 }
525
Sunny Goyal32554d12015-12-03 15:31:25 -0800526 /**
527 * Call this after onCreate to set or clear overlay.
528 */
529 public void setLauncherOverlay(LauncherOverlay overlay) {
530 if (overlay != null) {
531 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
532 }
533 mWorkspace.setLauncherOverlay(overlay);
534 }
535
Adam Cohen9211d422014-10-07 18:14:53 -0700536 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
537 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700538 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700539 private boolean mWorkspaceImportanceStored = false;
540 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700541 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800542 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700543 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
544
545 @Override
546 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700547 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700548 return;
549 }
550 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700551 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700552 if (mWorkspace != null) {
553 mWorkspaceImportanceForAccessibility =
554 mWorkspace.getImportantForAccessibility();
555 mWorkspace.setImportantForAccessibility(
556 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
557 mWorkspaceImportanceStored = true;
558 }
559 if (mHotseat != null) {
560 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
561 mHotseat.setImportantForAccessibility(
562 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
563 mHotseatImportanceStored = true;
564 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700565 }
566
567 @Override
568 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700569 if (mWorkspaceImportanceStored && mWorkspace != null) {
570 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
571 }
572 if (mHotseatImportanceStored && mHotseat != null) {
573 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
574 }
575 mWorkspaceImportanceStored = false;
576 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700577 }
578 });
Adam Cohen9211d422014-10-07 18:14:53 -0700579 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700580 }
581
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700582 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700583 public void onLauncherProviderChange() {
584 if (mLauncherCallbacks != null) {
585 mLauncherCallbacks.onLauncherProviderChange();
586 }
587 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700588
Adam Cohen9211d422014-10-07 18:14:53 -0700589 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700590 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700591 if (mLauncherCallbacks != null) {
592 return mLauncherCallbacks.hasCustomContentToLeft();
593 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700594 return false;
595 }
596
Dave Hawkeya8881582013-09-17 15:55:33 -0600597 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500598 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600599 * {@link #addToCustomContentPage}. This will only be invoked if
600 * {@link #hasCustomContentToLeft()} is {@code true}.
601 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500602 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700603 if (mLauncherCallbacks != null) {
604 mLauncherCallbacks.populateCustomContentContainer();
605 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600606 }
607
608 /**
609 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
610 * ensure the custom content page is added or removed if necessary.
611 */
612 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600613 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600614 // Not bound yet, wait for bindScreens to be called.
615 return;
616 }
617
618 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
619 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500620 mWorkspace.createCustomContentContainer();
621 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600622 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
623 mWorkspace.removeCustomContentPage();
624 }
625 }
626
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800627 /**
628 * Logger object is a singleton and does not have to be coupled with the foreground activity.
629 * Since most user event logging is done on the UI, the object is retrieved from the
630 * callback for convenience.
631 */
Hyunyoung Songaa953652016-04-19 18:30:24 -0700632 private UserEventDispatcher createUserEventDispatcher() {
633 return new UserEventDispatcher() {
634 @Override
635 public void dispatchUserEvent(LauncherLogProto.LauncherEvent ev, Intent intent) {
636 if (!DEBUG_LOGGING) {
637 return;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800638 }
Hyunyoung Songaa953652016-04-19 18:30:24 -0700639 Log.d("UserEvent", String.format(Locale.US,
640 "action:%s\nchild:%s\nparent:%s\nelapsed container %d ms session %d ms",
641 LoggerUtils.getActionStr(ev.action),
642 LoggerUtils.getTargetStr(ev.srcTarget[0]),
643 LoggerUtils.getTargetStr(ev.srcTarget[1]),
644 ev.elapsedContainerMillis,
645 ev.elapsedSessionMillis));
646 }
647 };
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800648 }
649
Hyunyoung Songaa953652016-04-19 18:30:24 -0700650 public UserEventDispatcher getUserEventDispatcher() {
651 if (mLauncherCallbacks != null) {
652 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
653 if (dispatcher != null) {
654 return dispatcher;
655 }
656 }
657
658 if (mUserEventDispatcher == null) {
659 mUserEventDispatcher = createUserEventDispatcher();
660 }
661 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800662 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100663
Hyunyoung Song3f471442015-04-08 19:01:34 -0700664 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700665 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
666 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700667 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700668 }
669
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000670 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700671 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
672 // This cast is safe as long as the id < 0x00FFFFFF
673 // Since we jail all the dynamically generated views, there should be no clashes
674 // with any other views.
675 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000676 }
677
678 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700679 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
680 * a configuration step, this allows the proper animations to run after other transitions.
681 */
Adam Cohendb364c32014-05-20 14:23:40 -0700682 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700683 long screenId = args.screenId;
684 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
685 // When the screen id represents an actual screen (as opposed to a rank) we make sure
686 // that the drop page actually exists.
687 screenId = ensurePendingDropLayoutExists(args.screenId);
688 }
Adam Cohendb364c32014-05-20 14:23:40 -0700689
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800690 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800691 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700692 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700693 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700694 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800695 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700696 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700697 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700698 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyald478c832016-04-01 12:04:16 -0700699 completeRestoreAppWidget(args.appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700700 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700701 case REQUEST_BIND_PENDING_APPWIDGET: {
702 int widgetId = args.appWidgetId;
703 LauncherAppWidgetInfo info =
704 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
705 if (info != null) {
706 // Since the view was just bound, also launch the configure activity if needed
707 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
708 .getLauncherAppWidgetInfo(widgetId);
709 if (provider != null && provider.configure != null) {
710 startRestoredWidgetReconfigActivity(provider, info);
711 }
712 }
713 break;
714 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800715 }
Michael Jurka27614d22012-04-02 06:40:22 -0700716 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
717 // if you turned the screen off and then back while in All Apps, Launcher would not
718 // return to the workspace. Clearing mAddInfo.container here fixes this issue
719 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700720 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800721 }
722
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800723 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700724 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700725 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700726 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700727 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800728 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700729
Adam Cohenad4e15c2013-10-17 16:21:35 -0700730 Runnable exitSpringLoaded = new Runnable() {
731 @Override
732 public void run() {
733 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
734 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
735 }
736 };
737
Michael Jurka8b805b12012-04-18 14:23:14 -0700738 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700739 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700740 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700741 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
742 if (resultCode == RESULT_CANCELED) {
743 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700744 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700745 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700746 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800747 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700748 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700749 }
750 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200751 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
752 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700753 // User could have free-scrolled between pages before picking a wallpaper; make sure
754 // we move to the closest one now.
755 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700756 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200757 }
758 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700759 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200760
Adam Cohened66b2b2012-01-23 17:28:51 -0800761 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700762 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700763
Adam Cohendb364c32014-05-20 14:23:40 -0700764 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800765 // We have special handling for widgets
766 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800767 final int appWidgetId;
768 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
769 : -1;
770 if (widgetId < 0) {
771 appWidgetId = pendingAddWidgetId;
772 } else {
773 appWidgetId = widgetId;
774 }
775
Adam Cohenad4e15c2013-10-17 16:21:35 -0700776 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800777 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700778 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
779 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700780 result = RESULT_CANCELED;
781 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700782 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700783 @Override
784 public void run() {
785 exitSpringLoadedDragModeDelayed(false, 0, null);
786 }
787 };
Adam Cohendb364c32014-05-20 14:23:40 -0700788 if (workspaceLocked) {
789 // No need to remove the empty screen if we're mid-binding, as the
790 // the bind will not add the empty screen.
791 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
792 } else {
793 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
794 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
795 }
Winson Chung5aaab772012-04-27 15:24:02 -0700796 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700797 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700798 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
799 // When the screen id represents an actual screen (as opposed to a rank)
800 // we make sure that the drop page actually exists.
801 mPendingAddInfo.screenId =
802 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
803 }
Adam Cohendb364c32014-05-20 14:23:40 -0700804 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
805
806 dropLayout.setDropPending(true);
807 final Runnable onComplete = new Runnable() {
808 @Override
809 public void run() {
810 completeTwoStageWidgetDrop(resultCode, appWidgetId);
811 dropLayout.setDropPending(false);
812 }
813 };
814 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
815 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
816 } else {
817 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
818 mPendingAddInfo);
819 sPendingAddItem = args;
820 }
Winson Chung5aaab772012-04-27 15:24:02 -0700821 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800822 return;
823 }
824
Sunny Goyald478c832016-04-01 12:04:16 -0700825 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
826 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700827 if (resultCode == RESULT_OK) {
828 // Update the widget view.
829 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
830 pendingAddWidgetId, mPendingAddInfo);
831 if (workspaceLocked) {
832 sPendingAddItem = args;
833 } else {
834 completeAdd(args);
835 }
836 }
837 // Leave the widget in the pending state if the user canceled the configure.
838 return;
839 }
840
Sunny Goyalaad90582015-07-09 14:22:50 -0700841 if (requestCode == REQUEST_CREATE_SHORTCUT) {
842 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
843 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
844 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
845 mPendingAddInfo);
846 if (isWorkspaceLocked()) {
847 sPendingAddItem = args;
848 } else {
849 completeAdd(args);
850 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
851 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
852 }
853 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700854 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
855 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800858 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800859
860 }
861
862 @Override
863 protected void onActivityResult(
864 final int requestCode, final int resultCode, final Intent data) {
865 handleActivityResult(requestCode, resultCode, data);
866 if (mLauncherCallbacks != null) {
867 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
868 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800869 }
870
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600871 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700872 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600873 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700874 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
875 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
876 View v = null;
877 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
878 if (layout != null) {
879 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
880 }
881 Intent intent = sPendingAddItem.intent;
882 sPendingAddItem = null;
883 if (grantResults.length > 0
884 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700885 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700886 } else {
887 // TODO: Show a snack bar with link to settings
888 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
889 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
890 }
891 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600892 if (mLauncherCallbacks != null) {
893 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
894 grantResults);
895 }
896 }
897
Adam Cohendb364c32014-05-20 14:23:40 -0700898 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
899 appWidgetId, ItemInfo info) {
900 PendingAddArguments args = new PendingAddArguments();
901 args.requestCode = requestCode;
902 args.intent = data;
903 args.container = info.container;
904 args.screenId = info.screenId;
905 args.cellX = info.cellX;
906 args.cellY = info.cellY;
907 args.appWidgetId = appWidgetId;
908 return args;
909 }
910
911 /**
912 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
913 *
914 * @param screenId the screen id to check
915 * @return the new screen, or screenId if it exists
916 */
917 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800918 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700919 if (dropLayout == null) {
920 // it's possible that the add screen was removed because it was
921 // empty and a re-bind occurred
922 mWorkspace.addExtraEmptyScreen();
923 return mWorkspace.commitExtraEmptyScreen();
924 } else {
925 return screenId;
926 }
927 }
928
Adam Cohen091440a2015-03-18 14:16:05 -0700929 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800930 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800931 Runnable onCompleteRunnable = null;
932 int animationType = 0;
933
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700934 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800935 if (resultCode == RESULT_OK) {
936 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
937 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700938 mPendingAddWidgetInfo);
939 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800940 onCompleteRunnable = new Runnable() {
941 @Override
942 public void run() {
943 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700944 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700945 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
946 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800947 }
948 };
949 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800950 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800951 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800952 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700953 if (mDragLayer.getAnimatedView() != null) {
954 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
955 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
956 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700957 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700958 // The animated view may be null in the case of a rotation during widget configuration
959 onCompleteRunnable.run();
960 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 }
962
963 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700964 protected void onStop() {
965 super.onStop();
966 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700967
968 if (mLauncherCallbacks != null) {
969 mLauncherCallbacks.onStop();
970 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700971 }
972
973 @Override
974 protected void onStart() {
975 super.onStart();
976 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700977
978 if (mLauncherCallbacks != null) {
979 mLauncherCallbacks.onStart();
980 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700981 }
982
983 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200985 long startTime = 0;
986 if (DEBUG_RESUME_TIME) {
987 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400988 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200989 }
Adam Cohen9211d422014-10-07 18:14:53 -0700990
991 if (mLauncherCallbacks != null) {
992 mLauncherCallbacks.preOnResume();
993 }
994
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700996 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700997
Winson Chung4a2afa32012-07-19 14:53:05 -0700998 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700999 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001000 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001001 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -07001002 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -07001003 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -07001004 // view after launching an app, as they may be depending on the UI to be static to
1005 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -07001006 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07001007 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08001008 } else if (mOnResumeState == State.WIDGETS) {
1009 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001010 }
1011 mOnResumeState = State.NONE;
1012
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001013 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001014 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1015 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001016
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001017 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001018 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001019 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001020 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001021 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001022 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001024 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001025 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1026 // execute them here
1027 long startTimeCallbacks = 0;
1028 if (DEBUG_RESUME_TIME) {
1029 startTimeCallbacks = System.currentTimeMillis();
1030 }
1031
Michael Jurka1e2f4652013-07-08 18:03:46 -07001032 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1033 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001034 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001035 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001036 if (DEBUG_RESUME_TIME) {
1037 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1038 (System.currentTimeMillis() - startTimeCallbacks));
1039 }
Michael Jurka447bf842013-05-15 14:52:15 +02001040 }
Michael Jurka54554252013-08-01 12:52:23 +02001041 if (mOnResumeCallbacks.size() > 0) {
1042 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1043 mOnResumeCallbacks.get(i).run();
1044 }
1045 mOnResumeCallbacks.clear();
1046 }
Winson Chunge4e50662012-01-23 14:45:13 -08001047
1048 // Reset the pressed state of icons that were locked in the press state while activities
1049 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001050 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001051 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001052 mWaitingForResume.setStayPressed(false);
1053 }
Winson Chung82963d52013-10-09 11:20:57 -07001054
Adam Cohen06dff352012-06-01 17:17:08 -07001055 // It is possible that widgets can receive updates while launcher is not in the foreground.
1056 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1057 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1058 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001059 if (!isWorkspaceLoading()) {
1060 getWorkspace().reinflateWidgetsIfNecessary();
1061 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001062
Michael Jurka447bf842013-05-15 14:52:15 +02001063 if (DEBUG_RESUME_TIME) {
1064 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1065 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001066
Adam Cohen4b66bf32015-09-18 12:15:19 -07001067 // We want to suppress callbacks about CustomContent being shown if we have just received
1068 // onNewIntent while the user was present within launcher. In that case, we post a call
1069 // to move the user to the main screen (which will occur after onResume). We don't want to
1070 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1071 // suppress here.
1072 if (mWorkspace.getCustomContentCallbacks() != null
1073 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001074 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001075 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001076 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1077 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001078 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001079 }
1080 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001081 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001082 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001083 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001084
Sunny Goyal756adbc2015-04-16 15:20:51 -07001085 if (!isWorkspaceLoading()) {
1086 // Process any items that were added while Launcher was away.
1087 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1088 }
Adam Cohen9211d422014-10-07 18:14:53 -07001089
1090 if (mLauncherCallbacks != null) {
1091 mLauncherCallbacks.onResume();
1092 }
Adam Cohen06dff352012-06-01 17:17:08 -07001093 }
1094
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001096 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001097 // Ensure that items added to Launcher are queued until Launcher returns
1098 InstallShortcutReceiver.enableInstallQueue();
1099
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001100 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001101 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001102 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001103 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001104
1105 // We call onHide() aggressively. The custom content callbacks should be able to
1106 // debounce excess onHide calls.
1107 if (mWorkspace.getCustomContentCallbacks() != null) {
1108 mWorkspace.getCustomContentCallbacks().onHide();
1109 }
Romain Guycbb89e42009-06-08 15:52:54 -07001110
Adam Cohen9211d422014-10-07 18:14:53 -07001111 if (mLauncherCallbacks != null) {
1112 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001113 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001114 }
1115
1116 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001117 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1118 // by a onResume or by scrolling otherwise.
1119 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001120
1121 // Custom content is completely hidden
1122 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001123
1124 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1125 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001126
1127 // Indicates whether the user is allowed to scroll away from the custom content.
1128 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001129 }
1130
Adam Cohenc2d6e892014-10-16 09:49:24 -07001131 public interface LauncherOverlay {
1132
1133 /**
1134 * Touch interaction leading to overscroll has begun
1135 */
1136 public void onScrollInteractionBegin();
1137
1138 /**
1139 * Touch interaction related to overscroll has ended
1140 */
1141 public void onScrollInteractionEnd();
1142
1143 /**
1144 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1145 * screen (or in the case of RTL, the rightmost screen).
1146 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001147 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001148
1149 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001150 * Called when the launcher is ready to use the overlay
1151 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001152 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001153 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001154 }
1155
Jun Mukai8af0cd82015-05-12 12:32:12 -07001156 public interface LauncherSearchCallbacks {
1157 /**
1158 * Called when the search overlay is shown.
1159 */
1160 public void onSearchOverlayOpened();
1161
1162 /**
1163 * Called when the search overlay is dismissed.
1164 */
1165 public void onSearchOverlayClosed();
1166 }
1167
Adam Cohenc2d6e892014-10-16 09:49:24 -07001168 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001169 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001170 }
1171
1172 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1173
Sunny Goyalc86df472016-02-25 09:19:38 -08001174 public void onScrollChanged(float progress) {
1175 if (mWorkspace != null) {
1176 mWorkspace.onOverlayScrollChanged(progress);
1177 }
1178 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001179 }
1180
Jorim Jaggid017f882014-01-14 17:08:48 -08001181 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001182 if (mLauncherCallbacks != null) {
1183 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001184 } else {
1185 // On devices with a locked orientation, we will at least have the allow rotation
1186 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001187 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001188 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001189 }
1190
Adam Cohen9211d422014-10-07 18:14:53 -07001191 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001192 CustomContentCallbacks callbacks, String description) {
1193 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001194 }
1195
Adam Cohenedb40762013-07-18 16:45:45 -07001196 // The custom content needs to offset its content to account for the QSB
1197 public int getTopOffsetForCustomContent() {
1198 return mWorkspace.getPaddingTop();
1199 }
1200
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001201 @Override
1202 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001203 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001204 if (mModel.isCurrentCallbacks(this)) {
1205 mModel.stopLoader();
1206 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001207 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1208
Romain Guy13c2e7b2010-03-10 19:45:00 -08001209 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001210 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001211
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001212 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001213 @Override
1214 public void onWindowFocusChanged(boolean hasFocus) {
1215 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001216 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001217
1218 if (mLauncherCallbacks != null) {
1219 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1220 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001221 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001222
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001223 private boolean acceptFilter() {
1224 final InputMethodManager inputManager = (InputMethodManager)
1225 getSystemService(Context.INPUT_METHOD_SERVICE);
1226 return !inputManager.isFullscreenMode();
1227 }
1228
1229 @Override
1230 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001231 final int uniChar = event.getUnicodeChar();
1232 final boolean handled = super.onKeyDown(keyCode, event);
1233 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1234 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1236 keyCode, event);
1237 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001238 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001239 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001240 // showSearchDialog()
1241 // If there are multiple keystrokes before the search dialog takes focus,
1242 // onSearchRequested() will be called for every keystroke,
1243 // but it is idempotent, so it's fine.
1244 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245 }
1246 }
1247
Joe Onorato8a9625e2010-01-28 15:55:35 -08001248 // Eat the long press event so the keyboard doesn't come up.
1249 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1250 return true;
1251 }
1252
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 return handled;
1254 }
1255
Winson46163472015-09-22 17:31:56 -07001256 @Override
1257 public boolean onKeyUp(int keyCode, KeyEvent event) {
1258 if (keyCode == KeyEvent.KEYCODE_MENU) {
1259 // Ignore the menu key if we are currently dragging or are on the custom content screen
1260 if (!isOnCustomContent() && !mDragController.isDragging()) {
1261 // Close any open folders
1262 closeFolder();
1263
1264 // Stop resizing any widgets
1265 mWorkspace.exitWidgetResizeMode();
1266
1267 // Show the overview mode if we are on the workspace
1268 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1269 !mWorkspace.isSwitchingState()) {
1270 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001271 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001272 }
1273 }
1274 return true;
1275 }
1276 return super.onKeyUp(keyCode, event);
1277 }
1278
Karl Rosaen138a0412009-04-23 19:00:21 -07001279 private String getTypedText() {
1280 return mDefaultKeySsb.toString();
1281 }
1282
1283 private void clearTypedText() {
1284 mDefaultKeySsb.clear();
1285 mDefaultKeySsb.clearSpans();
1286 Selection.setSelection(mDefaultKeySsb, 0);
1287 }
1288
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001290 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1291 * State
1292 */
1293 private static State intToState(int stateOrdinal) {
1294 State state = State.WORKSPACE;
1295 final State[] stateValues = State.values();
1296 for (int i = 0; i < stateValues.length; i++) {
1297 if (stateValues[i].ordinal() == stateOrdinal) {
1298 state = stateValues[i];
1299 break;
1300 }
1301 }
1302 return state;
1303 }
1304
1305 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 * Restores the previous state, if it exists.
1307 *
1308 * @param savedState The previous state.
1309 */
1310 private void restoreState(Bundle savedState) {
1311 if (savedState == null) {
1312 return;
1313 }
1314
Michael Jurka883f55b2010-10-21 15:47:14 -07001315 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001316 if (state == State.APPS || state == State.WIDGETS) {
1317 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001318 }
1319
Adam Cohen21cd0022013-10-09 18:57:02 -07001320 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1321 PagedView.INVALID_RESTORE_PAGE);
1322 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001323 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 }
1325
Sunny Goyal756cd262015-08-20 12:33:21 -07001326 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1327 if (itemValues != null) {
1328 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001329 AppWidgetProviderInfo info = savedState.getParcelable(
1330 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001331 mPendingAddWidgetInfo = info == null ?
1332 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1333
Adam Cohen4637b5a2013-11-04 18:21:24 -08001334 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001335 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 mRestoring = true;
1337 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 }
1339
1340 /**
1341 * Finds all the views we need and configure them properly.
1342 */
1343 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001344 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001345 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001346 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
1347
Winson Chung4c98d922011-05-31 16:50:48 -07001348 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Hyunyoung Song7d2fc812016-06-15 12:51:30 -07001349 mPageIndicator = (PageIndicator) mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001350
Sunny Goyal784f9c32016-03-23 16:56:54 -07001351 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1352 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1353 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001354 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355
Winson Chung4c98d922011-05-31 16:50:48 -07001356 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001357
1358 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001359
Winson Chung3d503fb2011-07-13 17:25:49 -07001360 // Setup the hotseat
1361 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1362 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001363 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001364 }
1365
Winsone9f27272015-10-13 10:47:51 -07001366 // Setup the overview panel
1367 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001368
Winson Chung4c98d922011-05-31 16:50:48 -07001369 // Setup the workspace
1370 mWorkspace.setHapticFeedbackEnabled(false);
1371 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001372 mWorkspace.setup(mDragController);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001373 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001374 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001375
Tony Wickham34d2c912015-09-11 09:27:58 -07001376 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001377 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001378
Winson Chungef7f8742015-06-04 17:18:17 -07001379 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001380 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001381 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001382 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1383 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1384 } else {
1385 mAppsView.setSearchBarController(new DefaultAppSearchController());
1386 }
Craig Mautner360310b2012-10-26 15:13:08 -07001387
Winson Chung3d503fb2011-07-13 17:25:49 -07001388 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001389 mDragController.setDragScoller(mWorkspace);
1390 mDragController.setScrollView(mDragLayer);
1391 mDragController.setMoveTarget(mWorkspace);
1392 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001393 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001394
Sunny Goyal322d5562015-06-25 19:35:49 -07001395 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1396 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001397 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 }
1399
Winsone9f27272015-10-13 10:47:51 -07001400 private void setupOverviewPanel() {
1401 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1402
1403 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1404 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1405 @Override
1406 public boolean onLongClick(View v) {
1407 return v.performClick();
1408 }
1409 };
1410
1411 // Bind wallpaper button actions
1412 View wallpaperButton = findViewById(R.id.wallpaper_button);
1413 wallpaperButton.setOnClickListener(new OnClickListener() {
1414 @Override
1415 public void onClick(View view) {
1416 if (!mWorkspace.isSwitchingState()) {
1417 onClickWallpaperPicker(view);
1418 }
1419 }
1420 });
1421 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1422 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1423
1424 // Bind widget button actions
1425 mWidgetsButton = findViewById(R.id.widget_button);
1426 mWidgetsButton.setOnClickListener(new OnClickListener() {
1427 @Override
1428 public void onClick(View view) {
1429 if (!mWorkspace.isSwitchingState()) {
1430 onClickAddWidgetButton(view);
1431 }
1432 }
1433 });
1434 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1435 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1436
1437 // Bind settings actions
1438 View settingsButton = findViewById(R.id.settings_button);
1439 boolean hasSettings = hasSettings();
1440 if (hasSettings) {
1441 settingsButton.setOnClickListener(new OnClickListener() {
1442 @Override
1443 public void onClick(View view) {
1444 if (!mWorkspace.isSwitchingState()) {
1445 onClickSettingsButton(view);
1446 }
1447 }
1448 });
1449 settingsButton.setOnLongClickListener(performClickOnLongClick);
1450 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1451 } else {
1452 settingsButton.setVisibility(View.GONE);
1453 }
1454
1455 mOverviewPanel.setAlpha(0f);
1456 }
1457
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001459 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001460 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001461 */
1462 public void setAllAppsButton(View allAppsButton) {
1463 mAllAppsButton = allAppsButton;
1464 }
1465
Sunny Goyalbb011da2016-06-15 15:42:29 -07001466 public View getStartViewForAllAppsRevealAnimation() {
1467 return FeatureFlags.NO_ALL_APPS_ICON ? mPageIndicator : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001468 }
1469
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001470 public View getWidgetsButton() {
1471 return mWidgetsButton;
1472 }
1473
Anjali Koppal5ad44842014-03-10 20:34:39 -07001474 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 * Creates a view representing a shortcut.
1476 *
1477 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001479 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001480 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 }
1482
1483 /**
1484 * Creates a view representing a shortcut inflated from the specified resource.
1485 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 * @param parent The group the shortcut belongs to.
1487 * @param info The data structure describing the shortcut.
1488 *
1489 * @return A View inflated from layoutResId.
1490 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001491 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001492 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001493 parent, false);
1494 favorite.applyFromShortcutInfo(info, mIconCache);
1495 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001497 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 return favorite;
1499 }
1500
1501 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 * Add a shortcut to the workspace.
1503 *
1504 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001506 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001507 int cellY) {
1508 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001509 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001510
Sunny Goyal5c97f512015-05-19 16:03:28 -07001511 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001512 if (info == null) {
1513 return;
1514 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001515 final View view = createShortcut(info);
1516
Sunny Goyal5c97f512015-05-19 16:03:28 -07001517 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001518 // First we check if we already know the exact location where we want to add this item.
1519 if (cellX >= 0 && cellY >= 0) {
1520 cellXY[0] = cellX;
1521 cellXY[1] = cellY;
1522 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001523
1524 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001525 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001526 true, null,null)) {
1527 return;
1528 }
1529 DragObject dragObject = new DragObject();
1530 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001531 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1532 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001533 return;
1534 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001535 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001536 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001537 }
1538
1539 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001540 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001541 return;
1542 }
1543
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001544 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545
1546 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001547 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001548 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 }
1550 }
1551
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001553 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001555 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556 */
Adam Cohen091440a2015-03-18 14:16:05 -07001557 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001558 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1559
1560 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001561 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001562 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001563 }
Romain Guycbb89e42009-06-08 15:52:54 -07001564
Adam Cohen59400422014-03-05 18:07:04 -08001565 if (appWidgetInfo.isCustomWidget) {
1566 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001567 }
1568
Adam Cohen59400422014-03-05 18:07:04 -08001569 LauncherAppWidgetInfo launcherInfo;
1570 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1571 launcherInfo.spanX = info.spanX;
1572 launcherInfo.spanY = info.spanY;
1573 launcherInfo.minSpanX = info.minSpanX;
1574 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001575 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001576
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001578 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579
1580 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001581 if (hostView == null) {
1582 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001583 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001584 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001585 hostView.setVisibility(View.VISIBLE);
1586 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001588 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 }
Romain Guycbb89e42009-06-08 15:52:54 -07001590
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001591 private void addAppWidgetToWorkspace(
1592 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001593 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001594 hostView.setTag(item);
1595 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001596
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001597 hostView.setFocusable(true);
1598 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001599
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001600 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001601 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1602
1603 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001604 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001605 }
1606 }
1607
Adam Cohended9f8d2010-11-03 13:25:16 -07001608 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1609 @Override
1610 public void onReceive(Context context, Intent intent) {
1611 final String action = intent.getAction();
1612 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1613 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001614 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001615 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001616
Winson Chungc100e8e2011-08-09 16:02:43 -07001617 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001618 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001619 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001620 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001621 if (!showWorkspace(false)) {
1622 // If we are already on the workspace, then manually reset all apps
1623 mAppsView.reset();
1624 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001625 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001626 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1627 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001628 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001629 }
1630 }
1631 };
1632
1633 @Override
1634 public void onAttachedToWindow() {
1635 super.onAttachedToWindow();
1636
1637 // Listen for broadcasts related to user-presence
1638 final IntentFilter filter = new IntentFilter();
1639 filter.addAction(Intent.ACTION_SCREEN_OFF);
1640 filter.addAction(Intent.ACTION_USER_PRESENT);
1641 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001642 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001643 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001644 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001645
1646 if (mLauncherCallbacks != null) {
1647 mLauncherCallbacks.onAttachedToWindow();
1648 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001649 }
1650
1651 @Override
1652 public void onDetachedFromWindow() {
1653 super.onDetachedFromWindow();
1654 mVisible = false;
1655
Adam Cohend113e0c2010-11-11 10:48:05 -08001656 if (mAttached) {
1657 unregisterReceiver(mReceiver);
1658 mAttached = false;
1659 }
Winson Chungb745afb2015-03-02 11:51:23 -08001660 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001661
1662 if (mLauncherCallbacks != null) {
1663 mLauncherCallbacks.onDetachedFromWindow();
1664 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001665 }
1666
1667 public void onWindowVisibilityChanged(int visibility) {
1668 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001669 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001670 // The following code used to be in onResume, but it turns out onResume is called when
1671 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1672 // is a more appropriate event to handle
1673 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001674 if (!mWorkspaceLoading) {
1675 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001676 // We want to let Launcher draw itself at least once before we force it to build
1677 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001678 // apps is nice and speedy.
1679 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001680 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001681 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001682 if (mStarted) return;
1683 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001684 // We delay the layer building a bit in order to give
1685 // other message processing a time to run. In particular
1686 // this avoids a delay in hiding the IME if it was
1687 // currently shown, because doing that may involve
1688 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001689 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001690 final ViewTreeObserver.OnDrawListener listener = this;
1691 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001692 public void run() {
1693 if (mWorkspace != null &&
1694 mWorkspace.getViewTreeObserver() != null) {
1695 mWorkspace.getViewTreeObserver().
1696 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001697 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001698 }
1699 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001700 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001701 }
1702 });
1703 }
1704 clearTypedText();
1705 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001706 }
1707
Adam Cohen091440a2015-03-18 14:16:05 -07001708 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001709 mHandler.removeMessages(ADVANCE_MSG);
1710 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1711 mHandler.sendMessageDelayed(msg, delay);
1712 mAutoAdvanceSentTime = System.currentTimeMillis();
1713 }
1714
Adam Cohen091440a2015-03-18 14:16:05 -07001715 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001716 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1717 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1718 mAutoAdvanceRunning = autoAdvanceRunning;
1719 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001720 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001721 sendAdvanceMessage(delay);
1722 } else {
1723 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001724 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001725 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1726 }
1727 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001728 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001729 }
1730 }
1731 }
1732
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001733 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1734
Adam Cohended9f8d2010-11-03 13:25:16 -07001735 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001736 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001737 if (msg.what == ADVANCE_MSG) {
1738 int i = 0;
1739 for (View key: mWidgetsToAdvance.keySet()) {
1740 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001741 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001742 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001743 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001744 public void run() {
1745 ((Advanceable) v).advance();
1746 }
1747 }, delay);
1748 }
1749 i++;
1750 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001751 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001752 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001753 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001754 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001755 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001756
Winsonc0b52fe2015-09-09 16:38:15 -07001757 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001758 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001759 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1760 if (v instanceof Advanceable) {
1761 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001762 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001763 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001764 }
1765 }
1766
Winsonc0b52fe2015-09-09 16:38:15 -07001767 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001768 if (mWidgetsToAdvance.containsKey(hostView)) {
1769 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001770 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001771 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001772 }
1773
Hyunyoung Song3f471442015-04-08 19:01:34 -07001774 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001775 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1776 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001777 }
1778
Winson Chunga6945242014-01-08 14:04:34 -08001779 public DragLayer getDragLayer() {
1780 return mDragLayer;
1781 }
1782
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001783 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001784 return mAppsView;
1785 }
1786
Hyunyoung Song3f471442015-04-08 19:01:34 -07001787 public WidgetsContainerView getWidgetsView() {
1788 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001789 }
1790
Winson Chunga6945242014-01-08 14:04:34 -08001791 public Workspace getWorkspace() {
1792 return mWorkspace;
1793 }
1794
1795 public Hotseat getHotseat() {
1796 return mHotseat;
1797 }
1798
Jorim Jaggid017f882014-01-14 17:08:48 -08001799 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001800 return mOverviewPanel;
1801 }
1802
Sunny Goyal47328fd2016-05-25 18:56:41 -07001803 public DropTargetBar getDropTargetBar() {
1804 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001805 }
1806
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001807 public LauncherAppWidgetHost getAppWidgetHost() {
1808 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 }
Romain Guycbb89e42009-06-08 15:52:54 -07001810
Winson Chunga9abd0e2010-10-27 17:18:37 -07001811 public LauncherModel getModel() {
1812 return mModel;
1813 }
1814
Adam Cohen79d90c52016-04-22 13:29:20 -07001815 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001816 return mSharedPrefs;
1817 }
1818
Adam Cohen2e6da152015-05-06 11:42:25 -07001819 public DeviceProfile getDeviceProfile() {
1820 return mDeviceProfile;
1821 }
1822
Bjorn Bringertc459e522013-06-07 19:36:01 +01001823 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001824 getWindow().closeAllPanels();
1825
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001826 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001827 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001828 }
1829
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001830 @Override
1831 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001832 long startTime = 0;
1833 if (DEBUG_RESUME_TIME) {
1834 startTime = System.currentTimeMillis();
1835 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 super.onNewIntent(intent);
1837
1838 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001839 Folder openFolder = mWorkspace.getOpenFolder();
1840 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1841 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1842 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1843 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1844 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001845 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001846 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001847
Adam Cohen6fecd412013-10-02 17:41:50 -07001848 if (mWorkspace == null) {
1849 // Can be cases where mWorkspace is null, this prevents a NPE
1850 return;
1851 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001852 // In all these cases, only animate if we're already on home
1853 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001854
Sunny Goyal935fca12015-10-13 10:19:01 -07001855 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001856 exitSpringLoadedDragMode();
1857
1858 // If we are already on home, then just animate back to the workspace,
1859 // otherwise, just wait until onResume to set the state back to Workspace
1860 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001861 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001862 } else {
1863 mOnResumeState = State.WORKSPACE;
1864 }
1865
1866 final View v = getWindow().peekDecorView();
1867 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001868 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001869 INPUT_METHOD_SERVICE);
1870 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1871 }
1872
Winson Chungb745afb2015-03-02 11:51:23 -08001873 // Reset the apps view
1874 if (!alreadyOnHome && mAppsView != null) {
1875 mAppsView.scrollToTop();
1876 }
1877
Hyunyoung Song3f471442015-04-08 19:01:34 -07001878 // Reset the widgets view
1879 if (!alreadyOnHome && mWidgetsView != null) {
1880 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001881 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001882
Adam Cohen9211d422014-10-07 18:14:53 -07001883 if (mLauncherCallbacks != null) {
1884 mLauncherCallbacks.onHomeIntent();
1885 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886 }
Adam Cohened307df2013-10-02 09:37:31 -07001887
Adam Cohen9211d422014-10-07 18:14:53 -07001888 if (mLauncherCallbacks != null) {
1889 mLauncherCallbacks.onNewIntent(intent);
1890 }
Winson15f8b172015-08-19 11:02:39 -07001891
1892 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1893 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1894 // animation.
1895 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001896 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1897 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001898 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1899 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001900
1901 // We use this flag to suppress noisy callbacks above custom content state
1902 // from onResume.
1903 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001904 mWorkspace.post(new Runnable() {
1905 @Override
1906 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001907 if (mWorkspace != null) {
1908 mWorkspace.moveToDefaultScreen(true);
1909 }
Winson15f8b172015-08-19 11:02:39 -07001910 }
1911 });
1912 }
1913 }
1914
1915 if (DEBUG_RESUME_TIME) {
1916 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1917 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001918 }
1919
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001921 public void onRestoreInstanceState(Bundle state) {
1922 super.onRestoreInstanceState(state);
1923 for (int page: mSynchronouslyBoundPages) {
1924 mWorkspace.restoreInstanceStateForChild(page);
1925 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926 }
1927
1928 @Override
1929 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001930 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001931 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1932 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001933
Adam Cohen21cd0022013-10-09 18:57:02 -07001934 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001935 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936
Michael Jurka883f55b2010-10-21 15:47:14 -07001937 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001938 // We close any open folder since it will not be re-opened, and we need to make sure
1939 // this state is reflected.
1940 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1941 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942
Adam Cohendcd297f2013-06-18 13:13:40 -07001943 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001944 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001945 ContentValues itemValues = new ContentValues();
1946 mPendingAddInfo.writeToValues(itemValues);
1947 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001948 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001949 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 }
1951
Adam Cohen9211d422014-10-07 18:14:53 -07001952 if (mLauncherCallbacks != null) {
1953 mLauncherCallbacks.onSaveInstanceState(outState);
1954 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955 }
1956
1957 @Override
1958 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001960
Winson Chunge7a03942011-08-05 15:05:12 -07001961 // Remove all pending runnables
1962 mHandler.removeMessages(ADVANCE_MSG);
1963 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001964 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001965 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001966
Winson Chungcd2b0142011-06-08 16:02:26 -07001967 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001968 // It's possible to receive onDestroy after a new Launcher activity has
1969 // been created. In this case, don't interfere with the new Launcher.
1970 if (mModel.isCurrentCallbacks(this)) {
1971 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001972 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001973 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001974
Sunny Goyal745bad92016-05-02 10:54:12 -07001975 if (mRotationPrefChangeHandler != null) {
1976 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1977 }
1978
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001980 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001982 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001984 mAppWidgetHost = null;
1985
1986 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987
1988 TextKeyListener.getInstance().release();
1989
Tony Wickhame2217252016-03-22 16:34:23 -07001990 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1991 .removeAccessibilityStateChangeListener(this);
1992
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001993 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001994
Michael Jurka2ecf9952012-06-18 12:52:28 -07001995 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001996
1997 if (mLauncherCallbacks != null) {
1998 mLauncherCallbacks.onDestroy();
1999 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 }
2001
Adam Cohena9cf38f2011-05-02 15:36:58 -07002002 public DragController getDragController() {
2003 return mDragController;
2004 }
2005
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002006 @Override
2007 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002008 onStartForResult(requestCode);
2009 super.startActivityForResult(intent, requestCode);
2010 }
2011
2012 @Override
2013 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2014 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2015 onStartForResult(requestCode);
2016 try {
2017 super.startIntentSenderForResult(intent, requestCode,
2018 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2019 } catch (IntentSender.SendIntentException e) {
2020 throw new ActivityNotFoundException();
2021 }
2022 }
2023
2024 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002025 if (requestCode >= 0) {
2026 setWaitingForResult(true);
2027 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002028 }
2029
Winson Chung70d72102011-08-12 11:24:35 -07002030 /**
2031 * Indicates that we want global search for this activity by setting the globalSearch
2032 * argument for {@link #startSearch} to true.
2033 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002035 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002037
Karl Rosaen138a0412009-04-23 19:00:21 -07002038 if (initialQuery == null) {
2039 // Use any text typed in the launcher as the initial query
2040 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002041 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 if (appSearchData == null) {
2043 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002044 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002046
2047 // TODO send proper bounds.
2048 Rect sourceBounds = null;
Romain Guycbb89e42009-06-08 15:52:54 -07002049
Ian Parkinson047036e2014-09-01 15:40:53 +01002050 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002051 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002052 if (clearTextImmediately) {
2053 clearTypedText();
2054 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002055
2056 // We need to show the workspace after starting the search
2057 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002058 }
2059
Ian Parkinson047036e2014-09-01 15:40:53 +01002060 /**
2061 * Start a text search.
2062 *
2063 * @return {@code true} if the search will start immediately, so any further keypresses
2064 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2065 * to buffer keypresses.
2066 */
2067 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002068 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002069 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2070 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2071 sourceBounds);
2072 }
2073
Michael Jurkaa33411c2012-06-14 16:18:21 -07002074 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002075 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002076 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002077 }
2078
2079 /**
2080 * Starts the global search activity. This code is a copied from SearchManager
2081 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002082 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002083 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002084 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002085 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2086 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2087 if (globalSearchActivity == null) {
2088 Log.w(TAG, "No global search activity found.");
2089 return;
2090 }
2091 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2092 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2093 intent.setComponent(globalSearchActivity);
2094 // Make sure that we have a Bundle to put source in
2095 if (appSearchData == null) {
2096 appSearchData = new Bundle();
2097 } else {
2098 appSearchData = new Bundle(appSearchData);
2099 }
Adam Cohen9211d422014-10-07 18:14:53 -07002100 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002101 if (!appSearchData.containsKey("source")) {
2102 appSearchData.putString("source", getPackageName());
2103 }
2104 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2105 if (!TextUtils.isEmpty(initialQuery)) {
2106 intent.putExtra(SearchManager.QUERY, initialQuery);
2107 }
2108 if (selectInitialQuery) {
2109 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2110 }
2111 intent.setSourceBounds(sourceBounds);
2112 try {
2113 startActivity(intent);
2114 } catch (ActivityNotFoundException ex) {
2115 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2116 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002117 }
2118
Yura4f93ec62014-02-04 14:15:21 +00002119 public boolean isOnCustomContent() {
2120 return mWorkspace.isOnOrMovingToCustomContent();
2121 }
2122
Winson Chung70d72102011-08-12 11:24:35 -07002123 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002124 public boolean onPrepareOptionsMenu(Menu menu) {
2125 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002126 if (mLauncherCallbacks != null) {
2127 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2128 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002129 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002130 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002131
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002132 @Override
2133 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002134 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002135 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002136 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002137 }
2138
Joe Onorato9c1289c2009-08-17 11:03:03 -04002139 public boolean isWorkspaceLocked() {
2140 return mWorkspaceLoading || mWaitingForResult;
2141 }
2142
Adam Cohen517a7f52014-03-01 12:12:59 -08002143 public boolean isWorkspaceLoading() {
2144 return mWorkspaceLoading;
2145 }
2146
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002147 private void setWorkspaceLoading(boolean value) {
2148 boolean isLocked = isWorkspaceLocked();
2149 mWorkspaceLoading = value;
2150 if (isLocked != isWorkspaceLocked()) {
2151 onWorkspaceLockedChanged();
2152 }
2153 }
2154
2155 private void setWaitingForResult(boolean value) {
2156 boolean isLocked = isWorkspaceLocked();
2157 mWaitingForResult = value;
2158 if (isLocked != isWorkspaceLocked()) {
2159 onWorkspaceLockedChanged();
2160 }
2161 }
2162
Adam Cohen9211d422014-10-07 18:14:53 -07002163 protected void onWorkspaceLockedChanged() {
2164 if (mLauncherCallbacks != null) {
2165 mLauncherCallbacks.onWorkspaceLockedChanged();
2166 }
2167 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002168
Michael Jurka0280c3b2010-09-17 15:00:07 -07002169 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002170 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002171 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002172 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2173 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002174 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002175 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002176
Tony Wickhama0628cc2015-10-14 15:23:04 -07002177 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002178 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002179 if (LOGD) {
2180 Log.d(TAG, "Adding widget from drop");
2181 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002182 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2183 }
2184
Sunny Goyale6b63a32015-01-16 16:14:29 -08002185 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002186 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2187 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002188 if (appWidgetInfo.configure != null) {
2189 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002190 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002191
Bjorn Bringert7984c942009-12-09 15:38:25 +00002192 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002193 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2194 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2195
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002196 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002197 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002198 Runnable onComplete = new Runnable() {
2199 @Override
2200 public void run() {
2201 // Exit spring loaded mode if necessary after adding the widget
2202 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2203 null);
2204 }
2205 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002206 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002207 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002208 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002209 }
2210 }
Romain Guycbb89e42009-06-08 15:52:54 -07002211
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002212 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002213 // Close any folders that may be open.
2214 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002215 mWorkspace.moveToCustomContentScreen(animate);
2216 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002217
2218 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2219 int[] cell, int spanX, int spanY) {
2220 switch (info.itemType) {
2221 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2222 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2223 int span[] = new int[2];
2224 span[0] = spanX;
2225 span[1] = spanY;
2226 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2227 container, screenId, cell, span);
2228 break;
2229 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2230 processShortcutFromDrop(info.componentName, container, screenId, cell);
2231 break;
2232 default:
2233 throw new IllegalStateException("Unknown item type: " + info.itemType);
2234 }
2235 }
2236
Adam Cohenfbba09b2011-07-18 21:43:05 -07002237 /**
2238 * Process a shortcut drop.
2239 *
2240 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002241 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002242 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002243 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002244 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2245 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002246 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002247 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002248 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002249
2250 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002251 mPendingAddInfo.cellX = cell[0];
2252 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002253 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002254
2255 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2256 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002257 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002258 }
2259
Adam Cohenfbba09b2011-07-18 21:43:05 -07002260 /**
2261 * Process a widget drop.
2262 *
2263 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002264 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002265 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002266 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002267 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2268 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002269 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002270 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002271 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002272 mPendingAddInfo.minSpanX = info.minSpanX;
2273 mPendingAddInfo.minSpanY = info.minSpanY;
2274
Adam Cohenfbba09b2011-07-18 21:43:05 -07002275 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002276 mPendingAddInfo.cellX = cell[0];
2277 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002278 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002279 if (span != null) {
2280 mPendingAddInfo.spanX = span[0];
2281 mPendingAddInfo.spanY = span[1];
2282 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002283
Adam Cohened66b2b2012-01-23 17:28:51 -08002284 AppWidgetHostView hostView = info.boundWidget;
2285 int appWidgetId;
2286 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002287 // In the case where we've prebound the widget, we remove it from the DragLayer
2288 if (LOGD) {
2289 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2290 }
2291 getDragLayer().removeView(hostView);
2292
Adam Cohened66b2b2012-01-23 17:28:51 -08002293 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002294 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002295
2296 // Clear the boundWidget so that it doesn't get destroyed.
2297 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002298 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002299 // In this case, we either need to start an activity to get permission to bind
2300 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002301 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002302 Bundle options = info.bindOptions;
2303
Sunny Goyalffe83f12014-08-14 17:39:34 -07002304 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2305 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002306 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002307 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002308 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002309 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002310 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2311 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2312 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002313 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2314 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002315 // TODO: we need to make sure that this accounts for the options bundle.
2316 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002317 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2318 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002319 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002320 }
2321
Adam Cohendcd297f2013-06-18 13:13:40 -07002322 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002323 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002324 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002325 folderInfo.title = getText(R.string.folder_name);
2326
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002327 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002328 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2329 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002330
2331 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002332 FolderIcon newFolder =
2333 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002334 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002335 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002336 // Force measure the new folder icon
2337 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2338 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002339 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002340 }
Romain Guycbb89e42009-06-08 15:52:54 -07002341
Winsonc0b52fe2015-09-09 16:38:15 -07002342 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002343 * Unbinds the view for the specified item, and removes the item and all its children.
2344 *
2345 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002346 * @param itemInfo the {@link ItemInfo} for this view.
2347 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002348 */
Winson2949fb52015-09-24 09:56:11 -07002349 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002350 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002351 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002352 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2353 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002354 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002355 } else {
2356 mWorkspace.removeWorkspaceItem(v);
2357 }
Winsonc0b52fe2015-09-09 16:38:15 -07002358 if (deleteFromDb) {
2359 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2360 }
2361 } else if (itemInfo instanceof FolderInfo) {
2362 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002363 if (v instanceof FolderIcon) {
2364 ((FolderIcon) v).removeListeners();
2365 }
Winsonc0b52fe2015-09-09 16:38:15 -07002366 mWorkspace.removeWorkspaceItem(v);
2367 if (deleteFromDb) {
2368 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2369 }
2370 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2371 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002372 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002373 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002374 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002375 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002376 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002377
Winsonc0b52fe2015-09-09 16:38:15 -07002378 } else {
2379 return false;
2380 }
2381 return true;
2382 }
2383
2384 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002385 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002386 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002387 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002388 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002389 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002390 // Deleting an app widget ID is a void call but writes to disk before returning
2391 // to the caller...
2392 new AsyncTask<Void, Void, Void>() {
2393 public Void doInBackground(Void ... args) {
2394 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2395 return null;
2396 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002397 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002398 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002399 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002400 }
2401
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002402 @Override
2403 public boolean dispatchKeyEvent(KeyEvent event) {
2404 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2405 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002406 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002407 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002408 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002409 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002410 dumpState();
2411 return true;
2412 }
2413 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002414 }
2415 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2416 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002417 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002418 return true;
2419 }
2420 }
2421
2422 return super.dispatchKeyEvent(event);
2423 }
2424
Joe Onorato88ec0992009-11-19 13:16:06 -08002425 @Override
2426 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002427 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2428 return;
2429 }
2430
Sunny Goyal45478022015-06-08 16:52:41 -07002431 if (mDragController.isDragging()) {
2432 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002433 return;
2434 }
2435
Winson Chungb745afb2015-03-02 11:51:23 -08002436 if (isAppsViewVisible()) {
2437 showWorkspace(true);
2438 } else if (isWidgetsViewVisible()) {
2439 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002440 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002441 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002442 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002443 Folder openFolder = mWorkspace.getOpenFolder();
2444 if (openFolder.isEditingName()) {
2445 openFolder.dismissEditingName();
2446 } else {
2447 closeFolder();
2448 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002449 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002450 mWorkspace.exitWidgetResizeMode();
2451
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002452 // Back button is a no-op here, but give at least some feedback for the button press
2453 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002454 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002455 }
2456
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002457 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002458 * Launches the intent referred by the clicked shortcut.
2459 *
2460 * @param v The view representing the clicked shortcut.
2461 */
2462 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002463 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2464 // view has detached (it's possible for this to happen if the view is removed mid touch).
2465 if (v.getWindowToken() == null) {
2466 return;
2467 }
2468
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002469 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002470 return;
2471 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002472
Adam Cohen1697b792013-09-17 19:08:21 -07002473 if (v instanceof Workspace) {
2474 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002475 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002476 }
2477 return;
2478 }
2479
2480 if (v instanceof CellLayout) {
2481 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002482 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2483 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002484 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002485 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002486 }
2487
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002488 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002489 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002490 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002491 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002492 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002493 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002494 }
Sunny Goyalbb011da2016-06-15 15:42:29 -07002495 } else if (v instanceof PageIndicator || (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002496 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002497 } else if (tag instanceof AppInfo) {
2498 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002499 } else if (tag instanceof LauncherAppWidgetInfo) {
2500 if (v instanceof PendingAppWidgetHostView) {
2501 onClickPendingWidget((PendingAppWidgetHostView) v);
2502 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002503 }
2504 }
2505
Sunny Goyal70660032015-05-14 00:07:08 -07002506 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002507 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002508 return false;
2509 }
2510
Michael Jurkaaf442092010-06-10 17:01:57 -07002511 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002512 * Event handler for the app widget view which has not fully restored.
2513 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002514 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002515 if (mIsSafeModeEnabled) {
2516 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2517 return;
2518 }
2519
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002520 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002521 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002522 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2523 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2524 // This should not happen, as we make sure that an Id is allocated during bind.
2525 return;
2526 }
2527 LauncherAppWidgetProviderInfo appWidgetInfo =
2528 mAppWidgetManager.findProvider(info.providerName, info.user);
2529 if (appWidgetInfo != null) {
2530 mPendingAddWidgetId = info.appWidgetId;
2531 mPendingAddInfo.copyFrom(info);
2532 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002533
Sunny Goyald478c832016-04-01 12:04:16 -07002534 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2535 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingAddWidgetId);
2536 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
2537 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2538 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2539 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2540 }
2541 } else {
2542 LauncherAppWidgetProviderInfo appWidgetInfo =
2543 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2544 if (appWidgetInfo != null) {
2545 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2546 }
Sunny Goyalff572272014-07-23 13:58:07 -07002547 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002548 } else if (info.installProgress < 0) {
2549 // The install has not been queued
2550 final String packageName = info.providerName.getPackageName();
2551 showBrokenAppInstallDialog(packageName,
2552 new DialogInterface.OnClickListener() {
2553 public void onClick(DialogInterface dialog, int id) {
2554 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2555 }
2556 });
2557 } else {
2558 // Download has started.
2559 final String packageName = info.providerName.getPackageName();
2560 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002561 }
2562 }
2563
Sunny Goyald478c832016-04-01 12:04:16 -07002564 private void startRestoredWidgetReconfigActivity(
2565 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
2566 mPendingAddWidgetInfo = provider;
2567 mPendingAddInfo.copyFrom(info);
2568 mPendingAddWidgetId = info.appWidgetId;
2569 mAppWidgetManager.startConfigActivity(provider,
2570 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2571 }
2572
Sunny Goyalff572272014-07-23 13:58:07 -07002573 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002574 * Event handler for the "grid" button that appears on the home screen, which
2575 * enters all apps mode.
2576 *
2577 * @param v The view that was clicked.
2578 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002579 protected void onClickAllAppsButton(View v) {
2580 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002581 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002582 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002583 true /* updatePredictedApps */, false /* focusSearchBar */);
2584 }
2585 }
2586
2587 protected void onLongClickAllAppsButton(View v) {
2588 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2589 if (!isAppsViewVisible()) {
2590 showAppsView(true /* animated */, false /* resetListToTop */,
2591 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002592 }
2593 }
2594
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002595 private void showBrokenAppInstallDialog(final String packageName,
2596 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002597 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002598 .setTitle(R.string.abandoned_promises_title)
2599 .setMessage(R.string.abandoned_promise_explanation)
2600 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2601 .setNeutralButton(R.string.abandoned_clean_this,
2602 new DialogInterface.OnClickListener() {
2603 public void onClick(DialogInterface dialog, int id) {
2604 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2605 mWorkspace.removeAbandonedPromise(packageName, user);
2606 }
2607 })
2608 .create().show();
2609 return;
2610 }
2611
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002612 /**
2613 * Event handler for an app shortcut click.
2614 *
2615 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2616 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002617 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002618 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2619 Object tag = v.getTag();
2620 if (!(tag instanceof ShortcutInfo)) {
2621 throw new IllegalArgumentException("Input must be a Shortcut");
2622 }
2623
2624 // Open shortcut
2625 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002626
2627 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002628 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2629 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002630 // Launch activity anyway, framework will tell the user why the app is suspended.
2631 } else {
2632 int error = R.string.activity_not_available;
2633 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2634 error = R.string.safemode_shortcut_error;
2635 }
2636 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2637 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002638 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002639 }
2640
Chris Wren40c5ed32014-06-24 18:24:23 -04002641 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002642 if ((v instanceof BubbleTextView)
2643 && shortcut.isPromise()
2644 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002645 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002646 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002647 new DialogInterface.OnClickListener() {
2648 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002649 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002650 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002651 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002652 return;
2653 }
2654
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002655 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002656 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002657 }
2658
Sunny Goyala7ce1662016-05-31 15:01:35 -07002659 private void startAppShortcutOrInfoActivity(View v) {
2660 ItemInfo item = (ItemInfo) v.getTag();
2661 Intent intent = item.getIntent();
2662 if (intent == null) {
2663 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002664 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002665 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002666 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002667
2668 if (success && v instanceof BubbleTextView) {
2669 mWaitingForResume = (BubbleTextView) v;
2670 mWaitingForResume.setStayPressed(true);
2671 }
2672 }
2673
2674 /**
2675 * Event handler for a folder icon click.
2676 *
2677 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2678 */
2679 protected void onClickFolderIcon(View v) {
2680 if (LOGD) Log.d(TAG, "onClickFolder");
2681 if (!(v instanceof FolderIcon)){
2682 throw new IllegalArgumentException("Input must be a FolderIcon");
2683 }
2684
2685 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002686 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002687 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002688 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002689 }
Michael Jurka5130e402011-10-13 04:55:35 -07002690 }
2691
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002692 /**
2693 * Event handler for the (Add) Widgets button that appears after a long press
2694 * on the home screen.
2695 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002696 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002697 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002698 if (mIsSafeModeEnabled) {
2699 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2700 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002701 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002702 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002703 }
2704
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002705 /**
2706 * Event handler for the wallpaper picker button that appears after a long press
2707 * on the home screen.
2708 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002709 protected void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002710 if (!Utilities.isWallapaperAllowed(this)) {
2711 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2712 return;
2713 }
2714
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002715 String pickerPackage = getString(R.string.wallpaper_picker_package);
2716 if (TextUtils.isEmpty(pickerPackage)) {
2717 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2718 }
2719
Tony Wickham785f7a52015-08-31 17:28:32 -07002720 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2721 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002722 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER)
2723 .setPackage(pickerPackage)
2724 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset),
2725 REQUEST_PICK_WALLPAPER);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002726 }
2727
2728 /**
2729 * Event handler for a click on the settings button that appears after a long press
2730 * on the home screen.
2731 */
Sunny Goyal745bad92016-05-02 10:54:12 -07002732 private void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002733 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002734 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2735 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002736 }
2737
Adam Cohen61f560d2013-09-30 15:58:20 -07002738 public View.OnTouchListener getHapticFeedbackTouchListener() {
2739 if (mHapticFeedbackTouchListener == null) {
2740 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002741 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002742 @Override
2743 public boolean onTouch(View v, MotionEvent event) {
2744 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2745 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2746 }
2747 return false;
2748 }
2749 };
2750 }
2751 return mHapticFeedbackTouchListener;
2752 }
2753
Tony Wickhame2217252016-03-22 16:34:23 -07002754 @Override
2755 public void onAccessibilityStateChanged(boolean enabled) {
2756 mDragLayer.onAccessibilityStateChanged(enabled);
2757 }
2758
Adam Cohen9211d422014-10-07 18:14:53 -07002759 public void onDragStarted(View view) {
2760 if (isOnCustomContent()) {
2761 // Custom content screen doesn't participate in drag and drop. If on custom
2762 // content screen, move to default.
2763 moveWorkspaceToDefaultScreen();
2764 }
Adam Cohen9211d422014-10-07 18:14:53 -07002765 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002766
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002767 /**
2768 * Called when the user stops interacting with the launcher.
2769 * This implies that the user is now on the homescreen and is not doing housekeeping.
2770 */
Adam Cohen9211d422014-10-07 18:14:53 -07002771 protected void onInteractionEnd() {
2772 if (mLauncherCallbacks != null) {
2773 mLauncherCallbacks.onInteractionEnd();
2774 }
2775 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002776
2777 /**
2778 * Called when the user starts interacting with the launcher.
2779 * The possible interactions are:
2780 * - open all apps
2781 * - reorder an app shortcut, or a widget
2782 * - open the overview mode.
2783 * This is a good time to stop doing things that only make sense
2784 * when the user is on the homescreen and not doing housekeeping.
2785 */
Adam Cohen9211d422014-10-07 18:14:53 -07002786 protected void onInteractionBegin() {
2787 if (mLauncherCallbacks != null) {
2788 mLauncherCallbacks.onInteractionBegin();
2789 }
2790 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002791
Winson Chungcd99cd32015-04-29 11:03:24 -07002792 /** Updates the interaction state. */
2793 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002794 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002795 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002796 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2797 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002798 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002799 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002800 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002801 onInteractionEnd();
2802 }
2803 }
2804
Sunny Goyala7ce1662016-05-31 15:01:35 -07002805 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002806 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002807 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2808 try {
2809 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2810 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2811 // is enabled by default on NYC.
2812 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2813 .penaltyLog().build());
2814 // Could be launching some bookkeeping activity
2815 startActivity(intent, optsBundle);
2816 } finally {
2817 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002818 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002819 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002820 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2821 // corresponding permission. Show the appropriate permission prompt if that
2822 // is the case.
2823 if (intent.getComponent() == null
2824 && Intent.ACTION_CALL.equals(intent.getAction())
2825 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2826 PackageManager.PERMISSION_GRANTED) {
2827 // TODO: Rename sPendingAddItem to a generic name.
2828 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2829 0, info);
2830 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2831 REQUEST_PERMISSION_CALL_PHONE);
2832 } else {
2833 // No idea why this was thrown.
2834 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002835 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002836 }
2837 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002838
Sunny Goyala7ce1662016-05-31 15:01:35 -07002839 private Bundle getActivityLaunchOptions(View v) {
2840 if (Utilities.ATLEAST_MARSHMALLOW) {
2841 int left = 0, top = 0;
2842 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2843 if (v instanceof TextView) {
2844 // Launch from center of icon, not entire view
2845 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2846 if (icon != null) {
2847 Rect bounds = icon.getBounds();
2848 left = (width - bounds.width()) / 2;
2849 top = v.getPaddingTop();
2850 width = bounds.width();
2851 height = bounds.height();
2852 }
2853 }
2854 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2855 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2856 // On L devices, we use the device default slide-up transition.
2857 // On L MR1 devices, we use a custom version of the slide-up transition which
2858 // doesn't have the delay present in the device default.
2859 return ActivityOptions.makeCustomAnimation(
2860 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2861 }
2862 return null;
2863 }
2864
2865 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002866 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2867 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2868 return false;
2869 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002870 // Only launch using the new animation if the shortcut has not opted out (this is a
2871 // private contract between launcher and may be ignored in the future).
2872 boolean useLaunchAnimation = (v != null) &&
2873 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2874 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2875
2876 UserHandleCompat user = null;
2877 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2878 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2879 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002880 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002881
2882 // Prepare intent
2883 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2884 if (v != null) {
2885 int[] pos = new int[2];
2886 v.getLocationOnScreen(pos);
2887 intent.setSourceBounds(
2888 new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2889 }
2890 try {
2891 if (Utilities.ATLEAST_MARSHMALLOW &&
2892 item != null && item.itemType == Favorites.ITEM_TYPE_SHORTCUT) {
2893 // Shortcuts need some special checks due to legacy reasons.
2894 startShortcutIntentSafely(intent, optsBundle, item);
2895 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2896 // Could be launching some bookkeeping activity
2897 startActivity(intent, optsBundle);
2898 } else {
2899 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2900 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2901 }
2902 return true;
2903 } catch (ActivityNotFoundException|SecurityException e) {
2904 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2905 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2906 }
2907 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002908 }
2909
Adam Cohen268c4752012-06-06 17:47:33 -07002910 /**
2911 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2912 * in the DragLayer in the exact absolute location of the original FolderIcon.
2913 */
2914 private void copyFolderIconToImage(FolderIcon fi) {
2915 final int width = fi.getMeasuredWidth();
2916 final int height = fi.getMeasuredHeight();
2917
2918 // Lazy load ImageView, Bitmap and Canvas
2919 if (mFolderIconImageView == null) {
2920 mFolderIconImageView = new ImageView(this);
2921 }
2922 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2923 mFolderIconBitmap.getHeight() != height) {
2924 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2925 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2926 }
2927
2928 DragLayer.LayoutParams lp;
2929 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2930 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2931 } else {
2932 lp = new DragLayer.LayoutParams(width, height);
2933 }
2934
Adam Cohen307fe232012-08-16 17:55:58 -07002935 // The layout from which the folder is being opened may be scaled, adjust the starting
2936 // view size by this scale factor.
2937 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002938 lp.customPosition = true;
2939 lp.x = mRectForFolderAnimation.left;
2940 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002941 lp.width = (int) (scale * width);
2942 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002943
2944 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2945 fi.draw(mFolderIconCanvas);
2946 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002947 if (fi.getFolder() != null) {
2948 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2949 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002950 }
Adam Cohen268c4752012-06-06 17:47:33 -07002951 // Just in case this image view is still in the drag layer from a previous animation,
2952 // we remove it and re-add it.
2953 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2954 mDragLayer.removeView(mFolderIconImageView);
2955 }
2956 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002957 if (fi.getFolder() != null) {
2958 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002959 }
Adam Cohen268c4752012-06-06 17:47:33 -07002960 }
2961
Adam Cohen37b2a492016-04-06 18:36:06 -07002962 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002963 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002964 FolderInfo info = (FolderInfo) fi.getTag();
2965 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2966 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002967 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2968 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002969 }
2970
Adam Cohen268c4752012-06-06 17:47:33 -07002971 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2972 copyFolderIconToImage(fi);
2973 fi.setVisibility(View.INVISIBLE);
2974
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002975 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2976 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002977 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002978 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2979 }
2980 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002981 oa.start();
2982 }
2983
Sunny Goyal935fca12015-10-13 10:19:01 -07002984 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002985 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002986 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002987
Adam Cohen268c4752012-06-06 17:47:33 -07002988 // We remove and re-draw the FolderIcon in-case it has changed
2989 mDragLayer.removeView(mFolderIconImageView);
2990 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08002991
2992 if (cl != null) {
2993 cl.clearFolderLeaveBehind();
2994 }
2995
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002996 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002997 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002998 oa.addListener(new AnimatorListenerAdapter() {
2999 @Override
3000 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003001 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003002 // Remove the ImageView copy of the FolderIcon and make the original visible.
3003 mDragLayer.removeView(mFolderIconImageView);
3004 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003005 }
3006 }
3007 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003008 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003009 if (!animate) {
3010 oa.end();
3011 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003012 }
3013
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003014 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003015 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003016 * is animated relative to the specified View. If the View is null, no animation
3017 * is played.
3018 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003019 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003020 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003021 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003022
Adam Cohenfb91f302012-06-11 15:45:18 -07003023 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003024 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3025 if (openFolder != null && openFolder != folder) {
3026 // Close any open folder before opening a folder.
3027 closeFolder();
3028 }
3029
Adam Cohena9cf38f2011-05-02 15:36:58 -07003030 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003031
Adam Cohena9cf38f2011-05-02 15:36:58 -07003032 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003033
Sunny Goyalf4066152015-04-15 09:42:19 -07003034 // While the folder is open, the position of the icon cannot change.
3035 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3036
Adam Cohen4554ee12011-08-03 16:13:21 -07003037 // Just verify that the folder hasn't already been added to the DragLayer.
3038 // There was a one-off crash where the folder had a parent already.
3039 if (folder.getParent() == null) {
3040 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003041 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003042 } else {
3043 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3044 folder.getParent() + ").");
3045 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003046 folder.animateOpen();
3047
3048 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003049
3050 // Notify the accessibility manager that this folder "window" has appeared and occluded
3051 // the workspace items
3052 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3053 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003054 }
3055
3056 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003057 closeFolder(true);
3058 }
3059
3060 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003061 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003062 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003063 if (folder.isEditingName()) {
3064 folder.dismissEditingName();
3065 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003066 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003067 }
3068 }
3069
Sunny Goyal935fca12015-10-13 10:19:01 -07003070 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003071 animate &= !Utilities.isPowerSaverOn(this);
3072
Adam Cohen4554ee12011-08-03 16:13:21 -07003073 folder.getInfo().opened = false;
3074
3075 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3076 if (parent != null) {
3077 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003078 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003079 if (fi != null) {
3080 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3081 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003082 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003083 if (animate) {
3084 folder.animateClosed();
3085 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003086 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003087 }
Winson Chung83ca4802013-04-12 15:10:52 -07003088
Sunny Goyal935fca12015-10-13 10:19:01 -07003089 // Notify the accessibility manager that this folder "window" has disappeared and no
3090 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003091 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003092 }
3093
Tony Wickhamdadb3042016-02-24 11:07:00 -08003094 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003095 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003096 if (!isDraggingEnabled()) return false;
3097 if (isWorkspaceLocked()) return false;
3098 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003099
Sunny Goyalbb011da2016-06-15 15:42:29 -07003100 if (v == mAllAppsButton && mAllAppsButton != null) {
Winson Chung76648c52015-07-10 14:33:23 -07003101 onLongClickAllAppsButton(v);
3102 return true;
3103 }
3104
Adam Cohen1697b792013-09-17 19:08:21 -07003105 if (v instanceof Workspace) {
3106 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003107 if (!mWorkspace.isTouchActive()) {
3108 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003109 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3110 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3111 return true;
3112 } else {
3113 return false;
3114 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003115 } else {
3116 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003117 }
Adam Cohen1697b792013-09-17 19:08:21 -07003118 }
3119
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003120 CellLayout.CellInfo longClickCellInfo = null;
3121 View itemUnderLongClick = null;
3122 if (v.getTag() instanceof ItemInfo) {
3123 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003124 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003125 itemUnderLongClick = longClickCellInfo.cell;
3126 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003127 }
3128
Winson Chung3d503fb2011-07-13 17:25:49 -07003129 // The hotseat touch handling does not go through Workspace, and we always allow long press
3130 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003131 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003132 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003133 // User long pressed on empty space
3134 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3135 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003136 if (mWorkspace.isInOverviewMode()) {
3137 mWorkspace.startReordering(v);
3138 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003139 showOverviewMode(true);
Hyunyoung Song645764e2016-06-06 14:19:02 -07003140 mHotseat.requestDisallowInterceptTouchEvent(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003141 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003142 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003143 final boolean isAllAppsButton =
3144 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3145 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3146 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003147 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003148 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003149 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003150 }
3151 }
3152 }
3153 return true;
3154 }
3155
Winson Chung3d503fb2011-07-13 17:25:49 -07003156 boolean isHotseatLayout(View layout) {
3157 return mHotseat != null && layout != null &&
3158 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3159 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003160
Winson Chung3d503fb2011-07-13 17:25:49 -07003161 /**
3162 * Returns the CellLayout of the specified container at the specified screen.
3163 */
Sunny Goyal92820592015-03-02 11:31:35 -08003164 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003165 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3166 if (mHotseat != null) {
3167 return mHotseat.getLayout();
3168 } else {
3169 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003170 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003171 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003172 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003173 }
3174 }
3175
Winson Chungb745afb2015-03-02 11:51:23 -08003176 /**
3177 * For overridden classes.
3178 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003179 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003180 return isAppsViewVisible();
3181 }
3182
3183 public boolean isAppsViewVisible() {
3184 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3185 }
3186
3187 public boolean isWidgetsViewVisible() {
3188 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003189 }
3190
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003191 private void setWorkspaceBackground(int background) {
3192 switch (background) {
3193 case WORKSPACE_BACKGROUND_TRANSPARENT:
3194 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3195 break;
3196 case WORKSPACE_BACKGROUND_BLACK:
3197 getWindow().setBackgroundDrawable(null);
3198 break;
3199 default:
3200 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3201 }
Craig Mautner360310b2012-10-26 15:13:08 -07003202 }
3203
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003204 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003205 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3206 int curflags = getWindow().getAttributes().flags
3207 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3208 if (wpflags != curflags) {
3209 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3210 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003211 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003212 }
3213
Michael Jurkae326f182011-11-21 14:05:46 -08003214 @Override
3215 public void onTrimMemory(int level) {
3216 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003217 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3218 // The widget preview db can result in holding onto over
3219 // 3MB of memory for caching which isn't necessary.
3220 SQLiteDatabase.releaseMemory();
3221
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003222 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003223 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003224 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003225 if (mLauncherCallbacks != null) {
3226 mLauncherCallbacks.onTrimMemory(level);
3227 }
Michael Jurkae326f182011-11-21 14:05:46 -08003228 }
3229
Winson5c6bdbb2015-09-03 11:36:19 -07003230 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003231 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003232 }
3233
Winson5c6bdbb2015-09-03 11:36:19 -07003234 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003235 boolean changed = mState != State.WORKSPACE ||
3236 mWorkspace.getState() != Workspace.State.NORMAL;
3237 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003238 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003239 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003240 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003241
Michael Jurkab3e22d92011-10-31 15:58:33 -07003242 // Set focus to the AppsCustomize button
3243 if (mAllAppsButton != null) {
3244 mAllAppsButton.requestFocus();
3245 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003246 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003247
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003248 // Change the state *after* we've called all the transition code
3249 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003250
Winson Chung5fb63472011-02-02 17:03:37 -08003251 // Resume the auto-advance of widgets
3252 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003253 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003254
Winson Chung0f785722015-04-08 10:27:49 -07003255 if (changed) {
3256 // Send an accessibility event to announce the context change
3257 getWindow().getDecorView()
3258 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003259 }
Winson5c6bdbb2015-09-03 11:36:19 -07003260 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003261 }
3262
Winsone9f27272015-10-13 10:47:51 -07003263 /**
3264 * Shows the overview button.
3265 */
Adam Cohened307df2013-10-02 09:37:31 -07003266 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003267 showOverviewMode(animated, false);
3268 }
3269
3270 /**
3271 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3272 * onto one of the overview panel buttons.
3273 */
3274 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3275 Runnable postAnimRunnable = null;
3276 if (requestButtonFocus) {
3277 postAnimRunnable = new Runnable() {
3278 @Override
3279 public void run() {
3280 // Hitting the menu button when in touch mode does not trigger touch mode to
3281 // be disabled, so if requested, force focus on one of the overview panel
3282 // buttons.
3283 mOverviewPanel.requestFocusFromTouch();
3284 }
3285 };
3286 }
Adam Cohened307df2013-10-02 09:37:31 -07003287 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003288 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003289 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003290 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003291 }
3292
Winson Chungb745afb2015-03-02 11:51:23 -08003293 /**
3294 * Shows the apps view.
3295 */
Hyunyoung Song645764e2016-06-06 14:19:02 -07003296 public void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003297 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003298 if (resetListToTop) {
3299 mAppsView.scrollToTop();
3300 }
Winson Chung4ac30062015-05-08 17:34:17 -07003301 if (updatePredictedApps) {
3302 tryAndUpdatePredictedApps();
3303 }
Winson Chung76648c52015-07-10 14:33:23 -07003304 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003305 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003306
Winson Chungb745afb2015-03-02 11:51:23 -08003307 /**
3308 * Shows the widgets view.
3309 */
3310 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003311 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003312 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003313 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003314 }
Winson Chung76648c52015-07-10 14:33:23 -07003315 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003316
3317 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003318 @Override
3319 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003320 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003321 }
3322 });
Winson Chungb745afb2015-03-02 11:51:23 -08003323 }
3324
3325 /**
3326 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003327 *
3328 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003329 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003330 // TODO: calling method should use the return value so that when {@code false} is returned
3331 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003332 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003333 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3334 mState != State.WIDGETS_SPRING_LOADED) {
3335 return false;
3336 }
3337 if (toState != State.APPS && toState != State.WIDGETS) {
3338 return false;
3339 }
Winson Chungb745afb2015-03-02 11:51:23 -08003340
3341 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003342 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3343 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003344 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003345 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003346 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003347
Michael Jurkab3e22d92011-10-31 15:58:33 -07003348 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003349 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003350
3351 // Pause the auto-advance of widgets until we are out of AllApps
3352 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003353 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003354 closeFolder();
3355
3356 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003357 getWindow().getDecorView()
3358 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003359 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003360 }
3361
Winson Chungcd99cd32015-04-29 11:03:24 -07003362 /**
3363 * Updates the workspace and interaction state on state change, and return the animation to this
3364 * new state.
3365 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003366 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003367 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003368 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003369 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003370 updateInteraction(fromState, toState);
3371 return anim;
3372 }
3373
Jun Mukaif0033da2015-08-04 18:34:30 -07003374 public void onLauncherClingShown() {
3375 // When a launcher cling appears, it should cover the underlying layers, so their focus
3376 // should be blocked.
3377 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3378 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3379 }
3380 }
3381
3382 public void onLauncherClingDismissed() {
3383 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3384 }
3385
Hyunyoung Song3f471442015-04-08 19:01:34 -07003386 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003387 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003388 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003389 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003390 }
Winson Chungb745afb2015-03-02 11:51:23 -08003391
Winson Chung006ee262015-08-03 14:40:11 -07003392 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003393 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003394 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003395
3396 if (isAppsViewVisible()) {
3397 mState = State.APPS_SPRING_LOADED;
3398 } else if (isWidgetsViewVisible()) {
3399 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003400 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003401 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003402 } else {
3403 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003404 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003405 }
Adam Cohen7777d962011-08-18 18:58:38 -07003406
Hyunyoung Song3f471442015-04-08 19:01:34 -07003407 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003408 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003409 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003410
Winson Chunge7a03942011-08-05 15:05:12 -07003411 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003412 @Override
3413 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003414 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003415 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3416 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003417 // Before we show workspace, hide all apps again because
3418 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3419 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003420 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003421 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003422 } else {
3423 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003424 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003425 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003426 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003427 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003428
Tony Wickhame0c33232016-02-08 11:37:04 -08003429 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003430 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3431 || mState == State.WIDGETS_SPRING_LOADED;
3432 }
3433
Michael Jurkad3ef3062010-11-23 16:23:58 -08003434 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003435 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003436 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003437 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003438 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003439 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003440 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3441 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003442 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003443 }
3444
Winson Chung4ac30062015-05-08 17:34:17 -07003445 /**
3446 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3447 * resumed.
3448 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003449 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003450 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003451 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003452 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003453 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003454 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003455 }
3456 }
3457 }
3458
Michael Jurkab3e22d92011-10-31 15:58:33 -07003459 void lockAllApps() {
3460 // TODO
3461 }
3462
3463 void unlockAllApps() {
3464 // TODO
3465 }
3466
Winsonf768d932015-09-25 16:12:35 -07003467 public boolean launcherCallbacksProvidesSearch() {
3468 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3469 }
3470
Michael Jurkad7c28052012-04-27 15:43:36 -07003471 @Override
3472 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003473 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003474 final List<CharSequence> text = event.getText();
3475 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003476 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003477 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003478 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003479 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003480 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003481 } else if (mWorkspace != null) {
3482 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003483 } else {
3484 text.add(getString(R.string.all_apps_home_button_label));
3485 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003486 return result;
3487 }
3488
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003489 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003490 * If the activity is currently paused, signal that we need to run the passed Runnable
3491 * in onResume.
3492 *
3493 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003494 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3495 * wrong when we're not running, and if the activity comes back to what the configuration was
3496 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003497 *
3498 * Implementation of the method from LauncherModel.Callbacks.
3499 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003500 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003501 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003502 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003503 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003504 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003505 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003506 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003507 }
3508 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003509 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003510 return true;
3511 } else {
3512 return false;
3513 }
3514 }
3515
Michael Jurkac402cd92013-05-20 15:49:32 +02003516 private boolean waitUntilResume(Runnable run) {
3517 return waitUntilResume(run, false);
3518 }
3519
Michael Jurka1e2f4652013-07-08 18:03:46 -07003520 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003521 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003522 }
3523
Michael Jurka7607c2f2013-04-03 14:33:19 -07003524 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003525 * If the activity is currently paused, signal that we need to re-run the loader
3526 * in onResume.
3527 *
3528 * This needs to be called from incoming places where resources might have been loaded
3529 * while we are paused. That is becaues the Configuration might be wrong
3530 * when we're not running, and if it comes back to what it was when we
3531 * were paused, we are not restarted.
3532 *
3533 * Implementation of the method from LauncherModel.Callbacks.
3534 *
3535 * @return true if we are currently paused. The caller might be able to
3536 * skip some work in that case since we will come back again.
3537 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003538 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003539 public boolean setLoadOnResume() {
3540 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003541 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003542 mOnResumeNeedsLoad = true;
3543 return true;
3544 } else {
3545 return false;
3546 }
3547 }
3548
3549 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003550 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003551 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003552 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003553 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003554 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003555 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003556 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003557 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003558 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003559 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003560
Joe Onorato9c1289c2009-08-17 11:03:03 -04003561 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003562 * Clear any pending bind callbacks. This is called when is loader is planning to
3563 * perform a full rebind from scratch.
3564 */
3565 @Override
3566 public void clearPendingBinds() {
3567 mBindOnResumeCallbacks.clear();
3568 if (mPendingExecutor != null) {
3569 mPendingExecutor.markCompleted();
3570 mPendingExecutor = null;
3571 }
3572 }
3573
3574 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003575 * Refreshes the shortcuts shown on the workspace.
3576 *
3577 * Implementation of the method from LauncherModel.Callbacks.
3578 */
3579 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003580 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003581
Winson Chungd64d1762013-08-20 14:37:16 -07003582 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003583 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003584 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003585
Michael Jurka05bf644e2011-11-30 20:28:53 -08003586 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003587 if (mHotseat != null) {
3588 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003589 }
3590 }
3591
Adam Cohendcd297f2013-06-18 13:13:40 -07003592 @Override
3593 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003594 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003595 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3596 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003597 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3598 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3599 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003600 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3601 // If there are no screens, we need to have an empty screen
3602 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003603 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003604 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003605
3606 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003607 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003608 if (hasCustomContentToLeft()) {
3609 mWorkspace.createCustomContentContainer();
3610 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003611 }
Winson Chung64359a52013-07-08 17:17:08 -07003612 }
3613
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003614 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003615 int count = orderedScreenIds.size();
3616 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003617 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003618 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003619 // No need to bind the first screen, as its always bound.
3620 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3621 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003622 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003623 }
3624
Winson Chungd64d1762013-08-20 14:37:16 -07003625 public void bindAppsAdded(final ArrayList<Long> newScreens,
3626 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003627 final ArrayList<ItemInfo> addAnimated,
3628 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003629 Runnable r = new Runnable() {
3630 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003631 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003632 }
3633 };
3634 if (waitUntilResume(r)) {
3635 return;
3636 }
3637
Winson Chungd64d1762013-08-20 14:37:16 -07003638 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003639 if (newScreens != null) {
3640 bindAddScreens(newScreens);
3641 }
Winson Chungd64d1762013-08-20 14:37:16 -07003642
3643 // We add the items without animation on non-visible pages, and with
3644 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003645 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003646 bindItems(addNotAnimated, 0,
3647 addNotAnimated.size(), false);
3648 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003649 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003650 bindItems(addAnimated, 0,
3651 addAnimated.size(), true);
3652 }
Winson Chungc58497e2013-09-03 17:48:37 -07003653
Winson Chung87412982013-10-03 18:34:14 -07003654 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003655 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003656
Winson Chungb745afb2015-03-02 11:51:23 -08003657 if (addedApps != null && mAppsView != null) {
3658 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003659 }
Winson Chungd64d1762013-08-20 14:37:16 -07003660 }
3661
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003662 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003663 * Bind the items start-end from the list.
3664 *
3665 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003666 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003667 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003668 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3669 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003670 Runnable r = new Runnable() {
3671 public void run() {
3672 bindItems(shortcuts, start, end, forceAnimateIcons);
3673 }
3674 };
3675 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003676 return;
3677 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003678
Winson Chungf0c6ae02012-03-21 16:10:31 -07003679 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003680 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3681 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003682 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003683 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003684 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003685 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003686 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003687
3688 // Short circuit if we are loading dock items for a configuration which has no dock
3689 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3690 mHotseat == null) {
3691 continue;
3692 }
3693
Sunny Goyal639e9062015-08-19 19:17:06 -07003694 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003695 switch (item.itemType) {
3696 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3697 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003698 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003699 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003700 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003701 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003702 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003703 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003704 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003705 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003706 default:
3707 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003708 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003709
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003710 /*
3711 * Remove colliding items.
3712 */
3713 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3714 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3715 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3716 View v = cl.getChildAt(item.cellX, item.cellY);
3717 Object tag = v.getTag();
3718 String desc = "Collision while binding workspace item: " + item
3719 + ". Collides with " + tag;
3720 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3721 throw (new RuntimeException(desc));
3722 } else {
3723 Log.d(TAG, desc);
3724 LauncherModel.deleteItemFromDatabase(this, item);
3725 continue;
3726 }
3727 }
3728 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003729 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3730 item.cellY, 1, 1);
3731 if (animateIcons) {
3732 // Animate all the applications up now
3733 view.setAlpha(0f);
3734 view.setScaleX(0f);
3735 view.setScaleY(0f);
3736 bounceAnims.add(createNewAppBounceAnimation(view, i));
3737 newShortcutsScreenId = item.screenId;
3738 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003739 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003740
Winson Chung997a9232013-07-24 15:33:46 -07003741 if (animateIcons) {
3742 // Animate to the correct page
3743 if (newShortcutsScreenId > -1) {
3744 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003745 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003746 final Runnable startBounceAnimRunnable = new Runnable() {
3747 public void run() {
3748 anim.playTogether(bounceAnims);
3749 anim.start();
3750 }
3751 };
Winson Chung997a9232013-07-24 15:33:46 -07003752 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003753 // We post the animation slightly delayed to prevent slowdowns
3754 // when we are loading right after we return to launcher.
3755 mWorkspace.postDelayed(new Runnable() {
3756 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003757 if (mWorkspace != null) {
3758 mWorkspace.snapToPage(newScreenIndex);
3759 mWorkspace.postDelayed(startBounceAnimRunnable,
3760 NEW_APPS_ANIMATION_DELAY);
3761 }
Winson Chung94d67682013-09-25 16:29:40 -07003762 }
3763 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003764 } else {
3765 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003766 }
3767 }
Winson Chung64359a52013-07-08 17:17:08 -07003768 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003769 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003770 }
3771
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003772 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3773 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3774 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003775 view.updateAppWidget(null);
3776 view.setOnClickListener(this);
3777 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003778 mWorkspace.requestLayout();
3779 }
3780
Joe Onorato9c1289c2009-08-17 11:03:03 -04003781 /**
3782 * Add the views for a widget to the workspace.
3783 *
3784 * Implementation of the method from LauncherModel.Callbacks.
3785 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003786 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003787 Runnable r = new Runnable() {
3788 public void run() {
3789 bindAppWidget(item);
3790 }
3791 };
3792 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003793 return;
3794 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003795
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003796 if (mIsSafeModeEnabled) {
3797 bindSafeModeWidget(item);
3798 return;
3799 }
3800
Daniel Sandler843e8602010-06-07 14:59:01 -04003801 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3802 if (DEBUG_WIDGETS) {
3803 Log.d(TAG, "bindAppWidget: " + item);
3804 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003805
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003806 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003807
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003808 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3809 // If the provider is not ready, bind as a pending widget.
3810 appWidgetInfo = null;
3811 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3812 // The widget id is not valid. Try to find the widget based on the provider info.
3813 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3814 } else {
3815 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3816 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003817
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003818 // If the provider is ready, but the width is not yet restored, try to restore it.
3819 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3820 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003821 if (appWidgetInfo == null) {
3822 if (DEBUG_WIDGETS) {
3823 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3824 + " belongs to component " + item.providerName
3825 + ", as the povider is null");
3826 }
3827 LauncherModel.deleteItemFromDatabase(this, item);
3828 return;
3829 }
Sunny Goyalff572272014-07-23 13:58:07 -07003830
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003831 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003832 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003833 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3834 // Id has not been allocated yet. Allocate a new id.
3835 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3836 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003837
Sunny Goyald478c832016-04-01 12:04:16 -07003838 // Also try to bind the widget. If the bind fails, the user will be shown
3839 // a click to setup UI, which will ask for the bind permission.
3840 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3841 pendingInfo.spanX = item.spanX;
3842 pendingInfo.spanY = item.spanY;
3843 pendingInfo.minSpanX = item.minSpanX;
3844 pendingInfo.minSpanY = item.minSpanY;
3845 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
3846 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3847 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003848
Sunny Goyald478c832016-04-01 12:04:16 -07003849 // Bind succeeded
3850 if (success) {
3851 // If the widget has a configure activity, it is still needs to set it up,
3852 // otherwise the widget is ready to go.
3853 item.restoreStatus = (appWidgetInfo.configure == null)
3854 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3855 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003856 }
Sunny Goyald478c832016-04-01 12:04:16 -07003857
3858 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003859 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003860 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003861 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003862 // The widget was marked as UI not ready, but there is no configure activity to
3863 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003864 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3865 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003866 }
Sunny Goyalff572272014-07-23 13:58:07 -07003867 }
3868
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003869 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003870 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003871 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3872 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003873 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003874
Sunny Goyal56c73602015-09-25 12:55:01 -07003875 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003876 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003877 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003878 deleteWidgetInfo(item);
3879 return;
3880 }
3881
Sunny Goyal233ee962015-08-03 13:05:01 -07003882 item.minSpanX = appWidgetInfo.minSpanX;
3883 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003884 addAppWidgetToWorkspace(
3885 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3886 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003887 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003888 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003889 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003890 view.updateAppWidget(null);
3891 view.setOnClickListener(this);
3892 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003893 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003894 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003895
Daniel Sandler843e8602010-06-07 14:59:01 -04003896 if (DEBUG_WIDGETS) {
3897 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3898 + (SystemClock.uptimeMillis()-start) + "ms");
3899 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003900 }
3901
Sunny Goyalff572272014-07-23 13:58:07 -07003902 /**
3903 * Restores a pending widget.
3904 *
3905 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003906 */
Sunny Goyald478c832016-04-01 12:04:16 -07003907 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003908 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3909 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3910 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003911 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003912 }
3913
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003914 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003915 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003916
3917 mWorkspace.reinflateWidgetsIfNecessary();
3918 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003919 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003920 }
3921
Adam Cohen1462de32012-07-24 22:34:36 -07003922 public void onPageBoundSynchronously(int page) {
3923 mSynchronouslyBoundPages.add(page);
3924 }
3925
Sunny Goyal527c7d32015-08-28 15:19:36 -07003926 @Override
3927 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3928 if (mPendingExecutor != null) {
3929 mPendingExecutor.markCompleted();
3930 }
3931 mPendingExecutor = executor;
3932 executor.attachTo(this);
3933 }
3934
3935 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3936 if (mPendingExecutor == executor) {
3937 mPendingExecutor = null;
3938 }
3939 }
3940
Joe Onorato9c1289c2009-08-17 11:03:03 -04003941 /**
3942 * Callback saying that there aren't any more items to bind.
3943 *
3944 * Implementation of the method from LauncherModel.Callbacks.
3945 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003946 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003947 Runnable r = new Runnable() {
3948 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003949 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003950 }
3951 };
3952 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003953 return;
3954 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003955 if (mSavedState != null) {
3956 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003957 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003958 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003959
Joe Onorato9c1289c2009-08-17 11:03:03 -04003960 mSavedState = null;
3961 }
3962
Adam Cohen1462de32012-07-24 22:34:36 -07003963 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003964
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003965 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003966
3967 // If we received the result of any pending adds while the loader was running (e.g. the
3968 // widget configuration forced an orientation change), process them now.
3969 if (sPendingAddItem != null) {
3970 final long screenId = completeAdd(sPendingAddItem);
3971
3972 // TODO: this moves the user to the page where the pending item was added. Ideally,
3973 // the screen would be guaranteed to exist after bind, and the page would be set through
3974 // the workspace restore process.
3975 mWorkspace.post(new Runnable() {
3976 @Override
3977 public void run() {
3978 mWorkspace.snapToScreenId(screenId);
3979 }
3980 });
3981 sPendingAddItem = null;
3982 }
3983
Sunny Goyal756adbc2015-04-16 15:20:51 -07003984 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003985
3986 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003987 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003988 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003989 }
3990
Winson Chunga2413752012-04-03 14:22:34 -07003991 private boolean canRunNewAppsAnimation() {
3992 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07003993 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
3994 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07003995 }
3996
Winson Chungc9168342013-06-26 14:54:55 -07003997 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003998 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003999 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4000 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004001 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004002 return bounceAnim;
4003 }
4004
Adam Cohen27772732013-11-11 14:00:56 +00004005 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004006 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004007 }
4008
Tony Wickham55616cd2015-09-23 14:55:17 -07004009 public int getSearchBarHeight() {
4010 if (mLauncherCallbacks != null) {
4011 return mLauncherCallbacks.getSearchBarHeight();
4012 }
4013 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4014 }
4015
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004016 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004017 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4018 * multiple calls to bind the same list.)
4019 */
4020 @Thunk ArrayList<AppInfo> mTmpAppsList;
4021 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4022 public void run() {
4023 bindAllApplications(mTmpAppsList);
4024 mTmpAppsList = null;
4025 }
4026 };
4027
4028 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004029 * Add the icons for all apps.
4030 *
4031 * Implementation of the method from LauncherModel.Callbacks.
4032 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004033 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004034 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4035 mTmpAppsList = apps;
4036 return;
4037 }
4038
Winson Chungb745afb2015-03-02 11:51:23 -08004039 if (mAppsView != null) {
4040 mAppsView.setApps(apps);
4041 }
Adam Cohen9211d422014-10-07 18:14:53 -07004042 if (mLauncherCallbacks != null) {
4043 mLauncherCallbacks.bindAllApplications(apps);
4044 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004045 }
4046
4047 /**
4048 * A package was updated.
4049 *
4050 * Implementation of the method from LauncherModel.Callbacks.
4051 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004052 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004053 Runnable r = new Runnable() {
4054 public void run() {
4055 bindAppsUpdated(apps);
4056 }
4057 };
4058 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004059 return;
4060 }
4061
Winson Chungb745afb2015-03-02 11:51:23 -08004062 if (mAppsView != null) {
4063 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004064 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004065 }
4066
Sunny Goyal4390ace2014-10-13 11:33:11 -07004067 @Override
4068 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4069 Runnable r = new Runnable() {
4070 public void run() {
4071 bindWidgetsRestored(widgets);
4072 }
4073 };
4074 if (waitUntilResume(r)) {
4075 return;
4076 }
4077 mWorkspace.widgetsRestored(widgets);
4078 }
4079
Joe Onorato9c1289c2009-08-17 11:03:03 -04004080 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004081 * Some shortcuts were updated in the background.
4082 *
4083 * Implementation of the method from LauncherModel.Callbacks.
4084 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004085 @Override
4086 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4087 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004088 Runnable r = new Runnable() {
4089 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004090 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004091 }
4092 };
4093 if (waitUntilResume(r)) {
4094 return;
4095 }
4096
Sunny Goyal4390ace2014-10-13 11:33:11 -07004097 if (!updated.isEmpty()) {
4098 mWorkspace.updateShortcuts(updated);
4099 }
4100
4101 if (!removed.isEmpty()) {
4102 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4103 for (ShortcutInfo si : removed) {
4104 removedComponents.add(si.getTargetComponent());
4105 }
4106 mWorkspace.removeItemsByComponentName(removedComponents, user);
4107 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004108 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004109 }
4110 }
4111
4112 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004113 * Update the state of a package, typically related to install state.
4114 *
4115 * Implementation of the method from LauncherModel.Callbacks.
4116 */
Sunny Goyale755d462014-07-22 13:48:29 -07004117 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004118 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4119 Runnable r = new Runnable() {
4120 public void run() {
4121 bindRestoreItemsChange(updates);
4122 }
4123 };
4124 if (waitUntilResume(r)) {
4125 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004126 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004127
Sunny Goyal756adbc2015-04-16 15:20:51 -07004128 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004129 }
4130
4131 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004132 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004133 * in addition to specific applications to remove, the reason being that
4134 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004135 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004136 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004137 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004138 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004139 public void bindWorkspaceComponentsRemoved(
4140 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4141 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004142 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004143 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004144 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004145 }
Winson Chungd64d1762013-08-20 14:37:16 -07004146 };
4147 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004148 return;
4149 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004150 if (!packageNames.isEmpty()) {
4151 mWorkspace.removeItemsByPackageName(packageNames, user);
4152 }
4153 if (!components.isEmpty()) {
4154 mWorkspace.removeItemsByComponentName(components, user);
4155 }
4156 // Notify the drag controller
4157 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004158
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004159 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004160
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004161 @Override
4162 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4163 Runnable r = new Runnable() {
4164 public void run() {
4165 bindAppInfosRemoved(appInfos);
4166 }
4167 };
4168 if (waitUntilResume(r)) {
4169 return;
Joe Onorato36115782010-06-17 13:28:48 -04004170 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004171
Winson Chungdf95eb12013-10-16 14:57:07 -07004172 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004173 if (mAppsView != null) {
4174 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004175 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004176 }
Joe Onoratobe386092009-11-17 17:32:16 -08004177
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004178 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004179 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004180 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004181 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004182 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004183
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004184 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004185 public void bindWidgetsModel(WidgetsModel model) {
4186 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004187 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004188 return;
4189 }
4190
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004191 if (mWidgetsView != null && model != null) {
4192 mWidgetsView.addWidgets(model);
4193 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004194 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004195 }
4196
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004197 @Override
4198 public void notifyWidgetProvidersChanged() {
4199 if (mWorkspace.getState().shouldUpdateWidget) {
4200 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4201 }
4202 }
4203
Winson Chung400438b2011-01-16 17:53:48 -08004204 private int mapConfigurationOriActivityInfoOri(int configOri) {
4205 final Display d = getWindowManager().getDefaultDisplay();
4206 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4207 switch (d.getRotation()) {
4208 case Surface.ROTATION_0:
4209 case Surface.ROTATION_180:
4210 // We are currently in the same basic orientation as the natural orientation
4211 naturalOri = configOri;
4212 break;
4213 case Surface.ROTATION_90:
4214 case Surface.ROTATION_270:
4215 // We are currently in the other basic orientation to the natural orientation
4216 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4217 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4218 break;
4219 }
4220
4221 int[] oriMap = {
4222 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4223 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4224 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4225 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4226 };
4227 // Since the map starts at portrait, we need to offset if this device's natural orientation
4228 // is landscape.
4229 int indexOffset = 0;
4230 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4231 indexOffset = 1;
4232 }
4233 return oriMap[(d.getRotation() + indexOffset) % 4];
4234 }
Adam Cohen446e9402011-09-15 18:21:21 -07004235
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004236 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004237 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004238 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004239 if (Utilities.ATLEAST_JB_MR2) {
4240 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4241 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004242 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4243 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004244 }
Winson Chung4b919f82012-05-01 10:44:08 -07004245 }
4246 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004247
Winson Chung4b919f82012-05-01 10:44:08 -07004248 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004249 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004250 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004251 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004252 } else {
4253 mHandler.postDelayed(new Runnable() {
4254 public void run() {
4255 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4256 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004257 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004258 }
Winson Chung4b919f82012-05-01 10:44:08 -07004259 }
Winson Chung400438b2011-01-16 17:53:48 -08004260 }
4261
Adam Cohenea90f832014-05-21 15:03:34 -07004262 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004263 * To be overridden by subclasses to indicate that there is an activity to launch
4264 * before showing the standard launcher experience.
4265 */
4266 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004267 if (mLauncherCallbacks != null) {
4268 return mLauncherCallbacks.hasFirstRunActivity();
4269 }
Adam Cohen432609a2014-03-13 17:03:22 -07004270 return false;
4271 }
4272
4273 /**
4274 * To be overridden by subclasses to launch any first run activity
4275 */
4276 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004277 if (mLauncherCallbacks != null) {
4278 return mLauncherCallbacks.getFirstRunActivity();
4279 }
Adam Cohen432609a2014-03-13 17:03:22 -07004280 return null;
4281 }
4282
Winson Chunga6945242014-01-08 14:04:34 -08004283 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004284 return !ActivityManager.isRunningInTestHarness() &&
4285 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004286 }
4287
Adam Cohen4a9423d2014-03-28 14:22:31 -07004288 protected boolean hasRunFirstRunActivity() {
4289 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4290 }
4291
Adam Cohen432609a2014-03-13 17:03:22 -07004292 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004293 if (shouldRunFirstRunActivity() &&
4294 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004295 Intent firstRunIntent = getFirstRunActivity();
4296 if (firstRunIntent != null) {
4297 startActivity(firstRunIntent);
4298 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004299 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004300 }
4301 }
Adam Cohen432609a2014-03-13 17:03:22 -07004302 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004303 }
4304
4305 private void markFirstRunActivityShown() {
4306 SharedPreferences.Editor editor = mSharedPrefs.edit();
4307 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4308 editor.apply();
4309 }
4310
Adam Cohen432609a2014-03-13 17:03:22 -07004311 /**
4312 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4313 * screen that must be displayed and dismissed.
4314 */
4315 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004316 if (mLauncherCallbacks != null) {
4317 return mLauncherCallbacks.hasDismissableIntroScreen();
4318 }
Adam Cohen432609a2014-03-13 17:03:22 -07004319 return false;
4320 }
4321
4322 /**
4323 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4324 */
4325 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004326 if (mLauncherCallbacks != null) {
4327 return mLauncherCallbacks.getIntroScreen();
4328 }
Adam Cohen432609a2014-03-13 17:03:22 -07004329 return null;
4330 }
4331
4332 /**
4333 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4334 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4335 */
4336 private boolean shouldShowIntroScreen() {
4337 return hasDismissableIntroScreen() &&
4338 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4339 }
4340
4341 protected void showIntroScreen() {
4342 View introScreen = getIntroScreen();
4343 changeWallpaperVisiblity(false);
4344 if (introScreen != null) {
4345 mDragLayer.showOverlayView(introScreen);
4346 }
4347 }
4348
4349 public void dismissIntroScreen() {
4350 markIntroScreenDismissed();
4351 if (showFirstRunActivity()) {
4352 // We delay hiding the intro view until the first run activity is showing. This
4353 // avoids a blip.
4354 mWorkspace.postDelayed(new Runnable() {
4355 @Override
4356 public void run() {
4357 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004358 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004359 }
4360 }, ACTIVITY_START_DELAY);
4361 } else {
4362 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004363 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004364 }
4365 changeWallpaperVisiblity(true);
4366 }
4367
4368 private void markIntroScreenDismissed() {
4369 SharedPreferences.Editor editor = mSharedPrefs.edit();
4370 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4371 editor.apply();
4372 }
4373
Adam Cohen091440a2015-03-18 14:16:05 -07004374 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004375 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4376 // on the device, then we always show the first run cling experience (or if there is no
4377 // launcher2). Otherwise, we prompt the user upon started for migration
4378 LauncherClings launcherClings = new LauncherClings(this);
4379 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004380 mClings = launcherClings;
Sunny Goyalded0fdb2016-05-23 15:55:41 -07004381 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004382 }
4383 }
4384
Winson Chung5ffd51d2015-06-25 11:36:50 -07004385 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004386 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004387 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004388 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004389 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004390 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004391 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4392 if (userHandle != null) {
4393 user = UserHandleCompat.fromUser(userHandle);
4394 }
4395 }
4396 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004397 }
4398
4399 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004400 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004401 if (user == null) {
4402 user = UserHandleCompat.myUserHandle();
4403 }
4404
4405 // Called from search suggestion, add the profile extra to the intent to ensure that we
4406 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004407 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004408 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004409 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004410 return null;
4411 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004412 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004413 }
4414
Winson Chung5ffd51d2015-06-25 11:36:50 -07004415 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004416 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4417 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004418 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004419 UserHandleCompat.myUserHandle());
4420 }
4421
Winson Chung5ffd51d2015-06-25 11:36:50 -07004422 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004423 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4424 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004425 mWorkspace.onExternalDragStartedWithItem(dragView);
4426 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004427 }
4428
Winson Chung5ffd51d2015-06-25 11:36:50 -07004429 protected void moveWorkspaceToDefaultScreen() {
4430 mWorkspace.moveToDefaultScreen(false);
4431 }
4432
Winson Chung80baf5a2010-08-09 16:03:15 -07004433 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004434 * Returns a FastBitmapDrawable with the icon, accurately sized.
4435 */
4436 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4437 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4438 d.setFilterBitmap(true);
4439 resizeIconDrawable(d);
4440 return d;
4441 }
4442
4443 /**
4444 * Resizes an icon drawable to the correct icon size.
4445 */
Winson5fbe0742015-09-30 15:33:00 -07004446 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004447 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004448 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004449 }
4450
4451 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004452 * Prints out out state for debugging.
4453 */
4454 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004455 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004456 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004457 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4458 Log.d(TAG, "mRestoring=" + mRestoring);
4459 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004460 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004461 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004462
Daniel Sandler325dc232013-06-05 22:57:57 -04004463 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004464 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004465
4466 @Override
4467 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4468 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004469 // Dump workspace
4470 writer.println(prefix + "Workspace Items");
4471 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4472 writer.println(prefix + " Homescreen " + i);
4473
4474 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4475 for (int j = 0; j < layout.getChildCount(); j++) {
4476 Object tag = layout.getChildAt(j).getTag();
4477 if (tag != null) {
4478 writer.println(prefix + " " + tag.toString());
4479 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004480 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004481 }
Sunny Goyala1365452015-10-01 15:46:24 -07004482
4483 writer.println(prefix + " Hotseat");
4484 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4485 for (int j = 0; j < layout.getChildCount(); j++) {
4486 Object tag = layout.getChildAt(j).getTag();
4487 if (tag != null) {
4488 writer.println(prefix + " " + tag.toString());
4489 }
4490 }
4491
Sunny Goyal713edfc2016-05-06 09:58:34 -07004492 try {
4493 FileLog.flushAll(writer);
4494 } catch (Exception e) {
4495 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004496 }
4497
Adam Cohen9211d422014-10-07 18:14:53 -07004498 if (mLauncherCallbacks != null) {
4499 mLauncherCallbacks.dump(prefix, fd, writer, args);
4500 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004501 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004502
Adam Cohen59400422014-03-05 18:07:04 -08004503 public static CustomAppWidget getCustomAppWidget(String name) {
4504 return sCustomAppWidgets.get(name);
4505 }
4506
4507 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4508 return sCustomAppWidgets;
4509 }
4510
Sunny Goyal29538332016-02-18 09:10:19 -08004511 public static List<View> getFolderContents(View icon) {
4512 if (icon instanceof FolderIcon) {
4513 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4514 } else {
4515 return Collections.EMPTY_LIST;
4516 }
4517 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004518
4519 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4520
4521 @Override
4522 public void onSharedPreferenceChanged(
4523 SharedPreferences sharedPreferences, String key) {
4524 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4525 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4526 if (!waitUntilResume(this, true)) {
4527 run();
4528 }
4529 }
4530 }
4531
4532 @Override
4533 public void run() {
4534 setOrientation();
4535 }
4536 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004537}