blob: ca31ea54d529261bf7a547ec18f728537a6310d9 [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;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070094import com.android.launcher3.allapps.AllAppsContainerView;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080095import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -070096import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010097import com.android.launcher3.compat.LauncherActivityInfoCompat;
98import com.android.launcher3.compat.LauncherAppsCompat;
99import com.android.launcher3.compat.UserHandleCompat;
100import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800101import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700102import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700103import com.android.launcher3.dragndrop.DragController;
104import com.android.launcher3.dragndrop.DragLayer;
105import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -0700106import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000107import com.android.launcher3.folder.Folder;
108import com.android.launcher3.folder.FolderIcon;
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800109import com.android.launcher3.logging.LoggerUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700110import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700111import com.android.launcher3.model.WidgetsModel;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700112import com.android.launcher3.pageindicators.PageIndicatorLine;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800113import com.android.launcher3.userevent.nano.LauncherLogProto;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700114import com.android.launcher3.util.ComponentKey;
Sunny Goyal713edfc2016-05-06 09:58:34 -0700115import com.android.launcher3.logging.FileLog;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700116import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal322d5562015-06-25 19:35:49 -0700117import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700118import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700119import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700120import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700121import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700122import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700123
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700124import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700125import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700126import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700127import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800128import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700129import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700130import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700131import java.util.List;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800132import java.util.Locale;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700133
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134/**
135 * Default launcher application.
136 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100137public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700138 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
139 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700140 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700141 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700142 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
Daniel Sandlerf061f822013-06-27 13:59:36 -0400144 static final boolean PROFILE_STARTUP = false;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700145 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700146 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400147 static final boolean DEBUG_RESUME_TIME = false;
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700148 static final boolean DEBUG_LOGGING = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700149
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700151 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700152 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700153 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Michael Jurka8b805b12012-04-18 14:23:14 -0700155 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700156 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700157 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700158
Sunny Goyal28c6b962015-10-12 11:42:05 -0700159 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
160
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700161 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
162 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
163 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
164
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700165 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
166
Mathew Inwood876a8462013-06-14 14:12:41 +0100167 /**
168 * IntentStarter uses request codes starting with this. This must be greater than all activity
169 * request codes used internally.
170 */
171 protected static final int REQUEST_LAST = 100;
172
Michael Jurka0a457bf2012-11-19 14:05:05 -0800173 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700174 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800175 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
Winson Chung2672ff92012-05-04 16:22:30 -0700177 // The Intent extra that defines whether to ignore the launch animation
178 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400179 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700180
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700181 public static final String ACTION_APPWIDGET_HOST_RESET =
182 "com.android.launcher3.intent.ACTION_APPWIDGET_HOST_RESET";
183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 // Type: int
185 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700186 // Type: int
187 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700188 // Type: Content Values / parcelable
189 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700190 // Type: parcelable
191 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000192 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800193 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194
Adam Cohen432609a2014-03-13 17:03:22 -0700195 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800196 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
197
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700198 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700199 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
200 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700201
Adam Cohen091440a2015-03-18 14:16:05 -0700202 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700203 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700204
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700205 private boolean mIsSafeModeEnabled;
206
Joe Onorato9c1289c2009-08-17 11:03:03 -0400207 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800208 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700209 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700210 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211
Winson Chunga2413752012-04-03 14:22:34 -0700212 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700213 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
214 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700215 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700216
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700217 private final BroadcastReceiver mUiBroadcastReceiver = new BroadcastReceiver() {
218
219 @Override
220 public void onReceive(Context context, Intent intent) {
221 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
222 closeSystemDialogs();
223 } else if (ACTION_APPWIDGET_HOST_RESET.equals(intent.getAction())) {
224 if (mAppWidgetHost != null) {
225 mAppWidgetHost.startListening();
226 }
227 }
228 }
229 };
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800230
Adam Cohen091440a2015-03-18 14:16:05 -0700231 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700232 private View mLauncherView;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700233 private PageIndicatorLine mPageIndicator;
Adam Cohen091440a2015-03-18 14:16:05 -0700234 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800235 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700236
237 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700238
Sunny Goyalffe83f12014-08-14 17:39:34 -0700239 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700240 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700241
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700242 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800243 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800244 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700245
Michael Jurka0280c3b2010-09-17 15:00:07 -0700246 private int[] mTmpAddItemCellCoordinates = new int[2];
247
Sunny Goyal316490e2015-06-02 09:38:28 -0700248 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800249 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700250
Michael Jurka838a4ca2011-02-07 13:33:06 -0800251 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700252 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700253
Winson Chungc51db6a2011-10-05 11:44:49 -0700254 private SearchDropTargetBar mSearchDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -0700255 private AppInfoDropTargetBar mAppInfoDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700256
257 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700258 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700259
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700260 // Main container view and the model for the widget tray screen.
261 @Thunk WidgetsContainerView mWidgetsView;
262 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700263
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700265 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
266 // scroll issues (because the workspace may not have been measured yet) and extra work.
267 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
268 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269
270 private SpannableStringBuilder mDefaultKeySsb = null;
271
Adam Cohen091440a2015-03-18 14:16:05 -0700272 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400273
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800274 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 private boolean mRestoring;
276 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700277 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278
Michael Jurka1e2f4652013-07-08 18:03:46 -0700279 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700280 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700281 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700282
Joe Onorato9c1289c2009-08-17 11:03:03 -0400283 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800284 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700285 private ExtractedColors mExtractedColors;
Adam Cohen091440a2015-03-18 14:16:05 -0700286 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800287 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700288 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800289 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400290
Sunny Goyal639e9062015-08-19 19:17:06 -0700291 private LauncherClings mClings;
292
Adam Cohen61f560d2013-09-30 15:58:20 -0700293 private View.OnTouchListener mHapticFeedbackTouchListener;
294
Adam Cohended9f8d2010-11-03 13:25:16 -0700295 // Related to the auto-advancing of widgets
296 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700297 private static final int ADVANCE_INTERVAL = 20000;
298 private static final int ADVANCE_STAGGER = 250;
299
300 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700301 private long mAutoAdvanceSentTime;
302 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700303 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700304
Winson Chung400438b2011-01-16 17:53:48 -0800305 // Determines how long to wait after a rotation before restoring the screen orientation to
306 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700307 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800308
Adam Cohen091440a2015-03-18 14:16:05 -0700309 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700310
Adam Cohen1462de32012-07-24 22:34:36 -0700311 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700312 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700313
Winson Chung46353de2012-02-16 14:05:10 -0800314 // We only want to get the SharedPreferences once since it does an FS stat each time we get
315 // it from the context.
316 private SharedPreferences mSharedPrefs;
317
Winson Chungf0c6ae02012-03-21 16:10:31 -0700318 // Holds the page that we need to animate to, and the icon views that we need to animate up
319 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700320 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700321 private Bitmap mFolderIconBitmap;
322 private Canvas mFolderIconCanvas;
323 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700324
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700325 private DeviceProfile mDeviceProfile;
326
Adam Cohen4b66bf32015-09-18 12:15:19 -0700327 private boolean mMoveToDefaultScreenFromNewIntent;
328
Winson Chung13eb5272015-05-11 16:30:13 -0700329 // This is set to the view that launched the activity that navigated the user away from
330 // launcher. Since there is no callback for when the activity has finished launching, enable
331 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800332 private BubbleTextView mWaitingForResume;
333
Adam Cohen59400422014-03-05 18:07:04 -0800334 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
335 new HashMap<String, CustomAppWidget>();
336
Adam Cohen59400422014-03-05 18:07:04 -0800337 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700338 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
339 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800340 }
341 }
342
Adam Cohen091440a2015-03-18 14:16:05 -0700343 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700344 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700345 if (mWorkspace != null) {
346 mWorkspace.buildPageHardwareLayers();
347 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700348 }
349 };
350
Adam Cohendb364c32014-05-20 14:23:40 -0700351 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800352
Adam Cohen091440a2015-03-18 14:16:05 -0700353 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800354 int requestCode;
355 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700356 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700357 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800358 int cellX;
359 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700360 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800361 }
362
Hyunyoung Songaa953652016-04-19 18:30:24 -0700363 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800364
Adam Cohenf9c184a2016-01-15 16:47:43 -0800365 public FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700366 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400367
368 @Thunk void setOrientation() {
369 if (mRotationEnabled) {
370 unlockScreenOrientation(true);
371 } else {
372 setRequestedOrientation(
373 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700374 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400375 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700376
Sunny Goyal745bad92016-05-02 10:54:12 -0700377 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700378
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 @Override
380 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700381 if (DEBUG_STRICT_MODE) {
382 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
383 .detectDiskReads()
384 .detectDiskWrites()
385 .detectNetwork() // or .detectAll() for all detectable problems
386 .penaltyLog()
387 .build());
388 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
389 .detectLeakedSqlLiteObjects()
390 .detectLeakedClosableObjects()
391 .penaltyLog()
392 .penaltyDeath()
393 .build());
394 }
395
Adam Cohen9211d422014-10-07 18:14:53 -0700396 if (mLauncherCallbacks != null) {
397 mLauncherCallbacks.preOnCreate();
398 }
399
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400401
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400402 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700403
Adam Cohen2e6da152015-05-06 11:42:25 -0700404 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700405 mDeviceProfile = getResources().getConfiguration().orientation
406 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700407 app.getInvariantDeviceProfile().landscapeProfile
408 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700409
Sunny Goyalf7258242015-10-19 16:59:07 -0700410 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700411 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800412 mModel = app.setLauncher(this);
413 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700414
Joe Onorato41a12d22009-10-31 18:30:00 -0400415 mDragController = new DragController(this);
Winson Chung006ee262015-08-03 14:40:11 -0700416 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700417
Sunny Goyalffe83f12014-08-14 17:39:34 -0700418 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700419
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700420 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
421 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700422
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700423 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
424 // this also ensures that any synchronous binding below doesn't re-trigger another
425 // LauncherModel load.
426 mPaused = false;
427
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200429 android.os.Debug.startMethodTracing(
430 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 }
432
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700434
Tony Wickhameef44322015-10-20 13:24:36 -0700435 app.getInvariantDeviceProfile().landscapeProfile.setSearchBarHeight(getSearchBarHeight());
436 app.getInvariantDeviceProfile().portraitProfile.setSearchBarHeight(getSearchBarHeight());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700438 mDeviceProfile.layout(this);
Tony Wickham827cef22016-03-17 15:39:39 -0700439 mExtractedColors = new ExtractedColors();
440 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441
Tony Wickhame2217252016-03-22 16:34:23 -0700442 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
443 .addAccessibilityStateChangeListener(this);
444
Joe Onorato7c312c12009-08-13 21:36:53 -0700445 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700446
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 mSavedState = savedInstanceState;
448 restoreState(mSavedState);
449
450 if (PROFILE_STARTUP) {
451 android.os.Debug.stopMethodTracing();
452 }
453
454 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700455 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700456 // If the user leaves launcher, then we should just load items asynchronously when
457 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700458 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700459 } else {
460 // We only load the page synchronously if the user rotates (or triggers a
461 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700462 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700463 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464 }
465
466 // For handling default keys
467 mDefaultKeySsb = new SpannableStringBuilder();
468 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800469
470 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700471 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
472 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800473
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800474 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700475 // In case we are on a device with locked rotation, we should look at preferences to check
476 // if the user has specifically allowed rotation.
477 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700478 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700479 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
480 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700481 }
482
483 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
484 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400485 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700486
Adam Cohen9211d422014-10-07 18:14:53 -0700487 if (mLauncherCallbacks != null) {
488 mLauncherCallbacks.onCreate(savedInstanceState);
489 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700490
491 if (shouldShowIntroScreen()) {
492 showIntroScreen();
493 } else {
494 showFirstRunActivity();
495 showFirstRunClings();
496 }
Adam Cohen9211d422014-10-07 18:14:53 -0700497 }
498
Sunny Goyal7779d622015-06-11 16:18:39 -0700499 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700500 public void onExtractedColorsChanged() {
501 loadExtractedColorsAndColorItems();
502 }
503
504 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700505 // TODO: do this in pre-N as well, once the extraction part is complete.
506 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700507 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700508 mHotseat.updateColor(mExtractedColors, !mPaused);
Tony Wickham770217c2016-05-18 15:16:40 -0700509 if (mPageIndicator != null) {
510 mPageIndicator.updateColor(mExtractedColors);
511 }
Tony Wickham827cef22016-03-17 15:39:39 -0700512 }
513 }
514
Adam Cohen9211d422014-10-07 18:14:53 -0700515 private LauncherCallbacks mLauncherCallbacks;
516
517 public void onPostCreate(Bundle savedInstanceState) {
518 super.onPostCreate(savedInstanceState);
519 if (mLauncherCallbacks != null) {
520 mLauncherCallbacks.onPostCreate(savedInstanceState);
521 }
522 }
523
Sunny Goyal32554d12015-12-03 15:31:25 -0800524 /**
525 * Call this after onCreate to set or clear overlay.
526 */
527 public void setLauncherOverlay(LauncherOverlay overlay) {
528 if (overlay != null) {
529 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
530 }
531 mWorkspace.setLauncherOverlay(overlay);
532 }
533
Adam Cohen9211d422014-10-07 18:14:53 -0700534 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
535 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700536 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700537 private boolean mWorkspaceImportanceStored = false;
538 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700539 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800540 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700541 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
542
543 @Override
544 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700545 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700546 return;
547 }
548 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700549 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700550 if (mWorkspace != null) {
551 mWorkspaceImportanceForAccessibility =
552 mWorkspace.getImportantForAccessibility();
553 mWorkspace.setImportantForAccessibility(
554 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
555 mWorkspaceImportanceStored = true;
556 }
557 if (mHotseat != null) {
558 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
559 mHotseat.setImportantForAccessibility(
560 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
561 mHotseatImportanceStored = true;
562 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700563 }
564
565 @Override
566 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700567 if (mWorkspaceImportanceStored && mWorkspace != null) {
568 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
569 }
570 if (mHotseatImportanceStored && mHotseat != null) {
571 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
572 }
573 mWorkspaceImportanceStored = false;
574 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700575 }
576 });
Adam Cohen9211d422014-10-07 18:14:53 -0700577 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700578 }
579
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700580 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700581 public void onLauncherProviderChange() {
582 if (mLauncherCallbacks != null) {
583 mLauncherCallbacks.onLauncherProviderChange();
584 }
585 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700586
Adam Cohen9211d422014-10-07 18:14:53 -0700587 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700588 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700589 if (mLauncherCallbacks != null) {
590 return mLauncherCallbacks.hasCustomContentToLeft();
591 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700592 return false;
593 }
594
Dave Hawkeya8881582013-09-17 15:55:33 -0600595 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500596 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600597 * {@link #addToCustomContentPage}. This will only be invoked if
598 * {@link #hasCustomContentToLeft()} is {@code true}.
599 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500600 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700601 if (mLauncherCallbacks != null) {
602 mLauncherCallbacks.populateCustomContentContainer();
603 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600604 }
605
606 /**
607 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
608 * ensure the custom content page is added or removed if necessary.
609 */
610 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600611 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600612 // Not bound yet, wait for bindScreens to be called.
613 return;
614 }
615
616 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
617 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500618 mWorkspace.createCustomContentContainer();
619 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600620 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
621 mWorkspace.removeCustomContentPage();
622 }
623 }
624
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800625 /**
626 * Logger object is a singleton and does not have to be coupled with the foreground activity.
627 * Since most user event logging is done on the UI, the object is retrieved from the
628 * callback for convenience.
629 */
Hyunyoung Songaa953652016-04-19 18:30:24 -0700630 private UserEventDispatcher createUserEventDispatcher() {
631 return new UserEventDispatcher() {
632 @Override
633 public void dispatchUserEvent(LauncherLogProto.LauncherEvent ev, Intent intent) {
634 if (!DEBUG_LOGGING) {
635 return;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800636 }
Hyunyoung Songaa953652016-04-19 18:30:24 -0700637 Log.d("UserEvent", String.format(Locale.US,
638 "action:%s\nchild:%s\nparent:%s\nelapsed container %d ms session %d ms",
639 LoggerUtils.getActionStr(ev.action),
640 LoggerUtils.getTargetStr(ev.srcTarget[0]),
641 LoggerUtils.getTargetStr(ev.srcTarget[1]),
642 ev.elapsedContainerMillis,
643 ev.elapsedSessionMillis));
644 }
645 };
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800646 }
647
Hyunyoung Songaa953652016-04-19 18:30:24 -0700648 public UserEventDispatcher getUserEventDispatcher() {
649 if (mLauncherCallbacks != null) {
650 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
651 if (dispatcher != null) {
652 return dispatcher;
653 }
654 }
655
656 if (mUserEventDispatcher == null) {
657 mUserEventDispatcher = createUserEventDispatcher();
658 }
659 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800660 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100661
Hyunyoung Song3f471442015-04-08 19:01:34 -0700662 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700663 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
664 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700665 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700666 }
667
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000668 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700669 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
670 // This cast is safe as long as the id < 0x00FFFFFF
671 // Since we jail all the dynamically generated views, there should be no clashes
672 // with any other views.
673 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000674 }
675
676 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700677 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
678 * a configuration step, this allows the proper animations to run after other transitions.
679 */
Adam Cohendb364c32014-05-20 14:23:40 -0700680 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700681 long screenId = args.screenId;
682 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
683 // When the screen id represents an actual screen (as opposed to a rank) we make sure
684 // that the drop page actually exists.
685 screenId = ensurePendingDropLayoutExists(args.screenId);
686 }
Adam Cohendb364c32014-05-20 14:23:40 -0700687
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800688 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800689 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700690 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700691 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700692 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800693 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700694 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700695 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700696 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyald478c832016-04-01 12:04:16 -0700697 completeRestoreAppWidget(args.appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700698 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700699 case REQUEST_BIND_PENDING_APPWIDGET: {
700 int widgetId = args.appWidgetId;
701 LauncherAppWidgetInfo info =
702 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
703 if (info != null) {
704 // Since the view was just bound, also launch the configure activity if needed
705 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
706 .getLauncherAppWidgetInfo(widgetId);
707 if (provider != null && provider.configure != null) {
708 startRestoredWidgetReconfigActivity(provider, info);
709 }
710 }
711 break;
712 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800713 }
Michael Jurka27614d22012-04-02 06:40:22 -0700714 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
715 // if you turned the screen off and then back while in All Apps, Launcher would not
716 // return to the workspace. Clearing mAddInfo.container here fixes this issue
717 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700718 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800719 }
720
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800721 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700722 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700723 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700724 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700725 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800726 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700727
Adam Cohenad4e15c2013-10-17 16:21:35 -0700728 Runnable exitSpringLoaded = new Runnable() {
729 @Override
730 public void run() {
731 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
732 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
733 }
734 };
735
Michael Jurka8b805b12012-04-18 14:23:14 -0700736 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700737 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700738 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700739 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
740 if (resultCode == RESULT_CANCELED) {
741 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700742 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700743 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700744 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800745 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700746 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700747 }
748 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200749 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
750 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700751 // User could have free-scrolled between pages before picking a wallpaper; make sure
752 // we move to the closest one now.
753 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700754 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200755 }
756 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700757 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200758
Adam Cohened66b2b2012-01-23 17:28:51 -0800759 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700760 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700761
Adam Cohendb364c32014-05-20 14:23:40 -0700762 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800763 // We have special handling for widgets
764 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800765 final int appWidgetId;
766 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
767 : -1;
768 if (widgetId < 0) {
769 appWidgetId = pendingAddWidgetId;
770 } else {
771 appWidgetId = widgetId;
772 }
773
Adam Cohenad4e15c2013-10-17 16:21:35 -0700774 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800775 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700776 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
777 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700778 result = RESULT_CANCELED;
779 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700780 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700781 @Override
782 public void run() {
783 exitSpringLoadedDragModeDelayed(false, 0, null);
784 }
785 };
Adam Cohendb364c32014-05-20 14:23:40 -0700786 if (workspaceLocked) {
787 // No need to remove the empty screen if we're mid-binding, as the
788 // the bind will not add the empty screen.
789 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
790 } else {
791 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
792 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
793 }
Winson Chung5aaab772012-04-27 15:24:02 -0700794 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700795 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700796 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
797 // When the screen id represents an actual screen (as opposed to a rank)
798 // we make sure that the drop page actually exists.
799 mPendingAddInfo.screenId =
800 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
801 }
Adam Cohendb364c32014-05-20 14:23:40 -0700802 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
803
804 dropLayout.setDropPending(true);
805 final Runnable onComplete = new Runnable() {
806 @Override
807 public void run() {
808 completeTwoStageWidgetDrop(resultCode, appWidgetId);
809 dropLayout.setDropPending(false);
810 }
811 };
812 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
813 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
814 } else {
815 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
816 mPendingAddInfo);
817 sPendingAddItem = args;
818 }
Winson Chung5aaab772012-04-27 15:24:02 -0700819 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800820 return;
821 }
822
Sunny Goyald478c832016-04-01 12:04:16 -0700823 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
824 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700825 if (resultCode == RESULT_OK) {
826 // Update the widget view.
827 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
828 pendingAddWidgetId, mPendingAddInfo);
829 if (workspaceLocked) {
830 sPendingAddItem = args;
831 } else {
832 completeAdd(args);
833 }
834 }
835 // Leave the widget in the pending state if the user canceled the configure.
836 return;
837 }
838
Sunny Goyalaad90582015-07-09 14:22:50 -0700839 if (requestCode == REQUEST_CREATE_SHORTCUT) {
840 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
841 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
842 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
843 mPendingAddInfo);
844 if (isWorkspaceLocked()) {
845 sPendingAddItem = args;
846 } else {
847 completeAdd(args);
848 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
849 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
850 }
851 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700852 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
853 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800856 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800857
858 }
859
860 @Override
861 protected void onActivityResult(
862 final int requestCode, final int resultCode, final Intent data) {
863 handleActivityResult(requestCode, resultCode, data);
864 if (mLauncherCallbacks != null) {
865 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
866 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800867 }
868
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600869 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700870 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600871 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700872 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
873 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
874 View v = null;
875 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
876 if (layout != null) {
877 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
878 }
879 Intent intent = sPendingAddItem.intent;
880 sPendingAddItem = null;
881 if (grantResults.length > 0
882 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700883 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700884 } else {
885 // TODO: Show a snack bar with link to settings
886 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
887 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
888 }
889 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600890 if (mLauncherCallbacks != null) {
891 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
892 grantResults);
893 }
894 }
895
Adam Cohendb364c32014-05-20 14:23:40 -0700896 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
897 appWidgetId, ItemInfo info) {
898 PendingAddArguments args = new PendingAddArguments();
899 args.requestCode = requestCode;
900 args.intent = data;
901 args.container = info.container;
902 args.screenId = info.screenId;
903 args.cellX = info.cellX;
904 args.cellY = info.cellY;
905 args.appWidgetId = appWidgetId;
906 return args;
907 }
908
909 /**
910 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
911 *
912 * @param screenId the screen id to check
913 * @return the new screen, or screenId if it exists
914 */
915 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800916 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700917 if (dropLayout == null) {
918 // it's possible that the add screen was removed because it was
919 // empty and a re-bind occurred
920 mWorkspace.addExtraEmptyScreen();
921 return mWorkspace.commitExtraEmptyScreen();
922 } else {
923 return screenId;
924 }
925 }
926
Adam Cohen091440a2015-03-18 14:16:05 -0700927 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800928 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800929 Runnable onCompleteRunnable = null;
930 int animationType = 0;
931
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700932 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800933 if (resultCode == RESULT_OK) {
934 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
935 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700936 mPendingAddWidgetInfo);
937 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800938 onCompleteRunnable = new Runnable() {
939 @Override
940 public void run() {
941 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700942 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700943 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
944 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800945 }
946 };
947 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800948 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800949 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800950 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700951 if (mDragLayer.getAnimatedView() != null) {
952 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
953 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
954 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700955 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700956 // The animated view may be null in the case of a rotation during widget configuration
957 onCompleteRunnable.run();
958 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 }
960
961 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700962 protected void onStop() {
963 super.onStop();
964 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700965
966 if (mLauncherCallbacks != null) {
967 mLauncherCallbacks.onStop();
968 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700969 }
970
971 @Override
972 protected void onStart() {
973 super.onStart();
974 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700975
976 if (mLauncherCallbacks != null) {
977 mLauncherCallbacks.onStart();
978 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700979 }
980
981 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200983 long startTime = 0;
984 if (DEBUG_RESUME_TIME) {
985 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400986 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200987 }
Adam Cohen9211d422014-10-07 18:14:53 -0700988
989 if (mLauncherCallbacks != null) {
990 mLauncherCallbacks.preOnResume();
991 }
992
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700994 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700995
Winson Chung4a2afa32012-07-19 14:53:05 -0700996 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700997 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700998 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800999 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -07001000 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -07001001 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -07001002 // view after launching an app, as they may be depending on the UI to be static to
1003 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -07001004 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07001005 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08001006 } else if (mOnResumeState == State.WIDGETS) {
1007 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001008 }
1009 mOnResumeState = State.NONE;
1010
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001011 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001012 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1013 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001014
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001015 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001016 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001017 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001018 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001019 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001020 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001021 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001022 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001023 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1024 // execute them here
1025 long startTimeCallbacks = 0;
1026 if (DEBUG_RESUME_TIME) {
1027 startTimeCallbacks = System.currentTimeMillis();
1028 }
1029
Michael Jurka1e2f4652013-07-08 18:03:46 -07001030 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1031 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001032 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001033 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001034 if (DEBUG_RESUME_TIME) {
1035 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1036 (System.currentTimeMillis() - startTimeCallbacks));
1037 }
Michael Jurka447bf842013-05-15 14:52:15 +02001038 }
Michael Jurka54554252013-08-01 12:52:23 +02001039 if (mOnResumeCallbacks.size() > 0) {
1040 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1041 mOnResumeCallbacks.get(i).run();
1042 }
1043 mOnResumeCallbacks.clear();
1044 }
Winson Chunge4e50662012-01-23 14:45:13 -08001045
1046 // Reset the pressed state of icons that were locked in the press state while activities
1047 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001048 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001049 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001050 mWaitingForResume.setStayPressed(false);
1051 }
Winson Chung82963d52013-10-09 11:20:57 -07001052
Adam Cohen06dff352012-06-01 17:17:08 -07001053 // It is possible that widgets can receive updates while launcher is not in the foreground.
1054 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1055 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1056 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001057 if (!isWorkspaceLoading()) {
1058 getWorkspace().reinflateWidgetsIfNecessary();
1059 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001060
Michael Jurka447bf842013-05-15 14:52:15 +02001061 if (DEBUG_RESUME_TIME) {
1062 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1063 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001064
Adam Cohen4b66bf32015-09-18 12:15:19 -07001065 // We want to suppress callbacks about CustomContent being shown if we have just received
1066 // onNewIntent while the user was present within launcher. In that case, we post a call
1067 // to move the user to the main screen (which will occur after onResume). We don't want to
1068 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1069 // suppress here.
1070 if (mWorkspace.getCustomContentCallbacks() != null
1071 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001072 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001073 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001074 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1075 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001076 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001077 }
1078 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001079 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001080 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001081 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001082
Sunny Goyal756adbc2015-04-16 15:20:51 -07001083 if (!isWorkspaceLoading()) {
1084 // Process any items that were added while Launcher was away.
1085 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1086 }
Adam Cohen9211d422014-10-07 18:14:53 -07001087
1088 if (mLauncherCallbacks != null) {
1089 mLauncherCallbacks.onResume();
1090 }
Adam Cohen06dff352012-06-01 17:17:08 -07001091 }
1092
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001094 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001095 // Ensure that items added to Launcher are queued until Launcher returns
1096 InstallShortcutReceiver.enableInstallQueue();
1097
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001098 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001099 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001100 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001101 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001102
1103 // We call onHide() aggressively. The custom content callbacks should be able to
1104 // debounce excess onHide calls.
1105 if (mWorkspace.getCustomContentCallbacks() != null) {
1106 mWorkspace.getCustomContentCallbacks().onHide();
1107 }
Romain Guycbb89e42009-06-08 15:52:54 -07001108
Adam Cohen9211d422014-10-07 18:14:53 -07001109 if (mLauncherCallbacks != null) {
1110 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001111 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001112 }
1113
1114 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001115 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1116 // by a onResume or by scrolling otherwise.
1117 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001118
1119 // Custom content is completely hidden
1120 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001121
1122 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1123 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001124
1125 // Indicates whether the user is allowed to scroll away from the custom content.
1126 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001127 }
1128
Adam Cohenc2d6e892014-10-16 09:49:24 -07001129 public interface LauncherOverlay {
1130
1131 /**
1132 * Touch interaction leading to overscroll has begun
1133 */
1134 public void onScrollInteractionBegin();
1135
1136 /**
1137 * Touch interaction related to overscroll has ended
1138 */
1139 public void onScrollInteractionEnd();
1140
1141 /**
1142 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1143 * screen (or in the case of RTL, the rightmost screen).
1144 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001145 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001146
1147 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001148 * Called when the launcher is ready to use the overlay
1149 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001150 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001151 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001152 }
1153
Jun Mukai8af0cd82015-05-12 12:32:12 -07001154 public interface LauncherSearchCallbacks {
1155 /**
1156 * Called when the search overlay is shown.
1157 */
1158 public void onSearchOverlayOpened();
1159
1160 /**
1161 * Called when the search overlay is dismissed.
1162 */
1163 public void onSearchOverlayClosed();
1164 }
1165
Adam Cohenc2d6e892014-10-16 09:49:24 -07001166 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001167 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001168 }
1169
1170 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1171
Sunny Goyalc86df472016-02-25 09:19:38 -08001172 public void onScrollChanged(float progress) {
1173 if (mWorkspace != null) {
1174 mWorkspace.onOverlayScrollChanged(progress);
1175 }
1176 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001177 }
1178
Jorim Jaggid017f882014-01-14 17:08:48 -08001179 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001180 if (mLauncherCallbacks != null) {
1181 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001182 } else {
1183 // On devices with a locked orientation, we will at least have the allow rotation
1184 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001185 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001186 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001187 }
1188
Adam Cohen9211d422014-10-07 18:14:53 -07001189 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001190 CustomContentCallbacks callbacks, String description) {
1191 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001192 }
1193
Adam Cohenedb40762013-07-18 16:45:45 -07001194 // The custom content needs to offset its content to account for the QSB
1195 public int getTopOffsetForCustomContent() {
1196 return mWorkspace.getPaddingTop();
1197 }
1198
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001199 @Override
1200 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001201 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001202 if (mModel.isCurrentCallbacks(this)) {
1203 mModel.stopLoader();
1204 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001205 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1206
Romain Guy13c2e7b2010-03-10 19:45:00 -08001207 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001208 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001209
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001210 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001211 @Override
1212 public void onWindowFocusChanged(boolean hasFocus) {
1213 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001214 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001215
1216 if (mLauncherCallbacks != null) {
1217 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1218 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001219 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001220
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221 private boolean acceptFilter() {
1222 final InputMethodManager inputManager = (InputMethodManager)
1223 getSystemService(Context.INPUT_METHOD_SERVICE);
1224 return !inputManager.isFullscreenMode();
1225 }
1226
1227 @Override
1228 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001229 final int uniChar = event.getUnicodeChar();
1230 final boolean handled = super.onKeyDown(keyCode, event);
1231 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1232 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1234 keyCode, event);
1235 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001236 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001237 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001238 // showSearchDialog()
1239 // If there are multiple keystrokes before the search dialog takes focus,
1240 // onSearchRequested() will be called for every keystroke,
1241 // but it is idempotent, so it's fine.
1242 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 }
1244 }
1245
Joe Onorato8a9625e2010-01-28 15:55:35 -08001246 // Eat the long press event so the keyboard doesn't come up.
1247 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1248 return true;
1249 }
1250
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 return handled;
1252 }
1253
Winson46163472015-09-22 17:31:56 -07001254 @Override
1255 public boolean onKeyUp(int keyCode, KeyEvent event) {
1256 if (keyCode == KeyEvent.KEYCODE_MENU) {
1257 // Ignore the menu key if we are currently dragging or are on the custom content screen
1258 if (!isOnCustomContent() && !mDragController.isDragging()) {
1259 // Close any open folders
1260 closeFolder();
1261
1262 // Stop resizing any widgets
1263 mWorkspace.exitWidgetResizeMode();
1264
1265 // Show the overview mode if we are on the workspace
1266 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1267 !mWorkspace.isSwitchingState()) {
1268 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001269 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001270 }
1271 }
1272 return true;
1273 }
1274 return super.onKeyUp(keyCode, event);
1275 }
1276
Karl Rosaen138a0412009-04-23 19:00:21 -07001277 private String getTypedText() {
1278 return mDefaultKeySsb.toString();
1279 }
1280
1281 private void clearTypedText() {
1282 mDefaultKeySsb.clear();
1283 mDefaultKeySsb.clearSpans();
1284 Selection.setSelection(mDefaultKeySsb, 0);
1285 }
1286
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001288 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1289 * State
1290 */
1291 private static State intToState(int stateOrdinal) {
1292 State state = State.WORKSPACE;
1293 final State[] stateValues = State.values();
1294 for (int i = 0; i < stateValues.length; i++) {
1295 if (stateValues[i].ordinal() == stateOrdinal) {
1296 state = stateValues[i];
1297 break;
1298 }
1299 }
1300 return state;
1301 }
1302
1303 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 * Restores the previous state, if it exists.
1305 *
1306 * @param savedState The previous state.
1307 */
1308 private void restoreState(Bundle savedState) {
1309 if (savedState == null) {
1310 return;
1311 }
1312
Michael Jurka883f55b2010-10-21 15:47:14 -07001313 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001314 if (state == State.APPS || state == State.WIDGETS) {
1315 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001316 }
1317
Adam Cohen21cd0022013-10-09 18:57:02 -07001318 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1319 PagedView.INVALID_RESTORE_PAGE);
1320 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001321 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 }
1323
Sunny Goyal756cd262015-08-20 12:33:21 -07001324 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1325 if (itemValues != null) {
1326 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001327 AppWidgetProviderInfo info = savedState.getParcelable(
1328 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001329 mPendingAddWidgetInfo = info == null ?
1330 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1331
Adam Cohen4637b5a2013-11-04 18:21:24 -08001332 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001333 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 mRestoring = true;
1335 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 }
1337
1338 /**
1339 * Finds all the views we need and configure them properly.
1340 */
1341 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001342 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001343
Craig Mautner360310b2012-10-26 15:13:08 -07001344 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001345 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001346 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1347 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Tony Wickhamf549dab2016-05-16 09:54:06 -07001348 mPageIndicator = (PageIndicatorLine) mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001349
Sunny Goyal784f9c32016-03-23 16:56:54 -07001350 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1351 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1352 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001353 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001354
Winson Chung4c98d922011-05-31 16:50:48 -07001355 // Setup the drag layer
1356 mDragLayer.setup(this, dragController);
1357
Winson Chung3d503fb2011-07-13 17:25:49 -07001358 // Setup the hotseat
1359 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1360 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001361 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001362 }
1363
Winsone9f27272015-10-13 10:47:51 -07001364 // Setup the overview panel
1365 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001366
Winson Chung4c98d922011-05-31 16:50:48 -07001367 // Setup the workspace
1368 mWorkspace.setHapticFeedbackEnabled(false);
1369 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001370 mWorkspace.setup(dragController);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001371 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Michael Jurkad74c9842011-07-10 12:44:21 -07001372 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001373
Tony Wickham34d2c912015-09-11 09:27:58 -07001374 // Get the search/delete/uninstall bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001375 mSearchDropTargetBar = (SearchDropTargetBar)
1376 mDragLayer.findViewById(R.id.search_drop_target_bar);
Tony Wickham34d2c912015-09-11 09:27:58 -07001377 // Get the app info bar
1378 mAppInfoDropTargetBar = (AppInfoDropTargetBar)
1379 mDragLayer.findViewById(R.id.app_info_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001380
Winson Chungef7f8742015-06-04 17:18:17 -07001381 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001382 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001383 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001384 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1385 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1386 } else {
1387 mAppsView.setSearchBarController(new DefaultAppSearchController());
1388 }
Craig Mautner360310b2012-10-26 15:13:08 -07001389
Winson Chung3d503fb2011-07-13 17:25:49 -07001390 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001391 dragController.setDragScoller(mWorkspace);
Tony Wickham0f97b782015-12-02 17:55:07 -08001392 dragController.setScrollView(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -07001393 dragController.setMoveTarget(mWorkspace);
1394 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001395 if (mSearchDropTargetBar != null) {
1396 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001397 }
Tony Wickham34d2c912015-09-11 09:27:58 -07001398 if (mAppInfoDropTargetBar != null) {
1399 mAppInfoDropTargetBar.setup(this, dragController);
1400 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001401
Sunny Goyal322d5562015-06-25 19:35:49 -07001402 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1403 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001404 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 }
1406
Winsone9f27272015-10-13 10:47:51 -07001407 private void setupOverviewPanel() {
1408 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1409
1410 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1411 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1412 @Override
1413 public boolean onLongClick(View v) {
1414 return v.performClick();
1415 }
1416 };
1417
1418 // Bind wallpaper button actions
1419 View wallpaperButton = findViewById(R.id.wallpaper_button);
1420 wallpaperButton.setOnClickListener(new OnClickListener() {
1421 @Override
1422 public void onClick(View view) {
1423 if (!mWorkspace.isSwitchingState()) {
1424 onClickWallpaperPicker(view);
1425 }
1426 }
1427 });
1428 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1429 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1430
1431 // Bind widget button actions
1432 mWidgetsButton = findViewById(R.id.widget_button);
1433 mWidgetsButton.setOnClickListener(new OnClickListener() {
1434 @Override
1435 public void onClick(View view) {
1436 if (!mWorkspace.isSwitchingState()) {
1437 onClickAddWidgetButton(view);
1438 }
1439 }
1440 });
1441 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1442 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1443
1444 // Bind settings actions
1445 View settingsButton = findViewById(R.id.settings_button);
1446 boolean hasSettings = hasSettings();
1447 if (hasSettings) {
1448 settingsButton.setOnClickListener(new OnClickListener() {
1449 @Override
1450 public void onClick(View view) {
1451 if (!mWorkspace.isSwitchingState()) {
1452 onClickSettingsButton(view);
1453 }
1454 }
1455 });
1456 settingsButton.setOnLongClickListener(performClickOnLongClick);
1457 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1458 } else {
1459 settingsButton.setVisibility(View.GONE);
1460 }
1461
1462 mOverviewPanel.setAlpha(0f);
1463 }
1464
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001466 * Sets the all apps button. This method is called from {@link Hotseat}.
1467 */
1468 public void setAllAppsButton(View allAppsButton) {
1469 mAllAppsButton = allAppsButton;
1470 }
1471
1472 public View getAllAppsButton() {
1473 return mAllAppsButton;
1474 }
1475
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001476 public View getWidgetsButton() {
1477 return mWidgetsButton;
1478 }
1479
Anjali Koppal5ad44842014-03-10 20:34:39 -07001480 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 * Creates a view representing a shortcut.
1482 *
1483 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001485 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001486 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 }
1488
1489 /**
1490 * Creates a view representing a shortcut inflated from the specified resource.
1491 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 * @param parent The group the shortcut belongs to.
1493 * @param info The data structure describing the shortcut.
1494 *
1495 * @return A View inflated from layoutResId.
1496 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001497 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001498 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001499 parent, false);
1500 favorite.applyFromShortcutInfo(info, mIconCache);
1501 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001503 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 return favorite;
1505 }
1506
1507 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 * Add a shortcut to the workspace.
1509 *
1510 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001512 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001513 int cellY) {
1514 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001515 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001516
Sunny Goyal5c97f512015-05-19 16:03:28 -07001517 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001518 if (info == null) {
1519 return;
1520 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001521 final View view = createShortcut(info);
1522
Sunny Goyal5c97f512015-05-19 16:03:28 -07001523 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001524 // First we check if we already know the exact location where we want to add this item.
1525 if (cellX >= 0 && cellY >= 0) {
1526 cellXY[0] = cellX;
1527 cellXY[1] = cellY;
1528 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001529
1530 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001531 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001532 true, null,null)) {
1533 return;
1534 }
1535 DragObject dragObject = new DragObject();
1536 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001537 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1538 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001539 return;
1540 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001541 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001542 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001543 }
1544
1545 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001546 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001547 return;
1548 }
1549
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001550 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551
1552 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001553 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001554 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 }
1556 }
1557
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001559 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001561 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 */
Adam Cohen091440a2015-03-18 14:16:05 -07001563 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001564 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1565
1566 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001567 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001568 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001569 }
Romain Guycbb89e42009-06-08 15:52:54 -07001570
Adam Cohen59400422014-03-05 18:07:04 -08001571 if (appWidgetInfo.isCustomWidget) {
1572 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001573 }
1574
Adam Cohen59400422014-03-05 18:07:04 -08001575 LauncherAppWidgetInfo launcherInfo;
1576 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1577 launcherInfo.spanX = info.spanX;
1578 launcherInfo.spanY = info.spanY;
1579 launcherInfo.minSpanX = info.minSpanX;
1580 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001581 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001582
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001584 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585
1586 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001587 if (hostView == null) {
1588 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001589 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001590 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001591 hostView.setVisibility(View.VISIBLE);
1592 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001594 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 }
Romain Guycbb89e42009-06-08 15:52:54 -07001596
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001597 private void addAppWidgetToWorkspace(
1598 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001599 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001600 hostView.setTag(item);
1601 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001602
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001603 hostView.setFocusable(true);
1604 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001605
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001606 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001607 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1608
1609 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001610 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001611 }
1612 }
1613
Adam Cohended9f8d2010-11-03 13:25:16 -07001614 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1615 @Override
1616 public void onReceive(Context context, Intent intent) {
1617 final String action = intent.getAction();
1618 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1619 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001620 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001621 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001622
Winson Chungc100e8e2011-08-09 16:02:43 -07001623 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001624 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001625 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001626 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001627 if (!showWorkspace(false)) {
1628 // If we are already on the workspace, then manually reset all apps
1629 mAppsView.reset();
1630 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001631 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001632 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1633 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001634 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001635 }
1636 }
1637 };
1638
1639 @Override
1640 public void onAttachedToWindow() {
1641 super.onAttachedToWindow();
1642
1643 // Listen for broadcasts related to user-presence
1644 final IntentFilter filter = new IntentFilter();
1645 filter.addAction(Intent.ACTION_SCREEN_OFF);
1646 filter.addAction(Intent.ACTION_USER_PRESENT);
1647 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001648 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001649 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001650 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001651
1652 if (mLauncherCallbacks != null) {
1653 mLauncherCallbacks.onAttachedToWindow();
1654 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001655 }
1656
1657 @Override
1658 public void onDetachedFromWindow() {
1659 super.onDetachedFromWindow();
1660 mVisible = false;
1661
Adam Cohend113e0c2010-11-11 10:48:05 -08001662 if (mAttached) {
1663 unregisterReceiver(mReceiver);
1664 mAttached = false;
1665 }
Winson Chungb745afb2015-03-02 11:51:23 -08001666 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001667
1668 if (mLauncherCallbacks != null) {
1669 mLauncherCallbacks.onDetachedFromWindow();
1670 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001671 }
1672
1673 public void onWindowVisibilityChanged(int visibility) {
1674 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001675 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001676 // The following code used to be in onResume, but it turns out onResume is called when
1677 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1678 // is a more appropriate event to handle
1679 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001680 if (!mWorkspaceLoading) {
1681 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001682 // We want to let Launcher draw itself at least once before we force it to build
1683 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001684 // apps is nice and speedy.
1685 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001686 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001687 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001688 if (mStarted) return;
1689 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001690 // We delay the layer building a bit in order to give
1691 // other message processing a time to run. In particular
1692 // this avoids a delay in hiding the IME if it was
1693 // currently shown, because doing that may involve
1694 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001695 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001696 final ViewTreeObserver.OnDrawListener listener = this;
1697 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001698 public void run() {
1699 if (mWorkspace != null &&
1700 mWorkspace.getViewTreeObserver() != null) {
1701 mWorkspace.getViewTreeObserver().
1702 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001703 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001704 }
1705 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001706 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001707 }
1708 });
1709 }
1710 clearTypedText();
1711 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001712 }
1713
Adam Cohen091440a2015-03-18 14:16:05 -07001714 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001715 mHandler.removeMessages(ADVANCE_MSG);
1716 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1717 mHandler.sendMessageDelayed(msg, delay);
1718 mAutoAdvanceSentTime = System.currentTimeMillis();
1719 }
1720
Adam Cohen091440a2015-03-18 14:16:05 -07001721 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001722 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1723 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1724 mAutoAdvanceRunning = autoAdvanceRunning;
1725 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001726 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001727 sendAdvanceMessage(delay);
1728 } else {
1729 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001730 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001731 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1732 }
1733 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001734 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001735 }
1736 }
1737 }
1738
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001739 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1740
Adam Cohended9f8d2010-11-03 13:25:16 -07001741 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001742 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001743 if (msg.what == ADVANCE_MSG) {
1744 int i = 0;
1745 for (View key: mWidgetsToAdvance.keySet()) {
1746 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001747 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001748 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001749 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001750 public void run() {
1751 ((Advanceable) v).advance();
1752 }
1753 }, delay);
1754 }
1755 i++;
1756 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001757 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001758 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001759 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001760 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001761 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001762
Winsonc0b52fe2015-09-09 16:38:15 -07001763 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001764 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001765 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1766 if (v instanceof Advanceable) {
1767 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001768 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001769 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001770 }
1771 }
1772
Winsonc0b52fe2015-09-09 16:38:15 -07001773 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001774 if (mWidgetsToAdvance.containsKey(hostView)) {
1775 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001776 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001777 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001778 }
1779
Hyunyoung Song3f471442015-04-08 19:01:34 -07001780 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001781 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1782 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001783 }
1784
Winson Chunga6945242014-01-08 14:04:34 -08001785 public DragLayer getDragLayer() {
1786 return mDragLayer;
1787 }
1788
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001789 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001790 return mAppsView;
1791 }
1792
Hyunyoung Song3f471442015-04-08 19:01:34 -07001793 public WidgetsContainerView getWidgetsView() {
1794 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001795 }
1796
Winson Chunga6945242014-01-08 14:04:34 -08001797 public Workspace getWorkspace() {
1798 return mWorkspace;
1799 }
1800
1801 public Hotseat getHotseat() {
1802 return mHotseat;
1803 }
1804
Jorim Jaggid017f882014-01-14 17:08:48 -08001805 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001806 return mOverviewPanel;
1807 }
1808
Winson Chung006ee262015-08-03 14:40:11 -07001809 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001810 return mSearchDropTargetBar;
1811 }
1812
Tony Wickham34d2c912015-09-11 09:27:58 -07001813 public AppInfoDropTargetBar getAppInfoDropTargetBar() {
1814 return mAppInfoDropTargetBar;
1815 }
1816
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001817 public LauncherAppWidgetHost getAppWidgetHost() {
1818 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001819 }
Romain Guycbb89e42009-06-08 15:52:54 -07001820
Winson Chunga9abd0e2010-10-27 17:18:37 -07001821 public LauncherModel getModel() {
1822 return mModel;
1823 }
1824
Adam Cohen79d90c52016-04-22 13:29:20 -07001825 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001826 return mSharedPrefs;
1827 }
1828
Adam Cohen2e6da152015-05-06 11:42:25 -07001829 public DeviceProfile getDeviceProfile() {
1830 return mDeviceProfile;
1831 }
1832
Bjorn Bringertc459e522013-06-07 19:36:01 +01001833 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001834 getWindow().closeAllPanels();
1835
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001836 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001837 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001838 }
1839
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840 @Override
1841 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001842 long startTime = 0;
1843 if (DEBUG_RESUME_TIME) {
1844 startTime = System.currentTimeMillis();
1845 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001846 super.onNewIntent(intent);
1847
1848 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001849 Folder openFolder = mWorkspace.getOpenFolder();
1850 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1851 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1852 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1853 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1854 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001855 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001856 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001857
Adam Cohen6fecd412013-10-02 17:41:50 -07001858 if (mWorkspace == null) {
1859 // Can be cases where mWorkspace is null, this prevents a NPE
1860 return;
1861 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001862 // In all these cases, only animate if we're already on home
1863 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001864
Sunny Goyal935fca12015-10-13 10:19:01 -07001865 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001866 exitSpringLoadedDragMode();
1867
1868 // If we are already on home, then just animate back to the workspace,
1869 // otherwise, just wait until onResume to set the state back to Workspace
1870 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001871 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001872 } else {
1873 mOnResumeState = State.WORKSPACE;
1874 }
1875
1876 final View v = getWindow().peekDecorView();
1877 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001878 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001879 INPUT_METHOD_SERVICE);
1880 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1881 }
1882
Winson Chungb745afb2015-03-02 11:51:23 -08001883 // Reset the apps view
1884 if (!alreadyOnHome && mAppsView != null) {
1885 mAppsView.scrollToTop();
1886 }
1887
Hyunyoung Song3f471442015-04-08 19:01:34 -07001888 // Reset the widgets view
1889 if (!alreadyOnHome && mWidgetsView != null) {
1890 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001891 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001892
Adam Cohen9211d422014-10-07 18:14:53 -07001893 if (mLauncherCallbacks != null) {
1894 mLauncherCallbacks.onHomeIntent();
1895 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 }
Adam Cohened307df2013-10-02 09:37:31 -07001897
Adam Cohen9211d422014-10-07 18:14:53 -07001898 if (mLauncherCallbacks != null) {
1899 mLauncherCallbacks.onNewIntent(intent);
1900 }
Winson15f8b172015-08-19 11:02:39 -07001901
1902 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1903 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1904 // animation.
1905 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001906 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1907 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001908 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1909 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001910
1911 // We use this flag to suppress noisy callbacks above custom content state
1912 // from onResume.
1913 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001914 mWorkspace.post(new Runnable() {
1915 @Override
1916 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001917 if (mWorkspace != null) {
1918 mWorkspace.moveToDefaultScreen(true);
1919 }
Winson15f8b172015-08-19 11:02:39 -07001920 }
1921 });
1922 }
1923 }
1924
1925 if (DEBUG_RESUME_TIME) {
1926 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1927 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001928 }
1929
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001931 public void onRestoreInstanceState(Bundle state) {
1932 super.onRestoreInstanceState(state);
1933 for (int page: mSynchronouslyBoundPages) {
1934 mWorkspace.restoreInstanceStateForChild(page);
1935 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 }
1937
1938 @Override
1939 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001940 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001941 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1942 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001943 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001944 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945
Michael Jurka883f55b2010-10-21 15:47:14 -07001946 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001947 // We close any open folder since it will not be re-opened, and we need to make sure
1948 // this state is reflected.
1949 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1950 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951
Adam Cohendcd297f2013-06-18 13:13:40 -07001952 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001953 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001954 ContentValues itemValues = new ContentValues();
1955 mPendingAddInfo.writeToValues(itemValues);
1956 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001957 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001958 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 }
1960
Adam Cohen9211d422014-10-07 18:14:53 -07001961 if (mLauncherCallbacks != null) {
1962 mLauncherCallbacks.onSaveInstanceState(outState);
1963 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 }
1965
1966 @Override
1967 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001969
Winson Chunge7a03942011-08-05 15:05:12 -07001970 // Remove all pending runnables
1971 mHandler.removeMessages(ADVANCE_MSG);
1972 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001973 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001974 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001975
Winson Chungcd2b0142011-06-08 16:02:26 -07001976 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001977 // It's possible to receive onDestroy after a new Launcher activity has
1978 // been created. In this case, don't interfere with the new Launcher.
1979 if (mModel.isCurrentCallbacks(this)) {
1980 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001981 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001982 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001983
Sunny Goyal745bad92016-05-02 10:54:12 -07001984 if (mRotationPrefChangeHandler != null) {
1985 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1986 }
1987
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001989 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001991 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001993 mAppWidgetHost = null;
1994
1995 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996
1997 TextKeyListener.getInstance().release();
1998
Tony Wickhame2217252016-03-22 16:34:23 -07001999 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
2000 .removeAccessibilityStateChangeListener(this);
2001
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002002 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002003
Michael Jurka2ecf9952012-06-18 12:52:28 -07002004 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002005
2006 if (mLauncherCallbacks != null) {
2007 mLauncherCallbacks.onDestroy();
2008 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 }
2010
Adam Cohena9cf38f2011-05-02 15:36:58 -07002011 public DragController getDragController() {
2012 return mDragController;
2013 }
2014
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 @Override
2016 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002017 onStartForResult(requestCode);
2018 super.startActivityForResult(intent, requestCode);
2019 }
2020
2021 @Override
2022 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2023 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2024 onStartForResult(requestCode);
2025 try {
2026 super.startIntentSenderForResult(intent, requestCode,
2027 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2028 } catch (IntentSender.SendIntentException e) {
2029 throw new ActivityNotFoundException();
2030 }
2031 }
2032
2033 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002034 if (requestCode >= 0) {
2035 setWaitingForResult(true);
2036 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037 }
2038
Winson Chung70d72102011-08-12 11:24:35 -07002039 /**
2040 * Indicates that we want global search for this activity by setting the globalSearch
2041 * argument for {@link #startSearch} to true.
2042 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002044 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002046
Karl Rosaen138a0412009-04-23 19:00:21 -07002047 if (initialQuery == null) {
2048 // Use any text typed in the launcher as the initial query
2049 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002050 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 if (appSearchData == null) {
2052 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002053 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002055
2056 // TODO send proper bounds.
2057 Rect sourceBounds = null;
Romain Guycbb89e42009-06-08 15:52:54 -07002058
Ian Parkinson047036e2014-09-01 15:40:53 +01002059 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002060 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002061 if (clearTextImmediately) {
2062 clearTypedText();
2063 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002064
2065 // We need to show the workspace after starting the search
2066 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002067 }
2068
Ian Parkinson047036e2014-09-01 15:40:53 +01002069 /**
2070 * Start a text search.
2071 *
2072 * @return {@code true} if the search will start immediately, so any further keypresses
2073 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2074 * to buffer keypresses.
2075 */
2076 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002077 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002078 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2079 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2080 sourceBounds);
2081 }
2082
Michael Jurkaa33411c2012-06-14 16:18:21 -07002083 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002084 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002085 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002086 }
2087
2088 /**
2089 * Starts the global search activity. This code is a copied from SearchManager
2090 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002091 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002092 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002093 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002094 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2095 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2096 if (globalSearchActivity == null) {
2097 Log.w(TAG, "No global search activity found.");
2098 return;
2099 }
2100 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2101 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2102 intent.setComponent(globalSearchActivity);
2103 // Make sure that we have a Bundle to put source in
2104 if (appSearchData == null) {
2105 appSearchData = new Bundle();
2106 } else {
2107 appSearchData = new Bundle(appSearchData);
2108 }
Adam Cohen9211d422014-10-07 18:14:53 -07002109 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002110 if (!appSearchData.containsKey("source")) {
2111 appSearchData.putString("source", getPackageName());
2112 }
2113 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2114 if (!TextUtils.isEmpty(initialQuery)) {
2115 intent.putExtra(SearchManager.QUERY, initialQuery);
2116 }
2117 if (selectInitialQuery) {
2118 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2119 }
2120 intent.setSourceBounds(sourceBounds);
2121 try {
2122 startActivity(intent);
2123 } catch (ActivityNotFoundException ex) {
2124 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2125 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 }
2127
Yura4f93ec62014-02-04 14:15:21 +00002128 public boolean isOnCustomContent() {
2129 return mWorkspace.isOnOrMovingToCustomContent();
2130 }
2131
Winson Chung70d72102011-08-12 11:24:35 -07002132 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002133 public boolean onPrepareOptionsMenu(Menu menu) {
2134 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002135 if (mLauncherCallbacks != null) {
2136 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2137 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002138 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002139 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002140
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002141 @Override
2142 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002143 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002144 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002145 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002146 }
2147
Joe Onorato9c1289c2009-08-17 11:03:03 -04002148 public boolean isWorkspaceLocked() {
2149 return mWorkspaceLoading || mWaitingForResult;
2150 }
2151
Adam Cohen517a7f52014-03-01 12:12:59 -08002152 public boolean isWorkspaceLoading() {
2153 return mWorkspaceLoading;
2154 }
2155
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002156 private void setWorkspaceLoading(boolean value) {
2157 boolean isLocked = isWorkspaceLocked();
2158 mWorkspaceLoading = value;
2159 if (isLocked != isWorkspaceLocked()) {
2160 onWorkspaceLockedChanged();
2161 }
2162 }
2163
2164 private void setWaitingForResult(boolean value) {
2165 boolean isLocked = isWorkspaceLocked();
2166 mWaitingForResult = value;
2167 if (isLocked != isWorkspaceLocked()) {
2168 onWorkspaceLockedChanged();
2169 }
2170 }
2171
Adam Cohen9211d422014-10-07 18:14:53 -07002172 protected void onWorkspaceLockedChanged() {
2173 if (mLauncherCallbacks != null) {
2174 mLauncherCallbacks.onWorkspaceLockedChanged();
2175 }
2176 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002177
Michael Jurka0280c3b2010-09-17 15:00:07 -07002178 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002179 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002180 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002181 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2182 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002183 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002184 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002185
Tony Wickhama0628cc2015-10-14 15:23:04 -07002186 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002187 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002188 if (LOGD) {
2189 Log.d(TAG, "Adding widget from drop");
2190 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002191 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2192 }
2193
Sunny Goyale6b63a32015-01-16 16:14:29 -08002194 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002195 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2196 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002197 if (appWidgetInfo.configure != null) {
2198 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002199 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002200
Bjorn Bringert7984c942009-12-09 15:38:25 +00002201 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002202 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2203 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2204
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002205 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002206 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002207 Runnable onComplete = new Runnable() {
2208 @Override
2209 public void run() {
2210 // Exit spring loaded mode if necessary after adding the widget
2211 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2212 null);
2213 }
2214 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002215 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002216 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002217 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002218 }
2219 }
Romain Guycbb89e42009-06-08 15:52:54 -07002220
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002221 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002222 // Close any folders that may be open.
2223 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002224 mWorkspace.moveToCustomContentScreen(animate);
2225 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002226
2227 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2228 int[] cell, int spanX, int spanY) {
2229 switch (info.itemType) {
2230 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2231 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2232 int span[] = new int[2];
2233 span[0] = spanX;
2234 span[1] = spanY;
2235 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2236 container, screenId, cell, span);
2237 break;
2238 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2239 processShortcutFromDrop(info.componentName, container, screenId, cell);
2240 break;
2241 default:
2242 throw new IllegalStateException("Unknown item type: " + info.itemType);
2243 }
2244 }
2245
Adam Cohenfbba09b2011-07-18 21:43:05 -07002246 /**
2247 * Process a shortcut drop.
2248 *
2249 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002250 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002251 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002252 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002253 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2254 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002255 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002256 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002257 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002258
2259 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002260 mPendingAddInfo.cellX = cell[0];
2261 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002262 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002263
2264 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2265 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002266 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002267 }
2268
Adam Cohenfbba09b2011-07-18 21:43:05 -07002269 /**
2270 * Process a widget drop.
2271 *
2272 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002273 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002274 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002275 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002276 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2277 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002278 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002279 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002280 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002281 mPendingAddInfo.minSpanX = info.minSpanX;
2282 mPendingAddInfo.minSpanY = info.minSpanY;
2283
Adam Cohenfbba09b2011-07-18 21:43:05 -07002284 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002285 mPendingAddInfo.cellX = cell[0];
2286 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002287 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002288 if (span != null) {
2289 mPendingAddInfo.spanX = span[0];
2290 mPendingAddInfo.spanY = span[1];
2291 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002292
Adam Cohened66b2b2012-01-23 17:28:51 -08002293 AppWidgetHostView hostView = info.boundWidget;
2294 int appWidgetId;
2295 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002296 // In the case where we've prebound the widget, we remove it from the DragLayer
2297 if (LOGD) {
2298 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2299 }
2300 getDragLayer().removeView(hostView);
2301
Adam Cohened66b2b2012-01-23 17:28:51 -08002302 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002303 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002304
2305 // Clear the boundWidget so that it doesn't get destroyed.
2306 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002307 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002308 // In this case, we either need to start an activity to get permission to bind
2309 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002310 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002311 Bundle options = info.bindOptions;
2312
Sunny Goyalffe83f12014-08-14 17:39:34 -07002313 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2314 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002315 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002316 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002317 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002318 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002319 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2320 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2321 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002322 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2323 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002324 // TODO: we need to make sure that this accounts for the options bundle.
2325 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002326 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2327 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002328 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002329 }
2330
Adam Cohendcd297f2013-06-18 13:13:40 -07002331 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002332 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002333 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002334 folderInfo.title = getText(R.string.folder_name);
2335
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002336 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002337 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2338 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002339
2340 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002341 FolderIcon newFolder =
2342 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002343 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002344 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002345 // Force measure the new folder icon
2346 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2347 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002348 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002349 }
Romain Guycbb89e42009-06-08 15:52:54 -07002350
Winsonc0b52fe2015-09-09 16:38:15 -07002351 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002352 * Unbinds the view for the specified item, and removes the item and all its children.
2353 *
2354 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002355 * @param itemInfo the {@link ItemInfo} for this view.
2356 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002357 */
Winson2949fb52015-09-24 09:56:11 -07002358 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002359 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002360 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002361 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2362 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002363 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002364 } else {
2365 mWorkspace.removeWorkspaceItem(v);
2366 }
Winsonc0b52fe2015-09-09 16:38:15 -07002367 if (deleteFromDb) {
2368 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2369 }
2370 } else if (itemInfo instanceof FolderInfo) {
2371 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002372 if (v instanceof FolderIcon) {
2373 ((FolderIcon) v).removeListeners();
2374 }
Winsonc0b52fe2015-09-09 16:38:15 -07002375 mWorkspace.removeWorkspaceItem(v);
2376 if (deleteFromDb) {
2377 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2378 }
2379 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2380 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002381 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002382 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002383 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002384 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002385 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002386
Winsonc0b52fe2015-09-09 16:38:15 -07002387 } else {
2388 return false;
2389 }
2390 return true;
2391 }
2392
2393 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002394 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002395 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002396 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002397 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002398 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002399 // Deleting an app widget ID is a void call but writes to disk before returning
2400 // to the caller...
2401 new AsyncTask<Void, Void, Void>() {
2402 public Void doInBackground(Void ... args) {
2403 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2404 return null;
2405 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002406 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002407 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002408 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002409 }
2410
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411 @Override
2412 public boolean dispatchKeyEvent(KeyEvent event) {
2413 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2414 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002415 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002416 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002417 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002418 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002419 dumpState();
2420 return true;
2421 }
2422 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002423 }
2424 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2425 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002426 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002427 return true;
2428 }
2429 }
2430
2431 return super.dispatchKeyEvent(event);
2432 }
2433
Joe Onorato88ec0992009-11-19 13:16:06 -08002434 @Override
2435 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002436 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2437 return;
2438 }
2439
Sunny Goyal45478022015-06-08 16:52:41 -07002440 if (mDragController.isDragging()) {
2441 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002442 return;
2443 }
2444
Winson Chungb745afb2015-03-02 11:51:23 -08002445 if (isAppsViewVisible()) {
2446 showWorkspace(true);
2447 } else if (isWidgetsViewVisible()) {
2448 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002449 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002450 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002451 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002452 Folder openFolder = mWorkspace.getOpenFolder();
2453 if (openFolder.isEditingName()) {
2454 openFolder.dismissEditingName();
2455 } else {
2456 closeFolder();
2457 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002458 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002459 mWorkspace.exitWidgetResizeMode();
2460
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002461 // Back button is a no-op here, but give at least some feedback for the button press
2462 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002463 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002464 }
2465
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002466 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002467 * Launches the intent referred by the clicked shortcut.
2468 *
2469 * @param v The view representing the clicked shortcut.
2470 */
2471 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002472 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2473 // view has detached (it's possible for this to happen if the view is removed mid touch).
2474 if (v.getWindowToken() == null) {
2475 return;
2476 }
2477
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002478 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002479 return;
2480 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002481
Adam Cohen1697b792013-09-17 19:08:21 -07002482 if (v instanceof Workspace) {
2483 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002484 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002485 }
2486 return;
2487 }
2488
2489 if (v instanceof CellLayout) {
2490 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002491 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2492 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002493 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002494 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002495 }
2496
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002497 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002498 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002499 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002500 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002501 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002502 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002503 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002504 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002505 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002506 } else if (tag instanceof AppInfo) {
2507 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002508 } else if (tag instanceof LauncherAppWidgetInfo) {
2509 if (v instanceof PendingAppWidgetHostView) {
2510 onClickPendingWidget((PendingAppWidgetHostView) v);
2511 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002512 }
2513 }
2514
Sunny Goyal70660032015-05-14 00:07:08 -07002515 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002516 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002517 return false;
2518 }
2519
Michael Jurkaaf442092010-06-10 17:01:57 -07002520 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002521 * Event handler for the app widget view which has not fully restored.
2522 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002523 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002524 if (mIsSafeModeEnabled) {
2525 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2526 return;
2527 }
2528
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002529 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002530 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002531 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2532 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2533 // This should not happen, as we make sure that an Id is allocated during bind.
2534 return;
2535 }
2536 LauncherAppWidgetProviderInfo appWidgetInfo =
2537 mAppWidgetManager.findProvider(info.providerName, info.user);
2538 if (appWidgetInfo != null) {
2539 mPendingAddWidgetId = info.appWidgetId;
2540 mPendingAddInfo.copyFrom(info);
2541 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002542
Sunny Goyald478c832016-04-01 12:04:16 -07002543 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2544 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingAddWidgetId);
2545 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
2546 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2547 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2548 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2549 }
2550 } else {
2551 LauncherAppWidgetProviderInfo appWidgetInfo =
2552 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2553 if (appWidgetInfo != null) {
2554 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2555 }
Sunny Goyalff572272014-07-23 13:58:07 -07002556 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002557 } else if (info.installProgress < 0) {
2558 // The install has not been queued
2559 final String packageName = info.providerName.getPackageName();
2560 showBrokenAppInstallDialog(packageName,
2561 new DialogInterface.OnClickListener() {
2562 public void onClick(DialogInterface dialog, int id) {
2563 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2564 }
2565 });
2566 } else {
2567 // Download has started.
2568 final String packageName = info.providerName.getPackageName();
2569 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002570 }
2571 }
2572
Sunny Goyald478c832016-04-01 12:04:16 -07002573 private void startRestoredWidgetReconfigActivity(
2574 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
2575 mPendingAddWidgetInfo = provider;
2576 mPendingAddInfo.copyFrom(info);
2577 mPendingAddWidgetId = info.appWidgetId;
2578 mAppWidgetManager.startConfigActivity(provider,
2579 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2580 }
2581
Sunny Goyalff572272014-07-23 13:58:07 -07002582 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002583 * Event handler for the "grid" button that appears on the home screen, which
2584 * enters all apps mode.
2585 *
2586 * @param v The view that was clicked.
2587 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002588 protected void onClickAllAppsButton(View v) {
2589 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002590 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002591 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002592 true /* updatePredictedApps */, false /* focusSearchBar */);
2593 }
2594 }
2595
2596 protected void onLongClickAllAppsButton(View v) {
2597 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2598 if (!isAppsViewVisible()) {
2599 showAppsView(true /* animated */, false /* resetListToTop */,
2600 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002601 }
2602 }
2603
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002604 private void showBrokenAppInstallDialog(final String packageName,
2605 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002606 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002607 .setTitle(R.string.abandoned_promises_title)
2608 .setMessage(R.string.abandoned_promise_explanation)
2609 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2610 .setNeutralButton(R.string.abandoned_clean_this,
2611 new DialogInterface.OnClickListener() {
2612 public void onClick(DialogInterface dialog, int id) {
2613 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2614 mWorkspace.removeAbandonedPromise(packageName, user);
2615 }
2616 })
2617 .create().show();
2618 return;
2619 }
2620
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002621 /**
2622 * Event handler for an app shortcut click.
2623 *
2624 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2625 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002626 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002627 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2628 Object tag = v.getTag();
2629 if (!(tag instanceof ShortcutInfo)) {
2630 throw new IllegalArgumentException("Input must be a Shortcut");
2631 }
2632
2633 // Open shortcut
2634 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002635
2636 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002637 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2638 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002639 // Launch activity anyway, framework will tell the user why the app is suspended.
2640 } else {
2641 int error = R.string.activity_not_available;
2642 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2643 error = R.string.safemode_shortcut_error;
2644 }
2645 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2646 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002647 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002648 }
2649
Chris Wren40c5ed32014-06-24 18:24:23 -04002650 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002651 if ((v instanceof BubbleTextView)
2652 && shortcut.isPromise()
2653 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002654 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002655 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002656 new DialogInterface.OnClickListener() {
2657 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002658 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002659 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002660 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002661 return;
2662 }
2663
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002664 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002665 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002666 }
2667
Adam Cohen091440a2015-03-18 14:16:05 -07002668 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002669 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002670 final ShortcutInfo shortcut;
2671 final Intent intent;
2672 if (tag instanceof ShortcutInfo) {
2673 shortcut = (ShortcutInfo) tag;
2674 intent = shortcut.intent;
2675 int[] pos = new int[2];
2676 v.getLocationOnScreen(pos);
2677 intent.setSourceBounds(new Rect(pos[0], pos[1],
2678 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002679
Sunny Goyal508da152014-08-14 10:53:27 -07002680 } else if (tag instanceof AppInfo) {
2681 shortcut = null;
2682 intent = ((AppInfo) tag).intent;
2683 } else {
2684 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2685 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002686
2687 boolean success = startActivitySafely(v, intent, tag);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002688 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002689
2690 if (success && v instanceof BubbleTextView) {
2691 mWaitingForResume = (BubbleTextView) v;
2692 mWaitingForResume.setStayPressed(true);
2693 }
2694 }
2695
2696 /**
2697 * Event handler for a folder icon click.
2698 *
2699 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2700 */
2701 protected void onClickFolderIcon(View v) {
2702 if (LOGD) Log.d(TAG, "onClickFolder");
2703 if (!(v instanceof FolderIcon)){
2704 throw new IllegalArgumentException("Input must be a FolderIcon");
2705 }
2706
2707 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002708 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002709 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002710 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002711 }
Michael Jurka5130e402011-10-13 04:55:35 -07002712 }
2713
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002714 /**
2715 * Event handler for the (Add) Widgets button that appears after a long press
2716 * on the home screen.
2717 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002718 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002719 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002720 if (mIsSafeModeEnabled) {
2721 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2722 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002723 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002724 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002725 }
2726
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002727 /**
2728 * Event handler for the wallpaper picker button that appears after a long press
2729 * on the home screen.
2730 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002731 protected void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002732 if (!Utilities.isWallapaperAllowed(this)) {
2733 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2734 return;
2735 }
2736
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002737 String pickerPackage = getString(R.string.wallpaper_picker_package);
2738 if (TextUtils.isEmpty(pickerPackage)) {
2739 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2740 }
2741
Tony Wickham785f7a52015-08-31 17:28:32 -07002742 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2743 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002744 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER)
2745 .setPackage(pickerPackage)
2746 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset),
2747 REQUEST_PICK_WALLPAPER);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002748 }
2749
2750 /**
2751 * Event handler for a click on the settings button that appears after a long press
2752 * on the home screen.
2753 */
Sunny Goyal745bad92016-05-02 10:54:12 -07002754 private void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002755 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002756 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2757 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002758 }
2759
Adam Cohen61f560d2013-09-30 15:58:20 -07002760 public View.OnTouchListener getHapticFeedbackTouchListener() {
2761 if (mHapticFeedbackTouchListener == null) {
2762 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002763 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002764 @Override
2765 public boolean onTouch(View v, MotionEvent event) {
2766 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2767 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2768 }
2769 return false;
2770 }
2771 };
2772 }
2773 return mHapticFeedbackTouchListener;
2774 }
2775
Tony Wickhame2217252016-03-22 16:34:23 -07002776 @Override
2777 public void onAccessibilityStateChanged(boolean enabled) {
2778 mDragLayer.onAccessibilityStateChanged(enabled);
2779 }
2780
Adam Cohen9211d422014-10-07 18:14:53 -07002781 public void onDragStarted(View view) {
2782 if (isOnCustomContent()) {
2783 // Custom content screen doesn't participate in drag and drop. If on custom
2784 // content screen, move to default.
2785 moveWorkspaceToDefaultScreen();
2786 }
Adam Cohen9211d422014-10-07 18:14:53 -07002787 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002788
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002789 /**
2790 * Called when the user stops interacting with the launcher.
2791 * This implies that the user is now on the homescreen and is not doing housekeeping.
2792 */
Adam Cohen9211d422014-10-07 18:14:53 -07002793 protected void onInteractionEnd() {
2794 if (mLauncherCallbacks != null) {
2795 mLauncherCallbacks.onInteractionEnd();
2796 }
2797 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002798
2799 /**
2800 * Called when the user starts interacting with the launcher.
2801 * The possible interactions are:
2802 * - open all apps
2803 * - reorder an app shortcut, or a widget
2804 * - open the overview mode.
2805 * This is a good time to stop doing things that only make sense
2806 * when the user is on the homescreen and not doing housekeeping.
2807 */
Adam Cohen9211d422014-10-07 18:14:53 -07002808 protected void onInteractionBegin() {
2809 if (mLauncherCallbacks != null) {
2810 mLauncherCallbacks.onInteractionBegin();
2811 }
2812 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002813
Winson Chungcd99cd32015-04-29 11:03:24 -07002814 /** Updates the interaction state. */
2815 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002816 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002817 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002818 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2819 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002820 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002821 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002822 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002823 onInteractionEnd();
2824 }
2825 }
2826
Winson Chung8f1eff72015-05-28 17:33:40 -07002827 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002828 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002829 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002830 // Only launch using the new animation if the shortcut has not opted out (this is a
2831 // private contract between launcher and may be ignored in the future).
2832 boolean useLaunchAnimation = (v != null) &&
2833 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002834 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2835 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002836
Kenny Guy1317e2d2014-05-08 18:52:50 +01002837 UserHandleCompat user = null;
2838 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2839 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2840 user = userManager.getUserForSerialNumber(serialNumber);
2841 }
2842
2843 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002844 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002845 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002846 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002847 int left = 0, top = 0;
2848 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2849 if (v instanceof TextView) {
2850 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002851 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2852 if (icon != null) {
2853 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002854 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002855 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002856 width = bounds.width();
2857 height = bounds.height();
2858 }
2859 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002860 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2861 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002862 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002863 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002864 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002865 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002866 // On L devices, we use the device default slide-up transition.
2867 // On L MR1 devices, we a custom version of the slide-up transition which
2868 // doesn't have the delay present in the device default.
2869 opts = ActivityOptions.makeCustomAnimation(this,
2870 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002871 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002872 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002873 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002874
2875 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -07002876 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2877 try {
2878 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2879 // containing file Uris would cause a crash as penaltyDeathOnFileUriExposure
2880 // is enabled by default on NYC.
2881 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2882 .penaltyLog().build());
2883 // Could be launching some bookkeeping activity
2884 startActivity(intent, optsBundle);
2885 } finally {
2886 StrictMode.setVmPolicy(oldPolicy);
2887 }
Winson Chungc7450e32012-04-17 17:34:08 -07002888 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002889 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002890 launcherApps.startActivityForProfile(intent.getComponent(), user,
2891 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002892 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002893 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002894 } catch (SecurityException e) {
Sunny Goyal28c6b962015-10-12 11:42:05 -07002895 if (Utilities.ATLEAST_MARSHMALLOW && tag instanceof ItemInfo) {
2896 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2897 // corresponding permission. Show the appropriate permission prompt if that
2898 // is the case.
2899 if (intent.getComponent() == null
2900 && Intent.ACTION_CALL.equals(intent.getAction())
2901 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2902 PackageManager.PERMISSION_GRANTED) {
2903 // TODO: Rename sPendingAddItem to a generic name.
2904 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2905 0, (ItemInfo) tag);
2906 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2907 REQUEST_PERMISSION_CALL_PHONE);
2908 return false;
2909 }
2910 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002911 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002912 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002913 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002914 "or use the exported attribute for this activity. "
2915 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002916 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002917 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002918 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002919
Winson Chungbedf9232015-07-10 12:38:30 -07002920 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002921 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002922 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2923 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2924 return false;
2925 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002926 try {
2927 success = startActivity(v, intent, tag);
2928 } catch (ActivityNotFoundException e) {
2929 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2930 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2931 }
2932 return success;
2933 }
2934
Adam Cohen268c4752012-06-06 17:47:33 -07002935 /**
2936 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2937 * in the DragLayer in the exact absolute location of the original FolderIcon.
2938 */
2939 private void copyFolderIconToImage(FolderIcon fi) {
2940 final int width = fi.getMeasuredWidth();
2941 final int height = fi.getMeasuredHeight();
2942
2943 // Lazy load ImageView, Bitmap and Canvas
2944 if (mFolderIconImageView == null) {
2945 mFolderIconImageView = new ImageView(this);
2946 }
2947 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2948 mFolderIconBitmap.getHeight() != height) {
2949 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2950 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2951 }
2952
2953 DragLayer.LayoutParams lp;
2954 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2955 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2956 } else {
2957 lp = new DragLayer.LayoutParams(width, height);
2958 }
2959
Adam Cohen307fe232012-08-16 17:55:58 -07002960 // The layout from which the folder is being opened may be scaled, adjust the starting
2961 // view size by this scale factor.
2962 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002963 lp.customPosition = true;
2964 lp.x = mRectForFolderAnimation.left;
2965 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002966 lp.width = (int) (scale * width);
2967 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002968
2969 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2970 fi.draw(mFolderIconCanvas);
2971 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002972 if (fi.getFolder() != null) {
2973 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2974 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002975 }
Adam Cohen268c4752012-06-06 17:47:33 -07002976 // Just in case this image view is still in the drag layer from a previous animation,
2977 // we remove it and re-add it.
2978 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2979 mDragLayer.removeView(mFolderIconImageView);
2980 }
2981 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002982 if (fi.getFolder() != null) {
2983 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002984 }
Adam Cohen268c4752012-06-06 17:47:33 -07002985 }
2986
Adam Cohen37b2a492016-04-06 18:36:06 -07002987 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002988 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002989 FolderInfo info = (FolderInfo) fi.getTag();
2990 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2991 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002992 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2993 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002994 }
2995
Adam Cohen268c4752012-06-06 17:47:33 -07002996 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2997 copyFolderIconToImage(fi);
2998 fi.setVisibility(View.INVISIBLE);
2999
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003000 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
3001 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003002 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003003 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3004 }
3005 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003006 oa.start();
3007 }
3008
Sunny Goyal935fca12015-10-13 10:19:01 -07003009 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003010 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003011 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003012
Adam Cohen268c4752012-06-06 17:47:33 -07003013 // We remove and re-draw the FolderIcon in-case it has changed
3014 mDragLayer.removeView(mFolderIconImageView);
3015 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08003016
3017 if (cl != null) {
3018 cl.clearFolderLeaveBehind();
3019 }
3020
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003021 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003022 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003023 oa.addListener(new AnimatorListenerAdapter() {
3024 @Override
3025 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003026 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003027 // Remove the ImageView copy of the FolderIcon and make the original visible.
3028 mDragLayer.removeView(mFolderIconImageView);
3029 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003030 }
3031 }
3032 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003033 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003034 if (!animate) {
3035 oa.end();
3036 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003037 }
3038
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003039 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003040 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003041 * is animated relative to the specified View. If the View is null, no animation
3042 * is played.
3043 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003044 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003045 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003046 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003047
Adam Cohenfb91f302012-06-11 15:45:18 -07003048 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003049 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3050 if (openFolder != null && openFolder != folder) {
3051 // Close any open folder before opening a folder.
3052 closeFolder();
3053 }
3054
Adam Cohena9cf38f2011-05-02 15:36:58 -07003055 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003056
Adam Cohena9cf38f2011-05-02 15:36:58 -07003057 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003058
Sunny Goyalf4066152015-04-15 09:42:19 -07003059 // While the folder is open, the position of the icon cannot change.
3060 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3061
Adam Cohen4554ee12011-08-03 16:13:21 -07003062 // Just verify that the folder hasn't already been added to the DragLayer.
3063 // There was a one-off crash where the folder had a parent already.
3064 if (folder.getParent() == null) {
3065 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003066 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003067 } else {
3068 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3069 folder.getParent() + ").");
3070 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003071 folder.animateOpen();
3072
3073 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003074
3075 // Notify the accessibility manager that this folder "window" has appeared and occluded
3076 // the workspace items
3077 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3078 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003079 }
3080
3081 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003082 closeFolder(true);
3083 }
3084
3085 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003086 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003087 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003088 if (folder.isEditingName()) {
3089 folder.dismissEditingName();
3090 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003091 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003092 }
3093 }
3094
Sunny Goyal935fca12015-10-13 10:19:01 -07003095 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003096 animate &= !Utilities.isPowerSaverOn(this);
3097
Adam Cohen4554ee12011-08-03 16:13:21 -07003098 folder.getInfo().opened = false;
3099
3100 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3101 if (parent != null) {
3102 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003103 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003104 if (fi != null) {
3105 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3106 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003107 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003108 if (animate) {
3109 folder.animateClosed();
3110 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003111 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003112 }
Winson Chung83ca4802013-04-12 15:10:52 -07003113
Sunny Goyal935fca12015-10-13 10:19:01 -07003114 // Notify the accessibility manager that this folder "window" has disappeared and no
3115 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003116 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003117 }
3118
Tony Wickhamdadb3042016-02-24 11:07:00 -08003119 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003120 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003121 if (!isDraggingEnabled()) return false;
3122 if (isWorkspaceLocked()) return false;
3123 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003124
Winson Chung76648c52015-07-10 14:33:23 -07003125 if (v == mAllAppsButton) {
3126 onLongClickAllAppsButton(v);
3127 return true;
3128 }
3129
Adam Cohen1697b792013-09-17 19:08:21 -07003130 if (v instanceof Workspace) {
3131 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003132 if (!mWorkspace.isTouchActive()) {
3133 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003134 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3135 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3136 return true;
3137 } else {
3138 return false;
3139 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003140 } else {
3141 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003142 }
Adam Cohen1697b792013-09-17 19:08:21 -07003143 }
3144
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003145 CellLayout.CellInfo longClickCellInfo = null;
3146 View itemUnderLongClick = null;
3147 if (v.getTag() instanceof ItemInfo) {
3148 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003149 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003150 itemUnderLongClick = longClickCellInfo.cell;
3151 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003152 }
3153
Winson Chung3d503fb2011-07-13 17:25:49 -07003154 // The hotseat touch handling does not go through Workspace, and we always allow long press
3155 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003156 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003157 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003158 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003159 // User long pressed on empty space
3160 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3161 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003162 if (mWorkspace.isInOverviewMode()) {
3163 mWorkspace.startReordering(v);
3164 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003165 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003166 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003167 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003168 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3169 mHotseat.getOrderInHotseat(
3170 longClickCellInfo.cellX,
3171 longClickCellInfo.cellY));
3172 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003173 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003174 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003175 }
3176 }
3177 }
3178 return true;
3179 }
3180
Winson Chung3d503fb2011-07-13 17:25:49 -07003181 boolean isHotseatLayout(View layout) {
3182 return mHotseat != null && layout != null &&
3183 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3184 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003185
Winson Chung3d503fb2011-07-13 17:25:49 -07003186 /**
3187 * Returns the CellLayout of the specified container at the specified screen.
3188 */
Sunny Goyal92820592015-03-02 11:31:35 -08003189 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003190 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3191 if (mHotseat != null) {
3192 return mHotseat.getLayout();
3193 } else {
3194 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003195 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003196 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003197 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003198 }
3199 }
3200
Winson Chungb745afb2015-03-02 11:51:23 -08003201 /**
3202 * For overridden classes.
3203 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003204 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003205 return isAppsViewVisible();
3206 }
3207
3208 public boolean isAppsViewVisible() {
3209 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3210 }
3211
3212 public boolean isWidgetsViewVisible() {
3213 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003214 }
3215
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003216 private void setWorkspaceBackground(int background) {
3217 switch (background) {
3218 case WORKSPACE_BACKGROUND_TRANSPARENT:
3219 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3220 break;
3221 case WORKSPACE_BACKGROUND_BLACK:
3222 getWindow().setBackgroundDrawable(null);
3223 break;
3224 default:
3225 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3226 }
Craig Mautner360310b2012-10-26 15:13:08 -07003227 }
3228
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003229 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003230 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3231 int curflags = getWindow().getAttributes().flags
3232 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3233 if (wpflags != curflags) {
3234 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3235 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003236 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003237 }
3238
Michael Jurkae326f182011-11-21 14:05:46 -08003239 @Override
3240 public void onTrimMemory(int level) {
3241 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003242 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3243 // The widget preview db can result in holding onto over
3244 // 3MB of memory for caching which isn't necessary.
3245 SQLiteDatabase.releaseMemory();
3246
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003247 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003248 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003249 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003250 if (mLauncherCallbacks != null) {
3251 mLauncherCallbacks.onTrimMemory(level);
3252 }
Michael Jurkae326f182011-11-21 14:05:46 -08003253 }
3254
Winson5c6bdbb2015-09-03 11:36:19 -07003255 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003256 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003257 }
3258
Winson5c6bdbb2015-09-03 11:36:19 -07003259 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003260 boolean changed = mState != State.WORKSPACE ||
3261 mWorkspace.getState() != Workspace.State.NORMAL;
3262 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003263 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003264 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003265 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003266
Michael Jurkab3e22d92011-10-31 15:58:33 -07003267 // Set focus to the AppsCustomize button
3268 if (mAllAppsButton != null) {
3269 mAllAppsButton.requestFocus();
3270 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003271 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003272
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003273 // Change the state *after* we've called all the transition code
3274 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003275
Winson Chung5fb63472011-02-02 17:03:37 -08003276 // Resume the auto-advance of widgets
3277 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003278 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003279
Winson Chung0f785722015-04-08 10:27:49 -07003280 if (changed) {
3281 // Send an accessibility event to announce the context change
3282 getWindow().getDecorView()
3283 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003284 }
Winson5c6bdbb2015-09-03 11:36:19 -07003285 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003286 }
3287
Winsone9f27272015-10-13 10:47:51 -07003288 /**
3289 * Shows the overview button.
3290 */
Adam Cohened307df2013-10-02 09:37:31 -07003291 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003292 showOverviewMode(animated, false);
3293 }
3294
3295 /**
3296 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3297 * onto one of the overview panel buttons.
3298 */
3299 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3300 Runnable postAnimRunnable = null;
3301 if (requestButtonFocus) {
3302 postAnimRunnable = new Runnable() {
3303 @Override
3304 public void run() {
3305 // Hitting the menu button when in touch mode does not trigger touch mode to
3306 // be disabled, so if requested, force focus on one of the overview panel
3307 // buttons.
3308 mOverviewPanel.requestFocusFromTouch();
3309 }
3310 };
3311 }
Adam Cohened307df2013-10-02 09:37:31 -07003312 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003313 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003314 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003315 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003316 }
3317
Winson Chungb745afb2015-03-02 11:51:23 -08003318 /**
3319 * Shows the apps view.
3320 */
Winson Chung76648c52015-07-10 14:33:23 -07003321 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3322 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003323 if (resetListToTop) {
3324 mAppsView.scrollToTop();
3325 }
Winson Chung4ac30062015-05-08 17:34:17 -07003326 if (updatePredictedApps) {
3327 tryAndUpdatePredictedApps();
3328 }
Winson Chung76648c52015-07-10 14:33:23 -07003329 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003330 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003331
Winson Chungb745afb2015-03-02 11:51:23 -08003332 /**
3333 * Shows the widgets view.
3334 */
3335 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003336 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003337 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003338 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003339 }
Winson Chung76648c52015-07-10 14:33:23 -07003340 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003341
3342 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003343 @Override
3344 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003345 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003346 }
3347 });
Winson Chungb745afb2015-03-02 11:51:23 -08003348 }
3349
3350 /**
3351 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003352 *
3353 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003354 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003355 // TODO: calling method should use the return value so that when {@code false} is returned
3356 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003357 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003358 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3359 mState != State.WIDGETS_SPRING_LOADED) {
3360 return false;
3361 }
3362 if (toState != State.APPS && toState != State.WIDGETS) {
3363 return false;
3364 }
Winson Chungb745afb2015-03-02 11:51:23 -08003365
3366 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003367 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3368 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003369 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003370 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003371 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003372
Michael Jurkab3e22d92011-10-31 15:58:33 -07003373 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003374 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003375
3376 // Pause the auto-advance of widgets until we are out of AllApps
3377 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003378 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003379 closeFolder();
3380
3381 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003382 getWindow().getDecorView()
3383 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003384 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003385 }
3386
Winson Chungcd99cd32015-04-29 11:03:24 -07003387 /**
3388 * Updates the workspace and interaction state on state change, and return the animation to this
3389 * new state.
3390 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003391 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003392 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003393 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003394 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003395 updateInteraction(fromState, toState);
3396 return anim;
3397 }
3398
Jun Mukaif0033da2015-08-04 18:34:30 -07003399 public void onLauncherClingShown() {
3400 // When a launcher cling appears, it should cover the underlying layers, so their focus
3401 // should be blocked.
3402 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3403 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3404 }
3405 }
3406
3407 public void onLauncherClingDismissed() {
3408 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3409 }
3410
Hyunyoung Song3f471442015-04-08 19:01:34 -07003411 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003412 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003413 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003414 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003415 }
Winson Chungb745afb2015-03-02 11:51:23 -08003416
Winson Chung006ee262015-08-03 14:40:11 -07003417 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003418 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003419 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003420
3421 if (isAppsViewVisible()) {
3422 mState = State.APPS_SPRING_LOADED;
3423 } else if (isWidgetsViewVisible()) {
3424 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003425 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003426 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003427 } else {
3428 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003429 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003430 }
Adam Cohen7777d962011-08-18 18:58:38 -07003431
Hyunyoung Song3f471442015-04-08 19:01:34 -07003432 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003433 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003434 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003435
Winson Chunge7a03942011-08-05 15:05:12 -07003436 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003437 @Override
3438 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003439 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003440 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3441 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003442 // Before we show workspace, hide all apps again because
3443 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3444 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003445 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003446 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003447 } else {
3448 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003449 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003450 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003451 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003452 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003453
Tony Wickhame0c33232016-02-08 11:37:04 -08003454 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003455 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3456 || mState == State.WIDGETS_SPRING_LOADED;
3457 }
3458
Michael Jurkad3ef3062010-11-23 16:23:58 -08003459 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003460 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003461 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003462 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003463 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003464 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003465 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3466 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003467 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003468 }
3469
Winson Chung4ac30062015-05-08 17:34:17 -07003470 /**
3471 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3472 * resumed.
3473 */
3474 private void tryAndUpdatePredictedApps() {
3475 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003476 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003477 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003478 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003479 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003480 }
3481 }
3482 }
3483
Michael Jurkab3e22d92011-10-31 15:58:33 -07003484 void lockAllApps() {
3485 // TODO
3486 }
3487
3488 void unlockAllApps() {
3489 // TODO
3490 }
3491
Winsonf768d932015-09-25 16:12:35 -07003492 public boolean launcherCallbacksProvidesSearch() {
3493 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3494 }
3495
Michael Jurkad7c28052012-04-27 15:43:36 -07003496 @Override
3497 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003498 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003499 final List<CharSequence> text = event.getText();
3500 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003501 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003502 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003503 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003504 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003505 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003506 } else if (mWorkspace != null) {
3507 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003508 } else {
3509 text.add(getString(R.string.all_apps_home_button_label));
3510 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003511 return result;
3512 }
3513
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003514 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003515 * If the activity is currently paused, signal that we need to run the passed Runnable
3516 * in onResume.
3517 *
3518 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003519 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3520 * wrong when we're not running, and if the activity comes back to what the configuration was
3521 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003522 *
3523 * Implementation of the method from LauncherModel.Callbacks.
3524 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003525 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003526 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003527 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003528 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003529 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003530 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003531 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003532 }
3533 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003534 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003535 return true;
3536 } else {
3537 return false;
3538 }
3539 }
3540
Michael Jurkac402cd92013-05-20 15:49:32 +02003541 private boolean waitUntilResume(Runnable run) {
3542 return waitUntilResume(run, false);
3543 }
3544
Michael Jurka1e2f4652013-07-08 18:03:46 -07003545 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003546 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003547 }
3548
Michael Jurka7607c2f2013-04-03 14:33:19 -07003549 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003550 * If the activity is currently paused, signal that we need to re-run the loader
3551 * in onResume.
3552 *
3553 * This needs to be called from incoming places where resources might have been loaded
3554 * while we are paused. That is becaues the Configuration might be wrong
3555 * when we're not running, and if it comes back to what it was when we
3556 * were paused, we are not restarted.
3557 *
3558 * Implementation of the method from LauncherModel.Callbacks.
3559 *
3560 * @return true if we are currently paused. The caller might be able to
3561 * skip some work in that case since we will come back again.
3562 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003563 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003564 public boolean setLoadOnResume() {
3565 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003566 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003567 mOnResumeNeedsLoad = true;
3568 return true;
3569 } else {
3570 return false;
3571 }
3572 }
3573
3574 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003575 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003576 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003577 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003578 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003579 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003580 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003581 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003582 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003583 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003584 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003585
Joe Onorato9c1289c2009-08-17 11:03:03 -04003586 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003587 * Clear any pending bind callbacks. This is called when is loader is planning to
3588 * perform a full rebind from scratch.
3589 */
3590 @Override
3591 public void clearPendingBinds() {
3592 mBindOnResumeCallbacks.clear();
3593 if (mPendingExecutor != null) {
3594 mPendingExecutor.markCompleted();
3595 mPendingExecutor = null;
3596 }
3597 }
3598
3599 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003600 * Refreshes the shortcuts shown on the workspace.
3601 *
3602 * Implementation of the method from LauncherModel.Callbacks.
3603 */
3604 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003605 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003606
Winson Chungd64d1762013-08-20 14:37:16 -07003607 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003608 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003609 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003610
Michael Jurka05bf644e2011-11-30 20:28:53 -08003611 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003612 if (mHotseat != null) {
3613 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003614 }
3615 }
3616
Adam Cohendcd297f2013-06-18 13:13:40 -07003617 @Override
3618 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003619 // Make sure the first screen is always at the start.
3620 if (orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
3621 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3622 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3623 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Adam Cohen5084cba2013-09-03 12:01:16 -07003624 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003625 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003626
3627 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003628 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003629 if (hasCustomContentToLeft()) {
3630 mWorkspace.createCustomContentContainer();
3631 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003632 }
Winson Chung64359a52013-07-08 17:17:08 -07003633 }
3634
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003635 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003636 int count = orderedScreenIds.size();
3637 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003638 long screenId = orderedScreenIds.get(i);
3639 if (screenId != Workspace.FIRST_SCREEN_ID) {
3640 // No need to bind the first screen, as its always bound.
3641 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3642 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003643 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003644 }
3645
Winson Chungd64d1762013-08-20 14:37:16 -07003646 public void bindAppsAdded(final ArrayList<Long> newScreens,
3647 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003648 final ArrayList<ItemInfo> addAnimated,
3649 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003650 Runnable r = new Runnable() {
3651 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003652 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003653 }
3654 };
3655 if (waitUntilResume(r)) {
3656 return;
3657 }
3658
Winson Chungd64d1762013-08-20 14:37:16 -07003659 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003660 if (newScreens != null) {
3661 bindAddScreens(newScreens);
3662 }
Winson Chungd64d1762013-08-20 14:37:16 -07003663
3664 // We add the items without animation on non-visible pages, and with
3665 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003666 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003667 bindItems(addNotAnimated, 0,
3668 addNotAnimated.size(), false);
3669 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003670 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003671 bindItems(addAnimated, 0,
3672 addAnimated.size(), true);
3673 }
Winson Chungc58497e2013-09-03 17:48:37 -07003674
Winson Chung87412982013-10-03 18:34:14 -07003675 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003676 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003677
Winson Chungb745afb2015-03-02 11:51:23 -08003678 if (addedApps != null && mAppsView != null) {
3679 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003680 }
Winson Chungd64d1762013-08-20 14:37:16 -07003681 }
3682
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003683 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003684 * Bind the items start-end from the list.
3685 *
3686 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003687 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003688 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003689 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3690 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003691 Runnable r = new Runnable() {
3692 public void run() {
3693 bindItems(shortcuts, start, end, forceAnimateIcons);
3694 }
3695 };
3696 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003697 return;
3698 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003699
Winson Chungf0c6ae02012-03-21 16:10:31 -07003700 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003701 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3702 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003703 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003704 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003705 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003706 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003707 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003708
3709 // Short circuit if we are loading dock items for a configuration which has no dock
3710 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3711 mHotseat == null) {
3712 continue;
3713 }
3714
Sunny Goyal639e9062015-08-19 19:17:06 -07003715 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003716 switch (item.itemType) {
3717 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3718 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003719 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003720 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003721 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003722 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003723 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003724 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003725 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003726 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003727 default:
3728 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003729 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003730
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003731 /*
3732 * Remove colliding items.
3733 */
3734 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3735 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3736 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3737 View v = cl.getChildAt(item.cellX, item.cellY);
3738 Object tag = v.getTag();
3739 String desc = "Collision while binding workspace item: " + item
3740 + ". Collides with " + tag;
3741 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3742 throw (new RuntimeException(desc));
3743 } else {
3744 Log.d(TAG, desc);
3745 LauncherModel.deleteItemFromDatabase(this, item);
3746 continue;
3747 }
3748 }
3749 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003750 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3751 item.cellY, 1, 1);
3752 if (animateIcons) {
3753 // Animate all the applications up now
3754 view.setAlpha(0f);
3755 view.setScaleX(0f);
3756 view.setScaleY(0f);
3757 bounceAnims.add(createNewAppBounceAnimation(view, i));
3758 newShortcutsScreenId = item.screenId;
3759 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003760 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003761
Winson Chung997a9232013-07-24 15:33:46 -07003762 if (animateIcons) {
3763 // Animate to the correct page
3764 if (newShortcutsScreenId > -1) {
3765 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003766 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003767 final Runnable startBounceAnimRunnable = new Runnable() {
3768 public void run() {
3769 anim.playTogether(bounceAnims);
3770 anim.start();
3771 }
3772 };
Winson Chung997a9232013-07-24 15:33:46 -07003773 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003774 // We post the animation slightly delayed to prevent slowdowns
3775 // when we are loading right after we return to launcher.
3776 mWorkspace.postDelayed(new Runnable() {
3777 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003778 if (mWorkspace != null) {
3779 mWorkspace.snapToPage(newScreenIndex);
3780 mWorkspace.postDelayed(startBounceAnimRunnable,
3781 NEW_APPS_ANIMATION_DELAY);
3782 }
Winson Chung94d67682013-09-25 16:29:40 -07003783 }
3784 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003785 } else {
3786 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003787 }
3788 }
Winson Chung64359a52013-07-08 17:17:08 -07003789 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003790 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003791 }
3792
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003793 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3794 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3795 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003796 view.updateAppWidget(null);
3797 view.setOnClickListener(this);
3798 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003799 mWorkspace.requestLayout();
3800 }
3801
Joe Onorato9c1289c2009-08-17 11:03:03 -04003802 /**
3803 * Add the views for a widget to the workspace.
3804 *
3805 * Implementation of the method from LauncherModel.Callbacks.
3806 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003807 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003808 Runnable r = new Runnable() {
3809 public void run() {
3810 bindAppWidget(item);
3811 }
3812 };
3813 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003814 return;
3815 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003816
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003817 if (mIsSafeModeEnabled) {
3818 bindSafeModeWidget(item);
3819 return;
3820 }
3821
Daniel Sandler843e8602010-06-07 14:59:01 -04003822 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3823 if (DEBUG_WIDGETS) {
3824 Log.d(TAG, "bindAppWidget: " + item);
3825 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003826
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003827 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003828
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003829 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3830 // If the provider is not ready, bind as a pending widget.
3831 appWidgetInfo = null;
3832 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3833 // The widget id is not valid. Try to find the widget based on the provider info.
3834 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3835 } else {
3836 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3837 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003838
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003839 // If the provider is ready, but the width is not yet restored, try to restore it.
3840 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3841 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003842 if (appWidgetInfo == null) {
3843 if (DEBUG_WIDGETS) {
3844 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3845 + " belongs to component " + item.providerName
3846 + ", as the povider is null");
3847 }
3848 LauncherModel.deleteItemFromDatabase(this, item);
3849 return;
3850 }
Sunny Goyalff572272014-07-23 13:58:07 -07003851
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003852 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003853 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003854 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3855 // Id has not been allocated yet. Allocate a new id.
3856 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3857 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003858
Sunny Goyald478c832016-04-01 12:04:16 -07003859 // Also try to bind the widget. If the bind fails, the user will be shown
3860 // a click to setup UI, which will ask for the bind permission.
3861 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3862 pendingInfo.spanX = item.spanX;
3863 pendingInfo.spanY = item.spanY;
3864 pendingInfo.minSpanX = item.minSpanX;
3865 pendingInfo.minSpanY = item.minSpanY;
3866 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
3867 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3868 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003869
Sunny Goyald478c832016-04-01 12:04:16 -07003870 // Bind succeeded
3871 if (success) {
3872 // If the widget has a configure activity, it is still needs to set it up,
3873 // otherwise the widget is ready to go.
3874 item.restoreStatus = (appWidgetInfo.configure == null)
3875 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3876 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003877 }
Sunny Goyald478c832016-04-01 12:04:16 -07003878
3879 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003880 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003881 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003882 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003883 // The widget was marked as UI not ready, but there is no configure activity to
3884 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003885 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3886 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003887 }
Sunny Goyalff572272014-07-23 13:58:07 -07003888 }
3889
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003890 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003891 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003892 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3893 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003894 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003895
Sunny Goyal56c73602015-09-25 12:55:01 -07003896 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003897 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003898 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003899 deleteWidgetInfo(item);
3900 return;
3901 }
3902
Sunny Goyal233ee962015-08-03 13:05:01 -07003903 item.minSpanX = appWidgetInfo.minSpanX;
3904 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003905 addAppWidgetToWorkspace(
3906 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3907 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003908 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003909 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003910 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003911 view.updateAppWidget(null);
3912 view.setOnClickListener(this);
3913 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003914 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003915 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003916
Daniel Sandler843e8602010-06-07 14:59:01 -04003917 if (DEBUG_WIDGETS) {
3918 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3919 + (SystemClock.uptimeMillis()-start) + "ms");
3920 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003921 }
3922
Sunny Goyalff572272014-07-23 13:58:07 -07003923 /**
3924 * Restores a pending widget.
3925 *
3926 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003927 */
Sunny Goyald478c832016-04-01 12:04:16 -07003928 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003929 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3930 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3931 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003932 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003933 }
3934
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003935 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003936 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003937
3938 mWorkspace.reinflateWidgetsIfNecessary();
3939 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003940 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003941 }
3942
Adam Cohen1462de32012-07-24 22:34:36 -07003943 public void onPageBoundSynchronously(int page) {
3944 mSynchronouslyBoundPages.add(page);
3945 }
3946
Sunny Goyal527c7d32015-08-28 15:19:36 -07003947 @Override
3948 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3949 if (mPendingExecutor != null) {
3950 mPendingExecutor.markCompleted();
3951 }
3952 mPendingExecutor = executor;
3953 executor.attachTo(this);
3954 }
3955
3956 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3957 if (mPendingExecutor == executor) {
3958 mPendingExecutor = null;
3959 }
3960 }
3961
Joe Onorato9c1289c2009-08-17 11:03:03 -04003962 /**
3963 * Callback saying that there aren't any more items to bind.
3964 *
3965 * Implementation of the method from LauncherModel.Callbacks.
3966 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003967 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003968 Runnable r = new Runnable() {
3969 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003970 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003971 }
3972 };
3973 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003974 return;
3975 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003976 if (mSavedState != null) {
3977 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003978 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003979 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003980
Joe Onorato9c1289c2009-08-17 11:03:03 -04003981 mSavedState = null;
3982 }
3983
Adam Cohen1462de32012-07-24 22:34:36 -07003984 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003985
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003986 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003987
3988 // If we received the result of any pending adds while the loader was running (e.g. the
3989 // widget configuration forced an orientation change), process them now.
3990 if (sPendingAddItem != null) {
3991 final long screenId = completeAdd(sPendingAddItem);
3992
3993 // TODO: this moves the user to the page where the pending item was added. Ideally,
3994 // the screen would be guaranteed to exist after bind, and the page would be set through
3995 // the workspace restore process.
3996 mWorkspace.post(new Runnable() {
3997 @Override
3998 public void run() {
3999 mWorkspace.snapToScreenId(screenId);
4000 }
4001 });
4002 sPendingAddItem = null;
4003 }
4004
Sunny Goyal756adbc2015-04-16 15:20:51 -07004005 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004006
4007 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004008 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004009 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004010 }
4011
Winson Chunga0b7e862013-09-05 16:03:15 -07004012 public boolean isAllAppsButtonRank(int rank) {
4013 if (mHotseat != null) {
4014 return mHotseat.isAllAppsButtonRank(rank);
4015 }
4016 return false;
4017 }
4018
Winson Chunga2413752012-04-03 14:22:34 -07004019 private boolean canRunNewAppsAnimation() {
4020 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004021 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4022 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004023 }
4024
Winson Chungc9168342013-06-26 14:54:55 -07004025 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004026 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004027 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4028 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004029 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004030 return bounceAnim;
4031 }
4032
Adam Cohen27772732013-11-11 14:00:56 +00004033 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004034 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004035 }
4036
Tony Wickham3a3517f2015-10-06 11:27:04 -07004037 /** Returns the search bar bounds in pixels. */
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004038 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004039 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004040 }
4041
Tony Wickham55616cd2015-09-23 14:55:17 -07004042 public int getSearchBarHeight() {
4043 if (mLauncherCallbacks != null) {
4044 return mLauncherCallbacks.getSearchBarHeight();
4045 }
4046 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4047 }
4048
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004049 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004050 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4051 * multiple calls to bind the same list.)
4052 */
4053 @Thunk ArrayList<AppInfo> mTmpAppsList;
4054 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4055 public void run() {
4056 bindAllApplications(mTmpAppsList);
4057 mTmpAppsList = null;
4058 }
4059 };
4060
4061 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004062 * Add the icons for all apps.
4063 *
4064 * Implementation of the method from LauncherModel.Callbacks.
4065 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004066 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004067 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4068 mTmpAppsList = apps;
4069 return;
4070 }
4071
Winson Chungb745afb2015-03-02 11:51:23 -08004072 if (mAppsView != null) {
4073 mAppsView.setApps(apps);
4074 }
Adam Cohen9211d422014-10-07 18:14:53 -07004075 if (mLauncherCallbacks != null) {
4076 mLauncherCallbacks.bindAllApplications(apps);
4077 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004078 }
4079
4080 /**
4081 * A package was updated.
4082 *
4083 * Implementation of the method from LauncherModel.Callbacks.
4084 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004085 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004086 Runnable r = new Runnable() {
4087 public void run() {
4088 bindAppsUpdated(apps);
4089 }
4090 };
4091 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004092 return;
4093 }
4094
Winson Chungb745afb2015-03-02 11:51:23 -08004095 if (mAppsView != null) {
4096 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004097 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004098 }
4099
Sunny Goyal4390ace2014-10-13 11:33:11 -07004100 @Override
4101 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4102 Runnable r = new Runnable() {
4103 public void run() {
4104 bindWidgetsRestored(widgets);
4105 }
4106 };
4107 if (waitUntilResume(r)) {
4108 return;
4109 }
4110 mWorkspace.widgetsRestored(widgets);
4111 }
4112
Joe Onorato9c1289c2009-08-17 11:03:03 -04004113 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004114 * Some shortcuts were updated in the background.
4115 *
4116 * Implementation of the method from LauncherModel.Callbacks.
4117 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004118 @Override
4119 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4120 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004121 Runnable r = new Runnable() {
4122 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004123 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004124 }
4125 };
4126 if (waitUntilResume(r)) {
4127 return;
4128 }
4129
Sunny Goyal4390ace2014-10-13 11:33:11 -07004130 if (!updated.isEmpty()) {
4131 mWorkspace.updateShortcuts(updated);
4132 }
4133
4134 if (!removed.isEmpty()) {
4135 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4136 for (ShortcutInfo si : removed) {
4137 removedComponents.add(si.getTargetComponent());
4138 }
4139 mWorkspace.removeItemsByComponentName(removedComponents, user);
4140 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004141 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004142 }
4143 }
4144
4145 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004146 * Update the state of a package, typically related to install state.
4147 *
4148 * Implementation of the method from LauncherModel.Callbacks.
4149 */
Sunny Goyale755d462014-07-22 13:48:29 -07004150 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004151 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4152 Runnable r = new Runnable() {
4153 public void run() {
4154 bindRestoreItemsChange(updates);
4155 }
4156 };
4157 if (waitUntilResume(r)) {
4158 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004159 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004160
Sunny Goyal756adbc2015-04-16 15:20:51 -07004161 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004162 }
4163
4164 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004165 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004166 * in addition to specific applications to remove, the reason being that
4167 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004168 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004169 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004170 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004171 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004172 public void bindWorkspaceComponentsRemoved(
4173 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4174 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004175 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004176 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004177 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004178 }
Winson Chungd64d1762013-08-20 14:37:16 -07004179 };
4180 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004181 return;
4182 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004183 if (!packageNames.isEmpty()) {
4184 mWorkspace.removeItemsByPackageName(packageNames, user);
4185 }
4186 if (!components.isEmpty()) {
4187 mWorkspace.removeItemsByComponentName(components, user);
4188 }
4189 // Notify the drag controller
4190 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004191
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004192 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004193
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004194 @Override
4195 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4196 Runnable r = new Runnable() {
4197 public void run() {
4198 bindAppInfosRemoved(appInfos);
4199 }
4200 };
4201 if (waitUntilResume(r)) {
4202 return;
Joe Onorato36115782010-06-17 13:28:48 -04004203 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004204
Winson Chungdf95eb12013-10-16 14:57:07 -07004205 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004206 if (mAppsView != null) {
4207 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004208 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004209 }
Joe Onoratobe386092009-11-17 17:32:16 -08004210
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004211 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004212 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004213 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004214 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004215 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004216
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004217 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004218 public void bindWidgetsModel(WidgetsModel model) {
4219 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004220 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004221 return;
4222 }
4223
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004224 if (mWidgetsView != null && model != null) {
4225 mWidgetsView.addWidgets(model);
4226 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004227 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004228 }
4229
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004230 @Override
4231 public void notifyWidgetProvidersChanged() {
4232 if (mWorkspace.getState().shouldUpdateWidget) {
4233 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4234 }
4235 }
4236
Winson Chung400438b2011-01-16 17:53:48 -08004237 private int mapConfigurationOriActivityInfoOri(int configOri) {
4238 final Display d = getWindowManager().getDefaultDisplay();
4239 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4240 switch (d.getRotation()) {
4241 case Surface.ROTATION_0:
4242 case Surface.ROTATION_180:
4243 // We are currently in the same basic orientation as the natural orientation
4244 naturalOri = configOri;
4245 break;
4246 case Surface.ROTATION_90:
4247 case Surface.ROTATION_270:
4248 // We are currently in the other basic orientation to the natural orientation
4249 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4250 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4251 break;
4252 }
4253
4254 int[] oriMap = {
4255 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4256 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4257 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4258 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4259 };
4260 // Since the map starts at portrait, we need to offset if this device's natural orientation
4261 // is landscape.
4262 int indexOffset = 0;
4263 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4264 indexOffset = 1;
4265 }
4266 return oriMap[(d.getRotation() + indexOffset) % 4];
4267 }
Adam Cohen446e9402011-09-15 18:21:21 -07004268
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004269 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004270 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004271 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004272 if (Utilities.ATLEAST_JB_MR2) {
4273 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4274 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004275 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4276 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004277 }
Winson Chung4b919f82012-05-01 10:44:08 -07004278 }
4279 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004280
Winson Chung4b919f82012-05-01 10:44:08 -07004281 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004282 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004283 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004284 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004285 } else {
4286 mHandler.postDelayed(new Runnable() {
4287 public void run() {
4288 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4289 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004290 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004291 }
Winson Chung4b919f82012-05-01 10:44:08 -07004292 }
Winson Chung400438b2011-01-16 17:53:48 -08004293 }
4294
Adam Cohenea90f832014-05-21 15:03:34 -07004295 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004296 * To be overridden by subclasses to indicate that there is an activity to launch
4297 * before showing the standard launcher experience.
4298 */
4299 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004300 if (mLauncherCallbacks != null) {
4301 return mLauncherCallbacks.hasFirstRunActivity();
4302 }
Adam Cohen432609a2014-03-13 17:03:22 -07004303 return false;
4304 }
4305
4306 /**
4307 * To be overridden by subclasses to launch any first run activity
4308 */
4309 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004310 if (mLauncherCallbacks != null) {
4311 return mLauncherCallbacks.getFirstRunActivity();
4312 }
Adam Cohen432609a2014-03-13 17:03:22 -07004313 return null;
4314 }
4315
Winson Chunga6945242014-01-08 14:04:34 -08004316 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004317 return !ActivityManager.isRunningInTestHarness() &&
4318 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004319 }
4320
Adam Cohen4a9423d2014-03-28 14:22:31 -07004321 protected boolean hasRunFirstRunActivity() {
4322 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4323 }
4324
Adam Cohen432609a2014-03-13 17:03:22 -07004325 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004326 if (shouldRunFirstRunActivity() &&
4327 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004328 Intent firstRunIntent = getFirstRunActivity();
4329 if (firstRunIntent != null) {
4330 startActivity(firstRunIntent);
4331 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004332 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004333 }
4334 }
Adam Cohen432609a2014-03-13 17:03:22 -07004335 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004336 }
4337
4338 private void markFirstRunActivityShown() {
4339 SharedPreferences.Editor editor = mSharedPrefs.edit();
4340 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4341 editor.apply();
4342 }
4343
Adam Cohen432609a2014-03-13 17:03:22 -07004344 /**
4345 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4346 * screen that must be displayed and dismissed.
4347 */
4348 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004349 if (mLauncherCallbacks != null) {
4350 return mLauncherCallbacks.hasDismissableIntroScreen();
4351 }
Adam Cohen432609a2014-03-13 17:03:22 -07004352 return false;
4353 }
4354
4355 /**
4356 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4357 */
4358 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004359 if (mLauncherCallbacks != null) {
4360 return mLauncherCallbacks.getIntroScreen();
4361 }
Adam Cohen432609a2014-03-13 17:03:22 -07004362 return null;
4363 }
4364
4365 /**
4366 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4367 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4368 */
4369 private boolean shouldShowIntroScreen() {
4370 return hasDismissableIntroScreen() &&
4371 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4372 }
4373
4374 protected void showIntroScreen() {
4375 View introScreen = getIntroScreen();
4376 changeWallpaperVisiblity(false);
4377 if (introScreen != null) {
4378 mDragLayer.showOverlayView(introScreen);
4379 }
4380 }
4381
4382 public void dismissIntroScreen() {
4383 markIntroScreenDismissed();
4384 if (showFirstRunActivity()) {
4385 // We delay hiding the intro view until the first run activity is showing. This
4386 // avoids a blip.
4387 mWorkspace.postDelayed(new Runnable() {
4388 @Override
4389 public void run() {
4390 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004391 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004392 }
4393 }, ACTIVITY_START_DELAY);
4394 } else {
4395 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004396 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004397 }
4398 changeWallpaperVisiblity(true);
4399 }
4400
4401 private void markIntroScreenDismissed() {
4402 SharedPreferences.Editor editor = mSharedPrefs.edit();
4403 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4404 editor.apply();
4405 }
4406
Adam Cohen091440a2015-03-18 14:16:05 -07004407 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004408 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4409 // on the device, then we always show the first run cling experience (or if there is no
4410 // launcher2). Otherwise, we prompt the user upon started for migration
4411 LauncherClings launcherClings = new LauncherClings(this);
4412 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004413 mClings = launcherClings;
Sunny Goyalded0fdb2016-05-23 15:55:41 -07004414 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004415 }
4416 }
4417
Winson Chung5ffd51d2015-06-25 11:36:50 -07004418 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004419 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004420 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004421 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004422 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004423 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004424 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4425 if (userHandle != null) {
4426 user = UserHandleCompat.fromUser(userHandle);
4427 }
4428 }
4429 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004430 }
4431
4432 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004433 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004434 if (user == null) {
4435 user = UserHandleCompat.myUserHandle();
4436 }
4437
4438 // Called from search suggestion, add the profile extra to the intent to ensure that we
4439 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004440 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004441 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004442 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004443 return null;
4444 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004445 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004446 }
4447
Winson Chung5ffd51d2015-06-25 11:36:50 -07004448 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004449 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4450 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004451 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004452 UserHandleCompat.myUserHandle());
4453 }
4454
Winson Chung5ffd51d2015-06-25 11:36:50 -07004455 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004456 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4457 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004458 mWorkspace.onExternalDragStartedWithItem(dragView);
4459 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004460 }
4461
Winson Chung5ffd51d2015-06-25 11:36:50 -07004462 protected void moveWorkspaceToDefaultScreen() {
4463 mWorkspace.moveToDefaultScreen(false);
4464 }
4465
Winson Chung80baf5a2010-08-09 16:03:15 -07004466 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004467 * Returns a FastBitmapDrawable with the icon, accurately sized.
4468 */
4469 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4470 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4471 d.setFilterBitmap(true);
4472 resizeIconDrawable(d);
4473 return d;
4474 }
4475
4476 /**
4477 * Resizes an icon drawable to the correct icon size.
4478 */
Winson5fbe0742015-09-30 15:33:00 -07004479 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004480 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004481 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004482 }
4483
4484 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004485 * Prints out out state for debugging.
4486 */
4487 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004488 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004489 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004490 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4491 Log.d(TAG, "mRestoring=" + mRestoring);
4492 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004493 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004494 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004495
Daniel Sandler325dc232013-06-05 22:57:57 -04004496 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004497 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004498
4499 @Override
4500 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4501 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004502 // Dump workspace
4503 writer.println(prefix + "Workspace Items");
4504 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4505 writer.println(prefix + " Homescreen " + i);
4506
4507 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4508 for (int j = 0; j < layout.getChildCount(); j++) {
4509 Object tag = layout.getChildAt(j).getTag();
4510 if (tag != null) {
4511 writer.println(prefix + " " + tag.toString());
4512 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004513 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004514 }
Sunny Goyala1365452015-10-01 15:46:24 -07004515
4516 writer.println(prefix + " Hotseat");
4517 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4518 for (int j = 0; j < layout.getChildCount(); j++) {
4519 Object tag = layout.getChildAt(j).getTag();
4520 if (tag != null) {
4521 writer.println(prefix + " " + tag.toString());
4522 }
4523 }
4524
Sunny Goyal713edfc2016-05-06 09:58:34 -07004525 try {
4526 FileLog.flushAll(writer);
4527 } catch (Exception e) {
4528 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004529 }
4530
Adam Cohen9211d422014-10-07 18:14:53 -07004531 if (mLauncherCallbacks != null) {
4532 mLauncherCallbacks.dump(prefix, fd, writer, args);
4533 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004534 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004535
Adam Cohen59400422014-03-05 18:07:04 -08004536 public static CustomAppWidget getCustomAppWidget(String name) {
4537 return sCustomAppWidgets.get(name);
4538 }
4539
4540 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4541 return sCustomAppWidgets;
4542 }
4543
Sunny Goyal29538332016-02-18 09:10:19 -08004544 public static List<View> getFolderContents(View icon) {
4545 if (icon instanceof FolderIcon) {
4546 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4547 } else {
4548 return Collections.EMPTY_LIST;
4549 }
4550 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004551
4552 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4553
4554 @Override
4555 public void onSharedPreferenceChanged(
4556 SharedPreferences sharedPreferences, String key) {
4557 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4558 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4559 if (!waitUntilResume(this, true)) {
4560 run();
4561 }
4562 }
4563 }
4564
4565 @Override
4566 public void run() {
4567 setOrientation();
4568 }
4569 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004570}