blob: 91014a6a61d616f526f97af63f4681f1bf50a19d [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyal28c6b962015-10-12 11:42:05 -070019import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070025import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000026import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080037import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Sunny Goyal756cd262015-08-20 12:33:21 -070039import android.content.ContentValues;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040041import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070044import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070046import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070050import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070053import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070056import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020059import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020061import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080062import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070063import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070064import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070066import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.HapticFeedbackConstants;
74import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.MotionEvent;
77import android.view.Surface;
78import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070079import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080080import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070081import android.view.ViewGroup;
82import android.view.ViewTreeObserver;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080084import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070085import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070086import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080090import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070092
Sunny Goyal651077b2014-06-30 14:15:31 -070093import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070094import com.android.launcher3.LauncherSettings.Favorites;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070095import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070096import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080097import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -070098import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010099import com.android.launcher3.compat.LauncherActivityInfoCompat;
100import com.android.launcher3.compat.LauncherAppsCompat;
101import com.android.launcher3.compat.UserHandleCompat;
102import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800103import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700104import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700105import com.android.launcher3.dragndrop.DragController;
106import com.android.launcher3.dragndrop.DragLayer;
107import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -0700108import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000109import com.android.launcher3.folder.Folder;
110import com.android.launcher3.folder.FolderIcon;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700111import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700112import com.android.launcher3.logging.FileLog;
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800113import com.android.launcher3.logging.LoggerUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700114import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700115import com.android.launcher3.model.WidgetsModel;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700116import com.android.launcher3.pageindicators.PageIndicator;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800117import com.android.launcher3.userevent.nano.LauncherLogProto;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700118import com.android.launcher3.util.ComponentKey;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700119import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal322d5562015-06-25 19:35:49 -0700120import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700121import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700122import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700123import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700124import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700125import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700126
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700127import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700128import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700129import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700130import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800131import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700132import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700133import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700134import java.util.List;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800135import java.util.Locale;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700136
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137/**
138 * Default launcher application.
139 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100140public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700141 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
142 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700143 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700144 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700145 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
Daniel Sandlerf061f822013-06-27 13:59:36 -0400147 static final boolean PROFILE_STARTUP = false;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700148 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700149 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400150 static final boolean DEBUG_RESUME_TIME = false;
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700151 static final boolean DEBUG_LOGGING = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700154 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700155 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700156 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
Michael Jurka8b805b12012-04-18 14:23:14 -0700158 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700159 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700160 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700161
Sunny Goyal28c6b962015-10-12 11:42:05 -0700162 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
163
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700164 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
165 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
166 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
167
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700168 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
169
Mathew Inwood876a8462013-06-14 14:12:41 +0100170 /**
171 * IntentStarter uses request codes starting with this. This must be greater than all activity
172 * request codes used internally.
173 */
174 protected static final int REQUEST_LAST = 100;
175
Michael Jurka0a457bf2012-11-19 14:05:05 -0800176 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700177 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800178 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
Winson Chung2672ff92012-05-04 16:22:30 -0700180 // The Intent extra that defines whether to ignore the launch animation
181 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400182 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700183
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700184 public static final String ACTION_APPWIDGET_HOST_RESET =
185 "com.android.launcher3.intent.ACTION_APPWIDGET_HOST_RESET";
186
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 // Type: int
188 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700189 // Type: int
190 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700191 // Type: Content Values / parcelable
192 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700193 // Type: parcelable
194 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000195 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800196 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197
Adam Cohen432609a2014-03-13 17:03:22 -0700198 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800199 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
200
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700201 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700202 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
203 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700204
Adam Cohen091440a2015-03-18 14:16:05 -0700205 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700206 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700207
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700208 private boolean mIsSafeModeEnabled;
209
Joe Onorato9c1289c2009-08-17 11:03:03 -0400210 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800211 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700212 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700213 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214
Winson Chunga2413752012-04-03 14:22:34 -0700215 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700216 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
217 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700218 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700219
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700220 private final BroadcastReceiver mUiBroadcastReceiver = new BroadcastReceiver() {
221
222 @Override
223 public void onReceive(Context context, Intent intent) {
224 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
225 closeSystemDialogs();
226 } else if (ACTION_APPWIDGET_HOST_RESET.equals(intent.getAction())) {
227 if (mAppWidgetHost != null) {
228 mAppWidgetHost.startListening();
229 }
230 }
231 }
232 };
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800233
Adam Cohen091440a2015-03-18 14:16:05 -0700234 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700235 private View mLauncherView;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700236 private PageIndicator mPageIndicator;
Adam Cohen091440a2015-03-18 14:16:05 -0700237 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800238 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700239
240 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700241
Sunny Goyalffe83f12014-08-14 17:39:34 -0700242 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700243 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700244
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700245 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800246 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800247 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700248
Michael Jurka0280c3b2010-09-17 15:00:07 -0700249 private int[] mTmpAddItemCellCoordinates = new int[2];
250
Sunny Goyal316490e2015-06-02 09:38:28 -0700251 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800252 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700253
Michael Jurka838a4ca2011-02-07 13:33:06 -0800254 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700255 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700256
Sunny Goyal47328fd2016-05-25 18:56:41 -0700257 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700258
259 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700260 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700261 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700262
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700263 // Main container view and the model for the widget tray screen.
264 @Thunk WidgetsContainerView mWidgetsView;
265 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700266
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700268 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
269 // scroll issues (because the workspace may not have been measured yet) and extra work.
270 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
271 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272
273 private SpannableStringBuilder mDefaultKeySsb = null;
274
Adam Cohen091440a2015-03-18 14:16:05 -0700275 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400276
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800277 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278 private boolean mRestoring;
279 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700280 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281
Michael Jurka1e2f4652013-07-08 18:03:46 -0700282 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700283 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700284 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700285
Joe Onorato9c1289c2009-08-17 11:03:03 -0400286 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800287 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700288 private ExtractedColors mExtractedColors;
Adam Cohen091440a2015-03-18 14:16:05 -0700289 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800290 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700291 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800292 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400293
Sunny Goyal639e9062015-08-19 19:17:06 -0700294 private LauncherClings mClings;
295
Adam Cohen61f560d2013-09-30 15:58:20 -0700296 private View.OnTouchListener mHapticFeedbackTouchListener;
297
Adam Cohended9f8d2010-11-03 13:25:16 -0700298 // Related to the auto-advancing of widgets
299 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700300 private static final int ADVANCE_INTERVAL = 20000;
301 private static final int ADVANCE_STAGGER = 250;
302
303 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700304 private long mAutoAdvanceSentTime;
305 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700306 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700307
Winson Chung400438b2011-01-16 17:53:48 -0800308 // Determines how long to wait after a rotation before restoring the screen orientation to
309 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700310 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800311
Adam Cohen091440a2015-03-18 14:16:05 -0700312 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700313
Adam Cohen1462de32012-07-24 22:34:36 -0700314 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700315 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700316
Winson Chung46353de2012-02-16 14:05:10 -0800317 // We only want to get the SharedPreferences once since it does an FS stat each time we get
318 // it from the context.
319 private SharedPreferences mSharedPrefs;
320
Winson Chungf0c6ae02012-03-21 16:10:31 -0700321 // Holds the page that we need to animate to, and the icon views that we need to animate up
322 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700323 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700324 private Bitmap mFolderIconBitmap;
325 private Canvas mFolderIconCanvas;
326 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700327
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700328 private DeviceProfile mDeviceProfile;
329
Adam Cohen4b66bf32015-09-18 12:15:19 -0700330 private boolean mMoveToDefaultScreenFromNewIntent;
331
Winson Chung13eb5272015-05-11 16:30:13 -0700332 // This is set to the view that launched the activity that navigated the user away from
333 // launcher. Since there is no callback for when the activity has finished launching, enable
334 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800335 private BubbleTextView mWaitingForResume;
336
Adam Cohen59400422014-03-05 18:07:04 -0800337 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
338 new HashMap<String, CustomAppWidget>();
339
Adam Cohen59400422014-03-05 18:07:04 -0800340 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700341 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
342 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800343 }
344 }
345
Adam Cohen091440a2015-03-18 14:16:05 -0700346 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700347 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700348 if (mWorkspace != null) {
349 mWorkspace.buildPageHardwareLayers();
350 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700351 }
352 };
353
Adam Cohendb364c32014-05-20 14:23:40 -0700354 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800355
Adam Cohen091440a2015-03-18 14:16:05 -0700356 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800357 int requestCode;
358 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700359 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700360 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800361 int cellX;
362 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700363 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800364 }
365
Hyunyoung Songaa953652016-04-19 18:30:24 -0700366 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800367
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700368 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700369 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400370
371 @Thunk void setOrientation() {
372 if (mRotationEnabled) {
373 unlockScreenOrientation(true);
374 } else {
375 setRequestedOrientation(
376 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700377 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400378 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700379
Sunny Goyal745bad92016-05-02 10:54:12 -0700380 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700381
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 @Override
383 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700384 if (DEBUG_STRICT_MODE) {
385 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
386 .detectDiskReads()
387 .detectDiskWrites()
388 .detectNetwork() // or .detectAll() for all detectable problems
389 .penaltyLog()
390 .build());
391 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
392 .detectLeakedSqlLiteObjects()
393 .detectLeakedClosableObjects()
394 .penaltyLog()
395 .penaltyDeath()
396 .build());
397 }
398
Adam Cohen9211d422014-10-07 18:14:53 -0700399 if (mLauncherCallbacks != null) {
400 mLauncherCallbacks.preOnCreate();
401 }
402
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400404
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400405 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700406
Adam Cohen2e6da152015-05-06 11:42:25 -0700407 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700408 mDeviceProfile = getResources().getConfiguration().orientation
409 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700410 app.getInvariantDeviceProfile().landscapeProfile
411 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700412
Sunny Goyalf7258242015-10-19 16:59:07 -0700413 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700414 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800415 mModel = app.setLauncher(this);
416 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700417
Joe Onorato41a12d22009-10-31 18:30:00 -0400418 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700419 mAllAppsController = new AllAppsTransitionController(this);
420 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700421
Sunny Goyalffe83f12014-08-14 17:39:34 -0700422 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700423
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700424 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
425 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700426
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700427 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
428 // this also ensures that any synchronous binding below doesn't re-trigger another
429 // LauncherModel load.
430 mPaused = false;
431
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200433 android.os.Debug.startMethodTracing(
434 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800435 }
436
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700438
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700440 mDeviceProfile.layout(this);
Tony Wickham827cef22016-03-17 15:39:39 -0700441 mExtractedColors = new ExtractedColors();
442 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443
Tony Wickhame2217252016-03-22 16:34:23 -0700444 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
445 .addAccessibilityStateChangeListener(this);
446
Joe Onorato7c312c12009-08-13 21:36:53 -0700447 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700448
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 mSavedState = savedInstanceState;
450 restoreState(mSavedState);
451
452 if (PROFILE_STARTUP) {
453 android.os.Debug.stopMethodTracing();
454 }
455
456 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700457 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700458 // If the user leaves launcher, then we should just load items asynchronously when
459 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700460 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700461 } else {
462 // We only load the page synchronously if the user rotates (or triggers a
463 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700464 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700465 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 }
467
468 // For handling default keys
469 mDefaultKeySsb = new SpannableStringBuilder();
470 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800471
472 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700473 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
474 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800475
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800476 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700477 // In case we are on a device with locked rotation, we should look at preferences to check
478 // if the user has specifically allowed rotation.
479 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700480 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700481 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
482 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700483 }
484
485 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
486 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400487 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700488
Adam Cohen9211d422014-10-07 18:14:53 -0700489 if (mLauncherCallbacks != null) {
490 mLauncherCallbacks.onCreate(savedInstanceState);
491 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700492
493 if (shouldShowIntroScreen()) {
494 showIntroScreen();
495 } else {
496 showFirstRunActivity();
497 showFirstRunClings();
498 }
Adam Cohen9211d422014-10-07 18:14:53 -0700499 }
500
Sunny Goyal7779d622015-06-11 16:18:39 -0700501 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700502 public void onExtractedColorsChanged() {
503 loadExtractedColorsAndColorItems();
504 }
505
506 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700507 // TODO: do this in pre-N as well, once the extraction part is complete.
508 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700509 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700510 mHotseat.updateColor(mExtractedColors, !mPaused);
Tony Wickham770217c2016-05-18 15:16:40 -0700511 if (mPageIndicator != null) {
512 mPageIndicator.updateColor(mExtractedColors);
513 }
Tony Wickham827cef22016-03-17 15:39:39 -0700514 }
515 }
516
Adam Cohen9211d422014-10-07 18:14:53 -0700517 private LauncherCallbacks mLauncherCallbacks;
518
519 public void onPostCreate(Bundle savedInstanceState) {
520 super.onPostCreate(savedInstanceState);
521 if (mLauncherCallbacks != null) {
522 mLauncherCallbacks.onPostCreate(savedInstanceState);
523 }
524 }
525
Sunny Goyal32554d12015-12-03 15:31:25 -0800526 /**
527 * Call this after onCreate to set or clear overlay.
528 */
529 public void setLauncherOverlay(LauncherOverlay overlay) {
530 if (overlay != null) {
531 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
532 }
533 mWorkspace.setLauncherOverlay(overlay);
534 }
535
Adam Cohen9211d422014-10-07 18:14:53 -0700536 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
537 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700538 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700539 private boolean mWorkspaceImportanceStored = false;
540 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700541 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800542 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700543 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
544
545 @Override
546 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700547 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700548 return;
549 }
550 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700551 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700552 if (mWorkspace != null) {
553 mWorkspaceImportanceForAccessibility =
554 mWorkspace.getImportantForAccessibility();
555 mWorkspace.setImportantForAccessibility(
556 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
557 mWorkspaceImportanceStored = true;
558 }
559 if (mHotseat != null) {
560 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
561 mHotseat.setImportantForAccessibility(
562 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
563 mHotseatImportanceStored = true;
564 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700565 }
566
567 @Override
568 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700569 if (mWorkspaceImportanceStored && mWorkspace != null) {
570 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
571 }
572 if (mHotseatImportanceStored && mHotseat != null) {
573 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
574 }
575 mWorkspaceImportanceStored = false;
576 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700577 }
578 });
Adam Cohen9211d422014-10-07 18:14:53 -0700579 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700580 }
581
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700582 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700583 public void onLauncherProviderChange() {
584 if (mLauncherCallbacks != null) {
585 mLauncherCallbacks.onLauncherProviderChange();
586 }
587 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700588
Adam Cohen9211d422014-10-07 18:14:53 -0700589 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700590 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700591 if (mLauncherCallbacks != null) {
592 return mLauncherCallbacks.hasCustomContentToLeft();
593 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700594 return false;
595 }
596
Dave Hawkeya8881582013-09-17 15:55:33 -0600597 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500598 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600599 * {@link #addToCustomContentPage}. This will only be invoked if
600 * {@link #hasCustomContentToLeft()} is {@code true}.
601 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500602 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700603 if (mLauncherCallbacks != null) {
604 mLauncherCallbacks.populateCustomContentContainer();
605 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600606 }
607
608 /**
609 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
610 * ensure the custom content page is added or removed if necessary.
611 */
612 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600613 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600614 // Not bound yet, wait for bindScreens to be called.
615 return;
616 }
617
618 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
619 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500620 mWorkspace.createCustomContentContainer();
621 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600622 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
623 mWorkspace.removeCustomContentPage();
624 }
625 }
626
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800627 /**
628 * Logger object is a singleton and does not have to be coupled with the foreground activity.
629 * Since most user event logging is done on the UI, the object is retrieved from the
630 * callback for convenience.
631 */
Hyunyoung Songaa953652016-04-19 18:30:24 -0700632 private UserEventDispatcher createUserEventDispatcher() {
633 return new UserEventDispatcher() {
634 @Override
635 public void dispatchUserEvent(LauncherLogProto.LauncherEvent ev, Intent intent) {
636 if (!DEBUG_LOGGING) {
637 return;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800638 }
Hyunyoung Songaa953652016-04-19 18:30:24 -0700639 Log.d("UserEvent", String.format(Locale.US,
640 "action:%s\nchild:%s\nparent:%s\nelapsed container %d ms session %d ms",
641 LoggerUtils.getActionStr(ev.action),
642 LoggerUtils.getTargetStr(ev.srcTarget[0]),
643 LoggerUtils.getTargetStr(ev.srcTarget[1]),
644 ev.elapsedContainerMillis,
645 ev.elapsedSessionMillis));
646 }
647 };
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800648 }
649
Hyunyoung Songaa953652016-04-19 18:30:24 -0700650 public UserEventDispatcher getUserEventDispatcher() {
651 if (mLauncherCallbacks != null) {
652 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
653 if (dispatcher != null) {
654 return dispatcher;
655 }
656 }
657
658 if (mUserEventDispatcher == null) {
659 mUserEventDispatcher = createUserEventDispatcher();
660 }
661 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800662 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100663
Hyunyoung Song3f471442015-04-08 19:01:34 -0700664 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700665 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
666 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700667 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700668 }
669
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000670 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700671 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
672 // This cast is safe as long as the id < 0x00FFFFFF
673 // Since we jail all the dynamically generated views, there should be no clashes
674 // with any other views.
675 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000676 }
677
678 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700679 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
680 * a configuration step, this allows the proper animations to run after other transitions.
681 */
Adam Cohendb364c32014-05-20 14:23:40 -0700682 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700683 long screenId = args.screenId;
684 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
685 // When the screen id represents an actual screen (as opposed to a rank) we make sure
686 // that the drop page actually exists.
687 screenId = ensurePendingDropLayoutExists(args.screenId);
688 }
Adam Cohendb364c32014-05-20 14:23:40 -0700689
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800690 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800691 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700692 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700693 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700694 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800695 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700696 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700697 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700698 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyald478c832016-04-01 12:04:16 -0700699 completeRestoreAppWidget(args.appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700700 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700701 case REQUEST_BIND_PENDING_APPWIDGET: {
702 int widgetId = args.appWidgetId;
703 LauncherAppWidgetInfo info =
704 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
705 if (info != null) {
706 // Since the view was just bound, also launch the configure activity if needed
707 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
708 .getLauncherAppWidgetInfo(widgetId);
709 if (provider != null && provider.configure != null) {
710 startRestoredWidgetReconfigActivity(provider, info);
711 }
712 }
713 break;
714 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800715 }
Michael Jurka27614d22012-04-02 06:40:22 -0700716 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
717 // if you turned the screen off and then back while in All Apps, Launcher would not
718 // return to the workspace. Clearing mAddInfo.container here fixes this issue
719 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700720 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800721 }
722
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800723 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700724 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700725 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700726 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700727 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800728 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700729
Adam Cohenad4e15c2013-10-17 16:21:35 -0700730 Runnable exitSpringLoaded = new Runnable() {
731 @Override
732 public void run() {
733 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
734 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
735 }
736 };
737
Michael Jurka8b805b12012-04-18 14:23:14 -0700738 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700739 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700740 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700741 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
742 if (resultCode == RESULT_CANCELED) {
743 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700744 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700745 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700746 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800747 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700748 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700749 }
750 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200751 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
752 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700753 // User could have free-scrolled between pages before picking a wallpaper; make sure
754 // we move to the closest one now.
755 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700756 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200757 }
758 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700759 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200760
Adam Cohened66b2b2012-01-23 17:28:51 -0800761 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700762 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700763
Adam Cohendb364c32014-05-20 14:23:40 -0700764 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800765 // We have special handling for widgets
766 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800767 final int appWidgetId;
768 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
769 : -1;
770 if (widgetId < 0) {
771 appWidgetId = pendingAddWidgetId;
772 } else {
773 appWidgetId = widgetId;
774 }
775
Adam Cohenad4e15c2013-10-17 16:21:35 -0700776 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800777 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700778 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
779 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700780 result = RESULT_CANCELED;
781 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700782 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700783 @Override
784 public void run() {
785 exitSpringLoadedDragModeDelayed(false, 0, null);
786 }
787 };
Adam Cohendb364c32014-05-20 14:23:40 -0700788 if (workspaceLocked) {
789 // No need to remove the empty screen if we're mid-binding, as the
790 // the bind will not add the empty screen.
791 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
792 } else {
793 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
794 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
795 }
Winson Chung5aaab772012-04-27 15:24:02 -0700796 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700797 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700798 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
799 // When the screen id represents an actual screen (as opposed to a rank)
800 // we make sure that the drop page actually exists.
801 mPendingAddInfo.screenId =
802 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
803 }
Adam Cohendb364c32014-05-20 14:23:40 -0700804 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
805
806 dropLayout.setDropPending(true);
807 final Runnable onComplete = new Runnable() {
808 @Override
809 public void run() {
810 completeTwoStageWidgetDrop(resultCode, appWidgetId);
811 dropLayout.setDropPending(false);
812 }
813 };
814 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
815 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
816 } else {
817 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
818 mPendingAddInfo);
819 sPendingAddItem = args;
820 }
Winson Chung5aaab772012-04-27 15:24:02 -0700821 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800822 return;
823 }
824
Sunny Goyald478c832016-04-01 12:04:16 -0700825 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
826 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700827 if (resultCode == RESULT_OK) {
828 // Update the widget view.
829 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
830 pendingAddWidgetId, mPendingAddInfo);
831 if (workspaceLocked) {
832 sPendingAddItem = args;
833 } else {
834 completeAdd(args);
835 }
836 }
837 // Leave the widget in the pending state if the user canceled the configure.
838 return;
839 }
840
Sunny Goyalaad90582015-07-09 14:22:50 -0700841 if (requestCode == REQUEST_CREATE_SHORTCUT) {
842 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
843 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
844 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
845 mPendingAddInfo);
846 if (isWorkspaceLocked()) {
847 sPendingAddItem = args;
848 } else {
849 completeAdd(args);
850 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
851 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
852 }
853 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700854 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
855 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800858 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800859
860 }
861
862 @Override
863 protected void onActivityResult(
864 final int requestCode, final int resultCode, final Intent data) {
865 handleActivityResult(requestCode, resultCode, data);
866 if (mLauncherCallbacks != null) {
867 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
868 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800869 }
870
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600871 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700872 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600873 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700874 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
875 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
876 View v = null;
877 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
878 if (layout != null) {
879 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
880 }
881 Intent intent = sPendingAddItem.intent;
882 sPendingAddItem = null;
883 if (grantResults.length > 0
884 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700885 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700886 } else {
887 // TODO: Show a snack bar with link to settings
888 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
889 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
890 }
891 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600892 if (mLauncherCallbacks != null) {
893 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
894 grantResults);
895 }
896 }
897
Adam Cohendb364c32014-05-20 14:23:40 -0700898 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
899 appWidgetId, ItemInfo info) {
900 PendingAddArguments args = new PendingAddArguments();
901 args.requestCode = requestCode;
902 args.intent = data;
903 args.container = info.container;
904 args.screenId = info.screenId;
905 args.cellX = info.cellX;
906 args.cellY = info.cellY;
907 args.appWidgetId = appWidgetId;
908 return args;
909 }
910
911 /**
912 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
913 *
914 * @param screenId the screen id to check
915 * @return the new screen, or screenId if it exists
916 */
917 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800918 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700919 if (dropLayout == null) {
920 // it's possible that the add screen was removed because it was
921 // empty and a re-bind occurred
922 mWorkspace.addExtraEmptyScreen();
923 return mWorkspace.commitExtraEmptyScreen();
924 } else {
925 return screenId;
926 }
927 }
928
Adam Cohen091440a2015-03-18 14:16:05 -0700929 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800930 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800931 Runnable onCompleteRunnable = null;
932 int animationType = 0;
933
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700934 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800935 if (resultCode == RESULT_OK) {
936 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
937 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700938 mPendingAddWidgetInfo);
939 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800940 onCompleteRunnable = new Runnable() {
941 @Override
942 public void run() {
943 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700944 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700945 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
946 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800947 }
948 };
949 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800950 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800951 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800952 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700953 if (mDragLayer.getAnimatedView() != null) {
954 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
955 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
956 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700957 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700958 // The animated view may be null in the case of a rotation during widget configuration
959 onCompleteRunnable.run();
960 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 }
962
963 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700964 protected void onStop() {
965 super.onStop();
966 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700967
968 if (mLauncherCallbacks != null) {
969 mLauncherCallbacks.onStop();
970 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700971 }
972
973 @Override
974 protected void onStart() {
975 super.onStart();
976 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700977
978 if (mLauncherCallbacks != null) {
979 mLauncherCallbacks.onStart();
980 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700981 }
982
983 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200985 long startTime = 0;
986 if (DEBUG_RESUME_TIME) {
987 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400988 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200989 }
Adam Cohen9211d422014-10-07 18:14:53 -0700990
991 if (mLauncherCallbacks != null) {
992 mLauncherCallbacks.preOnResume();
993 }
994
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700996 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700997
Winson Chung4a2afa32012-07-19 14:53:05 -0700998 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700999 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001000 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001001 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -07001002 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -07001003 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -07001004 // view after launching an app, as they may be depending on the UI to be static to
1005 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -07001006 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07001007 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08001008 } else if (mOnResumeState == State.WIDGETS) {
1009 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001010 }
1011 mOnResumeState = State.NONE;
1012
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001013 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001014 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1015 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001016
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001017 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001018 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001019 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001020 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001021 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001022 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001024 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001025 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1026 // execute them here
1027 long startTimeCallbacks = 0;
1028 if (DEBUG_RESUME_TIME) {
1029 startTimeCallbacks = System.currentTimeMillis();
1030 }
1031
Michael Jurka1e2f4652013-07-08 18:03:46 -07001032 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1033 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001034 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001035 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001036 if (DEBUG_RESUME_TIME) {
1037 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1038 (System.currentTimeMillis() - startTimeCallbacks));
1039 }
Michael Jurka447bf842013-05-15 14:52:15 +02001040 }
Michael Jurka54554252013-08-01 12:52:23 +02001041 if (mOnResumeCallbacks.size() > 0) {
1042 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1043 mOnResumeCallbacks.get(i).run();
1044 }
1045 mOnResumeCallbacks.clear();
1046 }
Winson Chunge4e50662012-01-23 14:45:13 -08001047
1048 // Reset the pressed state of icons that were locked in the press state while activities
1049 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001050 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001051 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001052 mWaitingForResume.setStayPressed(false);
1053 }
Winson Chung82963d52013-10-09 11:20:57 -07001054
Adam Cohen06dff352012-06-01 17:17:08 -07001055 // It is possible that widgets can receive updates while launcher is not in the foreground.
1056 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1057 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1058 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001059 if (!isWorkspaceLoading()) {
1060 getWorkspace().reinflateWidgetsIfNecessary();
1061 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001062
Michael Jurka447bf842013-05-15 14:52:15 +02001063 if (DEBUG_RESUME_TIME) {
1064 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1065 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001066
Adam Cohen4b66bf32015-09-18 12:15:19 -07001067 // We want to suppress callbacks about CustomContent being shown if we have just received
1068 // onNewIntent while the user was present within launcher. In that case, we post a call
1069 // to move the user to the main screen (which will occur after onResume). We don't want to
1070 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1071 // suppress here.
1072 if (mWorkspace.getCustomContentCallbacks() != null
1073 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001074 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001075 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001076 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1077 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001078 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001079 }
1080 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001081 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001082 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001083 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001084
Sunny Goyal756adbc2015-04-16 15:20:51 -07001085 if (!isWorkspaceLoading()) {
1086 // Process any items that were added while Launcher was away.
1087 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1088 }
Adam Cohen9211d422014-10-07 18:14:53 -07001089
1090 if (mLauncherCallbacks != null) {
1091 mLauncherCallbacks.onResume();
1092 }
Adam Cohen06dff352012-06-01 17:17:08 -07001093 }
1094
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001096 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001097 // Ensure that items added to Launcher are queued until Launcher returns
1098 InstallShortcutReceiver.enableInstallQueue();
1099
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001100 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001101 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001102 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001103 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001104
1105 // We call onHide() aggressively. The custom content callbacks should be able to
1106 // debounce excess onHide calls.
1107 if (mWorkspace.getCustomContentCallbacks() != null) {
1108 mWorkspace.getCustomContentCallbacks().onHide();
1109 }
Romain Guycbb89e42009-06-08 15:52:54 -07001110
Adam Cohen9211d422014-10-07 18:14:53 -07001111 if (mLauncherCallbacks != null) {
1112 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001113 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001114 }
1115
1116 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001117 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1118 // by a onResume or by scrolling otherwise.
1119 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001120
1121 // Custom content is completely hidden
1122 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001123
1124 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1125 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001126
1127 // Indicates whether the user is allowed to scroll away from the custom content.
1128 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001129 }
1130
Adam Cohenc2d6e892014-10-16 09:49:24 -07001131 public interface LauncherOverlay {
1132
1133 /**
1134 * Touch interaction leading to overscroll has begun
1135 */
1136 public void onScrollInteractionBegin();
1137
1138 /**
1139 * Touch interaction related to overscroll has ended
1140 */
1141 public void onScrollInteractionEnd();
1142
1143 /**
1144 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1145 * screen (or in the case of RTL, the rightmost screen).
1146 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001147 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001148
1149 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001150 * Called when the launcher is ready to use the overlay
1151 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001152 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001153 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001154 }
1155
Jun Mukai8af0cd82015-05-12 12:32:12 -07001156 public interface LauncherSearchCallbacks {
1157 /**
1158 * Called when the search overlay is shown.
1159 */
1160 public void onSearchOverlayOpened();
1161
1162 /**
1163 * Called when the search overlay is dismissed.
1164 */
1165 public void onSearchOverlayClosed();
1166 }
1167
Adam Cohenc2d6e892014-10-16 09:49:24 -07001168 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001169 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001170 }
1171
1172 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1173
Sunny Goyalc86df472016-02-25 09:19:38 -08001174 public void onScrollChanged(float progress) {
1175 if (mWorkspace != null) {
1176 mWorkspace.onOverlayScrollChanged(progress);
1177 }
1178 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001179 }
1180
Jorim Jaggid017f882014-01-14 17:08:48 -08001181 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001182 if (mLauncherCallbacks != null) {
1183 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001184 } else {
1185 // On devices with a locked orientation, we will at least have the allow rotation
1186 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001187 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001188 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001189 }
1190
Adam Cohen9211d422014-10-07 18:14:53 -07001191 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001192 CustomContentCallbacks callbacks, String description) {
1193 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001194 }
1195
Adam Cohenedb40762013-07-18 16:45:45 -07001196 // The custom content needs to offset its content to account for the QSB
1197 public int getTopOffsetForCustomContent() {
1198 return mWorkspace.getPaddingTop();
1199 }
1200
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001201 @Override
1202 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001203 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001204 if (mModel.isCurrentCallbacks(this)) {
1205 mModel.stopLoader();
1206 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001207 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1208
Romain Guy13c2e7b2010-03-10 19:45:00 -08001209 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001210 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001211
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001212 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001213 @Override
1214 public void onWindowFocusChanged(boolean hasFocus) {
1215 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001216 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001217
1218 if (mLauncherCallbacks != null) {
1219 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1220 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001221 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001222
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001223 private boolean acceptFilter() {
1224 final InputMethodManager inputManager = (InputMethodManager)
1225 getSystemService(Context.INPUT_METHOD_SERVICE);
1226 return !inputManager.isFullscreenMode();
1227 }
1228
1229 @Override
1230 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001231 final int uniChar = event.getUnicodeChar();
1232 final boolean handled = super.onKeyDown(keyCode, event);
1233 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1234 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1236 keyCode, event);
1237 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001238 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001239 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001240 // showSearchDialog()
1241 // If there are multiple keystrokes before the search dialog takes focus,
1242 // onSearchRequested() will be called for every keystroke,
1243 // but it is idempotent, so it's fine.
1244 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245 }
1246 }
1247
Joe Onorato8a9625e2010-01-28 15:55:35 -08001248 // Eat the long press event so the keyboard doesn't come up.
1249 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1250 return true;
1251 }
1252
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 return handled;
1254 }
1255
Winson46163472015-09-22 17:31:56 -07001256 @Override
1257 public boolean onKeyUp(int keyCode, KeyEvent event) {
1258 if (keyCode == KeyEvent.KEYCODE_MENU) {
1259 // Ignore the menu key if we are currently dragging or are on the custom content screen
1260 if (!isOnCustomContent() && !mDragController.isDragging()) {
1261 // Close any open folders
1262 closeFolder();
1263
1264 // Stop resizing any widgets
1265 mWorkspace.exitWidgetResizeMode();
1266
1267 // Show the overview mode if we are on the workspace
1268 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1269 !mWorkspace.isSwitchingState()) {
1270 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001271 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001272 }
1273 }
1274 return true;
1275 }
1276 return super.onKeyUp(keyCode, event);
1277 }
1278
Karl Rosaen138a0412009-04-23 19:00:21 -07001279 private String getTypedText() {
1280 return mDefaultKeySsb.toString();
1281 }
1282
1283 private void clearTypedText() {
1284 mDefaultKeySsb.clear();
1285 mDefaultKeySsb.clearSpans();
1286 Selection.setSelection(mDefaultKeySsb, 0);
1287 }
1288
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001290 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1291 * State
1292 */
1293 private static State intToState(int stateOrdinal) {
1294 State state = State.WORKSPACE;
1295 final State[] stateValues = State.values();
1296 for (int i = 0; i < stateValues.length; i++) {
1297 if (stateValues[i].ordinal() == stateOrdinal) {
1298 state = stateValues[i];
1299 break;
1300 }
1301 }
1302 return state;
1303 }
1304
1305 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 * Restores the previous state, if it exists.
1307 *
1308 * @param savedState The previous state.
1309 */
1310 private void restoreState(Bundle savedState) {
1311 if (savedState == null) {
1312 return;
1313 }
1314
Michael Jurka883f55b2010-10-21 15:47:14 -07001315 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001316 if (state == State.APPS || state == State.WIDGETS) {
1317 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001318 }
1319
Adam Cohen21cd0022013-10-09 18:57:02 -07001320 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1321 PagedView.INVALID_RESTORE_PAGE);
1322 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001323 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 }
1325
Sunny Goyal756cd262015-08-20 12:33:21 -07001326 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1327 if (itemValues != null) {
1328 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001329 AppWidgetProviderInfo info = savedState.getParcelable(
1330 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001331 mPendingAddWidgetInfo = info == null ?
1332 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1333
Adam Cohen4637b5a2013-11-04 18:21:24 -08001334 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001335 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 mRestoring = true;
1337 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 }
1339
1340 /**
1341 * Finds all the views we need and configure them properly.
1342 */
1343 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001344 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001345 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001346 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
1347
Winson Chung4c98d922011-05-31 16:50:48 -07001348 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Hyunyoung Song7d2fc812016-06-15 12:51:30 -07001349 mPageIndicator = (PageIndicator) mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001350
Sunny Goyal784f9c32016-03-23 16:56:54 -07001351 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1352 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1353 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001354 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355
Winson Chung4c98d922011-05-31 16:50:48 -07001356 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001357
1358 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001359
Winson Chung3d503fb2011-07-13 17:25:49 -07001360 // Setup the hotseat
1361 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1362 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001363 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001364 }
1365
Winsone9f27272015-10-13 10:47:51 -07001366 // Setup the overview panel
1367 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001368
Winson Chung4c98d922011-05-31 16:50:48 -07001369 // Setup the workspace
1370 mWorkspace.setHapticFeedbackEnabled(false);
1371 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001372 mWorkspace.setup(mDragController);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001373 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001374 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001375
Tony Wickham34d2c912015-09-11 09:27:58 -07001376 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001377 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001378
Winson Chungef7f8742015-06-04 17:18:17 -07001379 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001380 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001381 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001382 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1383 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1384 } else {
1385 mAppsView.setSearchBarController(new DefaultAppSearchController());
1386 }
Craig Mautner360310b2012-10-26 15:13:08 -07001387
Winson Chung3d503fb2011-07-13 17:25:49 -07001388 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001389 mDragController.setDragScoller(mWorkspace);
1390 mDragController.setScrollView(mDragLayer);
1391 mDragController.setMoveTarget(mWorkspace);
1392 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001393 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001394
Sunny Goyal322d5562015-06-25 19:35:49 -07001395 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1396 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001397 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 }
1399
Winsone9f27272015-10-13 10:47:51 -07001400 private void setupOverviewPanel() {
1401 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1402
1403 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1404 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1405 @Override
1406 public boolean onLongClick(View v) {
1407 return v.performClick();
1408 }
1409 };
1410
1411 // Bind wallpaper button actions
1412 View wallpaperButton = findViewById(R.id.wallpaper_button);
1413 wallpaperButton.setOnClickListener(new OnClickListener() {
1414 @Override
1415 public void onClick(View view) {
1416 if (!mWorkspace.isSwitchingState()) {
1417 onClickWallpaperPicker(view);
1418 }
1419 }
1420 });
1421 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1422 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1423
1424 // Bind widget button actions
1425 mWidgetsButton = findViewById(R.id.widget_button);
1426 mWidgetsButton.setOnClickListener(new OnClickListener() {
1427 @Override
1428 public void onClick(View view) {
1429 if (!mWorkspace.isSwitchingState()) {
1430 onClickAddWidgetButton(view);
1431 }
1432 }
1433 });
1434 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1435 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1436
1437 // Bind settings actions
1438 View settingsButton = findViewById(R.id.settings_button);
1439 boolean hasSettings = hasSettings();
1440 if (hasSettings) {
1441 settingsButton.setOnClickListener(new OnClickListener() {
1442 @Override
1443 public void onClick(View view) {
1444 if (!mWorkspace.isSwitchingState()) {
1445 onClickSettingsButton(view);
1446 }
1447 }
1448 });
1449 settingsButton.setOnLongClickListener(performClickOnLongClick);
1450 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1451 } else {
1452 settingsButton.setVisibility(View.GONE);
1453 }
1454
1455 mOverviewPanel.setAlpha(0f);
1456 }
1457
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001459 * Sets the all apps button. This method is called from {@link Hotseat}.
1460 */
1461 public void setAllAppsButton(View allAppsButton) {
1462 mAllAppsButton = allAppsButton;
1463 }
1464
1465 public View getAllAppsButton() {
1466 return mAllAppsButton;
1467 }
1468
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001469 public View getWidgetsButton() {
1470 return mWidgetsButton;
1471 }
1472
Anjali Koppal5ad44842014-03-10 20:34:39 -07001473 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 * Creates a view representing a shortcut.
1475 *
1476 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001478 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001479 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 }
1481
1482 /**
1483 * Creates a view representing a shortcut inflated from the specified resource.
1484 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 * @param parent The group the shortcut belongs to.
1486 * @param info The data structure describing the shortcut.
1487 *
1488 * @return A View inflated from layoutResId.
1489 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001490 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001491 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001492 parent, false);
1493 favorite.applyFromShortcutInfo(info, mIconCache);
1494 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001496 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 return favorite;
1498 }
1499
1500 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 * Add a shortcut to the workspace.
1502 *
1503 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001505 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001506 int cellY) {
1507 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001508 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001509
Sunny Goyal5c97f512015-05-19 16:03:28 -07001510 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001511 if (info == null) {
1512 return;
1513 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001514 final View view = createShortcut(info);
1515
Sunny Goyal5c97f512015-05-19 16:03:28 -07001516 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001517 // First we check if we already know the exact location where we want to add this item.
1518 if (cellX >= 0 && cellY >= 0) {
1519 cellXY[0] = cellX;
1520 cellXY[1] = cellY;
1521 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001522
1523 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001524 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001525 true, null,null)) {
1526 return;
1527 }
1528 DragObject dragObject = new DragObject();
1529 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001530 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1531 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001532 return;
1533 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001534 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001535 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001536 }
1537
1538 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001539 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001540 return;
1541 }
1542
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001543 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544
1545 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001546 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001547 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 }
1549 }
1550
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001552 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001554 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 */
Adam Cohen091440a2015-03-18 14:16:05 -07001556 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001557 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1558
1559 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001560 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001561 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001562 }
Romain Guycbb89e42009-06-08 15:52:54 -07001563
Adam Cohen59400422014-03-05 18:07:04 -08001564 if (appWidgetInfo.isCustomWidget) {
1565 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001566 }
1567
Adam Cohen59400422014-03-05 18:07:04 -08001568 LauncherAppWidgetInfo launcherInfo;
1569 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1570 launcherInfo.spanX = info.spanX;
1571 launcherInfo.spanY = info.spanY;
1572 launcherInfo.minSpanX = info.minSpanX;
1573 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001574 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001575
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001577 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578
1579 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001580 if (hostView == null) {
1581 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001582 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001583 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001584 hostView.setVisibility(View.VISIBLE);
1585 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001587 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588 }
Romain Guycbb89e42009-06-08 15:52:54 -07001589
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001590 private void addAppWidgetToWorkspace(
1591 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001592 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001593 hostView.setTag(item);
1594 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001595
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001596 hostView.setFocusable(true);
1597 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001598
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001599 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001600 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1601
1602 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001603 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001604 }
1605 }
1606
Adam Cohended9f8d2010-11-03 13:25:16 -07001607 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1608 @Override
1609 public void onReceive(Context context, Intent intent) {
1610 final String action = intent.getAction();
1611 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1612 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001613 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001614 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001615
Winson Chungc100e8e2011-08-09 16:02:43 -07001616 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001617 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001618 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001619 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001620 if (!showWorkspace(false)) {
1621 // If we are already on the workspace, then manually reset all apps
1622 mAppsView.reset();
1623 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001624 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001625 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1626 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001627 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001628 }
1629 }
1630 };
1631
1632 @Override
1633 public void onAttachedToWindow() {
1634 super.onAttachedToWindow();
1635
1636 // Listen for broadcasts related to user-presence
1637 final IntentFilter filter = new IntentFilter();
1638 filter.addAction(Intent.ACTION_SCREEN_OFF);
1639 filter.addAction(Intent.ACTION_USER_PRESENT);
1640 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001641 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001642 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001643 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001644
1645 if (mLauncherCallbacks != null) {
1646 mLauncherCallbacks.onAttachedToWindow();
1647 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001648 }
1649
1650 @Override
1651 public void onDetachedFromWindow() {
1652 super.onDetachedFromWindow();
1653 mVisible = false;
1654
Adam Cohend113e0c2010-11-11 10:48:05 -08001655 if (mAttached) {
1656 unregisterReceiver(mReceiver);
1657 mAttached = false;
1658 }
Winson Chungb745afb2015-03-02 11:51:23 -08001659 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001660
1661 if (mLauncherCallbacks != null) {
1662 mLauncherCallbacks.onDetachedFromWindow();
1663 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001664 }
1665
1666 public void onWindowVisibilityChanged(int visibility) {
1667 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001668 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001669 // The following code used to be in onResume, but it turns out onResume is called when
1670 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1671 // is a more appropriate event to handle
1672 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001673 if (!mWorkspaceLoading) {
1674 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001675 // We want to let Launcher draw itself at least once before we force it to build
1676 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001677 // apps is nice and speedy.
1678 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001679 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001680 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001681 if (mStarted) return;
1682 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001683 // We delay the layer building a bit in order to give
1684 // other message processing a time to run. In particular
1685 // this avoids a delay in hiding the IME if it was
1686 // currently shown, because doing that may involve
1687 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001688 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001689 final ViewTreeObserver.OnDrawListener listener = this;
1690 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001691 public void run() {
1692 if (mWorkspace != null &&
1693 mWorkspace.getViewTreeObserver() != null) {
1694 mWorkspace.getViewTreeObserver().
1695 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001696 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001697 }
1698 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001699 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001700 }
1701 });
1702 }
1703 clearTypedText();
1704 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001705 }
1706
Adam Cohen091440a2015-03-18 14:16:05 -07001707 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001708 mHandler.removeMessages(ADVANCE_MSG);
1709 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1710 mHandler.sendMessageDelayed(msg, delay);
1711 mAutoAdvanceSentTime = System.currentTimeMillis();
1712 }
1713
Adam Cohen091440a2015-03-18 14:16:05 -07001714 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001715 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1716 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1717 mAutoAdvanceRunning = autoAdvanceRunning;
1718 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001719 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001720 sendAdvanceMessage(delay);
1721 } else {
1722 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001723 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001724 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1725 }
1726 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001727 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001728 }
1729 }
1730 }
1731
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001732 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1733
Adam Cohended9f8d2010-11-03 13:25:16 -07001734 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001735 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001736 if (msg.what == ADVANCE_MSG) {
1737 int i = 0;
1738 for (View key: mWidgetsToAdvance.keySet()) {
1739 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001740 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001741 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001742 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001743 public void run() {
1744 ((Advanceable) v).advance();
1745 }
1746 }, delay);
1747 }
1748 i++;
1749 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001750 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001751 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001752 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001753 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001754 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001755
Winsonc0b52fe2015-09-09 16:38:15 -07001756 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001757 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001758 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1759 if (v instanceof Advanceable) {
1760 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001761 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001762 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001763 }
1764 }
1765
Winsonc0b52fe2015-09-09 16:38:15 -07001766 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001767 if (mWidgetsToAdvance.containsKey(hostView)) {
1768 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001769 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001770 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001771 }
1772
Hyunyoung Song3f471442015-04-08 19:01:34 -07001773 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001774 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1775 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001776 }
1777
Winson Chunga6945242014-01-08 14:04:34 -08001778 public DragLayer getDragLayer() {
1779 return mDragLayer;
1780 }
1781
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001782 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001783 return mAppsView;
1784 }
1785
Hyunyoung Song3f471442015-04-08 19:01:34 -07001786 public WidgetsContainerView getWidgetsView() {
1787 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001788 }
1789
Winson Chunga6945242014-01-08 14:04:34 -08001790 public Workspace getWorkspace() {
1791 return mWorkspace;
1792 }
1793
1794 public Hotseat getHotseat() {
1795 return mHotseat;
1796 }
1797
Jorim Jaggid017f882014-01-14 17:08:48 -08001798 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001799 return mOverviewPanel;
1800 }
1801
Sunny Goyal47328fd2016-05-25 18:56:41 -07001802 public DropTargetBar getDropTargetBar() {
1803 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001804 }
1805
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001806 public LauncherAppWidgetHost getAppWidgetHost() {
1807 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001808 }
Romain Guycbb89e42009-06-08 15:52:54 -07001809
Winson Chunga9abd0e2010-10-27 17:18:37 -07001810 public LauncherModel getModel() {
1811 return mModel;
1812 }
1813
Adam Cohen79d90c52016-04-22 13:29:20 -07001814 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001815 return mSharedPrefs;
1816 }
1817
Adam Cohen2e6da152015-05-06 11:42:25 -07001818 public DeviceProfile getDeviceProfile() {
1819 return mDeviceProfile;
1820 }
1821
Bjorn Bringertc459e522013-06-07 19:36:01 +01001822 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001823 getWindow().closeAllPanels();
1824
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001825 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001826 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001827 }
1828
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001829 @Override
1830 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001831 long startTime = 0;
1832 if (DEBUG_RESUME_TIME) {
1833 startTime = System.currentTimeMillis();
1834 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001835 super.onNewIntent(intent);
1836
1837 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001838 Folder openFolder = mWorkspace.getOpenFolder();
1839 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1840 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1841 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1842 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1843 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001844 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001845 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001846
Adam Cohen6fecd412013-10-02 17:41:50 -07001847 if (mWorkspace == null) {
1848 // Can be cases where mWorkspace is null, this prevents a NPE
1849 return;
1850 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001851 // In all these cases, only animate if we're already on home
1852 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001853
Sunny Goyal935fca12015-10-13 10:19:01 -07001854 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001855 exitSpringLoadedDragMode();
1856
1857 // If we are already on home, then just animate back to the workspace,
1858 // otherwise, just wait until onResume to set the state back to Workspace
1859 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001860 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001861 } else {
1862 mOnResumeState = State.WORKSPACE;
1863 }
1864
1865 final View v = getWindow().peekDecorView();
1866 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001867 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001868 INPUT_METHOD_SERVICE);
1869 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1870 }
1871
Winson Chungb745afb2015-03-02 11:51:23 -08001872 // Reset the apps view
1873 if (!alreadyOnHome && mAppsView != null) {
1874 mAppsView.scrollToTop();
1875 }
1876
Hyunyoung Song3f471442015-04-08 19:01:34 -07001877 // Reset the widgets view
1878 if (!alreadyOnHome && mWidgetsView != null) {
1879 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001880 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001881
Adam Cohen9211d422014-10-07 18:14:53 -07001882 if (mLauncherCallbacks != null) {
1883 mLauncherCallbacks.onHomeIntent();
1884 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 }
Adam Cohened307df2013-10-02 09:37:31 -07001886
Adam Cohen9211d422014-10-07 18:14:53 -07001887 if (mLauncherCallbacks != null) {
1888 mLauncherCallbacks.onNewIntent(intent);
1889 }
Winson15f8b172015-08-19 11:02:39 -07001890
1891 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1892 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1893 // animation.
1894 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001895 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1896 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001897 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1898 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001899
1900 // We use this flag to suppress noisy callbacks above custom content state
1901 // from onResume.
1902 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001903 mWorkspace.post(new Runnable() {
1904 @Override
1905 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001906 if (mWorkspace != null) {
1907 mWorkspace.moveToDefaultScreen(true);
1908 }
Winson15f8b172015-08-19 11:02:39 -07001909 }
1910 });
1911 }
1912 }
1913
1914 if (DEBUG_RESUME_TIME) {
1915 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1916 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001917 }
1918
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001920 public void onRestoreInstanceState(Bundle state) {
1921 super.onRestoreInstanceState(state);
1922 for (int page: mSynchronouslyBoundPages) {
1923 mWorkspace.restoreInstanceStateForChild(page);
1924 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 }
1926
1927 @Override
1928 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001929 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001930 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1931 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001932
Adam Cohen21cd0022013-10-09 18:57:02 -07001933 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001934 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935
Michael Jurka883f55b2010-10-21 15:47:14 -07001936 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001937 // We close any open folder since it will not be re-opened, and we need to make sure
1938 // this state is reflected.
1939 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1940 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941
Adam Cohendcd297f2013-06-18 13:13:40 -07001942 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001943 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001944 ContentValues itemValues = new ContentValues();
1945 mPendingAddInfo.writeToValues(itemValues);
1946 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001947 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001948 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949 }
1950
Adam Cohen9211d422014-10-07 18:14:53 -07001951 if (mLauncherCallbacks != null) {
1952 mLauncherCallbacks.onSaveInstanceState(outState);
1953 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 }
1955
1956 @Override
1957 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001959
Winson Chunge7a03942011-08-05 15:05:12 -07001960 // Remove all pending runnables
1961 mHandler.removeMessages(ADVANCE_MSG);
1962 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001963 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001964 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001965
Winson Chungcd2b0142011-06-08 16:02:26 -07001966 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001967 // It's possible to receive onDestroy after a new Launcher activity has
1968 // been created. In this case, don't interfere with the new Launcher.
1969 if (mModel.isCurrentCallbacks(this)) {
1970 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001971 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001972 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001973
Sunny Goyal745bad92016-05-02 10:54:12 -07001974 if (mRotationPrefChangeHandler != null) {
1975 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1976 }
1977
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001979 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001981 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001983 mAppWidgetHost = null;
1984
1985 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986
1987 TextKeyListener.getInstance().release();
1988
Tony Wickhame2217252016-03-22 16:34:23 -07001989 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1990 .removeAccessibilityStateChangeListener(this);
1991
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001992 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001993
Michael Jurka2ecf9952012-06-18 12:52:28 -07001994 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001995
1996 if (mLauncherCallbacks != null) {
1997 mLauncherCallbacks.onDestroy();
1998 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 }
2000
Adam Cohena9cf38f2011-05-02 15:36:58 -07002001 public DragController getDragController() {
2002 return mDragController;
2003 }
2004
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 @Override
2006 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002007 onStartForResult(requestCode);
2008 super.startActivityForResult(intent, requestCode);
2009 }
2010
2011 @Override
2012 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2013 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2014 onStartForResult(requestCode);
2015 try {
2016 super.startIntentSenderForResult(intent, requestCode,
2017 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2018 } catch (IntentSender.SendIntentException e) {
2019 throw new ActivityNotFoundException();
2020 }
2021 }
2022
2023 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002024 if (requestCode >= 0) {
2025 setWaitingForResult(true);
2026 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 }
2028
Winson Chung70d72102011-08-12 11:24:35 -07002029 /**
2030 * Indicates that we want global search for this activity by setting the globalSearch
2031 * argument for {@link #startSearch} to true.
2032 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002033 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002034 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002036
Karl Rosaen138a0412009-04-23 19:00:21 -07002037 if (initialQuery == null) {
2038 // Use any text typed in the launcher as the initial query
2039 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002040 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 if (appSearchData == null) {
2042 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002043 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002044 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002045
2046 // TODO send proper bounds.
2047 Rect sourceBounds = null;
Romain Guycbb89e42009-06-08 15:52:54 -07002048
Ian Parkinson047036e2014-09-01 15:40:53 +01002049 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002050 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002051 if (clearTextImmediately) {
2052 clearTypedText();
2053 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002054
2055 // We need to show the workspace after starting the search
2056 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002057 }
2058
Ian Parkinson047036e2014-09-01 15:40:53 +01002059 /**
2060 * Start a text search.
2061 *
2062 * @return {@code true} if the search will start immediately, so any further keypresses
2063 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2064 * to buffer keypresses.
2065 */
2066 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002067 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002068 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2069 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2070 sourceBounds);
2071 }
2072
Michael Jurkaa33411c2012-06-14 16:18:21 -07002073 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002074 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002075 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002076 }
2077
2078 /**
2079 * Starts the global search activity. This code is a copied from SearchManager
2080 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002081 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002082 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002083 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002084 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2085 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2086 if (globalSearchActivity == null) {
2087 Log.w(TAG, "No global search activity found.");
2088 return;
2089 }
2090 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2091 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2092 intent.setComponent(globalSearchActivity);
2093 // Make sure that we have a Bundle to put source in
2094 if (appSearchData == null) {
2095 appSearchData = new Bundle();
2096 } else {
2097 appSearchData = new Bundle(appSearchData);
2098 }
Adam Cohen9211d422014-10-07 18:14:53 -07002099 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002100 if (!appSearchData.containsKey("source")) {
2101 appSearchData.putString("source", getPackageName());
2102 }
2103 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2104 if (!TextUtils.isEmpty(initialQuery)) {
2105 intent.putExtra(SearchManager.QUERY, initialQuery);
2106 }
2107 if (selectInitialQuery) {
2108 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2109 }
2110 intent.setSourceBounds(sourceBounds);
2111 try {
2112 startActivity(intent);
2113 } catch (ActivityNotFoundException ex) {
2114 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2115 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002116 }
2117
Yura4f93ec62014-02-04 14:15:21 +00002118 public boolean isOnCustomContent() {
2119 return mWorkspace.isOnOrMovingToCustomContent();
2120 }
2121
Winson Chung70d72102011-08-12 11:24:35 -07002122 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002123 public boolean onPrepareOptionsMenu(Menu menu) {
2124 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002125 if (mLauncherCallbacks != null) {
2126 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2127 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002128 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002129 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002130
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002131 @Override
2132 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002133 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002134 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002135 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002136 }
2137
Joe Onorato9c1289c2009-08-17 11:03:03 -04002138 public boolean isWorkspaceLocked() {
2139 return mWorkspaceLoading || mWaitingForResult;
2140 }
2141
Adam Cohen517a7f52014-03-01 12:12:59 -08002142 public boolean isWorkspaceLoading() {
2143 return mWorkspaceLoading;
2144 }
2145
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002146 private void setWorkspaceLoading(boolean value) {
2147 boolean isLocked = isWorkspaceLocked();
2148 mWorkspaceLoading = value;
2149 if (isLocked != isWorkspaceLocked()) {
2150 onWorkspaceLockedChanged();
2151 }
2152 }
2153
2154 private void setWaitingForResult(boolean value) {
2155 boolean isLocked = isWorkspaceLocked();
2156 mWaitingForResult = value;
2157 if (isLocked != isWorkspaceLocked()) {
2158 onWorkspaceLockedChanged();
2159 }
2160 }
2161
Adam Cohen9211d422014-10-07 18:14:53 -07002162 protected void onWorkspaceLockedChanged() {
2163 if (mLauncherCallbacks != null) {
2164 mLauncherCallbacks.onWorkspaceLockedChanged();
2165 }
2166 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002167
Michael Jurka0280c3b2010-09-17 15:00:07 -07002168 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002169 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002170 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002171 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2172 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002173 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002174 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002175
Tony Wickhama0628cc2015-10-14 15:23:04 -07002176 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002177 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002178 if (LOGD) {
2179 Log.d(TAG, "Adding widget from drop");
2180 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002181 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2182 }
2183
Sunny Goyale6b63a32015-01-16 16:14:29 -08002184 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002185 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2186 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002187 if (appWidgetInfo.configure != null) {
2188 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002189 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002190
Bjorn Bringert7984c942009-12-09 15:38:25 +00002191 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002192 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2193 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2194
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002195 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002196 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002197 Runnable onComplete = new Runnable() {
2198 @Override
2199 public void run() {
2200 // Exit spring loaded mode if necessary after adding the widget
2201 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2202 null);
2203 }
2204 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002205 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002206 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002207 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002208 }
2209 }
Romain Guycbb89e42009-06-08 15:52:54 -07002210
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002211 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002212 // Close any folders that may be open.
2213 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002214 mWorkspace.moveToCustomContentScreen(animate);
2215 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002216
2217 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2218 int[] cell, int spanX, int spanY) {
2219 switch (info.itemType) {
2220 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2221 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2222 int span[] = new int[2];
2223 span[0] = spanX;
2224 span[1] = spanY;
2225 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2226 container, screenId, cell, span);
2227 break;
2228 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2229 processShortcutFromDrop(info.componentName, container, screenId, cell);
2230 break;
2231 default:
2232 throw new IllegalStateException("Unknown item type: " + info.itemType);
2233 }
2234 }
2235
Adam Cohenfbba09b2011-07-18 21:43:05 -07002236 /**
2237 * Process a shortcut drop.
2238 *
2239 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002240 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002241 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002242 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002243 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2244 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002245 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002246 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002247 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002248
2249 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002250 mPendingAddInfo.cellX = cell[0];
2251 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002252 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002253
2254 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2255 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002256 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002257 }
2258
Adam Cohenfbba09b2011-07-18 21:43:05 -07002259 /**
2260 * Process a widget drop.
2261 *
2262 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002263 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002264 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002265 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002266 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2267 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002268 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002269 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002270 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002271 mPendingAddInfo.minSpanX = info.minSpanX;
2272 mPendingAddInfo.minSpanY = info.minSpanY;
2273
Adam Cohenfbba09b2011-07-18 21:43:05 -07002274 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002275 mPendingAddInfo.cellX = cell[0];
2276 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002277 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002278 if (span != null) {
2279 mPendingAddInfo.spanX = span[0];
2280 mPendingAddInfo.spanY = span[1];
2281 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002282
Adam Cohened66b2b2012-01-23 17:28:51 -08002283 AppWidgetHostView hostView = info.boundWidget;
2284 int appWidgetId;
2285 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002286 // In the case where we've prebound the widget, we remove it from the DragLayer
2287 if (LOGD) {
2288 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2289 }
2290 getDragLayer().removeView(hostView);
2291
Adam Cohened66b2b2012-01-23 17:28:51 -08002292 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002293 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002294
2295 // Clear the boundWidget so that it doesn't get destroyed.
2296 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002297 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002298 // In this case, we either need to start an activity to get permission to bind
2299 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002300 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002301 Bundle options = info.bindOptions;
2302
Sunny Goyalffe83f12014-08-14 17:39:34 -07002303 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2304 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002305 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002306 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002307 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002308 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002309 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2310 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2311 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002312 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2313 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002314 // TODO: we need to make sure that this accounts for the options bundle.
2315 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002316 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2317 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002318 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002319 }
2320
Adam Cohendcd297f2013-06-18 13:13:40 -07002321 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002322 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002323 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002324 folderInfo.title = getText(R.string.folder_name);
2325
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002326 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002327 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2328 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002329
2330 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002331 FolderIcon newFolder =
2332 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002333 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002334 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002335 // Force measure the new folder icon
2336 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2337 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002338 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002339 }
Romain Guycbb89e42009-06-08 15:52:54 -07002340
Winsonc0b52fe2015-09-09 16:38:15 -07002341 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002342 * Unbinds the view for the specified item, and removes the item and all its children.
2343 *
2344 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002345 * @param itemInfo the {@link ItemInfo} for this view.
2346 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002347 */
Winson2949fb52015-09-24 09:56:11 -07002348 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002349 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002350 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002351 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2352 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002353 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002354 } else {
2355 mWorkspace.removeWorkspaceItem(v);
2356 }
Winsonc0b52fe2015-09-09 16:38:15 -07002357 if (deleteFromDb) {
2358 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2359 }
2360 } else if (itemInfo instanceof FolderInfo) {
2361 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002362 if (v instanceof FolderIcon) {
2363 ((FolderIcon) v).removeListeners();
2364 }
Winsonc0b52fe2015-09-09 16:38:15 -07002365 mWorkspace.removeWorkspaceItem(v);
2366 if (deleteFromDb) {
2367 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2368 }
2369 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2370 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002371 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002372 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002373 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002374 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002375 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002376
Winsonc0b52fe2015-09-09 16:38:15 -07002377 } else {
2378 return false;
2379 }
2380 return true;
2381 }
2382
2383 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002384 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002385 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002386 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002387 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002388 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002389 // Deleting an app widget ID is a void call but writes to disk before returning
2390 // to the caller...
2391 new AsyncTask<Void, Void, Void>() {
2392 public Void doInBackground(Void ... args) {
2393 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2394 return null;
2395 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002396 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002397 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002398 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002399 }
2400
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002401 @Override
2402 public boolean dispatchKeyEvent(KeyEvent event) {
2403 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2404 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002405 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002406 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002407 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002408 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002409 dumpState();
2410 return true;
2411 }
2412 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002413 }
2414 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2415 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002416 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417 return true;
2418 }
2419 }
2420
2421 return super.dispatchKeyEvent(event);
2422 }
2423
Joe Onorato88ec0992009-11-19 13:16:06 -08002424 @Override
2425 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002426 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2427 return;
2428 }
2429
Sunny Goyal45478022015-06-08 16:52:41 -07002430 if (mDragController.isDragging()) {
2431 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002432 return;
2433 }
2434
Winson Chungb745afb2015-03-02 11:51:23 -08002435 if (isAppsViewVisible()) {
2436 showWorkspace(true);
2437 } else if (isWidgetsViewVisible()) {
2438 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002439 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002440 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002441 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002442 Folder openFolder = mWorkspace.getOpenFolder();
2443 if (openFolder.isEditingName()) {
2444 openFolder.dismissEditingName();
2445 } else {
2446 closeFolder();
2447 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002448 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002449 mWorkspace.exitWidgetResizeMode();
2450
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002451 // Back button is a no-op here, but give at least some feedback for the button press
2452 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002453 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002454 }
2455
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002456 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002457 * Launches the intent referred by the clicked shortcut.
2458 *
2459 * @param v The view representing the clicked shortcut.
2460 */
2461 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002462 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2463 // view has detached (it's possible for this to happen if the view is removed mid touch).
2464 if (v.getWindowToken() == null) {
2465 return;
2466 }
2467
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002468 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002469 return;
2470 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002471
Adam Cohen1697b792013-09-17 19:08:21 -07002472 if (v instanceof Workspace) {
2473 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002474 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002475 }
2476 return;
2477 }
2478
2479 if (v instanceof CellLayout) {
2480 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002481 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2482 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002483 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002484 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002485 }
2486
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002487 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002488 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002489 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002490 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002491 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002492 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002493 }
Hyunyoung Song7d2fc812016-06-15 12:51:30 -07002494 } else if (v instanceof PageIndicator || v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002495 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002496 } else if (tag instanceof AppInfo) {
2497 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002498 } else if (tag instanceof LauncherAppWidgetInfo) {
2499 if (v instanceof PendingAppWidgetHostView) {
2500 onClickPendingWidget((PendingAppWidgetHostView) v);
2501 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002502 }
2503 }
2504
Sunny Goyal70660032015-05-14 00:07:08 -07002505 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002506 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002507 return false;
2508 }
2509
Michael Jurkaaf442092010-06-10 17:01:57 -07002510 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002511 * Event handler for the app widget view which has not fully restored.
2512 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002513 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002514 if (mIsSafeModeEnabled) {
2515 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2516 return;
2517 }
2518
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002519 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002520 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002521 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2522 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2523 // This should not happen, as we make sure that an Id is allocated during bind.
2524 return;
2525 }
2526 LauncherAppWidgetProviderInfo appWidgetInfo =
2527 mAppWidgetManager.findProvider(info.providerName, info.user);
2528 if (appWidgetInfo != null) {
2529 mPendingAddWidgetId = info.appWidgetId;
2530 mPendingAddInfo.copyFrom(info);
2531 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002532
Sunny Goyald478c832016-04-01 12:04:16 -07002533 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2534 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingAddWidgetId);
2535 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
2536 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2537 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2538 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2539 }
2540 } else {
2541 LauncherAppWidgetProviderInfo appWidgetInfo =
2542 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2543 if (appWidgetInfo != null) {
2544 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2545 }
Sunny Goyalff572272014-07-23 13:58:07 -07002546 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002547 } else if (info.installProgress < 0) {
2548 // The install has not been queued
2549 final String packageName = info.providerName.getPackageName();
2550 showBrokenAppInstallDialog(packageName,
2551 new DialogInterface.OnClickListener() {
2552 public void onClick(DialogInterface dialog, int id) {
2553 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2554 }
2555 });
2556 } else {
2557 // Download has started.
2558 final String packageName = info.providerName.getPackageName();
2559 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002560 }
2561 }
2562
Sunny Goyald478c832016-04-01 12:04:16 -07002563 private void startRestoredWidgetReconfigActivity(
2564 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
2565 mPendingAddWidgetInfo = provider;
2566 mPendingAddInfo.copyFrom(info);
2567 mPendingAddWidgetId = info.appWidgetId;
2568 mAppWidgetManager.startConfigActivity(provider,
2569 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2570 }
2571
Sunny Goyalff572272014-07-23 13:58:07 -07002572 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002573 * Event handler for the "grid" button that appears on the home screen, which
2574 * enters all apps mode.
2575 *
2576 * @param v The view that was clicked.
2577 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002578 protected void onClickAllAppsButton(View v) {
2579 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002580 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002581 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002582 true /* updatePredictedApps */, false /* focusSearchBar */);
2583 }
2584 }
2585
2586 protected void onLongClickAllAppsButton(View v) {
2587 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2588 if (!isAppsViewVisible()) {
2589 showAppsView(true /* animated */, false /* resetListToTop */,
2590 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002591 }
2592 }
2593
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002594 private void showBrokenAppInstallDialog(final String packageName,
2595 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002596 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002597 .setTitle(R.string.abandoned_promises_title)
2598 .setMessage(R.string.abandoned_promise_explanation)
2599 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2600 .setNeutralButton(R.string.abandoned_clean_this,
2601 new DialogInterface.OnClickListener() {
2602 public void onClick(DialogInterface dialog, int id) {
2603 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2604 mWorkspace.removeAbandonedPromise(packageName, user);
2605 }
2606 })
2607 .create().show();
2608 return;
2609 }
2610
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002611 /**
2612 * Event handler for an app shortcut click.
2613 *
2614 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2615 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002616 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002617 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2618 Object tag = v.getTag();
2619 if (!(tag instanceof ShortcutInfo)) {
2620 throw new IllegalArgumentException("Input must be a Shortcut");
2621 }
2622
2623 // Open shortcut
2624 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002625
2626 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002627 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2628 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002629 // Launch activity anyway, framework will tell the user why the app is suspended.
2630 } else {
2631 int error = R.string.activity_not_available;
2632 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2633 error = R.string.safemode_shortcut_error;
2634 }
2635 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2636 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002637 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002638 }
2639
Chris Wren40c5ed32014-06-24 18:24:23 -04002640 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002641 if ((v instanceof BubbleTextView)
2642 && shortcut.isPromise()
2643 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002644 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002645 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002646 new DialogInterface.OnClickListener() {
2647 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002648 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002649 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002650 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002651 return;
2652 }
2653
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002654 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002655 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002656 }
2657
Sunny Goyala7ce1662016-05-31 15:01:35 -07002658 private void startAppShortcutOrInfoActivity(View v) {
2659 ItemInfo item = (ItemInfo) v.getTag();
2660 Intent intent = item.getIntent();
2661 if (intent == null) {
2662 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002663 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002664 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002665 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002666
2667 if (success && v instanceof BubbleTextView) {
2668 mWaitingForResume = (BubbleTextView) v;
2669 mWaitingForResume.setStayPressed(true);
2670 }
2671 }
2672
2673 /**
2674 * Event handler for a folder icon click.
2675 *
2676 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2677 */
2678 protected void onClickFolderIcon(View v) {
2679 if (LOGD) Log.d(TAG, "onClickFolder");
2680 if (!(v instanceof FolderIcon)){
2681 throw new IllegalArgumentException("Input must be a FolderIcon");
2682 }
2683
2684 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002685 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002686 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002687 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002688 }
Michael Jurka5130e402011-10-13 04:55:35 -07002689 }
2690
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002691 /**
2692 * Event handler for the (Add) Widgets button that appears after a long press
2693 * on the home screen.
2694 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002695 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002696 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002697 if (mIsSafeModeEnabled) {
2698 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2699 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002700 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002701 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002702 }
2703
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002704 /**
2705 * Event handler for the wallpaper picker button that appears after a long press
2706 * on the home screen.
2707 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002708 protected void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002709 if (!Utilities.isWallapaperAllowed(this)) {
2710 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2711 return;
2712 }
2713
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002714 String pickerPackage = getString(R.string.wallpaper_picker_package);
2715 if (TextUtils.isEmpty(pickerPackage)) {
2716 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2717 }
2718
Tony Wickham785f7a52015-08-31 17:28:32 -07002719 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2720 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002721 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER)
2722 .setPackage(pickerPackage)
2723 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset),
2724 REQUEST_PICK_WALLPAPER);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002725 }
2726
2727 /**
2728 * Event handler for a click on the settings button that appears after a long press
2729 * on the home screen.
2730 */
Sunny Goyal745bad92016-05-02 10:54:12 -07002731 private void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002732 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002733 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2734 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002735 }
2736
Adam Cohen61f560d2013-09-30 15:58:20 -07002737 public View.OnTouchListener getHapticFeedbackTouchListener() {
2738 if (mHapticFeedbackTouchListener == null) {
2739 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002740 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002741 @Override
2742 public boolean onTouch(View v, MotionEvent event) {
2743 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2744 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2745 }
2746 return false;
2747 }
2748 };
2749 }
2750 return mHapticFeedbackTouchListener;
2751 }
2752
Tony Wickhame2217252016-03-22 16:34:23 -07002753 @Override
2754 public void onAccessibilityStateChanged(boolean enabled) {
2755 mDragLayer.onAccessibilityStateChanged(enabled);
2756 }
2757
Adam Cohen9211d422014-10-07 18:14:53 -07002758 public void onDragStarted(View view) {
2759 if (isOnCustomContent()) {
2760 // Custom content screen doesn't participate in drag and drop. If on custom
2761 // content screen, move to default.
2762 moveWorkspaceToDefaultScreen();
2763 }
Adam Cohen9211d422014-10-07 18:14:53 -07002764 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002765
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002766 /**
2767 * Called when the user stops interacting with the launcher.
2768 * This implies that the user is now on the homescreen and is not doing housekeeping.
2769 */
Adam Cohen9211d422014-10-07 18:14:53 -07002770 protected void onInteractionEnd() {
2771 if (mLauncherCallbacks != null) {
2772 mLauncherCallbacks.onInteractionEnd();
2773 }
2774 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002775
2776 /**
2777 * Called when the user starts interacting with the launcher.
2778 * The possible interactions are:
2779 * - open all apps
2780 * - reorder an app shortcut, or a widget
2781 * - open the overview mode.
2782 * This is a good time to stop doing things that only make sense
2783 * when the user is on the homescreen and not doing housekeeping.
2784 */
Adam Cohen9211d422014-10-07 18:14:53 -07002785 protected void onInteractionBegin() {
2786 if (mLauncherCallbacks != null) {
2787 mLauncherCallbacks.onInteractionBegin();
2788 }
2789 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002790
Winson Chungcd99cd32015-04-29 11:03:24 -07002791 /** Updates the interaction state. */
2792 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002793 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002794 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002795 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2796 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002797 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002798 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002799 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002800 onInteractionEnd();
2801 }
2802 }
2803
Sunny Goyala7ce1662016-05-31 15:01:35 -07002804 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002805 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002806 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2807 try {
2808 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2809 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2810 // is enabled by default on NYC.
2811 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2812 .penaltyLog().build());
2813 // Could be launching some bookkeeping activity
2814 startActivity(intent, optsBundle);
2815 } finally {
2816 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002817 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002818 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002819 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2820 // corresponding permission. Show the appropriate permission prompt if that
2821 // is the case.
2822 if (intent.getComponent() == null
2823 && Intent.ACTION_CALL.equals(intent.getAction())
2824 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2825 PackageManager.PERMISSION_GRANTED) {
2826 // TODO: Rename sPendingAddItem to a generic name.
2827 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2828 0, info);
2829 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2830 REQUEST_PERMISSION_CALL_PHONE);
2831 } else {
2832 // No idea why this was thrown.
2833 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002834 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002835 }
2836 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002837
Sunny Goyala7ce1662016-05-31 15:01:35 -07002838 private Bundle getActivityLaunchOptions(View v) {
2839 if (Utilities.ATLEAST_MARSHMALLOW) {
2840 int left = 0, top = 0;
2841 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2842 if (v instanceof TextView) {
2843 // Launch from center of icon, not entire view
2844 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2845 if (icon != null) {
2846 Rect bounds = icon.getBounds();
2847 left = (width - bounds.width()) / 2;
2848 top = v.getPaddingTop();
2849 width = bounds.width();
2850 height = bounds.height();
2851 }
2852 }
2853 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2854 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2855 // On L devices, we use the device default slide-up transition.
2856 // On L MR1 devices, we use a custom version of the slide-up transition which
2857 // doesn't have the delay present in the device default.
2858 return ActivityOptions.makeCustomAnimation(
2859 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2860 }
2861 return null;
2862 }
2863
2864 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002865 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2866 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2867 return false;
2868 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002869 // Only launch using the new animation if the shortcut has not opted out (this is a
2870 // private contract between launcher and may be ignored in the future).
2871 boolean useLaunchAnimation = (v != null) &&
2872 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2873 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2874
2875 UserHandleCompat user = null;
2876 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2877 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2878 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002879 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002880
2881 // Prepare intent
2882 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2883 if (v != null) {
2884 int[] pos = new int[2];
2885 v.getLocationOnScreen(pos);
2886 intent.setSourceBounds(
2887 new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2888 }
2889 try {
2890 if (Utilities.ATLEAST_MARSHMALLOW &&
2891 item != null && item.itemType == Favorites.ITEM_TYPE_SHORTCUT) {
2892 // Shortcuts need some special checks due to legacy reasons.
2893 startShortcutIntentSafely(intent, optsBundle, item);
2894 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2895 // Could be launching some bookkeeping activity
2896 startActivity(intent, optsBundle);
2897 } else {
2898 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2899 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2900 }
2901 return true;
2902 } catch (ActivityNotFoundException|SecurityException e) {
2903 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2904 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2905 }
2906 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002907 }
2908
Adam Cohen268c4752012-06-06 17:47:33 -07002909 /**
2910 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2911 * in the DragLayer in the exact absolute location of the original FolderIcon.
2912 */
2913 private void copyFolderIconToImage(FolderIcon fi) {
2914 final int width = fi.getMeasuredWidth();
2915 final int height = fi.getMeasuredHeight();
2916
2917 // Lazy load ImageView, Bitmap and Canvas
2918 if (mFolderIconImageView == null) {
2919 mFolderIconImageView = new ImageView(this);
2920 }
2921 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2922 mFolderIconBitmap.getHeight() != height) {
2923 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2924 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2925 }
2926
2927 DragLayer.LayoutParams lp;
2928 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2929 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2930 } else {
2931 lp = new DragLayer.LayoutParams(width, height);
2932 }
2933
Adam Cohen307fe232012-08-16 17:55:58 -07002934 // The layout from which the folder is being opened may be scaled, adjust the starting
2935 // view size by this scale factor.
2936 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002937 lp.customPosition = true;
2938 lp.x = mRectForFolderAnimation.left;
2939 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002940 lp.width = (int) (scale * width);
2941 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002942
2943 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2944 fi.draw(mFolderIconCanvas);
2945 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002946 if (fi.getFolder() != null) {
2947 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2948 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002949 }
Adam Cohen268c4752012-06-06 17:47:33 -07002950 // Just in case this image view is still in the drag layer from a previous animation,
2951 // we remove it and re-add it.
2952 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2953 mDragLayer.removeView(mFolderIconImageView);
2954 }
2955 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002956 if (fi.getFolder() != null) {
2957 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002958 }
Adam Cohen268c4752012-06-06 17:47:33 -07002959 }
2960
Adam Cohen37b2a492016-04-06 18:36:06 -07002961 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002962 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002963 FolderInfo info = (FolderInfo) fi.getTag();
2964 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2965 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002966 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2967 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002968 }
2969
Adam Cohen268c4752012-06-06 17:47:33 -07002970 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2971 copyFolderIconToImage(fi);
2972 fi.setVisibility(View.INVISIBLE);
2973
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002974 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2975 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002976 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002977 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2978 }
2979 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002980 oa.start();
2981 }
2982
Sunny Goyal935fca12015-10-13 10:19:01 -07002983 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002984 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002985 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002986
Adam Cohen268c4752012-06-06 17:47:33 -07002987 // We remove and re-draw the FolderIcon in-case it has changed
2988 mDragLayer.removeView(mFolderIconImageView);
2989 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08002990
2991 if (cl != null) {
2992 cl.clearFolderLeaveBehind();
2993 }
2994
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002995 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002996 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002997 oa.addListener(new AnimatorListenerAdapter() {
2998 @Override
2999 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003000 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003001 // Remove the ImageView copy of the FolderIcon and make the original visible.
3002 mDragLayer.removeView(mFolderIconImageView);
3003 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003004 }
3005 }
3006 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003007 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003008 if (!animate) {
3009 oa.end();
3010 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003011 }
3012
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003013 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003014 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003015 * is animated relative to the specified View. If the View is null, no animation
3016 * is played.
3017 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003018 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003019 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003020 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003021
Adam Cohenfb91f302012-06-11 15:45:18 -07003022 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003023 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3024 if (openFolder != null && openFolder != folder) {
3025 // Close any open folder before opening a folder.
3026 closeFolder();
3027 }
3028
Adam Cohena9cf38f2011-05-02 15:36:58 -07003029 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003030
Adam Cohena9cf38f2011-05-02 15:36:58 -07003031 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003032
Sunny Goyalf4066152015-04-15 09:42:19 -07003033 // While the folder is open, the position of the icon cannot change.
3034 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3035
Adam Cohen4554ee12011-08-03 16:13:21 -07003036 // Just verify that the folder hasn't already been added to the DragLayer.
3037 // There was a one-off crash where the folder had a parent already.
3038 if (folder.getParent() == null) {
3039 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003040 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003041 } else {
3042 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3043 folder.getParent() + ").");
3044 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003045 folder.animateOpen();
3046
3047 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003048
3049 // Notify the accessibility manager that this folder "window" has appeared and occluded
3050 // the workspace items
3051 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3052 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003053 }
3054
3055 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003056 closeFolder(true);
3057 }
3058
3059 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003060 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003061 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003062 if (folder.isEditingName()) {
3063 folder.dismissEditingName();
3064 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003065 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003066 }
3067 }
3068
Sunny Goyal935fca12015-10-13 10:19:01 -07003069 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003070 animate &= !Utilities.isPowerSaverOn(this);
3071
Adam Cohen4554ee12011-08-03 16:13:21 -07003072 folder.getInfo().opened = false;
3073
3074 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3075 if (parent != null) {
3076 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003077 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003078 if (fi != null) {
3079 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3080 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003081 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003082 if (animate) {
3083 folder.animateClosed();
3084 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003085 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003086 }
Winson Chung83ca4802013-04-12 15:10:52 -07003087
Sunny Goyal935fca12015-10-13 10:19:01 -07003088 // Notify the accessibility manager that this folder "window" has disappeared and no
3089 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003090 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003091 }
3092
Tony Wickhamdadb3042016-02-24 11:07:00 -08003093 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003094 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003095 if (!isDraggingEnabled()) return false;
3096 if (isWorkspaceLocked()) return false;
3097 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003098
Winson Chung76648c52015-07-10 14:33:23 -07003099 if (v == mAllAppsButton) {
3100 onLongClickAllAppsButton(v);
3101 return true;
3102 }
3103
Adam Cohen1697b792013-09-17 19:08:21 -07003104 if (v instanceof Workspace) {
3105 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003106 if (!mWorkspace.isTouchActive()) {
3107 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003108 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3109 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3110 return true;
3111 } else {
3112 return false;
3113 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003114 } else {
3115 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003116 }
Adam Cohen1697b792013-09-17 19:08:21 -07003117 }
3118
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003119 CellLayout.CellInfo longClickCellInfo = null;
3120 View itemUnderLongClick = null;
3121 if (v.getTag() instanceof ItemInfo) {
3122 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003123 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003124 itemUnderLongClick = longClickCellInfo.cell;
3125 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003126 }
3127
Winson Chung3d503fb2011-07-13 17:25:49 -07003128 // The hotseat touch handling does not go through Workspace, and we always allow long press
3129 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003130 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003131 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003132 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003133 // User long pressed on empty space
3134 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3135 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003136 if (mWorkspace.isInOverviewMode()) {
3137 mWorkspace.startReordering(v);
3138 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003139 showOverviewMode(true);
Hyunyoung Song645764e2016-06-06 14:19:02 -07003140 mHotseat.requestDisallowInterceptTouchEvent(true);
3141
Adam Cohendedbd962013-07-11 14:21:49 -07003142 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003143 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003144 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3145 mHotseat.getOrderInHotseat(
3146 longClickCellInfo.cellX,
3147 longClickCellInfo.cellY));
3148 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003149 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003150 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003151 }
3152 }
3153 }
3154 return true;
3155 }
3156
Winson Chung3d503fb2011-07-13 17:25:49 -07003157 boolean isHotseatLayout(View layout) {
3158 return mHotseat != null && layout != null &&
3159 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3160 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003161
Winson Chung3d503fb2011-07-13 17:25:49 -07003162 /**
3163 * Returns the CellLayout of the specified container at the specified screen.
3164 */
Sunny Goyal92820592015-03-02 11:31:35 -08003165 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003166 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3167 if (mHotseat != null) {
3168 return mHotseat.getLayout();
3169 } else {
3170 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003171 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003172 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003173 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003174 }
3175 }
3176
Winson Chungb745afb2015-03-02 11:51:23 -08003177 /**
3178 * For overridden classes.
3179 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003180 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003181 return isAppsViewVisible();
3182 }
3183
3184 public boolean isAppsViewVisible() {
3185 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3186 }
3187
3188 public boolean isWidgetsViewVisible() {
3189 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003190 }
3191
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003192 private void setWorkspaceBackground(int background) {
3193 switch (background) {
3194 case WORKSPACE_BACKGROUND_TRANSPARENT:
3195 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3196 break;
3197 case WORKSPACE_BACKGROUND_BLACK:
3198 getWindow().setBackgroundDrawable(null);
3199 break;
3200 default:
3201 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3202 }
Craig Mautner360310b2012-10-26 15:13:08 -07003203 }
3204
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003205 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003206 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3207 int curflags = getWindow().getAttributes().flags
3208 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3209 if (wpflags != curflags) {
3210 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3211 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003212 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003213 }
3214
Michael Jurkae326f182011-11-21 14:05:46 -08003215 @Override
3216 public void onTrimMemory(int level) {
3217 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003218 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3219 // The widget preview db can result in holding onto over
3220 // 3MB of memory for caching which isn't necessary.
3221 SQLiteDatabase.releaseMemory();
3222
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003223 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003224 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003225 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003226 if (mLauncherCallbacks != null) {
3227 mLauncherCallbacks.onTrimMemory(level);
3228 }
Michael Jurkae326f182011-11-21 14:05:46 -08003229 }
3230
Winson5c6bdbb2015-09-03 11:36:19 -07003231 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003232 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003233 }
3234
Winson5c6bdbb2015-09-03 11:36:19 -07003235 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003236 boolean changed = mState != State.WORKSPACE ||
3237 mWorkspace.getState() != Workspace.State.NORMAL;
3238 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003239 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003240 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003241 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003242
Michael Jurkab3e22d92011-10-31 15:58:33 -07003243 // Set focus to the AppsCustomize button
3244 if (mAllAppsButton != null) {
3245 mAllAppsButton.requestFocus();
3246 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003247 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003248
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003249 // Change the state *after* we've called all the transition code
3250 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003251
Winson Chung5fb63472011-02-02 17:03:37 -08003252 // Resume the auto-advance of widgets
3253 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003254 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003255
Winson Chung0f785722015-04-08 10:27:49 -07003256 if (changed) {
3257 // Send an accessibility event to announce the context change
3258 getWindow().getDecorView()
3259 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003260 }
Winson5c6bdbb2015-09-03 11:36:19 -07003261 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003262 }
3263
Winsone9f27272015-10-13 10:47:51 -07003264 /**
3265 * Shows the overview button.
3266 */
Adam Cohened307df2013-10-02 09:37:31 -07003267 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003268 showOverviewMode(animated, false);
3269 }
3270
3271 /**
3272 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3273 * onto one of the overview panel buttons.
3274 */
3275 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3276 Runnable postAnimRunnable = null;
3277 if (requestButtonFocus) {
3278 postAnimRunnable = new Runnable() {
3279 @Override
3280 public void run() {
3281 // Hitting the menu button when in touch mode does not trigger touch mode to
3282 // be disabled, so if requested, force focus on one of the overview panel
3283 // buttons.
3284 mOverviewPanel.requestFocusFromTouch();
3285 }
3286 };
3287 }
Adam Cohened307df2013-10-02 09:37:31 -07003288 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003289 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003290 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003291 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003292 }
3293
Winson Chungb745afb2015-03-02 11:51:23 -08003294 /**
3295 * Shows the apps view.
3296 */
Hyunyoung Song645764e2016-06-06 14:19:02 -07003297 public void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003298 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003299 if (resetListToTop) {
3300 mAppsView.scrollToTop();
3301 }
Winson Chung4ac30062015-05-08 17:34:17 -07003302 if (updatePredictedApps) {
3303 tryAndUpdatePredictedApps();
3304 }
Winson Chung76648c52015-07-10 14:33:23 -07003305 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003306 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003307
Winson Chungb745afb2015-03-02 11:51:23 -08003308 /**
3309 * Shows the widgets view.
3310 */
3311 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003312 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003313 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003314 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003315 }
Winson Chung76648c52015-07-10 14:33:23 -07003316 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003317
3318 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003319 @Override
3320 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003321 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003322 }
3323 });
Winson Chungb745afb2015-03-02 11:51:23 -08003324 }
3325
3326 /**
3327 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003328 *
3329 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003330 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003331 // TODO: calling method should use the return value so that when {@code false} is returned
3332 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003333 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003334 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3335 mState != State.WIDGETS_SPRING_LOADED) {
3336 return false;
3337 }
3338 if (toState != State.APPS && toState != State.WIDGETS) {
3339 return false;
3340 }
Winson Chungb745afb2015-03-02 11:51:23 -08003341
3342 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003343 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3344 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003345 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003346 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003347 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003348
Michael Jurkab3e22d92011-10-31 15:58:33 -07003349 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003350 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003351
3352 // Pause the auto-advance of widgets until we are out of AllApps
3353 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003354 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003355 closeFolder();
3356
3357 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003358 getWindow().getDecorView()
3359 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003360 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003361 }
3362
Winson Chungcd99cd32015-04-29 11:03:24 -07003363 /**
3364 * Updates the workspace and interaction state on state change, and return the animation to this
3365 * new state.
3366 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003367 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003368 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003369 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003370 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003371 updateInteraction(fromState, toState);
3372 return anim;
3373 }
3374
Jun Mukaif0033da2015-08-04 18:34:30 -07003375 public void onLauncherClingShown() {
3376 // When a launcher cling appears, it should cover the underlying layers, so their focus
3377 // should be blocked.
3378 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3379 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3380 }
3381 }
3382
3383 public void onLauncherClingDismissed() {
3384 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3385 }
3386
Hyunyoung Song3f471442015-04-08 19:01:34 -07003387 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003388 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003389 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003390 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003391 }
Winson Chungb745afb2015-03-02 11:51:23 -08003392
Winson Chung006ee262015-08-03 14:40:11 -07003393 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003394 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003395 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003396
3397 if (isAppsViewVisible()) {
3398 mState = State.APPS_SPRING_LOADED;
3399 } else if (isWidgetsViewVisible()) {
3400 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003401 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003402 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003403 } else {
3404 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003405 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003406 }
Adam Cohen7777d962011-08-18 18:58:38 -07003407
Hyunyoung Song3f471442015-04-08 19:01:34 -07003408 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003409 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003410 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003411
Winson Chunge7a03942011-08-05 15:05:12 -07003412 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003413 @Override
3414 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003415 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003416 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3417 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003418 // Before we show workspace, hide all apps again because
3419 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3420 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003421 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003422 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003423 } else {
3424 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003425 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003426 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003427 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003428 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003429
Tony Wickhame0c33232016-02-08 11:37:04 -08003430 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003431 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3432 || mState == State.WIDGETS_SPRING_LOADED;
3433 }
3434
Michael Jurkad3ef3062010-11-23 16:23:58 -08003435 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003436 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003437 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003438 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003439 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003440 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003441 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3442 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003443 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003444 }
3445
Winson Chung4ac30062015-05-08 17:34:17 -07003446 /**
3447 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3448 * resumed.
3449 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003450 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003451 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003452 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003453 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003454 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003455 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003456 }
3457 }
3458 }
3459
Michael Jurkab3e22d92011-10-31 15:58:33 -07003460 void lockAllApps() {
3461 // TODO
3462 }
3463
3464 void unlockAllApps() {
3465 // TODO
3466 }
3467
Winsonf768d932015-09-25 16:12:35 -07003468 public boolean launcherCallbacksProvidesSearch() {
3469 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3470 }
3471
Michael Jurkad7c28052012-04-27 15:43:36 -07003472 @Override
3473 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003474 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003475 final List<CharSequence> text = event.getText();
3476 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003477 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003478 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003479 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003480 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003481 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003482 } else if (mWorkspace != null) {
3483 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003484 } else {
3485 text.add(getString(R.string.all_apps_home_button_label));
3486 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003487 return result;
3488 }
3489
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003490 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003491 * If the activity is currently paused, signal that we need to run the passed Runnable
3492 * in onResume.
3493 *
3494 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003495 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3496 * wrong when we're not running, and if the activity comes back to what the configuration was
3497 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003498 *
3499 * Implementation of the method from LauncherModel.Callbacks.
3500 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003501 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003502 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003503 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003504 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003505 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003506 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003507 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003508 }
3509 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003510 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003511 return true;
3512 } else {
3513 return false;
3514 }
3515 }
3516
Michael Jurkac402cd92013-05-20 15:49:32 +02003517 private boolean waitUntilResume(Runnable run) {
3518 return waitUntilResume(run, false);
3519 }
3520
Michael Jurka1e2f4652013-07-08 18:03:46 -07003521 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003522 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003523 }
3524
Michael Jurka7607c2f2013-04-03 14:33:19 -07003525 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003526 * If the activity is currently paused, signal that we need to re-run the loader
3527 * in onResume.
3528 *
3529 * This needs to be called from incoming places where resources might have been loaded
3530 * while we are paused. That is becaues the Configuration might be wrong
3531 * when we're not running, and if it comes back to what it was when we
3532 * were paused, we are not restarted.
3533 *
3534 * Implementation of the method from LauncherModel.Callbacks.
3535 *
3536 * @return true if we are currently paused. The caller might be able to
3537 * skip some work in that case since we will come back again.
3538 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003539 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003540 public boolean setLoadOnResume() {
3541 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003542 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003543 mOnResumeNeedsLoad = true;
3544 return true;
3545 } else {
3546 return false;
3547 }
3548 }
3549
3550 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003551 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003552 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003553 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003554 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003555 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003556 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003557 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003558 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003559 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003560 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003561
Joe Onorato9c1289c2009-08-17 11:03:03 -04003562 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003563 * Clear any pending bind callbacks. This is called when is loader is planning to
3564 * perform a full rebind from scratch.
3565 */
3566 @Override
3567 public void clearPendingBinds() {
3568 mBindOnResumeCallbacks.clear();
3569 if (mPendingExecutor != null) {
3570 mPendingExecutor.markCompleted();
3571 mPendingExecutor = null;
3572 }
3573 }
3574
3575 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003576 * Refreshes the shortcuts shown on the workspace.
3577 *
3578 * Implementation of the method from LauncherModel.Callbacks.
3579 */
3580 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003581 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003582
Winson Chungd64d1762013-08-20 14:37:16 -07003583 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003584 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003585 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003586
Michael Jurka05bf644e2011-11-30 20:28:53 -08003587 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003588 if (mHotseat != null) {
3589 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003590 }
3591 }
3592
Adam Cohendcd297f2013-06-18 13:13:40 -07003593 @Override
3594 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003595 // Make sure the first screen is always at the start.
3596 if (orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
3597 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3598 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3599 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Adam Cohen5084cba2013-09-03 12:01:16 -07003600 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003601 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003602
3603 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003604 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003605 if (hasCustomContentToLeft()) {
3606 mWorkspace.createCustomContentContainer();
3607 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003608 }
Winson Chung64359a52013-07-08 17:17:08 -07003609 }
3610
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003611 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003612 int count = orderedScreenIds.size();
3613 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003614 long screenId = orderedScreenIds.get(i);
3615 if (screenId != Workspace.FIRST_SCREEN_ID) {
3616 // No need to bind the first screen, as its always bound.
3617 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3618 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003619 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003620 }
3621
Winson Chungd64d1762013-08-20 14:37:16 -07003622 public void bindAppsAdded(final ArrayList<Long> newScreens,
3623 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003624 final ArrayList<ItemInfo> addAnimated,
3625 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003626 Runnable r = new Runnable() {
3627 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003628 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003629 }
3630 };
3631 if (waitUntilResume(r)) {
3632 return;
3633 }
3634
Winson Chungd64d1762013-08-20 14:37:16 -07003635 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003636 if (newScreens != null) {
3637 bindAddScreens(newScreens);
3638 }
Winson Chungd64d1762013-08-20 14:37:16 -07003639
3640 // We add the items without animation on non-visible pages, and with
3641 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003642 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003643 bindItems(addNotAnimated, 0,
3644 addNotAnimated.size(), false);
3645 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003646 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003647 bindItems(addAnimated, 0,
3648 addAnimated.size(), true);
3649 }
Winson Chungc58497e2013-09-03 17:48:37 -07003650
Winson Chung87412982013-10-03 18:34:14 -07003651 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003652 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003653
Winson Chungb745afb2015-03-02 11:51:23 -08003654 if (addedApps != null && mAppsView != null) {
3655 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003656 }
Winson Chungd64d1762013-08-20 14:37:16 -07003657 }
3658
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003659 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003660 * Bind the items start-end from the list.
3661 *
3662 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003663 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003664 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003665 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3666 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003667 Runnable r = new Runnable() {
3668 public void run() {
3669 bindItems(shortcuts, start, end, forceAnimateIcons);
3670 }
3671 };
3672 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003673 return;
3674 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003675
Winson Chungf0c6ae02012-03-21 16:10:31 -07003676 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003677 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3678 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003679 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003680 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003681 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003682 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003683 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003684
3685 // Short circuit if we are loading dock items for a configuration which has no dock
3686 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3687 mHotseat == null) {
3688 continue;
3689 }
3690
Sunny Goyal639e9062015-08-19 19:17:06 -07003691 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003692 switch (item.itemType) {
3693 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3694 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003695 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003696 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003697 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003698 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003699 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003700 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003701 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003702 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003703 default:
3704 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003705 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003706
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003707 /*
3708 * Remove colliding items.
3709 */
3710 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3711 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3712 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3713 View v = cl.getChildAt(item.cellX, item.cellY);
3714 Object tag = v.getTag();
3715 String desc = "Collision while binding workspace item: " + item
3716 + ". Collides with " + tag;
3717 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3718 throw (new RuntimeException(desc));
3719 } else {
3720 Log.d(TAG, desc);
3721 LauncherModel.deleteItemFromDatabase(this, item);
3722 continue;
3723 }
3724 }
3725 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003726 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3727 item.cellY, 1, 1);
3728 if (animateIcons) {
3729 // Animate all the applications up now
3730 view.setAlpha(0f);
3731 view.setScaleX(0f);
3732 view.setScaleY(0f);
3733 bounceAnims.add(createNewAppBounceAnimation(view, i));
3734 newShortcutsScreenId = item.screenId;
3735 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003736 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003737
Winson Chung997a9232013-07-24 15:33:46 -07003738 if (animateIcons) {
3739 // Animate to the correct page
3740 if (newShortcutsScreenId > -1) {
3741 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003742 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003743 final Runnable startBounceAnimRunnable = new Runnable() {
3744 public void run() {
3745 anim.playTogether(bounceAnims);
3746 anim.start();
3747 }
3748 };
Winson Chung997a9232013-07-24 15:33:46 -07003749 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003750 // We post the animation slightly delayed to prevent slowdowns
3751 // when we are loading right after we return to launcher.
3752 mWorkspace.postDelayed(new Runnable() {
3753 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003754 if (mWorkspace != null) {
3755 mWorkspace.snapToPage(newScreenIndex);
3756 mWorkspace.postDelayed(startBounceAnimRunnable,
3757 NEW_APPS_ANIMATION_DELAY);
3758 }
Winson Chung94d67682013-09-25 16:29:40 -07003759 }
3760 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003761 } else {
3762 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003763 }
3764 }
Winson Chung64359a52013-07-08 17:17:08 -07003765 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003766 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003767 }
3768
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003769 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3770 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3771 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003772 view.updateAppWidget(null);
3773 view.setOnClickListener(this);
3774 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003775 mWorkspace.requestLayout();
3776 }
3777
Joe Onorato9c1289c2009-08-17 11:03:03 -04003778 /**
3779 * Add the views for a widget to the workspace.
3780 *
3781 * Implementation of the method from LauncherModel.Callbacks.
3782 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003783 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003784 Runnable r = new Runnable() {
3785 public void run() {
3786 bindAppWidget(item);
3787 }
3788 };
3789 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003790 return;
3791 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003792
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003793 if (mIsSafeModeEnabled) {
3794 bindSafeModeWidget(item);
3795 return;
3796 }
3797
Daniel Sandler843e8602010-06-07 14:59:01 -04003798 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3799 if (DEBUG_WIDGETS) {
3800 Log.d(TAG, "bindAppWidget: " + item);
3801 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003802
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003803 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003804
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003805 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3806 // If the provider is not ready, bind as a pending widget.
3807 appWidgetInfo = null;
3808 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3809 // The widget id is not valid. Try to find the widget based on the provider info.
3810 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3811 } else {
3812 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3813 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003814
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003815 // If the provider is ready, but the width is not yet restored, try to restore it.
3816 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3817 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003818 if (appWidgetInfo == null) {
3819 if (DEBUG_WIDGETS) {
3820 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3821 + " belongs to component " + item.providerName
3822 + ", as the povider is null");
3823 }
3824 LauncherModel.deleteItemFromDatabase(this, item);
3825 return;
3826 }
Sunny Goyalff572272014-07-23 13:58:07 -07003827
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003828 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003829 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003830 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3831 // Id has not been allocated yet. Allocate a new id.
3832 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3833 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003834
Sunny Goyald478c832016-04-01 12:04:16 -07003835 // Also try to bind the widget. If the bind fails, the user will be shown
3836 // a click to setup UI, which will ask for the bind permission.
3837 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3838 pendingInfo.spanX = item.spanX;
3839 pendingInfo.spanY = item.spanY;
3840 pendingInfo.minSpanX = item.minSpanX;
3841 pendingInfo.minSpanY = item.minSpanY;
3842 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
3843 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3844 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003845
Sunny Goyald478c832016-04-01 12:04:16 -07003846 // Bind succeeded
3847 if (success) {
3848 // If the widget has a configure activity, it is still needs to set it up,
3849 // otherwise the widget is ready to go.
3850 item.restoreStatus = (appWidgetInfo.configure == null)
3851 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3852 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003853 }
Sunny Goyald478c832016-04-01 12:04:16 -07003854
3855 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003856 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003857 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003858 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003859 // The widget was marked as UI not ready, but there is no configure activity to
3860 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003861 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3862 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003863 }
Sunny Goyalff572272014-07-23 13:58:07 -07003864 }
3865
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003866 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003867 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003868 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3869 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003870 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003871
Sunny Goyal56c73602015-09-25 12:55:01 -07003872 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003873 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003874 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003875 deleteWidgetInfo(item);
3876 return;
3877 }
3878
Sunny Goyal233ee962015-08-03 13:05:01 -07003879 item.minSpanX = appWidgetInfo.minSpanX;
3880 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003881 addAppWidgetToWorkspace(
3882 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3883 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003884 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003885 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003886 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003887 view.updateAppWidget(null);
3888 view.setOnClickListener(this);
3889 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003890 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003891 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003892
Daniel Sandler843e8602010-06-07 14:59:01 -04003893 if (DEBUG_WIDGETS) {
3894 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3895 + (SystemClock.uptimeMillis()-start) + "ms");
3896 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003897 }
3898
Sunny Goyalff572272014-07-23 13:58:07 -07003899 /**
3900 * Restores a pending widget.
3901 *
3902 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003903 */
Sunny Goyald478c832016-04-01 12:04:16 -07003904 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003905 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3906 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3907 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003908 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003909 }
3910
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003911 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003912 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003913
3914 mWorkspace.reinflateWidgetsIfNecessary();
3915 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003916 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003917 }
3918
Adam Cohen1462de32012-07-24 22:34:36 -07003919 public void onPageBoundSynchronously(int page) {
3920 mSynchronouslyBoundPages.add(page);
3921 }
3922
Sunny Goyal527c7d32015-08-28 15:19:36 -07003923 @Override
3924 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3925 if (mPendingExecutor != null) {
3926 mPendingExecutor.markCompleted();
3927 }
3928 mPendingExecutor = executor;
3929 executor.attachTo(this);
3930 }
3931
3932 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3933 if (mPendingExecutor == executor) {
3934 mPendingExecutor = null;
3935 }
3936 }
3937
Joe Onorato9c1289c2009-08-17 11:03:03 -04003938 /**
3939 * Callback saying that there aren't any more items to bind.
3940 *
3941 * Implementation of the method from LauncherModel.Callbacks.
3942 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003943 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003944 Runnable r = new Runnable() {
3945 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003946 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003947 }
3948 };
3949 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003950 return;
3951 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003952 if (mSavedState != null) {
3953 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003954 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003955 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003956
Joe Onorato9c1289c2009-08-17 11:03:03 -04003957 mSavedState = null;
3958 }
3959
Adam Cohen1462de32012-07-24 22:34:36 -07003960 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003961
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003962 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003963
3964 // If we received the result of any pending adds while the loader was running (e.g. the
3965 // widget configuration forced an orientation change), process them now.
3966 if (sPendingAddItem != null) {
3967 final long screenId = completeAdd(sPendingAddItem);
3968
3969 // TODO: this moves the user to the page where the pending item was added. Ideally,
3970 // the screen would be guaranteed to exist after bind, and the page would be set through
3971 // the workspace restore process.
3972 mWorkspace.post(new Runnable() {
3973 @Override
3974 public void run() {
3975 mWorkspace.snapToScreenId(screenId);
3976 }
3977 });
3978 sPendingAddItem = null;
3979 }
3980
Sunny Goyal756adbc2015-04-16 15:20:51 -07003981 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003982
3983 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003984 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003985 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003986 }
3987
Winson Chunga0b7e862013-09-05 16:03:15 -07003988 public boolean isAllAppsButtonRank(int rank) {
3989 if (mHotseat != null) {
3990 return mHotseat.isAllAppsButtonRank(rank);
3991 }
3992 return false;
3993 }
3994
Winson Chunga2413752012-04-03 14:22:34 -07003995 private boolean canRunNewAppsAnimation() {
3996 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07003997 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
3998 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07003999 }
4000
Winson Chungc9168342013-06-26 14:54:55 -07004001 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004002 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004003 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4004 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004005 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004006 return bounceAnim;
4007 }
4008
Adam Cohen27772732013-11-11 14:00:56 +00004009 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004010 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004011 }
4012
Tony Wickham55616cd2015-09-23 14:55:17 -07004013 public int getSearchBarHeight() {
4014 if (mLauncherCallbacks != null) {
4015 return mLauncherCallbacks.getSearchBarHeight();
4016 }
4017 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4018 }
4019
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004020 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004021 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4022 * multiple calls to bind the same list.)
4023 */
4024 @Thunk ArrayList<AppInfo> mTmpAppsList;
4025 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4026 public void run() {
4027 bindAllApplications(mTmpAppsList);
4028 mTmpAppsList = null;
4029 }
4030 };
4031
4032 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004033 * Add the icons for all apps.
4034 *
4035 * Implementation of the method from LauncherModel.Callbacks.
4036 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004037 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004038 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4039 mTmpAppsList = apps;
4040 return;
4041 }
4042
Winson Chungb745afb2015-03-02 11:51:23 -08004043 if (mAppsView != null) {
4044 mAppsView.setApps(apps);
4045 }
Adam Cohen9211d422014-10-07 18:14:53 -07004046 if (mLauncherCallbacks != null) {
4047 mLauncherCallbacks.bindAllApplications(apps);
4048 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004049 }
4050
4051 /**
4052 * A package was updated.
4053 *
4054 * Implementation of the method from LauncherModel.Callbacks.
4055 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004056 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004057 Runnable r = new Runnable() {
4058 public void run() {
4059 bindAppsUpdated(apps);
4060 }
4061 };
4062 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004063 return;
4064 }
4065
Winson Chungb745afb2015-03-02 11:51:23 -08004066 if (mAppsView != null) {
4067 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004068 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004069 }
4070
Sunny Goyal4390ace2014-10-13 11:33:11 -07004071 @Override
4072 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4073 Runnable r = new Runnable() {
4074 public void run() {
4075 bindWidgetsRestored(widgets);
4076 }
4077 };
4078 if (waitUntilResume(r)) {
4079 return;
4080 }
4081 mWorkspace.widgetsRestored(widgets);
4082 }
4083
Joe Onorato9c1289c2009-08-17 11:03:03 -04004084 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004085 * Some shortcuts were updated in the background.
4086 *
4087 * Implementation of the method from LauncherModel.Callbacks.
4088 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004089 @Override
4090 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4091 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004092 Runnable r = new Runnable() {
4093 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004094 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004095 }
4096 };
4097 if (waitUntilResume(r)) {
4098 return;
4099 }
4100
Sunny Goyal4390ace2014-10-13 11:33:11 -07004101 if (!updated.isEmpty()) {
4102 mWorkspace.updateShortcuts(updated);
4103 }
4104
4105 if (!removed.isEmpty()) {
4106 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4107 for (ShortcutInfo si : removed) {
4108 removedComponents.add(si.getTargetComponent());
4109 }
4110 mWorkspace.removeItemsByComponentName(removedComponents, user);
4111 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004112 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004113 }
4114 }
4115
4116 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004117 * Update the state of a package, typically related to install state.
4118 *
4119 * Implementation of the method from LauncherModel.Callbacks.
4120 */
Sunny Goyale755d462014-07-22 13:48:29 -07004121 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004122 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4123 Runnable r = new Runnable() {
4124 public void run() {
4125 bindRestoreItemsChange(updates);
4126 }
4127 };
4128 if (waitUntilResume(r)) {
4129 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004130 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004131
Sunny Goyal756adbc2015-04-16 15:20:51 -07004132 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004133 }
4134
4135 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004136 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004137 * in addition to specific applications to remove, the reason being that
4138 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004139 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004140 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004141 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004142 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004143 public void bindWorkspaceComponentsRemoved(
4144 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4145 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004146 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004147 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004148 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004149 }
Winson Chungd64d1762013-08-20 14:37:16 -07004150 };
4151 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004152 return;
4153 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004154 if (!packageNames.isEmpty()) {
4155 mWorkspace.removeItemsByPackageName(packageNames, user);
4156 }
4157 if (!components.isEmpty()) {
4158 mWorkspace.removeItemsByComponentName(components, user);
4159 }
4160 // Notify the drag controller
4161 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004162
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004163 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004164
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004165 @Override
4166 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4167 Runnable r = new Runnable() {
4168 public void run() {
4169 bindAppInfosRemoved(appInfos);
4170 }
4171 };
4172 if (waitUntilResume(r)) {
4173 return;
Joe Onorato36115782010-06-17 13:28:48 -04004174 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004175
Winson Chungdf95eb12013-10-16 14:57:07 -07004176 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004177 if (mAppsView != null) {
4178 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004179 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004180 }
Joe Onoratobe386092009-11-17 17:32:16 -08004181
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004182 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004183 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004184 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004185 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004186 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004187
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004188 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004189 public void bindWidgetsModel(WidgetsModel model) {
4190 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004191 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004192 return;
4193 }
4194
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004195 if (mWidgetsView != null && model != null) {
4196 mWidgetsView.addWidgets(model);
4197 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004198 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004199 }
4200
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004201 @Override
4202 public void notifyWidgetProvidersChanged() {
4203 if (mWorkspace.getState().shouldUpdateWidget) {
4204 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4205 }
4206 }
4207
Winson Chung400438b2011-01-16 17:53:48 -08004208 private int mapConfigurationOriActivityInfoOri(int configOri) {
4209 final Display d = getWindowManager().getDefaultDisplay();
4210 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4211 switch (d.getRotation()) {
4212 case Surface.ROTATION_0:
4213 case Surface.ROTATION_180:
4214 // We are currently in the same basic orientation as the natural orientation
4215 naturalOri = configOri;
4216 break;
4217 case Surface.ROTATION_90:
4218 case Surface.ROTATION_270:
4219 // We are currently in the other basic orientation to the natural orientation
4220 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4221 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4222 break;
4223 }
4224
4225 int[] oriMap = {
4226 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4227 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4228 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4229 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4230 };
4231 // Since the map starts at portrait, we need to offset if this device's natural orientation
4232 // is landscape.
4233 int indexOffset = 0;
4234 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4235 indexOffset = 1;
4236 }
4237 return oriMap[(d.getRotation() + indexOffset) % 4];
4238 }
Adam Cohen446e9402011-09-15 18:21:21 -07004239
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004240 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004241 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004242 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004243 if (Utilities.ATLEAST_JB_MR2) {
4244 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4245 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004246 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4247 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004248 }
Winson Chung4b919f82012-05-01 10:44:08 -07004249 }
4250 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004251
Winson Chung4b919f82012-05-01 10:44:08 -07004252 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004253 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004254 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004255 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004256 } else {
4257 mHandler.postDelayed(new Runnable() {
4258 public void run() {
4259 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4260 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004261 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004262 }
Winson Chung4b919f82012-05-01 10:44:08 -07004263 }
Winson Chung400438b2011-01-16 17:53:48 -08004264 }
4265
Adam Cohenea90f832014-05-21 15:03:34 -07004266 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004267 * To be overridden by subclasses to indicate that there is an activity to launch
4268 * before showing the standard launcher experience.
4269 */
4270 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004271 if (mLauncherCallbacks != null) {
4272 return mLauncherCallbacks.hasFirstRunActivity();
4273 }
Adam Cohen432609a2014-03-13 17:03:22 -07004274 return false;
4275 }
4276
4277 /**
4278 * To be overridden by subclasses to launch any first run activity
4279 */
4280 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004281 if (mLauncherCallbacks != null) {
4282 return mLauncherCallbacks.getFirstRunActivity();
4283 }
Adam Cohen432609a2014-03-13 17:03:22 -07004284 return null;
4285 }
4286
Winson Chunga6945242014-01-08 14:04:34 -08004287 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004288 return !ActivityManager.isRunningInTestHarness() &&
4289 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004290 }
4291
Adam Cohen4a9423d2014-03-28 14:22:31 -07004292 protected boolean hasRunFirstRunActivity() {
4293 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4294 }
4295
Adam Cohen432609a2014-03-13 17:03:22 -07004296 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004297 if (shouldRunFirstRunActivity() &&
4298 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004299 Intent firstRunIntent = getFirstRunActivity();
4300 if (firstRunIntent != null) {
4301 startActivity(firstRunIntent);
4302 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004303 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004304 }
4305 }
Adam Cohen432609a2014-03-13 17:03:22 -07004306 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004307 }
4308
4309 private void markFirstRunActivityShown() {
4310 SharedPreferences.Editor editor = mSharedPrefs.edit();
4311 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4312 editor.apply();
4313 }
4314
Adam Cohen432609a2014-03-13 17:03:22 -07004315 /**
4316 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4317 * screen that must be displayed and dismissed.
4318 */
4319 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004320 if (mLauncherCallbacks != null) {
4321 return mLauncherCallbacks.hasDismissableIntroScreen();
4322 }
Adam Cohen432609a2014-03-13 17:03:22 -07004323 return false;
4324 }
4325
4326 /**
4327 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4328 */
4329 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004330 if (mLauncherCallbacks != null) {
4331 return mLauncherCallbacks.getIntroScreen();
4332 }
Adam Cohen432609a2014-03-13 17:03:22 -07004333 return null;
4334 }
4335
4336 /**
4337 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4338 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4339 */
4340 private boolean shouldShowIntroScreen() {
4341 return hasDismissableIntroScreen() &&
4342 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4343 }
4344
4345 protected void showIntroScreen() {
4346 View introScreen = getIntroScreen();
4347 changeWallpaperVisiblity(false);
4348 if (introScreen != null) {
4349 mDragLayer.showOverlayView(introScreen);
4350 }
4351 }
4352
4353 public void dismissIntroScreen() {
4354 markIntroScreenDismissed();
4355 if (showFirstRunActivity()) {
4356 // We delay hiding the intro view until the first run activity is showing. This
4357 // avoids a blip.
4358 mWorkspace.postDelayed(new Runnable() {
4359 @Override
4360 public void run() {
4361 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004362 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004363 }
4364 }, ACTIVITY_START_DELAY);
4365 } else {
4366 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004367 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004368 }
4369 changeWallpaperVisiblity(true);
4370 }
4371
4372 private void markIntroScreenDismissed() {
4373 SharedPreferences.Editor editor = mSharedPrefs.edit();
4374 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4375 editor.apply();
4376 }
4377
Adam Cohen091440a2015-03-18 14:16:05 -07004378 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004379 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4380 // on the device, then we always show the first run cling experience (or if there is no
4381 // launcher2). Otherwise, we prompt the user upon started for migration
4382 LauncherClings launcherClings = new LauncherClings(this);
4383 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004384 mClings = launcherClings;
Sunny Goyalded0fdb2016-05-23 15:55:41 -07004385 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004386 }
4387 }
4388
Winson Chung5ffd51d2015-06-25 11:36:50 -07004389 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004390 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004391 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004392 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004393 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004394 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004395 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4396 if (userHandle != null) {
4397 user = UserHandleCompat.fromUser(userHandle);
4398 }
4399 }
4400 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004401 }
4402
4403 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004404 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004405 if (user == null) {
4406 user = UserHandleCompat.myUserHandle();
4407 }
4408
4409 // Called from search suggestion, add the profile extra to the intent to ensure that we
4410 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004411 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004412 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004413 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004414 return null;
4415 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004416 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004417 }
4418
Winson Chung5ffd51d2015-06-25 11:36:50 -07004419 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004420 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4421 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004422 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004423 UserHandleCompat.myUserHandle());
4424 }
4425
Winson Chung5ffd51d2015-06-25 11:36:50 -07004426 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004427 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4428 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004429 mWorkspace.onExternalDragStartedWithItem(dragView);
4430 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004431 }
4432
Winson Chung5ffd51d2015-06-25 11:36:50 -07004433 protected void moveWorkspaceToDefaultScreen() {
4434 mWorkspace.moveToDefaultScreen(false);
4435 }
4436
Winson Chung80baf5a2010-08-09 16:03:15 -07004437 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004438 * Returns a FastBitmapDrawable with the icon, accurately sized.
4439 */
4440 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4441 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4442 d.setFilterBitmap(true);
4443 resizeIconDrawable(d);
4444 return d;
4445 }
4446
4447 /**
4448 * Resizes an icon drawable to the correct icon size.
4449 */
Winson5fbe0742015-09-30 15:33:00 -07004450 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004451 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004452 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004453 }
4454
4455 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004456 * Prints out out state for debugging.
4457 */
4458 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004459 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004460 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004461 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4462 Log.d(TAG, "mRestoring=" + mRestoring);
4463 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004464 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004465 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004466
Daniel Sandler325dc232013-06-05 22:57:57 -04004467 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004468 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004469
4470 @Override
4471 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4472 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004473 // Dump workspace
4474 writer.println(prefix + "Workspace Items");
4475 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4476 writer.println(prefix + " Homescreen " + i);
4477
4478 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4479 for (int j = 0; j < layout.getChildCount(); j++) {
4480 Object tag = layout.getChildAt(j).getTag();
4481 if (tag != null) {
4482 writer.println(prefix + " " + tag.toString());
4483 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004484 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004485 }
Sunny Goyala1365452015-10-01 15:46:24 -07004486
4487 writer.println(prefix + " Hotseat");
4488 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4489 for (int j = 0; j < layout.getChildCount(); j++) {
4490 Object tag = layout.getChildAt(j).getTag();
4491 if (tag != null) {
4492 writer.println(prefix + " " + tag.toString());
4493 }
4494 }
4495
Sunny Goyal713edfc2016-05-06 09:58:34 -07004496 try {
4497 FileLog.flushAll(writer);
4498 } catch (Exception e) {
4499 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004500 }
4501
Adam Cohen9211d422014-10-07 18:14:53 -07004502 if (mLauncherCallbacks != null) {
4503 mLauncherCallbacks.dump(prefix, fd, writer, args);
4504 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004505 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004506
Adam Cohen59400422014-03-05 18:07:04 -08004507 public static CustomAppWidget getCustomAppWidget(String name) {
4508 return sCustomAppWidgets.get(name);
4509 }
4510
4511 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4512 return sCustomAppWidgets;
4513 }
4514
Sunny Goyal29538332016-02-18 09:10:19 -08004515 public static List<View> getFolderContents(View icon) {
4516 if (icon instanceof FolderIcon) {
4517 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4518 } else {
4519 return Collections.EMPTY_LIST;
4520 }
4521 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004522
4523 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4524
4525 @Override
4526 public void onSharedPreferenceChanged(
4527 SharedPreferences sharedPreferences, String key) {
4528 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4529 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4530 if (!waitUntilResume(this, true)) {
4531 run();
4532 }
4533 }
4534 }
4535
4536 @Override
4537 public void run() {
4538 setOrientation();
4539 }
4540 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004541}