blob: 13690b43a8f2e7364b15208e64aea9cc7a24cf91 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyal28c6b962015-10-12 11:42:05 -070019import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070025import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000026import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080037import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Sunny Goyal756cd262015-08-20 12:33:21 -070039import android.content.ContentValues;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040041import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070044import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070046import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070050import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070053import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070056import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020059import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020061import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080062import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070063import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070064import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070066import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.HapticFeedbackConstants;
74import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.MotionEvent;
77import android.view.Surface;
78import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070079import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080080import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070081import android.view.ViewGroup;
82import android.view.ViewTreeObserver;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080084import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070085import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070086import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080090import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070092
Sunny Goyal651077b2014-06-30 14:15:31 -070093import com.android.launcher3.DropTarget.DragObject;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070094import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070095import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080096import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -070097import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010098import com.android.launcher3.compat.LauncherActivityInfoCompat;
99import com.android.launcher3.compat.LauncherAppsCompat;
100import com.android.launcher3.compat.UserHandleCompat;
101import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800102import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700103import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700104import com.android.launcher3.dragndrop.DragController;
105import com.android.launcher3.dragndrop.DragLayer;
106import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -0700107import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000108import com.android.launcher3.folder.Folder;
109import com.android.launcher3.folder.FolderIcon;
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800110import com.android.launcher3.logging.LoggerUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700111import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700112import com.android.launcher3.model.WidgetsModel;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700113import com.android.launcher3.pageindicators.PageIndicatorLine;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800114import com.android.launcher3.userevent.nano.LauncherLogProto;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700115import com.android.launcher3.util.ComponentKey;
Sunny Goyal713edfc2016-05-06 09:58:34 -0700116import com.android.launcher3.logging.FileLog;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700117import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal322d5562015-06-25 19:35:49 -0700118import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700119import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700120import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700121import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700122import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700123import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700124
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700125import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700126import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700127import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700128import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800129import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700130import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700131import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700132import java.util.List;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800133import java.util.Locale;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700134
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135/**
136 * Default launcher application.
137 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100138public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700139 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
140 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700141 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700142 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700143 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Daniel Sandlerf061f822013-06-27 13:59:36 -0400145 static final boolean PROFILE_STARTUP = false;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700146 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700147 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400148 static final boolean DEBUG_RESUME_TIME = false;
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700149 static final boolean DEBUG_LOGGING = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700150
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700152 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700153 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700154 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155
Michael Jurka8b805b12012-04-18 14:23:14 -0700156 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700157 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700158 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700159
Sunny Goyal28c6b962015-10-12 11:42:05 -0700160 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
161
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700162 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
163 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
164 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
165
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700166 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
167
Mathew Inwood876a8462013-06-14 14:12:41 +0100168 /**
169 * IntentStarter uses request codes starting with this. This must be greater than all activity
170 * request codes used internally.
171 */
172 protected static final int REQUEST_LAST = 100;
173
Michael Jurka0a457bf2012-11-19 14:05:05 -0800174 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700175 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800176 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177
Winson Chung2672ff92012-05-04 16:22:30 -0700178 // The Intent extra that defines whether to ignore the launch animation
179 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400180 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700181
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700182 public static final String ACTION_APPWIDGET_HOST_RESET =
183 "com.android.launcher3.intent.ACTION_APPWIDGET_HOST_RESET";
184
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 // Type: int
186 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700187 // Type: int
188 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700189 // Type: Content Values / parcelable
190 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700191 // Type: parcelable
192 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000193 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800194 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195
Adam Cohen432609a2014-03-13 17:03:22 -0700196 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800197 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
198
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700199 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700200 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
201 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700202
Adam Cohen091440a2015-03-18 14:16:05 -0700203 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700204 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700205
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700206 private boolean mIsSafeModeEnabled;
207
Joe Onorato9c1289c2009-08-17 11:03:03 -0400208 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800209 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700210 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700211 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212
Winson Chunga2413752012-04-03 14:22:34 -0700213 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700214 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
215 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700216 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700217
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700218 private final BroadcastReceiver mUiBroadcastReceiver = new BroadcastReceiver() {
219
220 @Override
221 public void onReceive(Context context, Intent intent) {
222 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
223 closeSystemDialogs();
224 } else if (ACTION_APPWIDGET_HOST_RESET.equals(intent.getAction())) {
225 if (mAppWidgetHost != null) {
226 mAppWidgetHost.startListening();
227 }
228 }
229 }
230 };
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800231
Adam Cohen091440a2015-03-18 14:16:05 -0700232 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700233 private View mLauncherView;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700234 private PageIndicatorLine mPageIndicator;
Adam Cohen091440a2015-03-18 14:16:05 -0700235 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800236 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700237
238 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700239
Sunny Goyalffe83f12014-08-14 17:39:34 -0700240 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700241 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700242
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700243 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800244 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800245 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700246
Michael Jurka0280c3b2010-09-17 15:00:07 -0700247 private int[] mTmpAddItemCellCoordinates = new int[2];
248
Sunny Goyal316490e2015-06-02 09:38:28 -0700249 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800250 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700251
Michael Jurka838a4ca2011-02-07 13:33:06 -0800252 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700253 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700254
Winson Chungc51db6a2011-10-05 11:44:49 -0700255 private SearchDropTargetBar mSearchDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -0700256 private AppInfoDropTargetBar mAppInfoDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700257
258 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700259 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700260 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700261
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700262 // Main container view and the model for the widget tray screen.
263 @Thunk WidgetsContainerView mWidgetsView;
264 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700265
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700267 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
268 // scroll issues (because the workspace may not have been measured yet) and extra work.
269 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
270 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271
272 private SpannableStringBuilder mDefaultKeySsb = null;
273
Adam Cohen091440a2015-03-18 14:16:05 -0700274 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400275
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800276 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277 private boolean mRestoring;
278 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700279 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280
Michael Jurka1e2f4652013-07-08 18:03:46 -0700281 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700282 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700283 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700284
Joe Onorato9c1289c2009-08-17 11:03:03 -0400285 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800286 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700287 private ExtractedColors mExtractedColors;
Adam Cohen091440a2015-03-18 14:16:05 -0700288 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800289 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700290 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800291 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400292
Sunny Goyal639e9062015-08-19 19:17:06 -0700293 private LauncherClings mClings;
294
Adam Cohen61f560d2013-09-30 15:58:20 -0700295 private View.OnTouchListener mHapticFeedbackTouchListener;
296
Adam Cohended9f8d2010-11-03 13:25:16 -0700297 // Related to the auto-advancing of widgets
298 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700299 private static final int ADVANCE_INTERVAL = 20000;
300 private static final int ADVANCE_STAGGER = 250;
301
302 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700303 private long mAutoAdvanceSentTime;
304 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700305 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700306
Winson Chung400438b2011-01-16 17:53:48 -0800307 // Determines how long to wait after a rotation before restoring the screen orientation to
308 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700309 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800310
Adam Cohen091440a2015-03-18 14:16:05 -0700311 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700312
Adam Cohen1462de32012-07-24 22:34:36 -0700313 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700314 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700315
Winson Chung46353de2012-02-16 14:05:10 -0800316 // We only want to get the SharedPreferences once since it does an FS stat each time we get
317 // it from the context.
318 private SharedPreferences mSharedPrefs;
319
Winson Chungf0c6ae02012-03-21 16:10:31 -0700320 // Holds the page that we need to animate to, and the icon views that we need to animate up
321 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700322 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700323 private Bitmap mFolderIconBitmap;
324 private Canvas mFolderIconCanvas;
325 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700326
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700327 private DeviceProfile mDeviceProfile;
328
Adam Cohen4b66bf32015-09-18 12:15:19 -0700329 private boolean mMoveToDefaultScreenFromNewIntent;
330
Winson Chung13eb5272015-05-11 16:30:13 -0700331 // This is set to the view that launched the activity that navigated the user away from
332 // launcher. Since there is no callback for when the activity has finished launching, enable
333 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800334 private BubbleTextView mWaitingForResume;
335
Adam Cohen59400422014-03-05 18:07:04 -0800336 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
337 new HashMap<String, CustomAppWidget>();
338
Adam Cohen59400422014-03-05 18:07:04 -0800339 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700340 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
341 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800342 }
343 }
344
Adam Cohen091440a2015-03-18 14:16:05 -0700345 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700346 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700347 if (mWorkspace != null) {
348 mWorkspace.buildPageHardwareLayers();
349 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700350 }
351 };
352
Adam Cohendb364c32014-05-20 14:23:40 -0700353 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800354
Adam Cohen091440a2015-03-18 14:16:05 -0700355 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800356 int requestCode;
357 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700358 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700359 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800360 int cellX;
361 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700362 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800363 }
364
Hyunyoung Songaa953652016-04-19 18:30:24 -0700365 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800366
Adam Cohenf9c184a2016-01-15 16:47:43 -0800367 public FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700368 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400369
370 @Thunk void setOrientation() {
371 if (mRotationEnabled) {
372 unlockScreenOrientation(true);
373 } else {
374 setRequestedOrientation(
375 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700376 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400377 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700378
Sunny Goyal745bad92016-05-02 10:54:12 -0700379 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700380
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 @Override
382 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700383 if (DEBUG_STRICT_MODE) {
384 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
385 .detectDiskReads()
386 .detectDiskWrites()
387 .detectNetwork() // or .detectAll() for all detectable problems
388 .penaltyLog()
389 .build());
390 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
391 .detectLeakedSqlLiteObjects()
392 .detectLeakedClosableObjects()
393 .penaltyLog()
394 .penaltyDeath()
395 .build());
396 }
397
Adam Cohen9211d422014-10-07 18:14:53 -0700398 if (mLauncherCallbacks != null) {
399 mLauncherCallbacks.preOnCreate();
400 }
401
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400403
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400404 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700405
Adam Cohen2e6da152015-05-06 11:42:25 -0700406 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700407 mDeviceProfile = getResources().getConfiguration().orientation
408 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700409 app.getInvariantDeviceProfile().landscapeProfile
410 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700411
Sunny Goyalf7258242015-10-19 16:59:07 -0700412 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700413 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800414 mModel = app.setLauncher(this);
415 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700416
Joe Onorato41a12d22009-10-31 18:30:00 -0400417 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700418 mAllAppsController = new AllAppsTransitionController(this);
419 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700420
Sunny Goyalffe83f12014-08-14 17:39:34 -0700421 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700422
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700423 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
424 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700425
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700426 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
427 // this also ensures that any synchronous binding below doesn't re-trigger another
428 // LauncherModel load.
429 mPaused = false;
430
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200432 android.os.Debug.startMethodTracing(
433 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800434 }
435
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700437
Tony Wickhameef44322015-10-20 13:24:36 -0700438 app.getInvariantDeviceProfile().landscapeProfile.setSearchBarHeight(getSearchBarHeight());
439 app.getInvariantDeviceProfile().portraitProfile.setSearchBarHeight(getSearchBarHeight());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700441 mDeviceProfile.layout(this);
Tony Wickham827cef22016-03-17 15:39:39 -0700442 mExtractedColors = new ExtractedColors();
443 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444
Tony Wickhame2217252016-03-22 16:34:23 -0700445 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
446 .addAccessibilityStateChangeListener(this);
447
Joe Onorato7c312c12009-08-13 21:36:53 -0700448 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700449
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 mSavedState = savedInstanceState;
451 restoreState(mSavedState);
452
453 if (PROFILE_STARTUP) {
454 android.os.Debug.stopMethodTracing();
455 }
456
457 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700458 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700459 // If the user leaves launcher, then we should just load items asynchronously when
460 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700461 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700462 } else {
463 // We only load the page synchronously if the user rotates (or triggers a
464 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700465 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700466 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467 }
468
469 // For handling default keys
470 mDefaultKeySsb = new SpannableStringBuilder();
471 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800472
473 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700474 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
475 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800476
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800477 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700478 // In case we are on a device with locked rotation, we should look at preferences to check
479 // if the user has specifically allowed rotation.
480 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700481 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700482 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
483 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700484 }
485
486 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
487 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400488 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700489
Adam Cohen9211d422014-10-07 18:14:53 -0700490 if (mLauncherCallbacks != null) {
491 mLauncherCallbacks.onCreate(savedInstanceState);
492 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700493
494 if (shouldShowIntroScreen()) {
495 showIntroScreen();
496 } else {
497 showFirstRunActivity();
498 showFirstRunClings();
499 }
Adam Cohen9211d422014-10-07 18:14:53 -0700500 }
501
Sunny Goyal7779d622015-06-11 16:18:39 -0700502 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700503 public void onExtractedColorsChanged() {
504 loadExtractedColorsAndColorItems();
505 }
506
507 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700508 // TODO: do this in pre-N as well, once the extraction part is complete.
509 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700510 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700511 mHotseat.updateColor(mExtractedColors, !mPaused);
Tony Wickham770217c2016-05-18 15:16:40 -0700512 if (mPageIndicator != null) {
513 mPageIndicator.updateColor(mExtractedColors);
514 }
Tony Wickham827cef22016-03-17 15:39:39 -0700515 }
516 }
517
Adam Cohen9211d422014-10-07 18:14:53 -0700518 private LauncherCallbacks mLauncherCallbacks;
519
520 public void onPostCreate(Bundle savedInstanceState) {
521 super.onPostCreate(savedInstanceState);
522 if (mLauncherCallbacks != null) {
523 mLauncherCallbacks.onPostCreate(savedInstanceState);
524 }
525 }
526
Sunny Goyal32554d12015-12-03 15:31:25 -0800527 /**
528 * Call this after onCreate to set or clear overlay.
529 */
530 public void setLauncherOverlay(LauncherOverlay overlay) {
531 if (overlay != null) {
532 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
533 }
534 mWorkspace.setLauncherOverlay(overlay);
535 }
536
Adam Cohen9211d422014-10-07 18:14:53 -0700537 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
538 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700539 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700540 private boolean mWorkspaceImportanceStored = false;
541 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700542 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800543 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700544 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
545
546 @Override
547 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700548 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700549 return;
550 }
551 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700552 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700553 if (mWorkspace != null) {
554 mWorkspaceImportanceForAccessibility =
555 mWorkspace.getImportantForAccessibility();
556 mWorkspace.setImportantForAccessibility(
557 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
558 mWorkspaceImportanceStored = true;
559 }
560 if (mHotseat != null) {
561 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
562 mHotseat.setImportantForAccessibility(
563 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
564 mHotseatImportanceStored = true;
565 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700566 }
567
568 @Override
569 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700570 if (mWorkspaceImportanceStored && mWorkspace != null) {
571 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
572 }
573 if (mHotseatImportanceStored && mHotseat != null) {
574 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
575 }
576 mWorkspaceImportanceStored = false;
577 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700578 }
579 });
Adam Cohen9211d422014-10-07 18:14:53 -0700580 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700581 }
582
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700583 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700584 public void onLauncherProviderChange() {
585 if (mLauncherCallbacks != null) {
586 mLauncherCallbacks.onLauncherProviderChange();
587 }
588 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700589
Adam Cohen9211d422014-10-07 18:14:53 -0700590 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700591 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700592 if (mLauncherCallbacks != null) {
593 return mLauncherCallbacks.hasCustomContentToLeft();
594 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700595 return false;
596 }
597
Dave Hawkeya8881582013-09-17 15:55:33 -0600598 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500599 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600600 * {@link #addToCustomContentPage}. This will only be invoked if
601 * {@link #hasCustomContentToLeft()} is {@code true}.
602 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500603 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700604 if (mLauncherCallbacks != null) {
605 mLauncherCallbacks.populateCustomContentContainer();
606 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600607 }
608
609 /**
610 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
611 * ensure the custom content page is added or removed if necessary.
612 */
613 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600614 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600615 // Not bound yet, wait for bindScreens to be called.
616 return;
617 }
618
619 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
620 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500621 mWorkspace.createCustomContentContainer();
622 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600623 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
624 mWorkspace.removeCustomContentPage();
625 }
626 }
627
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800628 /**
629 * Logger object is a singleton and does not have to be coupled with the foreground activity.
630 * Since most user event logging is done on the UI, the object is retrieved from the
631 * callback for convenience.
632 */
Hyunyoung Songaa953652016-04-19 18:30:24 -0700633 private UserEventDispatcher createUserEventDispatcher() {
634 return new UserEventDispatcher() {
635 @Override
636 public void dispatchUserEvent(LauncherLogProto.LauncherEvent ev, Intent intent) {
637 if (!DEBUG_LOGGING) {
638 return;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800639 }
Hyunyoung Songaa953652016-04-19 18:30:24 -0700640 Log.d("UserEvent", String.format(Locale.US,
641 "action:%s\nchild:%s\nparent:%s\nelapsed container %d ms session %d ms",
642 LoggerUtils.getActionStr(ev.action),
643 LoggerUtils.getTargetStr(ev.srcTarget[0]),
644 LoggerUtils.getTargetStr(ev.srcTarget[1]),
645 ev.elapsedContainerMillis,
646 ev.elapsedSessionMillis));
647 }
648 };
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800649 }
650
Hyunyoung Songaa953652016-04-19 18:30:24 -0700651 public UserEventDispatcher getUserEventDispatcher() {
652 if (mLauncherCallbacks != null) {
653 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
654 if (dispatcher != null) {
655 return dispatcher;
656 }
657 }
658
659 if (mUserEventDispatcher == null) {
660 mUserEventDispatcher = createUserEventDispatcher();
661 }
662 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800663 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100664
Hyunyoung Song3f471442015-04-08 19:01:34 -0700665 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700666 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
667 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700668 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700669 }
670
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000671 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700672 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
673 // This cast is safe as long as the id < 0x00FFFFFF
674 // Since we jail all the dynamically generated views, there should be no clashes
675 // with any other views.
676 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000677 }
678
679 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700680 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
681 * a configuration step, this allows the proper animations to run after other transitions.
682 */
Adam Cohendb364c32014-05-20 14:23:40 -0700683 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700684 long screenId = args.screenId;
685 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
686 // When the screen id represents an actual screen (as opposed to a rank) we make sure
687 // that the drop page actually exists.
688 screenId = ensurePendingDropLayoutExists(args.screenId);
689 }
Adam Cohendb364c32014-05-20 14:23:40 -0700690
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800691 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800692 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700693 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700694 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700695 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800696 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700697 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700698 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700699 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyald478c832016-04-01 12:04:16 -0700700 completeRestoreAppWidget(args.appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700701 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700702 case REQUEST_BIND_PENDING_APPWIDGET: {
703 int widgetId = args.appWidgetId;
704 LauncherAppWidgetInfo info =
705 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
706 if (info != null) {
707 // Since the view was just bound, also launch the configure activity if needed
708 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
709 .getLauncherAppWidgetInfo(widgetId);
710 if (provider != null && provider.configure != null) {
711 startRestoredWidgetReconfigActivity(provider, info);
712 }
713 }
714 break;
715 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800716 }
Michael Jurka27614d22012-04-02 06:40:22 -0700717 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
718 // if you turned the screen off and then back while in All Apps, Launcher would not
719 // return to the workspace. Clearing mAddInfo.container here fixes this issue
720 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700721 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800722 }
723
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800724 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700725 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700726 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700727 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700728 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800729 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700730
Adam Cohenad4e15c2013-10-17 16:21:35 -0700731 Runnable exitSpringLoaded = new Runnable() {
732 @Override
733 public void run() {
734 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
735 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
736 }
737 };
738
Michael Jurka8b805b12012-04-18 14:23:14 -0700739 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700740 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700741 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700742 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
743 if (resultCode == RESULT_CANCELED) {
744 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700745 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700746 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700747 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800748 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700749 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700750 }
751 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200752 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
753 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700754 // User could have free-scrolled between pages before picking a wallpaper; make sure
755 // we move to the closest one now.
756 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700757 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200758 }
759 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700760 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200761
Adam Cohened66b2b2012-01-23 17:28:51 -0800762 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700763 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700764
Adam Cohendb364c32014-05-20 14:23:40 -0700765 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800766 // We have special handling for widgets
767 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800768 final int appWidgetId;
769 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
770 : -1;
771 if (widgetId < 0) {
772 appWidgetId = pendingAddWidgetId;
773 } else {
774 appWidgetId = widgetId;
775 }
776
Adam Cohenad4e15c2013-10-17 16:21:35 -0700777 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800778 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700779 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
780 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700781 result = RESULT_CANCELED;
782 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700783 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700784 @Override
785 public void run() {
786 exitSpringLoadedDragModeDelayed(false, 0, null);
787 }
788 };
Adam Cohendb364c32014-05-20 14:23:40 -0700789 if (workspaceLocked) {
790 // No need to remove the empty screen if we're mid-binding, as the
791 // the bind will not add the empty screen.
792 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
793 } else {
794 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
795 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
796 }
Winson Chung5aaab772012-04-27 15:24:02 -0700797 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700798 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700799 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
800 // When the screen id represents an actual screen (as opposed to a rank)
801 // we make sure that the drop page actually exists.
802 mPendingAddInfo.screenId =
803 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
804 }
Adam Cohendb364c32014-05-20 14:23:40 -0700805 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
806
807 dropLayout.setDropPending(true);
808 final Runnable onComplete = new Runnable() {
809 @Override
810 public void run() {
811 completeTwoStageWidgetDrop(resultCode, appWidgetId);
812 dropLayout.setDropPending(false);
813 }
814 };
815 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
816 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
817 } else {
818 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
819 mPendingAddInfo);
820 sPendingAddItem = args;
821 }
Winson Chung5aaab772012-04-27 15:24:02 -0700822 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800823 return;
824 }
825
Sunny Goyald478c832016-04-01 12:04:16 -0700826 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
827 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700828 if (resultCode == RESULT_OK) {
829 // Update the widget view.
830 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
831 pendingAddWidgetId, mPendingAddInfo);
832 if (workspaceLocked) {
833 sPendingAddItem = args;
834 } else {
835 completeAdd(args);
836 }
837 }
838 // Leave the widget in the pending state if the user canceled the configure.
839 return;
840 }
841
Sunny Goyalaad90582015-07-09 14:22:50 -0700842 if (requestCode == REQUEST_CREATE_SHORTCUT) {
843 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
844 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
845 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
846 mPendingAddInfo);
847 if (isWorkspaceLocked()) {
848 sPendingAddItem = args;
849 } else {
850 completeAdd(args);
851 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
852 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
853 }
854 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700855 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
856 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800859 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800860
861 }
862
863 @Override
864 protected void onActivityResult(
865 final int requestCode, final int resultCode, final Intent data) {
866 handleActivityResult(requestCode, resultCode, data);
867 if (mLauncherCallbacks != null) {
868 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
869 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800870 }
871
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600872 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700873 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600874 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700875 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
876 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
877 View v = null;
878 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
879 if (layout != null) {
880 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
881 }
882 Intent intent = sPendingAddItem.intent;
883 sPendingAddItem = null;
884 if (grantResults.length > 0
885 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700886 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700887 } else {
888 // TODO: Show a snack bar with link to settings
889 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
890 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
891 }
892 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600893 if (mLauncherCallbacks != null) {
894 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
895 grantResults);
896 }
897 }
898
Adam Cohendb364c32014-05-20 14:23:40 -0700899 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
900 appWidgetId, ItemInfo info) {
901 PendingAddArguments args = new PendingAddArguments();
902 args.requestCode = requestCode;
903 args.intent = data;
904 args.container = info.container;
905 args.screenId = info.screenId;
906 args.cellX = info.cellX;
907 args.cellY = info.cellY;
908 args.appWidgetId = appWidgetId;
909 return args;
910 }
911
912 /**
913 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
914 *
915 * @param screenId the screen id to check
916 * @return the new screen, or screenId if it exists
917 */
918 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800919 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700920 if (dropLayout == null) {
921 // it's possible that the add screen was removed because it was
922 // empty and a re-bind occurred
923 mWorkspace.addExtraEmptyScreen();
924 return mWorkspace.commitExtraEmptyScreen();
925 } else {
926 return screenId;
927 }
928 }
929
Adam Cohen091440a2015-03-18 14:16:05 -0700930 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800931 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800932 Runnable onCompleteRunnable = null;
933 int animationType = 0;
934
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700935 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800936 if (resultCode == RESULT_OK) {
937 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
938 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700939 mPendingAddWidgetInfo);
940 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800941 onCompleteRunnable = new Runnable() {
942 @Override
943 public void run() {
944 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700945 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700946 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
947 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800948 }
949 };
950 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800951 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800952 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800953 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700954 if (mDragLayer.getAnimatedView() != null) {
955 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
956 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
957 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700958 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700959 // The animated view may be null in the case of a rotation during widget configuration
960 onCompleteRunnable.run();
961 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800962 }
963
964 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700965 protected void onStop() {
966 super.onStop();
967 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700968
969 if (mLauncherCallbacks != null) {
970 mLauncherCallbacks.onStop();
971 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700972 }
973
974 @Override
975 protected void onStart() {
976 super.onStart();
977 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700978
979 if (mLauncherCallbacks != null) {
980 mLauncherCallbacks.onStart();
981 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700982 }
983
984 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200986 long startTime = 0;
987 if (DEBUG_RESUME_TIME) {
988 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400989 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200990 }
Adam Cohen9211d422014-10-07 18:14:53 -0700991
992 if (mLauncherCallbacks != null) {
993 mLauncherCallbacks.preOnResume();
994 }
995
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700997 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700998
Winson Chung4a2afa32012-07-19 14:53:05 -0700999 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001000 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001001 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001002 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -07001003 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -07001004 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -07001005 // view after launching an app, as they may be depending on the UI to be static to
1006 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -07001007 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07001008 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08001009 } else if (mOnResumeState == State.WIDGETS) {
1010 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001011 }
1012 mOnResumeState = State.NONE;
1013
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001014 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001015 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1016 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001017
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001018 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001019 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001020 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001021 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001022 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001023 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001025 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001026 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1027 // execute them here
1028 long startTimeCallbacks = 0;
1029 if (DEBUG_RESUME_TIME) {
1030 startTimeCallbacks = System.currentTimeMillis();
1031 }
1032
Michael Jurka1e2f4652013-07-08 18:03:46 -07001033 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1034 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001035 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001036 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001037 if (DEBUG_RESUME_TIME) {
1038 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1039 (System.currentTimeMillis() - startTimeCallbacks));
1040 }
Michael Jurka447bf842013-05-15 14:52:15 +02001041 }
Michael Jurka54554252013-08-01 12:52:23 +02001042 if (mOnResumeCallbacks.size() > 0) {
1043 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1044 mOnResumeCallbacks.get(i).run();
1045 }
1046 mOnResumeCallbacks.clear();
1047 }
Winson Chunge4e50662012-01-23 14:45:13 -08001048
1049 // Reset the pressed state of icons that were locked in the press state while activities
1050 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001051 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001052 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001053 mWaitingForResume.setStayPressed(false);
1054 }
Winson Chung82963d52013-10-09 11:20:57 -07001055
Adam Cohen06dff352012-06-01 17:17:08 -07001056 // It is possible that widgets can receive updates while launcher is not in the foreground.
1057 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1058 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1059 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001060 if (!isWorkspaceLoading()) {
1061 getWorkspace().reinflateWidgetsIfNecessary();
1062 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001063
Michael Jurka447bf842013-05-15 14:52:15 +02001064 if (DEBUG_RESUME_TIME) {
1065 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1066 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001067
Adam Cohen4b66bf32015-09-18 12:15:19 -07001068 // We want to suppress callbacks about CustomContent being shown if we have just received
1069 // onNewIntent while the user was present within launcher. In that case, we post a call
1070 // to move the user to the main screen (which will occur after onResume). We don't want to
1071 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1072 // suppress here.
1073 if (mWorkspace.getCustomContentCallbacks() != null
1074 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001075 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001076 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001077 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1078 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001079 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001080 }
1081 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001082 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001083 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001084 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001085
Sunny Goyal756adbc2015-04-16 15:20:51 -07001086 if (!isWorkspaceLoading()) {
1087 // Process any items that were added while Launcher was away.
1088 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1089 }
Adam Cohen9211d422014-10-07 18:14:53 -07001090
1091 if (mLauncherCallbacks != null) {
1092 mLauncherCallbacks.onResume();
1093 }
Adam Cohen06dff352012-06-01 17:17:08 -07001094 }
1095
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001097 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001098 // Ensure that items added to Launcher are queued until Launcher returns
1099 InstallShortcutReceiver.enableInstallQueue();
1100
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001101 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001102 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001103 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001104 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001105
1106 // We call onHide() aggressively. The custom content callbacks should be able to
1107 // debounce excess onHide calls.
1108 if (mWorkspace.getCustomContentCallbacks() != null) {
1109 mWorkspace.getCustomContentCallbacks().onHide();
1110 }
Romain Guycbb89e42009-06-08 15:52:54 -07001111
Adam Cohen9211d422014-10-07 18:14:53 -07001112 if (mLauncherCallbacks != null) {
1113 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001114 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001115 }
1116
1117 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001118 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1119 // by a onResume or by scrolling otherwise.
1120 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001121
1122 // Custom content is completely hidden
1123 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001124
1125 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1126 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001127
1128 // Indicates whether the user is allowed to scroll away from the custom content.
1129 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001130 }
1131
Adam Cohenc2d6e892014-10-16 09:49:24 -07001132 public interface LauncherOverlay {
1133
1134 /**
1135 * Touch interaction leading to overscroll has begun
1136 */
1137 public void onScrollInteractionBegin();
1138
1139 /**
1140 * Touch interaction related to overscroll has ended
1141 */
1142 public void onScrollInteractionEnd();
1143
1144 /**
1145 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1146 * screen (or in the case of RTL, the rightmost screen).
1147 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001148 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001149
1150 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001151 * Called when the launcher is ready to use the overlay
1152 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001153 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001154 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001155 }
1156
Jun Mukai8af0cd82015-05-12 12:32:12 -07001157 public interface LauncherSearchCallbacks {
1158 /**
1159 * Called when the search overlay is shown.
1160 */
1161 public void onSearchOverlayOpened();
1162
1163 /**
1164 * Called when the search overlay is dismissed.
1165 */
1166 public void onSearchOverlayClosed();
1167 }
1168
Adam Cohenc2d6e892014-10-16 09:49:24 -07001169 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001170 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001171 }
1172
1173 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1174
Sunny Goyalc86df472016-02-25 09:19:38 -08001175 public void onScrollChanged(float progress) {
1176 if (mWorkspace != null) {
1177 mWorkspace.onOverlayScrollChanged(progress);
1178 }
1179 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001180 }
1181
Jorim Jaggid017f882014-01-14 17:08:48 -08001182 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001183 if (mLauncherCallbacks != null) {
1184 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001185 } else {
1186 // On devices with a locked orientation, we will at least have the allow rotation
1187 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001188 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001189 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001190 }
1191
Adam Cohen9211d422014-10-07 18:14:53 -07001192 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001193 CustomContentCallbacks callbacks, String description) {
1194 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001195 }
1196
Adam Cohenedb40762013-07-18 16:45:45 -07001197 // The custom content needs to offset its content to account for the QSB
1198 public int getTopOffsetForCustomContent() {
1199 return mWorkspace.getPaddingTop();
1200 }
1201
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001202 @Override
1203 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001204 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001205 if (mModel.isCurrentCallbacks(this)) {
1206 mModel.stopLoader();
1207 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001208 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1209
Romain Guy13c2e7b2010-03-10 19:45:00 -08001210 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001211 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001212
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001213 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001214 @Override
1215 public void onWindowFocusChanged(boolean hasFocus) {
1216 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001217 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001218
1219 if (mLauncherCallbacks != null) {
1220 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1221 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001222 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001223
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 private boolean acceptFilter() {
1225 final InputMethodManager inputManager = (InputMethodManager)
1226 getSystemService(Context.INPUT_METHOD_SERVICE);
1227 return !inputManager.isFullscreenMode();
1228 }
1229
1230 @Override
1231 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001232 final int uniChar = event.getUnicodeChar();
1233 final boolean handled = super.onKeyDown(keyCode, event);
1234 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1235 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1237 keyCode, event);
1238 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001239 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001240 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001241 // showSearchDialog()
1242 // If there are multiple keystrokes before the search dialog takes focus,
1243 // onSearchRequested() will be called for every keystroke,
1244 // but it is idempotent, so it's fine.
1245 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246 }
1247 }
1248
Joe Onorato8a9625e2010-01-28 15:55:35 -08001249 // Eat the long press event so the keyboard doesn't come up.
1250 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1251 return true;
1252 }
1253
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 return handled;
1255 }
1256
Winson46163472015-09-22 17:31:56 -07001257 @Override
1258 public boolean onKeyUp(int keyCode, KeyEvent event) {
1259 if (keyCode == KeyEvent.KEYCODE_MENU) {
1260 // Ignore the menu key if we are currently dragging or are on the custom content screen
1261 if (!isOnCustomContent() && !mDragController.isDragging()) {
1262 // Close any open folders
1263 closeFolder();
1264
1265 // Stop resizing any widgets
1266 mWorkspace.exitWidgetResizeMode();
1267
1268 // Show the overview mode if we are on the workspace
1269 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1270 !mWorkspace.isSwitchingState()) {
1271 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001272 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001273 }
1274 }
1275 return true;
1276 }
1277 return super.onKeyUp(keyCode, event);
1278 }
1279
Karl Rosaen138a0412009-04-23 19:00:21 -07001280 private String getTypedText() {
1281 return mDefaultKeySsb.toString();
1282 }
1283
1284 private void clearTypedText() {
1285 mDefaultKeySsb.clear();
1286 mDefaultKeySsb.clearSpans();
1287 Selection.setSelection(mDefaultKeySsb, 0);
1288 }
1289
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001291 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1292 * State
1293 */
1294 private static State intToState(int stateOrdinal) {
1295 State state = State.WORKSPACE;
1296 final State[] stateValues = State.values();
1297 for (int i = 0; i < stateValues.length; i++) {
1298 if (stateValues[i].ordinal() == stateOrdinal) {
1299 state = stateValues[i];
1300 break;
1301 }
1302 }
1303 return state;
1304 }
1305
1306 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 * Restores the previous state, if it exists.
1308 *
1309 * @param savedState The previous state.
1310 */
1311 private void restoreState(Bundle savedState) {
1312 if (savedState == null) {
1313 return;
1314 }
1315
Michael Jurka883f55b2010-10-21 15:47:14 -07001316 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001317 if (state == State.APPS || state == State.WIDGETS) {
1318 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001319 }
1320
Adam Cohen21cd0022013-10-09 18:57:02 -07001321 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1322 PagedView.INVALID_RESTORE_PAGE);
1323 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001324 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 }
1326
Sunny Goyal756cd262015-08-20 12:33:21 -07001327 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1328 if (itemValues != null) {
1329 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001330 AppWidgetProviderInfo info = savedState.getParcelable(
1331 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001332 mPendingAddWidgetInfo = info == null ?
1333 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1334
Adam Cohen4637b5a2013-11-04 18:21:24 -08001335 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001336 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 mRestoring = true;
1338 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339 }
1340
1341 /**
1342 * Finds all the views we need and configure them properly.
1343 */
1344 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001345 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001346 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001347 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1348 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Tony Wickhamf549dab2016-05-16 09:54:06 -07001349 mPageIndicator = (PageIndicatorLine) 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
Adam Cohen24ce0b32014-01-14 16:18:14 -08001377 mSearchDropTargetBar = (SearchDropTargetBar)
1378 mDragLayer.findViewById(R.id.search_drop_target_bar);
Tony Wickham34d2c912015-09-11 09:27:58 -07001379 // Get the app info bar
1380 mAppInfoDropTargetBar = (AppInfoDropTargetBar)
1381 mDragLayer.findViewById(R.id.app_info_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001382
Winson Chungef7f8742015-06-04 17:18:17 -07001383 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001384 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001385 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001386 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1387 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1388 } else {
1389 mAppsView.setSearchBarController(new DefaultAppSearchController());
1390 }
Craig Mautner360310b2012-10-26 15:13:08 -07001391
Winson Chung3d503fb2011-07-13 17:25:49 -07001392 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001393 mDragController.setDragScoller(mWorkspace);
1394 mDragController.setScrollView(mDragLayer);
1395 mDragController.setMoveTarget(mWorkspace);
1396 mDragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001397 if (mSearchDropTargetBar != null) {
Hyunyoung Song645764e2016-06-06 14:19:02 -07001398 mSearchDropTargetBar.setup(this, mDragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001399 }
Tony Wickham34d2c912015-09-11 09:27:58 -07001400 if (mAppInfoDropTargetBar != null) {
Hyunyoung Song645764e2016-06-06 14:19:02 -07001401 mAppInfoDropTargetBar.setup(this, mDragController);
Tony Wickham34d2c912015-09-11 09:27:58 -07001402 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001403
Sunny Goyal322d5562015-06-25 19:35:49 -07001404 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1405 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001406 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 }
1408
Winsone9f27272015-10-13 10:47:51 -07001409 private void setupOverviewPanel() {
1410 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1411
1412 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1413 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1414 @Override
1415 public boolean onLongClick(View v) {
1416 return v.performClick();
1417 }
1418 };
1419
1420 // Bind wallpaper button actions
1421 View wallpaperButton = findViewById(R.id.wallpaper_button);
1422 wallpaperButton.setOnClickListener(new OnClickListener() {
1423 @Override
1424 public void onClick(View view) {
1425 if (!mWorkspace.isSwitchingState()) {
1426 onClickWallpaperPicker(view);
1427 }
1428 }
1429 });
1430 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1431 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1432
1433 // Bind widget button actions
1434 mWidgetsButton = findViewById(R.id.widget_button);
1435 mWidgetsButton.setOnClickListener(new OnClickListener() {
1436 @Override
1437 public void onClick(View view) {
1438 if (!mWorkspace.isSwitchingState()) {
1439 onClickAddWidgetButton(view);
1440 }
1441 }
1442 });
1443 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1444 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1445
1446 // Bind settings actions
1447 View settingsButton = findViewById(R.id.settings_button);
1448 boolean hasSettings = hasSettings();
1449 if (hasSettings) {
1450 settingsButton.setOnClickListener(new OnClickListener() {
1451 @Override
1452 public void onClick(View view) {
1453 if (!mWorkspace.isSwitchingState()) {
1454 onClickSettingsButton(view);
1455 }
1456 }
1457 });
1458 settingsButton.setOnLongClickListener(performClickOnLongClick);
1459 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1460 } else {
1461 settingsButton.setVisibility(View.GONE);
1462 }
1463
1464 mOverviewPanel.setAlpha(0f);
1465 }
1466
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001468 * Sets the all apps button. This method is called from {@link Hotseat}.
1469 */
1470 public void setAllAppsButton(View allAppsButton) {
1471 mAllAppsButton = allAppsButton;
1472 }
1473
1474 public View getAllAppsButton() {
1475 return mAllAppsButton;
1476 }
1477
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001478 public View getWidgetsButton() {
1479 return mWidgetsButton;
1480 }
1481
Anjali Koppal5ad44842014-03-10 20:34:39 -07001482 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 * Creates a view representing a shortcut.
1484 *
1485 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001487 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001488 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 }
1490
1491 /**
1492 * Creates a view representing a shortcut inflated from the specified resource.
1493 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494 * @param parent The group the shortcut belongs to.
1495 * @param info The data structure describing the shortcut.
1496 *
1497 * @return A View inflated from layoutResId.
1498 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001499 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001500 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001501 parent, false);
1502 favorite.applyFromShortcutInfo(info, mIconCache);
1503 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001505 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 return favorite;
1507 }
1508
1509 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 * Add a shortcut to the workspace.
1511 *
1512 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001514 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001515 int cellY) {
1516 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001517 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001518
Sunny Goyal5c97f512015-05-19 16:03:28 -07001519 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001520 if (info == null) {
1521 return;
1522 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001523 final View view = createShortcut(info);
1524
Sunny Goyal5c97f512015-05-19 16:03:28 -07001525 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001526 // First we check if we already know the exact location where we want to add this item.
1527 if (cellX >= 0 && cellY >= 0) {
1528 cellXY[0] = cellX;
1529 cellXY[1] = cellY;
1530 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001531
1532 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001533 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001534 true, null,null)) {
1535 return;
1536 }
1537 DragObject dragObject = new DragObject();
1538 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001539 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1540 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001541 return;
1542 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001543 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001544 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001545 }
1546
1547 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001548 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001549 return;
1550 }
1551
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001552 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553
1554 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001555 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001556 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 }
1558 }
1559
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001561 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001563 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 */
Adam Cohen091440a2015-03-18 14:16:05 -07001565 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001566 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1567
1568 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001569 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001570 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001571 }
Romain Guycbb89e42009-06-08 15:52:54 -07001572
Adam Cohen59400422014-03-05 18:07:04 -08001573 if (appWidgetInfo.isCustomWidget) {
1574 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001575 }
1576
Adam Cohen59400422014-03-05 18:07:04 -08001577 LauncherAppWidgetInfo launcherInfo;
1578 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1579 launcherInfo.spanX = info.spanX;
1580 launcherInfo.spanY = info.spanY;
1581 launcherInfo.minSpanX = info.minSpanX;
1582 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001583 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001584
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001586 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587
1588 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001589 if (hostView == null) {
1590 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001591 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001592 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001593 hostView.setVisibility(View.VISIBLE);
1594 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001596 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597 }
Romain Guycbb89e42009-06-08 15:52:54 -07001598
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001599 private void addAppWidgetToWorkspace(
1600 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001601 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001602 hostView.setTag(item);
1603 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001604
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001605 hostView.setFocusable(true);
1606 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001607
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001608 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001609 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1610
1611 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001612 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001613 }
1614 }
1615
Adam Cohended9f8d2010-11-03 13:25:16 -07001616 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1617 @Override
1618 public void onReceive(Context context, Intent intent) {
1619 final String action = intent.getAction();
1620 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1621 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001622 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001623 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001624
Winson Chungc100e8e2011-08-09 16:02:43 -07001625 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001626 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001627 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001628 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001629 if (!showWorkspace(false)) {
1630 // If we are already on the workspace, then manually reset all apps
1631 mAppsView.reset();
1632 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001633 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001634 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1635 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001636 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001637 }
1638 }
1639 };
1640
1641 @Override
1642 public void onAttachedToWindow() {
1643 super.onAttachedToWindow();
1644
1645 // Listen for broadcasts related to user-presence
1646 final IntentFilter filter = new IntentFilter();
1647 filter.addAction(Intent.ACTION_SCREEN_OFF);
1648 filter.addAction(Intent.ACTION_USER_PRESENT);
1649 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001650 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001651 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001652 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001653
1654 if (mLauncherCallbacks != null) {
1655 mLauncherCallbacks.onAttachedToWindow();
1656 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001657 }
1658
1659 @Override
1660 public void onDetachedFromWindow() {
1661 super.onDetachedFromWindow();
1662 mVisible = false;
1663
Adam Cohend113e0c2010-11-11 10:48:05 -08001664 if (mAttached) {
1665 unregisterReceiver(mReceiver);
1666 mAttached = false;
1667 }
Winson Chungb745afb2015-03-02 11:51:23 -08001668 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001669
1670 if (mLauncherCallbacks != null) {
1671 mLauncherCallbacks.onDetachedFromWindow();
1672 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001673 }
1674
1675 public void onWindowVisibilityChanged(int visibility) {
1676 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001677 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001678 // The following code used to be in onResume, but it turns out onResume is called when
1679 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1680 // is a more appropriate event to handle
1681 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001682 if (!mWorkspaceLoading) {
1683 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001684 // We want to let Launcher draw itself at least once before we force it to build
1685 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001686 // apps is nice and speedy.
1687 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001688 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001689 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001690 if (mStarted) return;
1691 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001692 // We delay the layer building a bit in order to give
1693 // other message processing a time to run. In particular
1694 // this avoids a delay in hiding the IME if it was
1695 // currently shown, because doing that may involve
1696 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001697 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001698 final ViewTreeObserver.OnDrawListener listener = this;
1699 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001700 public void run() {
1701 if (mWorkspace != null &&
1702 mWorkspace.getViewTreeObserver() != null) {
1703 mWorkspace.getViewTreeObserver().
1704 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001705 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001706 }
1707 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001708 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001709 }
1710 });
1711 }
1712 clearTypedText();
1713 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001714 }
1715
Adam Cohen091440a2015-03-18 14:16:05 -07001716 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001717 mHandler.removeMessages(ADVANCE_MSG);
1718 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1719 mHandler.sendMessageDelayed(msg, delay);
1720 mAutoAdvanceSentTime = System.currentTimeMillis();
1721 }
1722
Adam Cohen091440a2015-03-18 14:16:05 -07001723 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001724 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1725 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1726 mAutoAdvanceRunning = autoAdvanceRunning;
1727 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001728 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001729 sendAdvanceMessage(delay);
1730 } else {
1731 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001732 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001733 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1734 }
1735 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001736 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001737 }
1738 }
1739 }
1740
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001741 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1742
Adam Cohended9f8d2010-11-03 13:25:16 -07001743 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001744 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001745 if (msg.what == ADVANCE_MSG) {
1746 int i = 0;
1747 for (View key: mWidgetsToAdvance.keySet()) {
1748 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001749 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001750 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001751 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001752 public void run() {
1753 ((Advanceable) v).advance();
1754 }
1755 }, delay);
1756 }
1757 i++;
1758 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001759 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001760 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001761 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001762 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001763 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001764
Winsonc0b52fe2015-09-09 16:38:15 -07001765 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001766 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001767 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1768 if (v instanceof Advanceable) {
1769 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001770 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001771 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001772 }
1773 }
1774
Winsonc0b52fe2015-09-09 16:38:15 -07001775 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001776 if (mWidgetsToAdvance.containsKey(hostView)) {
1777 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001778 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001779 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001780 }
1781
Hyunyoung Song3f471442015-04-08 19:01:34 -07001782 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001783 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1784 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001785 }
1786
Winson Chunga6945242014-01-08 14:04:34 -08001787 public DragLayer getDragLayer() {
1788 return mDragLayer;
1789 }
1790
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001791 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001792 return mAppsView;
1793 }
1794
Hyunyoung Song3f471442015-04-08 19:01:34 -07001795 public WidgetsContainerView getWidgetsView() {
1796 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001797 }
1798
Winson Chunga6945242014-01-08 14:04:34 -08001799 public Workspace getWorkspace() {
1800 return mWorkspace;
1801 }
1802
1803 public Hotseat getHotseat() {
1804 return mHotseat;
1805 }
1806
Jorim Jaggid017f882014-01-14 17:08:48 -08001807 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001808 return mOverviewPanel;
1809 }
1810
Winson Chung006ee262015-08-03 14:40:11 -07001811 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001812 return mSearchDropTargetBar;
1813 }
1814
Tony Wickham34d2c912015-09-11 09:27:58 -07001815 public AppInfoDropTargetBar getAppInfoDropTargetBar() {
1816 return mAppInfoDropTargetBar;
1817 }
1818
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001819 public LauncherAppWidgetHost getAppWidgetHost() {
1820 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 }
Romain Guycbb89e42009-06-08 15:52:54 -07001822
Winson Chunga9abd0e2010-10-27 17:18:37 -07001823 public LauncherModel getModel() {
1824 return mModel;
1825 }
1826
Adam Cohen79d90c52016-04-22 13:29:20 -07001827 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001828 return mSharedPrefs;
1829 }
1830
Adam Cohen2e6da152015-05-06 11:42:25 -07001831 public DeviceProfile getDeviceProfile() {
1832 return mDeviceProfile;
1833 }
1834
Bjorn Bringertc459e522013-06-07 19:36:01 +01001835 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001836 getWindow().closeAllPanels();
1837
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001838 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001839 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001840 }
1841
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001842 @Override
1843 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001844 long startTime = 0;
1845 if (DEBUG_RESUME_TIME) {
1846 startTime = System.currentTimeMillis();
1847 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 super.onNewIntent(intent);
1849
1850 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001851 Folder openFolder = mWorkspace.getOpenFolder();
1852 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1853 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1854 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1855 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1856 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001857 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001858 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001859
Adam Cohen6fecd412013-10-02 17:41:50 -07001860 if (mWorkspace == null) {
1861 // Can be cases where mWorkspace is null, this prevents a NPE
1862 return;
1863 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001864 // In all these cases, only animate if we're already on home
1865 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001866
Sunny Goyal935fca12015-10-13 10:19:01 -07001867 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001868 exitSpringLoadedDragMode();
1869
1870 // If we are already on home, then just animate back to the workspace,
1871 // otherwise, just wait until onResume to set the state back to Workspace
1872 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001873 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001874 } else {
1875 mOnResumeState = State.WORKSPACE;
1876 }
1877
1878 final View v = getWindow().peekDecorView();
1879 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001880 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001881 INPUT_METHOD_SERVICE);
1882 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1883 }
1884
Winson Chungb745afb2015-03-02 11:51:23 -08001885 // Reset the apps view
1886 if (!alreadyOnHome && mAppsView != null) {
1887 mAppsView.scrollToTop();
1888 }
1889
Hyunyoung Song3f471442015-04-08 19:01:34 -07001890 // Reset the widgets view
1891 if (!alreadyOnHome && mWidgetsView != null) {
1892 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001893 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001894
Adam Cohen9211d422014-10-07 18:14:53 -07001895 if (mLauncherCallbacks != null) {
1896 mLauncherCallbacks.onHomeIntent();
1897 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 }
Adam Cohened307df2013-10-02 09:37:31 -07001899
Adam Cohen9211d422014-10-07 18:14:53 -07001900 if (mLauncherCallbacks != null) {
1901 mLauncherCallbacks.onNewIntent(intent);
1902 }
Winson15f8b172015-08-19 11:02:39 -07001903
1904 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1905 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1906 // animation.
1907 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001908 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1909 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001910 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1911 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001912
1913 // We use this flag to suppress noisy callbacks above custom content state
1914 // from onResume.
1915 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001916 mWorkspace.post(new Runnable() {
1917 @Override
1918 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001919 if (mWorkspace != null) {
1920 mWorkspace.moveToDefaultScreen(true);
1921 }
Winson15f8b172015-08-19 11:02:39 -07001922 }
1923 });
1924 }
1925 }
1926
1927 if (DEBUG_RESUME_TIME) {
1928 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1929 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001930 }
1931
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001933 public void onRestoreInstanceState(Bundle state) {
1934 super.onRestoreInstanceState(state);
1935 for (int page: mSynchronouslyBoundPages) {
1936 mWorkspace.restoreInstanceStateForChild(page);
1937 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 }
1939
1940 @Override
1941 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001942 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001943 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1944 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001945
Adam Cohen21cd0022013-10-09 18:57:02 -07001946 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001947 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948
Michael Jurka883f55b2010-10-21 15:47:14 -07001949 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001950 // We close any open folder since it will not be re-opened, and we need to make sure
1951 // this state is reflected.
1952 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1953 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954
Adam Cohendcd297f2013-06-18 13:13:40 -07001955 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001956 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001957 ContentValues itemValues = new ContentValues();
1958 mPendingAddInfo.writeToValues(itemValues);
1959 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001960 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001961 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 }
1963
Adam Cohen9211d422014-10-07 18:14:53 -07001964 if (mLauncherCallbacks != null) {
1965 mLauncherCallbacks.onSaveInstanceState(outState);
1966 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 }
1968
1969 @Override
1970 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001972
Winson Chunge7a03942011-08-05 15:05:12 -07001973 // Remove all pending runnables
1974 mHandler.removeMessages(ADVANCE_MSG);
1975 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001976 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001977 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001978
Winson Chungcd2b0142011-06-08 16:02:26 -07001979 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001980 // It's possible to receive onDestroy after a new Launcher activity has
1981 // been created. In this case, don't interfere with the new Launcher.
1982 if (mModel.isCurrentCallbacks(this)) {
1983 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001984 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001985 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001986
Sunny Goyal745bad92016-05-02 10:54:12 -07001987 if (mRotationPrefChangeHandler != null) {
1988 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1989 }
1990
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001992 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001994 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001996 mAppWidgetHost = null;
1997
1998 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999
2000 TextKeyListener.getInstance().release();
2001
Tony Wickhame2217252016-03-22 16:34:23 -07002002 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
2003 .removeAccessibilityStateChangeListener(this);
2004
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002005 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002006
Michael Jurka2ecf9952012-06-18 12:52:28 -07002007 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002008
2009 if (mLauncherCallbacks != null) {
2010 mLauncherCallbacks.onDestroy();
2011 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002012 }
2013
Adam Cohena9cf38f2011-05-02 15:36:58 -07002014 public DragController getDragController() {
2015 return mDragController;
2016 }
2017
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002018 @Override
2019 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002020 onStartForResult(requestCode);
2021 super.startActivityForResult(intent, requestCode);
2022 }
2023
2024 @Override
2025 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2026 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2027 onStartForResult(requestCode);
2028 try {
2029 super.startIntentSenderForResult(intent, requestCode,
2030 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2031 } catch (IntentSender.SendIntentException e) {
2032 throw new ActivityNotFoundException();
2033 }
2034 }
2035
2036 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002037 if (requestCode >= 0) {
2038 setWaitingForResult(true);
2039 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 }
2041
Winson Chung70d72102011-08-12 11:24:35 -07002042 /**
2043 * Indicates that we want global search for this activity by setting the globalSearch
2044 * argument for {@link #startSearch} to true.
2045 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002047 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002048 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002049
Karl Rosaen138a0412009-04-23 19:00:21 -07002050 if (initialQuery == null) {
2051 // Use any text typed in the launcher as the initial query
2052 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002053 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 if (appSearchData == null) {
2055 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002056 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002058
2059 // TODO send proper bounds.
2060 Rect sourceBounds = null;
Romain Guycbb89e42009-06-08 15:52:54 -07002061
Ian Parkinson047036e2014-09-01 15:40:53 +01002062 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002063 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002064 if (clearTextImmediately) {
2065 clearTypedText();
2066 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002067
2068 // We need to show the workspace after starting the search
2069 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002070 }
2071
Ian Parkinson047036e2014-09-01 15:40:53 +01002072 /**
2073 * Start a text search.
2074 *
2075 * @return {@code true} if the search will start immediately, so any further keypresses
2076 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2077 * to buffer keypresses.
2078 */
2079 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002080 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002081 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2082 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2083 sourceBounds);
2084 }
2085
Michael Jurkaa33411c2012-06-14 16:18:21 -07002086 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002087 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002088 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002089 }
2090
2091 /**
2092 * Starts the global search activity. This code is a copied from SearchManager
2093 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002094 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002095 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002096 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002097 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2098 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2099 if (globalSearchActivity == null) {
2100 Log.w(TAG, "No global search activity found.");
2101 return;
2102 }
2103 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2104 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2105 intent.setComponent(globalSearchActivity);
2106 // Make sure that we have a Bundle to put source in
2107 if (appSearchData == null) {
2108 appSearchData = new Bundle();
2109 } else {
2110 appSearchData = new Bundle(appSearchData);
2111 }
Adam Cohen9211d422014-10-07 18:14:53 -07002112 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002113 if (!appSearchData.containsKey("source")) {
2114 appSearchData.putString("source", getPackageName());
2115 }
2116 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2117 if (!TextUtils.isEmpty(initialQuery)) {
2118 intent.putExtra(SearchManager.QUERY, initialQuery);
2119 }
2120 if (selectInitialQuery) {
2121 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2122 }
2123 intent.setSourceBounds(sourceBounds);
2124 try {
2125 startActivity(intent);
2126 } catch (ActivityNotFoundException ex) {
2127 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2128 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002129 }
2130
Yura4f93ec62014-02-04 14:15:21 +00002131 public boolean isOnCustomContent() {
2132 return mWorkspace.isOnOrMovingToCustomContent();
2133 }
2134
Winson Chung70d72102011-08-12 11:24:35 -07002135 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002136 public boolean onPrepareOptionsMenu(Menu menu) {
2137 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002138 if (mLauncherCallbacks != null) {
2139 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2140 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002141 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002142 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002143
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002144 @Override
2145 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002146 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002147 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002148 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002149 }
2150
Joe Onorato9c1289c2009-08-17 11:03:03 -04002151 public boolean isWorkspaceLocked() {
2152 return mWorkspaceLoading || mWaitingForResult;
2153 }
2154
Adam Cohen517a7f52014-03-01 12:12:59 -08002155 public boolean isWorkspaceLoading() {
2156 return mWorkspaceLoading;
2157 }
2158
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002159 private void setWorkspaceLoading(boolean value) {
2160 boolean isLocked = isWorkspaceLocked();
2161 mWorkspaceLoading = value;
2162 if (isLocked != isWorkspaceLocked()) {
2163 onWorkspaceLockedChanged();
2164 }
2165 }
2166
2167 private void setWaitingForResult(boolean value) {
2168 boolean isLocked = isWorkspaceLocked();
2169 mWaitingForResult = value;
2170 if (isLocked != isWorkspaceLocked()) {
2171 onWorkspaceLockedChanged();
2172 }
2173 }
2174
Adam Cohen9211d422014-10-07 18:14:53 -07002175 protected void onWorkspaceLockedChanged() {
2176 if (mLauncherCallbacks != null) {
2177 mLauncherCallbacks.onWorkspaceLockedChanged();
2178 }
2179 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002180
Michael Jurka0280c3b2010-09-17 15:00:07 -07002181 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002182 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002183 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002184 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2185 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002186 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002187 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002188
Tony Wickhama0628cc2015-10-14 15:23:04 -07002189 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002190 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002191 if (LOGD) {
2192 Log.d(TAG, "Adding widget from drop");
2193 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002194 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2195 }
2196
Sunny Goyale6b63a32015-01-16 16:14:29 -08002197 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002198 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2199 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002200 if (appWidgetInfo.configure != null) {
2201 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002202 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002203
Bjorn Bringert7984c942009-12-09 15:38:25 +00002204 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002205 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2206 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2207
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002208 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002209 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002210 Runnable onComplete = new Runnable() {
2211 @Override
2212 public void run() {
2213 // Exit spring loaded mode if necessary after adding the widget
2214 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2215 null);
2216 }
2217 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002218 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002219 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002220 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002221 }
2222 }
Romain Guycbb89e42009-06-08 15:52:54 -07002223
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002224 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002225 // Close any folders that may be open.
2226 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002227 mWorkspace.moveToCustomContentScreen(animate);
2228 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002229
2230 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2231 int[] cell, int spanX, int spanY) {
2232 switch (info.itemType) {
2233 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2234 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2235 int span[] = new int[2];
2236 span[0] = spanX;
2237 span[1] = spanY;
2238 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2239 container, screenId, cell, span);
2240 break;
2241 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2242 processShortcutFromDrop(info.componentName, container, screenId, cell);
2243 break;
2244 default:
2245 throw new IllegalStateException("Unknown item type: " + info.itemType);
2246 }
2247 }
2248
Adam Cohenfbba09b2011-07-18 21:43:05 -07002249 /**
2250 * Process a shortcut drop.
2251 *
2252 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002253 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002254 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002255 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002256 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2257 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002258 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002259 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002260 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002261
2262 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002263 mPendingAddInfo.cellX = cell[0];
2264 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002265 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002266
2267 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2268 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002269 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002270 }
2271
Adam Cohenfbba09b2011-07-18 21:43:05 -07002272 /**
2273 * Process a widget drop.
2274 *
2275 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002276 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002277 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002278 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002279 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2280 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002281 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002282 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002283 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002284 mPendingAddInfo.minSpanX = info.minSpanX;
2285 mPendingAddInfo.minSpanY = info.minSpanY;
2286
Adam Cohenfbba09b2011-07-18 21:43:05 -07002287 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002288 mPendingAddInfo.cellX = cell[0];
2289 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002290 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002291 if (span != null) {
2292 mPendingAddInfo.spanX = span[0];
2293 mPendingAddInfo.spanY = span[1];
2294 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002295
Adam Cohened66b2b2012-01-23 17:28:51 -08002296 AppWidgetHostView hostView = info.boundWidget;
2297 int appWidgetId;
2298 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002299 // In the case where we've prebound the widget, we remove it from the DragLayer
2300 if (LOGD) {
2301 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2302 }
2303 getDragLayer().removeView(hostView);
2304
Adam Cohened66b2b2012-01-23 17:28:51 -08002305 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002306 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002307
2308 // Clear the boundWidget so that it doesn't get destroyed.
2309 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002310 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002311 // In this case, we either need to start an activity to get permission to bind
2312 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002313 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002314 Bundle options = info.bindOptions;
2315
Sunny Goyalffe83f12014-08-14 17:39:34 -07002316 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2317 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002318 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002319 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002320 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002321 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002322 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2323 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2324 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002325 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2326 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002327 // TODO: we need to make sure that this accounts for the options bundle.
2328 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002329 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2330 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002331 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002332 }
2333
Adam Cohendcd297f2013-06-18 13:13:40 -07002334 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002335 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002336 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002337 folderInfo.title = getText(R.string.folder_name);
2338
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002339 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002340 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2341 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002342
2343 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002344 FolderIcon newFolder =
2345 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002346 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002347 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002348 // Force measure the new folder icon
2349 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2350 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002351 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002352 }
Romain Guycbb89e42009-06-08 15:52:54 -07002353
Winsonc0b52fe2015-09-09 16:38:15 -07002354 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002355 * Unbinds the view for the specified item, and removes the item and all its children.
2356 *
2357 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002358 * @param itemInfo the {@link ItemInfo} for this view.
2359 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002360 */
Winson2949fb52015-09-24 09:56:11 -07002361 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002362 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002363 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002364 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2365 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002366 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002367 } else {
2368 mWorkspace.removeWorkspaceItem(v);
2369 }
Winsonc0b52fe2015-09-09 16:38:15 -07002370 if (deleteFromDb) {
2371 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2372 }
2373 } else if (itemInfo instanceof FolderInfo) {
2374 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002375 if (v instanceof FolderIcon) {
2376 ((FolderIcon) v).removeListeners();
2377 }
Winsonc0b52fe2015-09-09 16:38:15 -07002378 mWorkspace.removeWorkspaceItem(v);
2379 if (deleteFromDb) {
2380 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2381 }
2382 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2383 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002384 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002385 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002386 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002387 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002388 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002389
Winsonc0b52fe2015-09-09 16:38:15 -07002390 } else {
2391 return false;
2392 }
2393 return true;
2394 }
2395
2396 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002397 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002398 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002399 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002400 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002401 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002402 // Deleting an app widget ID is a void call but writes to disk before returning
2403 // to the caller...
2404 new AsyncTask<Void, Void, Void>() {
2405 public Void doInBackground(Void ... args) {
2406 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2407 return null;
2408 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002409 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002410 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002411 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002412 }
2413
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002414 @Override
2415 public boolean dispatchKeyEvent(KeyEvent event) {
2416 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2417 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002418 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002419 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002420 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002421 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002422 dumpState();
2423 return true;
2424 }
2425 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002426 }
2427 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2428 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002429 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002430 return true;
2431 }
2432 }
2433
2434 return super.dispatchKeyEvent(event);
2435 }
2436
Joe Onorato88ec0992009-11-19 13:16:06 -08002437 @Override
2438 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002439 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2440 return;
2441 }
2442
Sunny Goyal45478022015-06-08 16:52:41 -07002443 if (mDragController.isDragging()) {
2444 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002445 return;
2446 }
2447
Winson Chungb745afb2015-03-02 11:51:23 -08002448 if (isAppsViewVisible()) {
2449 showWorkspace(true);
2450 } else if (isWidgetsViewVisible()) {
2451 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002452 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002453 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002454 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002455 Folder openFolder = mWorkspace.getOpenFolder();
2456 if (openFolder.isEditingName()) {
2457 openFolder.dismissEditingName();
2458 } else {
2459 closeFolder();
2460 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002461 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002462 mWorkspace.exitWidgetResizeMode();
2463
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002464 // Back button is a no-op here, but give at least some feedback for the button press
2465 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002466 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002467 }
2468
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002469 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002470 * Launches the intent referred by the clicked shortcut.
2471 *
2472 * @param v The view representing the clicked shortcut.
2473 */
2474 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002475 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2476 // view has detached (it's possible for this to happen if the view is removed mid touch).
2477 if (v.getWindowToken() == null) {
2478 return;
2479 }
2480
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002481 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002482 return;
2483 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002484
Adam Cohen1697b792013-09-17 19:08:21 -07002485 if (v instanceof Workspace) {
2486 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002487 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002488 }
2489 return;
2490 }
2491
2492 if (v instanceof CellLayout) {
2493 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002494 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2495 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002496 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002497 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002498 }
2499
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002500 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002501 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002502 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002503 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002504 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002505 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002506 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002507 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002508 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002509 } else if (tag instanceof AppInfo) {
2510 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002511 } else if (tag instanceof LauncherAppWidgetInfo) {
2512 if (v instanceof PendingAppWidgetHostView) {
2513 onClickPendingWidget((PendingAppWidgetHostView) v);
2514 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002515 }
2516 }
2517
Sunny Goyal70660032015-05-14 00:07:08 -07002518 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002519 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002520 return false;
2521 }
2522
Michael Jurkaaf442092010-06-10 17:01:57 -07002523 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002524 * Event handler for the app widget view which has not fully restored.
2525 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002526 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002527 if (mIsSafeModeEnabled) {
2528 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2529 return;
2530 }
2531
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002532 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002533 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002534 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2535 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2536 // This should not happen, as we make sure that an Id is allocated during bind.
2537 return;
2538 }
2539 LauncherAppWidgetProviderInfo appWidgetInfo =
2540 mAppWidgetManager.findProvider(info.providerName, info.user);
2541 if (appWidgetInfo != null) {
2542 mPendingAddWidgetId = info.appWidgetId;
2543 mPendingAddInfo.copyFrom(info);
2544 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002545
Sunny Goyald478c832016-04-01 12:04:16 -07002546 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2547 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingAddWidgetId);
2548 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
2549 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2550 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2551 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2552 }
2553 } else {
2554 LauncherAppWidgetProviderInfo appWidgetInfo =
2555 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2556 if (appWidgetInfo != null) {
2557 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2558 }
Sunny Goyalff572272014-07-23 13:58:07 -07002559 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002560 } else if (info.installProgress < 0) {
2561 // The install has not been queued
2562 final String packageName = info.providerName.getPackageName();
2563 showBrokenAppInstallDialog(packageName,
2564 new DialogInterface.OnClickListener() {
2565 public void onClick(DialogInterface dialog, int id) {
2566 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2567 }
2568 });
2569 } else {
2570 // Download has started.
2571 final String packageName = info.providerName.getPackageName();
2572 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002573 }
2574 }
2575
Sunny Goyald478c832016-04-01 12:04:16 -07002576 private void startRestoredWidgetReconfigActivity(
2577 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
2578 mPendingAddWidgetInfo = provider;
2579 mPendingAddInfo.copyFrom(info);
2580 mPendingAddWidgetId = info.appWidgetId;
2581 mAppWidgetManager.startConfigActivity(provider,
2582 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2583 }
2584
Sunny Goyalff572272014-07-23 13:58:07 -07002585 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002586 * Event handler for the "grid" button that appears on the home screen, which
2587 * enters all apps mode.
2588 *
2589 * @param v The view that was clicked.
2590 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002591 protected void onClickAllAppsButton(View v) {
2592 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002593 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002594 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002595 true /* updatePredictedApps */, false /* focusSearchBar */);
2596 }
2597 }
2598
2599 protected void onLongClickAllAppsButton(View v) {
2600 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2601 if (!isAppsViewVisible()) {
2602 showAppsView(true /* animated */, false /* resetListToTop */,
2603 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002604 }
2605 }
2606
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002607 private void showBrokenAppInstallDialog(final String packageName,
2608 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002609 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002610 .setTitle(R.string.abandoned_promises_title)
2611 .setMessage(R.string.abandoned_promise_explanation)
2612 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2613 .setNeutralButton(R.string.abandoned_clean_this,
2614 new DialogInterface.OnClickListener() {
2615 public void onClick(DialogInterface dialog, int id) {
2616 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2617 mWorkspace.removeAbandonedPromise(packageName, user);
2618 }
2619 })
2620 .create().show();
2621 return;
2622 }
2623
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002624 /**
2625 * Event handler for an app shortcut click.
2626 *
2627 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2628 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002629 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002630 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2631 Object tag = v.getTag();
2632 if (!(tag instanceof ShortcutInfo)) {
2633 throw new IllegalArgumentException("Input must be a Shortcut");
2634 }
2635
2636 // Open shortcut
2637 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002638
2639 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002640 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2641 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002642 // Launch activity anyway, framework will tell the user why the app is suspended.
2643 } else {
2644 int error = R.string.activity_not_available;
2645 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2646 error = R.string.safemode_shortcut_error;
2647 }
2648 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2649 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002650 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002651 }
2652
Chris Wren40c5ed32014-06-24 18:24:23 -04002653 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002654 if ((v instanceof BubbleTextView)
2655 && shortcut.isPromise()
2656 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002657 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002658 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002659 new DialogInterface.OnClickListener() {
2660 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002661 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002662 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002663 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002664 return;
2665 }
2666
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002667 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002668 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002669 }
2670
Adam Cohen091440a2015-03-18 14:16:05 -07002671 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002672 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002673 final ShortcutInfo shortcut;
2674 final Intent intent;
2675 if (tag instanceof ShortcutInfo) {
2676 shortcut = (ShortcutInfo) tag;
2677 intent = shortcut.intent;
2678 int[] pos = new int[2];
2679 v.getLocationOnScreen(pos);
2680 intent.setSourceBounds(new Rect(pos[0], pos[1],
2681 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002682
Sunny Goyal508da152014-08-14 10:53:27 -07002683 } else if (tag instanceof AppInfo) {
2684 shortcut = null;
2685 intent = ((AppInfo) tag).intent;
2686 } else {
2687 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2688 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002689
2690 boolean success = startActivitySafely(v, intent, tag);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002691 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002692
2693 if (success && v instanceof BubbleTextView) {
2694 mWaitingForResume = (BubbleTextView) v;
2695 mWaitingForResume.setStayPressed(true);
2696 }
2697 }
2698
2699 /**
2700 * Event handler for a folder icon click.
2701 *
2702 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2703 */
2704 protected void onClickFolderIcon(View v) {
2705 if (LOGD) Log.d(TAG, "onClickFolder");
2706 if (!(v instanceof FolderIcon)){
2707 throw new IllegalArgumentException("Input must be a FolderIcon");
2708 }
2709
2710 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002711 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002712 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002713 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002714 }
Michael Jurka5130e402011-10-13 04:55:35 -07002715 }
2716
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002717 /**
2718 * Event handler for the (Add) Widgets button that appears after a long press
2719 * on the home screen.
2720 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002721 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002722 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002723 if (mIsSafeModeEnabled) {
2724 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2725 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002726 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002727 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002728 }
2729
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002730 /**
2731 * Event handler for the wallpaper picker button that appears after a long press
2732 * on the home screen.
2733 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002734 protected void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002735 if (!Utilities.isWallapaperAllowed(this)) {
2736 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2737 return;
2738 }
2739
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002740 String pickerPackage = getString(R.string.wallpaper_picker_package);
2741 if (TextUtils.isEmpty(pickerPackage)) {
2742 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2743 }
2744
Tony Wickham785f7a52015-08-31 17:28:32 -07002745 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2746 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002747 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER)
2748 .setPackage(pickerPackage)
2749 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset),
2750 REQUEST_PICK_WALLPAPER);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002751 }
2752
2753 /**
2754 * Event handler for a click on the settings button that appears after a long press
2755 * on the home screen.
2756 */
Sunny Goyal745bad92016-05-02 10:54:12 -07002757 private void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002758 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002759 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2760 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002761 }
2762
Adam Cohen61f560d2013-09-30 15:58:20 -07002763 public View.OnTouchListener getHapticFeedbackTouchListener() {
2764 if (mHapticFeedbackTouchListener == null) {
2765 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002766 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002767 @Override
2768 public boolean onTouch(View v, MotionEvent event) {
2769 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2770 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2771 }
2772 return false;
2773 }
2774 };
2775 }
2776 return mHapticFeedbackTouchListener;
2777 }
2778
Tony Wickhame2217252016-03-22 16:34:23 -07002779 @Override
2780 public void onAccessibilityStateChanged(boolean enabled) {
2781 mDragLayer.onAccessibilityStateChanged(enabled);
2782 }
2783
Adam Cohen9211d422014-10-07 18:14:53 -07002784 public void onDragStarted(View view) {
2785 if (isOnCustomContent()) {
2786 // Custom content screen doesn't participate in drag and drop. If on custom
2787 // content screen, move to default.
2788 moveWorkspaceToDefaultScreen();
2789 }
Adam Cohen9211d422014-10-07 18:14:53 -07002790 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002791
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002792 /**
2793 * Called when the user stops interacting with the launcher.
2794 * This implies that the user is now on the homescreen and is not doing housekeeping.
2795 */
Adam Cohen9211d422014-10-07 18:14:53 -07002796 protected void onInteractionEnd() {
2797 if (mLauncherCallbacks != null) {
2798 mLauncherCallbacks.onInteractionEnd();
2799 }
2800 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002801
2802 /**
2803 * Called when the user starts interacting with the launcher.
2804 * The possible interactions are:
2805 * - open all apps
2806 * - reorder an app shortcut, or a widget
2807 * - open the overview mode.
2808 * This is a good time to stop doing things that only make sense
2809 * when the user is on the homescreen and not doing housekeeping.
2810 */
Adam Cohen9211d422014-10-07 18:14:53 -07002811 protected void onInteractionBegin() {
2812 if (mLauncherCallbacks != null) {
2813 mLauncherCallbacks.onInteractionBegin();
2814 }
2815 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002816
Winson Chungcd99cd32015-04-29 11:03:24 -07002817 /** Updates the interaction state. */
2818 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002819 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002820 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002821 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2822 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002823 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002824 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002825 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002826 onInteractionEnd();
2827 }
2828 }
2829
Winson Chung8f1eff72015-05-28 17:33:40 -07002830 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002831 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002832 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002833 // Only launch using the new animation if the shortcut has not opted out (this is a
2834 // private contract between launcher and may be ignored in the future).
2835 boolean useLaunchAnimation = (v != null) &&
2836 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002837 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2838 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002839
Kenny Guy1317e2d2014-05-08 18:52:50 +01002840 UserHandleCompat user = null;
2841 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2842 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2843 user = userManager.getUserForSerialNumber(serialNumber);
2844 }
2845
2846 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002847 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002848 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002849 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002850 int left = 0, top = 0;
2851 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2852 if (v instanceof TextView) {
2853 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002854 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2855 if (icon != null) {
2856 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002857 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002858 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002859 width = bounds.width();
2860 height = bounds.height();
2861 }
2862 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002863 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2864 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002865 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002866 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002867 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002868 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002869 // On L devices, we use the device default slide-up transition.
2870 // On L MR1 devices, we a custom version of the slide-up transition which
2871 // doesn't have the delay present in the device default.
2872 opts = ActivityOptions.makeCustomAnimation(this,
2873 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002874 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002875 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002876 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002877
2878 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -07002879 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2880 try {
2881 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2882 // containing file Uris would cause a crash as penaltyDeathOnFileUriExposure
2883 // is enabled by default on NYC.
2884 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2885 .penaltyLog().build());
2886 // Could be launching some bookkeeping activity
2887 startActivity(intent, optsBundle);
2888 } finally {
2889 StrictMode.setVmPolicy(oldPolicy);
2890 }
Winson Chungc7450e32012-04-17 17:34:08 -07002891 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002892 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002893 launcherApps.startActivityForProfile(intent.getComponent(), user,
2894 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002895 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002896 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002897 } catch (SecurityException e) {
Sunny Goyal28c6b962015-10-12 11:42:05 -07002898 if (Utilities.ATLEAST_MARSHMALLOW && tag instanceof ItemInfo) {
2899 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2900 // corresponding permission. Show the appropriate permission prompt if that
2901 // is the case.
2902 if (intent.getComponent() == null
2903 && Intent.ACTION_CALL.equals(intent.getAction())
2904 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2905 PackageManager.PERMISSION_GRANTED) {
2906 // TODO: Rename sPendingAddItem to a generic name.
2907 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2908 0, (ItemInfo) tag);
2909 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2910 REQUEST_PERMISSION_CALL_PHONE);
2911 return false;
2912 }
2913 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002914 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002915 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002916 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002917 "or use the exported attribute for this activity. "
2918 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002919 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002920 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002921 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002922
Winson Chungbedf9232015-07-10 12:38:30 -07002923 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002924 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002925 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2926 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2927 return false;
2928 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002929 try {
2930 success = startActivity(v, intent, tag);
2931 } catch (ActivityNotFoundException e) {
2932 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2933 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2934 }
2935 return success;
2936 }
2937
Adam Cohen268c4752012-06-06 17:47:33 -07002938 /**
2939 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2940 * in the DragLayer in the exact absolute location of the original FolderIcon.
2941 */
2942 private void copyFolderIconToImage(FolderIcon fi) {
2943 final int width = fi.getMeasuredWidth();
2944 final int height = fi.getMeasuredHeight();
2945
2946 // Lazy load ImageView, Bitmap and Canvas
2947 if (mFolderIconImageView == null) {
2948 mFolderIconImageView = new ImageView(this);
2949 }
2950 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2951 mFolderIconBitmap.getHeight() != height) {
2952 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2953 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2954 }
2955
2956 DragLayer.LayoutParams lp;
2957 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2958 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2959 } else {
2960 lp = new DragLayer.LayoutParams(width, height);
2961 }
2962
Adam Cohen307fe232012-08-16 17:55:58 -07002963 // The layout from which the folder is being opened may be scaled, adjust the starting
2964 // view size by this scale factor.
2965 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002966 lp.customPosition = true;
2967 lp.x = mRectForFolderAnimation.left;
2968 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002969 lp.width = (int) (scale * width);
2970 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002971
2972 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2973 fi.draw(mFolderIconCanvas);
2974 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002975 if (fi.getFolder() != null) {
2976 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2977 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002978 }
Adam Cohen268c4752012-06-06 17:47:33 -07002979 // Just in case this image view is still in the drag layer from a previous animation,
2980 // we remove it and re-add it.
2981 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2982 mDragLayer.removeView(mFolderIconImageView);
2983 }
2984 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002985 if (fi.getFolder() != null) {
2986 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002987 }
Adam Cohen268c4752012-06-06 17:47:33 -07002988 }
2989
Adam Cohen37b2a492016-04-06 18:36:06 -07002990 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002991 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002992 FolderInfo info = (FolderInfo) fi.getTag();
2993 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2994 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002995 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2996 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002997 }
2998
Adam Cohen268c4752012-06-06 17:47:33 -07002999 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3000 copyFolderIconToImage(fi);
3001 fi.setVisibility(View.INVISIBLE);
3002
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003003 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
3004 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003005 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003006 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3007 }
3008 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003009 oa.start();
3010 }
3011
Sunny Goyal935fca12015-10-13 10:19:01 -07003012 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003013 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003014 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003015
Adam Cohen268c4752012-06-06 17:47:33 -07003016 // We remove and re-draw the FolderIcon in-case it has changed
3017 mDragLayer.removeView(mFolderIconImageView);
3018 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08003019
3020 if (cl != null) {
3021 cl.clearFolderLeaveBehind();
3022 }
3023
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003024 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003025 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003026 oa.addListener(new AnimatorListenerAdapter() {
3027 @Override
3028 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003029 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003030 // Remove the ImageView copy of the FolderIcon and make the original visible.
3031 mDragLayer.removeView(mFolderIconImageView);
3032 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003033 }
3034 }
3035 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003036 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003037 if (!animate) {
3038 oa.end();
3039 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003040 }
3041
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003042 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003043 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003044 * is animated relative to the specified View. If the View is null, no animation
3045 * is played.
3046 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003047 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003048 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003049 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003050
Adam Cohenfb91f302012-06-11 15:45:18 -07003051 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003052 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3053 if (openFolder != null && openFolder != folder) {
3054 // Close any open folder before opening a folder.
3055 closeFolder();
3056 }
3057
Adam Cohena9cf38f2011-05-02 15:36:58 -07003058 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003059
Adam Cohena9cf38f2011-05-02 15:36:58 -07003060 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003061
Sunny Goyalf4066152015-04-15 09:42:19 -07003062 // While the folder is open, the position of the icon cannot change.
3063 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3064
Adam Cohen4554ee12011-08-03 16:13:21 -07003065 // Just verify that the folder hasn't already been added to the DragLayer.
3066 // There was a one-off crash where the folder had a parent already.
3067 if (folder.getParent() == null) {
3068 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003069 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003070 } else {
3071 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3072 folder.getParent() + ").");
3073 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003074 folder.animateOpen();
3075
3076 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003077
3078 // Notify the accessibility manager that this folder "window" has appeared and occluded
3079 // the workspace items
3080 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3081 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003082 }
3083
3084 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003085 closeFolder(true);
3086 }
3087
3088 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003089 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003090 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003091 if (folder.isEditingName()) {
3092 folder.dismissEditingName();
3093 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003094 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003095 }
3096 }
3097
Sunny Goyal935fca12015-10-13 10:19:01 -07003098 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003099 animate &= !Utilities.isPowerSaverOn(this);
3100
Adam Cohen4554ee12011-08-03 16:13:21 -07003101 folder.getInfo().opened = false;
3102
3103 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3104 if (parent != null) {
3105 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003106 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003107 if (fi != null) {
3108 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3109 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003110 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003111 if (animate) {
3112 folder.animateClosed();
3113 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003114 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003115 }
Winson Chung83ca4802013-04-12 15:10:52 -07003116
Sunny Goyal935fca12015-10-13 10:19:01 -07003117 // Notify the accessibility manager that this folder "window" has disappeared and no
3118 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003119 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003120 }
3121
Tony Wickhamdadb3042016-02-24 11:07:00 -08003122 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003123 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003124 if (!isDraggingEnabled()) return false;
3125 if (isWorkspaceLocked()) return false;
3126 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003127
Winson Chung76648c52015-07-10 14:33:23 -07003128 if (v == mAllAppsButton) {
3129 onLongClickAllAppsButton(v);
3130 return true;
3131 }
3132
Adam Cohen1697b792013-09-17 19:08:21 -07003133 if (v instanceof Workspace) {
3134 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003135 if (!mWorkspace.isTouchActive()) {
3136 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003137 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3138 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3139 return true;
3140 } else {
3141 return false;
3142 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003143 } else {
3144 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003145 }
Adam Cohen1697b792013-09-17 19:08:21 -07003146 }
3147
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003148 CellLayout.CellInfo longClickCellInfo = null;
3149 View itemUnderLongClick = null;
3150 if (v.getTag() instanceof ItemInfo) {
3151 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003152 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003153 itemUnderLongClick = longClickCellInfo.cell;
3154 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003155 }
3156
Winson Chung3d503fb2011-07-13 17:25:49 -07003157 // The hotseat touch handling does not go through Workspace, and we always allow long press
3158 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003159 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003160 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003161 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003162 // User long pressed on empty space
3163 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3164 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003165 if (mWorkspace.isInOverviewMode()) {
3166 mWorkspace.startReordering(v);
3167 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003168 showOverviewMode(true);
Hyunyoung Song645764e2016-06-06 14:19:02 -07003169 mHotseat.requestDisallowInterceptTouchEvent(true);
3170
Adam Cohendedbd962013-07-11 14:21:49 -07003171 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003172 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003173 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3174 mHotseat.getOrderInHotseat(
3175 longClickCellInfo.cellX,
3176 longClickCellInfo.cellY));
3177 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003178 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003179 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003180 }
3181 }
3182 }
3183 return true;
3184 }
3185
Winson Chung3d503fb2011-07-13 17:25:49 -07003186 boolean isHotseatLayout(View layout) {
3187 return mHotseat != null && layout != null &&
3188 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3189 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003190
Winson Chung3d503fb2011-07-13 17:25:49 -07003191 /**
3192 * Returns the CellLayout of the specified container at the specified screen.
3193 */
Sunny Goyal92820592015-03-02 11:31:35 -08003194 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003195 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3196 if (mHotseat != null) {
3197 return mHotseat.getLayout();
3198 } else {
3199 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003200 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003201 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003202 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003203 }
3204 }
3205
Winson Chungb745afb2015-03-02 11:51:23 -08003206 /**
3207 * For overridden classes.
3208 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003209 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003210 return isAppsViewVisible();
3211 }
3212
3213 public boolean isAppsViewVisible() {
3214 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3215 }
3216
3217 public boolean isWidgetsViewVisible() {
3218 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003219 }
3220
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003221 private void setWorkspaceBackground(int background) {
3222 switch (background) {
3223 case WORKSPACE_BACKGROUND_TRANSPARENT:
3224 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3225 break;
3226 case WORKSPACE_BACKGROUND_BLACK:
3227 getWindow().setBackgroundDrawable(null);
3228 break;
3229 default:
3230 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3231 }
Craig Mautner360310b2012-10-26 15:13:08 -07003232 }
3233
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003234 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003235 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3236 int curflags = getWindow().getAttributes().flags
3237 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3238 if (wpflags != curflags) {
3239 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3240 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003241 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003242 }
3243
Michael Jurkae326f182011-11-21 14:05:46 -08003244 @Override
3245 public void onTrimMemory(int level) {
3246 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003247 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3248 // The widget preview db can result in holding onto over
3249 // 3MB of memory for caching which isn't necessary.
3250 SQLiteDatabase.releaseMemory();
3251
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003252 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003253 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003254 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003255 if (mLauncherCallbacks != null) {
3256 mLauncherCallbacks.onTrimMemory(level);
3257 }
Michael Jurkae326f182011-11-21 14:05:46 -08003258 }
3259
Winson5c6bdbb2015-09-03 11:36:19 -07003260 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003261 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003262 }
3263
Winson5c6bdbb2015-09-03 11:36:19 -07003264 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003265 boolean changed = mState != State.WORKSPACE ||
3266 mWorkspace.getState() != Workspace.State.NORMAL;
3267 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003268 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003269 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003270 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003271
Michael Jurkab3e22d92011-10-31 15:58:33 -07003272 // Set focus to the AppsCustomize button
3273 if (mAllAppsButton != null) {
3274 mAllAppsButton.requestFocus();
3275 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003276 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003277
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003278 // Change the state *after* we've called all the transition code
3279 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003280
Winson Chung5fb63472011-02-02 17:03:37 -08003281 // Resume the auto-advance of widgets
3282 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003283 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003284
Winson Chung0f785722015-04-08 10:27:49 -07003285 if (changed) {
3286 // Send an accessibility event to announce the context change
3287 getWindow().getDecorView()
3288 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003289 }
Winson5c6bdbb2015-09-03 11:36:19 -07003290 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003291 }
3292
Winsone9f27272015-10-13 10:47:51 -07003293 /**
3294 * Shows the overview button.
3295 */
Adam Cohened307df2013-10-02 09:37:31 -07003296 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003297 showOverviewMode(animated, false);
3298 }
3299
3300 /**
3301 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3302 * onto one of the overview panel buttons.
3303 */
3304 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3305 Runnable postAnimRunnable = null;
3306 if (requestButtonFocus) {
3307 postAnimRunnable = new Runnable() {
3308 @Override
3309 public void run() {
3310 // Hitting the menu button when in touch mode does not trigger touch mode to
3311 // be disabled, so if requested, force focus on one of the overview panel
3312 // buttons.
3313 mOverviewPanel.requestFocusFromTouch();
3314 }
3315 };
3316 }
Adam Cohened307df2013-10-02 09:37:31 -07003317 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003318 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003319 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003320 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003321 }
3322
Winson Chungb745afb2015-03-02 11:51:23 -08003323 /**
3324 * Shows the apps view.
3325 */
Hyunyoung Song645764e2016-06-06 14:19:02 -07003326 public void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003327 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003328 if (resetListToTop) {
3329 mAppsView.scrollToTop();
3330 }
Winson Chung4ac30062015-05-08 17:34:17 -07003331 if (updatePredictedApps) {
3332 tryAndUpdatePredictedApps();
3333 }
Winson Chung76648c52015-07-10 14:33:23 -07003334 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003335 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003336
Winson Chungb745afb2015-03-02 11:51:23 -08003337 /**
3338 * Shows the widgets view.
3339 */
3340 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003341 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003342 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003343 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003344 }
Winson Chung76648c52015-07-10 14:33:23 -07003345 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003346
3347 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003348 @Override
3349 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003350 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003351 }
3352 });
Winson Chungb745afb2015-03-02 11:51:23 -08003353 }
3354
3355 /**
3356 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003357 *
3358 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003359 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003360 // TODO: calling method should use the return value so that when {@code false} is returned
3361 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003362 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003363 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3364 mState != State.WIDGETS_SPRING_LOADED) {
3365 return false;
3366 }
3367 if (toState != State.APPS && toState != State.WIDGETS) {
3368 return false;
3369 }
Winson Chungb745afb2015-03-02 11:51:23 -08003370
3371 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003372 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3373 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003374 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003375 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003376 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003377
Michael Jurkab3e22d92011-10-31 15:58:33 -07003378 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003379 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003380
3381 // Pause the auto-advance of widgets until we are out of AllApps
3382 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003383 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003384 closeFolder();
3385
3386 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003387 getWindow().getDecorView()
3388 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003389 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003390 }
3391
Winson Chungcd99cd32015-04-29 11:03:24 -07003392 /**
3393 * Updates the workspace and interaction state on state change, and return the animation to this
3394 * new state.
3395 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003396 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003397 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003398 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003399 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003400 updateInteraction(fromState, toState);
3401 return anim;
3402 }
3403
Jun Mukaif0033da2015-08-04 18:34:30 -07003404 public void onLauncherClingShown() {
3405 // When a launcher cling appears, it should cover the underlying layers, so their focus
3406 // should be blocked.
3407 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3408 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3409 }
3410 }
3411
3412 public void onLauncherClingDismissed() {
3413 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3414 }
3415
Hyunyoung Song3f471442015-04-08 19:01:34 -07003416 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003417 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003418 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003419 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003420 }
Winson Chungb745afb2015-03-02 11:51:23 -08003421
Winson Chung006ee262015-08-03 14:40:11 -07003422 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003423 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003424 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003425
3426 if (isAppsViewVisible()) {
3427 mState = State.APPS_SPRING_LOADED;
3428 } else if (isWidgetsViewVisible()) {
3429 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003430 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003431 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003432 } else {
3433 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003434 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003435 }
Adam Cohen7777d962011-08-18 18:58:38 -07003436
Hyunyoung Song3f471442015-04-08 19:01:34 -07003437 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003438 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003439 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003440
Winson Chunge7a03942011-08-05 15:05:12 -07003441 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003442 @Override
3443 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003444 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003445 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3446 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003447 // Before we show workspace, hide all apps again because
3448 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3449 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003450 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003451 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003452 } else {
3453 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003454 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003455 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003456 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003457 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003458
Tony Wickhame0c33232016-02-08 11:37:04 -08003459 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003460 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3461 || mState == State.WIDGETS_SPRING_LOADED;
3462 }
3463
Michael Jurkad3ef3062010-11-23 16:23:58 -08003464 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003465 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003466 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003467 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003468 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003469 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003470 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3471 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003472 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003473 }
3474
Winson Chung4ac30062015-05-08 17:34:17 -07003475 /**
3476 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3477 * resumed.
3478 */
3479 private void tryAndUpdatePredictedApps() {
3480 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003481 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003482 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003483 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003484 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003485 }
3486 }
3487 }
3488
Michael Jurkab3e22d92011-10-31 15:58:33 -07003489 void lockAllApps() {
3490 // TODO
3491 }
3492
3493 void unlockAllApps() {
3494 // TODO
3495 }
3496
Winsonf768d932015-09-25 16:12:35 -07003497 public boolean launcherCallbacksProvidesSearch() {
3498 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3499 }
3500
Michael Jurkad7c28052012-04-27 15:43:36 -07003501 @Override
3502 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003503 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003504 final List<CharSequence> text = event.getText();
3505 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003506 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003507 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003508 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003509 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003510 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003511 } else if (mWorkspace != null) {
3512 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003513 } else {
3514 text.add(getString(R.string.all_apps_home_button_label));
3515 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003516 return result;
3517 }
3518
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003519 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003520 * If the activity is currently paused, signal that we need to run the passed Runnable
3521 * in onResume.
3522 *
3523 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003524 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3525 * wrong when we're not running, and if the activity comes back to what the configuration was
3526 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003527 *
3528 * Implementation of the method from LauncherModel.Callbacks.
3529 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003530 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003531 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003532 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003533 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003534 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003535 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003536 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003537 }
3538 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003539 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003540 return true;
3541 } else {
3542 return false;
3543 }
3544 }
3545
Michael Jurkac402cd92013-05-20 15:49:32 +02003546 private boolean waitUntilResume(Runnable run) {
3547 return waitUntilResume(run, false);
3548 }
3549
Michael Jurka1e2f4652013-07-08 18:03:46 -07003550 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003551 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003552 }
3553
Michael Jurka7607c2f2013-04-03 14:33:19 -07003554 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003555 * If the activity is currently paused, signal that we need to re-run the loader
3556 * in onResume.
3557 *
3558 * This needs to be called from incoming places where resources might have been loaded
3559 * while we are paused. That is becaues the Configuration might be wrong
3560 * when we're not running, and if it comes back to what it was when we
3561 * were paused, we are not restarted.
3562 *
3563 * Implementation of the method from LauncherModel.Callbacks.
3564 *
3565 * @return true if we are currently paused. The caller might be able to
3566 * skip some work in that case since we will come back again.
3567 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003568 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003569 public boolean setLoadOnResume() {
3570 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003571 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003572 mOnResumeNeedsLoad = true;
3573 return true;
3574 } else {
3575 return false;
3576 }
3577 }
3578
3579 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003580 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003581 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003582 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003583 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003584 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003585 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003586 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003587 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003588 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003589 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003590
Joe Onorato9c1289c2009-08-17 11:03:03 -04003591 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003592 * Clear any pending bind callbacks. This is called when is loader is planning to
3593 * perform a full rebind from scratch.
3594 */
3595 @Override
3596 public void clearPendingBinds() {
3597 mBindOnResumeCallbacks.clear();
3598 if (mPendingExecutor != null) {
3599 mPendingExecutor.markCompleted();
3600 mPendingExecutor = null;
3601 }
3602 }
3603
3604 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003605 * Refreshes the shortcuts shown on the workspace.
3606 *
3607 * Implementation of the method from LauncherModel.Callbacks.
3608 */
3609 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003610 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003611
Winson Chungd64d1762013-08-20 14:37:16 -07003612 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003613 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003614 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003615
Michael Jurka05bf644e2011-11-30 20:28:53 -08003616 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003617 if (mHotseat != null) {
3618 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003619 }
3620 }
3621
Adam Cohendcd297f2013-06-18 13:13:40 -07003622 @Override
3623 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003624 // Make sure the first screen is always at the start.
3625 if (orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
3626 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3627 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3628 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Adam Cohen5084cba2013-09-03 12:01:16 -07003629 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003630 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003631
3632 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003633 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003634 if (hasCustomContentToLeft()) {
3635 mWorkspace.createCustomContentContainer();
3636 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003637 }
Winson Chung64359a52013-07-08 17:17:08 -07003638 }
3639
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003640 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003641 int count = orderedScreenIds.size();
3642 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003643 long screenId = orderedScreenIds.get(i);
3644 if (screenId != Workspace.FIRST_SCREEN_ID) {
3645 // No need to bind the first screen, as its always bound.
3646 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3647 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003648 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003649 }
3650
Winson Chungd64d1762013-08-20 14:37:16 -07003651 public void bindAppsAdded(final ArrayList<Long> newScreens,
3652 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003653 final ArrayList<ItemInfo> addAnimated,
3654 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003655 Runnable r = new Runnable() {
3656 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003657 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003658 }
3659 };
3660 if (waitUntilResume(r)) {
3661 return;
3662 }
3663
Winson Chungd64d1762013-08-20 14:37:16 -07003664 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003665 if (newScreens != null) {
3666 bindAddScreens(newScreens);
3667 }
Winson Chungd64d1762013-08-20 14:37:16 -07003668
3669 // We add the items without animation on non-visible pages, and with
3670 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003671 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003672 bindItems(addNotAnimated, 0,
3673 addNotAnimated.size(), false);
3674 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003675 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003676 bindItems(addAnimated, 0,
3677 addAnimated.size(), true);
3678 }
Winson Chungc58497e2013-09-03 17:48:37 -07003679
Winson Chung87412982013-10-03 18:34:14 -07003680 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003681 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003682
Winson Chungb745afb2015-03-02 11:51:23 -08003683 if (addedApps != null && mAppsView != null) {
3684 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003685 }
Winson Chungd64d1762013-08-20 14:37:16 -07003686 }
3687
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003688 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003689 * Bind the items start-end from the list.
3690 *
3691 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003692 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003693 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003694 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3695 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003696 Runnable r = new Runnable() {
3697 public void run() {
3698 bindItems(shortcuts, start, end, forceAnimateIcons);
3699 }
3700 };
3701 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003702 return;
3703 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003704
Winson Chungf0c6ae02012-03-21 16:10:31 -07003705 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003706 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3707 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003708 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003709 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003710 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003711 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003712 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003713
3714 // Short circuit if we are loading dock items for a configuration which has no dock
3715 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3716 mHotseat == null) {
3717 continue;
3718 }
3719
Sunny Goyal639e9062015-08-19 19:17:06 -07003720 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003721 switch (item.itemType) {
3722 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3723 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003724 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003725 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003726 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003727 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003728 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003729 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003730 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003731 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003732 default:
3733 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003734 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003735
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003736 /*
3737 * Remove colliding items.
3738 */
3739 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3740 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3741 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3742 View v = cl.getChildAt(item.cellX, item.cellY);
3743 Object tag = v.getTag();
3744 String desc = "Collision while binding workspace item: " + item
3745 + ". Collides with " + tag;
3746 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3747 throw (new RuntimeException(desc));
3748 } else {
3749 Log.d(TAG, desc);
3750 LauncherModel.deleteItemFromDatabase(this, item);
3751 continue;
3752 }
3753 }
3754 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003755 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3756 item.cellY, 1, 1);
3757 if (animateIcons) {
3758 // Animate all the applications up now
3759 view.setAlpha(0f);
3760 view.setScaleX(0f);
3761 view.setScaleY(0f);
3762 bounceAnims.add(createNewAppBounceAnimation(view, i));
3763 newShortcutsScreenId = item.screenId;
3764 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003765 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003766
Winson Chung997a9232013-07-24 15:33:46 -07003767 if (animateIcons) {
3768 // Animate to the correct page
3769 if (newShortcutsScreenId > -1) {
3770 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003771 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003772 final Runnable startBounceAnimRunnable = new Runnable() {
3773 public void run() {
3774 anim.playTogether(bounceAnims);
3775 anim.start();
3776 }
3777 };
Winson Chung997a9232013-07-24 15:33:46 -07003778 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003779 // We post the animation slightly delayed to prevent slowdowns
3780 // when we are loading right after we return to launcher.
3781 mWorkspace.postDelayed(new Runnable() {
3782 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003783 if (mWorkspace != null) {
3784 mWorkspace.snapToPage(newScreenIndex);
3785 mWorkspace.postDelayed(startBounceAnimRunnable,
3786 NEW_APPS_ANIMATION_DELAY);
3787 }
Winson Chung94d67682013-09-25 16:29:40 -07003788 }
3789 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003790 } else {
3791 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003792 }
3793 }
Winson Chung64359a52013-07-08 17:17:08 -07003794 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003795 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003796 }
3797
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003798 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3799 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3800 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003801 view.updateAppWidget(null);
3802 view.setOnClickListener(this);
3803 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003804 mWorkspace.requestLayout();
3805 }
3806
Joe Onorato9c1289c2009-08-17 11:03:03 -04003807 /**
3808 * Add the views for a widget to the workspace.
3809 *
3810 * Implementation of the method from LauncherModel.Callbacks.
3811 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003812 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003813 Runnable r = new Runnable() {
3814 public void run() {
3815 bindAppWidget(item);
3816 }
3817 };
3818 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003819 return;
3820 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003821
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003822 if (mIsSafeModeEnabled) {
3823 bindSafeModeWidget(item);
3824 return;
3825 }
3826
Daniel Sandler843e8602010-06-07 14:59:01 -04003827 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3828 if (DEBUG_WIDGETS) {
3829 Log.d(TAG, "bindAppWidget: " + item);
3830 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003831
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003832 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003833
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003834 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3835 // If the provider is not ready, bind as a pending widget.
3836 appWidgetInfo = null;
3837 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3838 // The widget id is not valid. Try to find the widget based on the provider info.
3839 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3840 } else {
3841 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3842 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003843
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003844 // If the provider is ready, but the width is not yet restored, try to restore it.
3845 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3846 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003847 if (appWidgetInfo == null) {
3848 if (DEBUG_WIDGETS) {
3849 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3850 + " belongs to component " + item.providerName
3851 + ", as the povider is null");
3852 }
3853 LauncherModel.deleteItemFromDatabase(this, item);
3854 return;
3855 }
Sunny Goyalff572272014-07-23 13:58:07 -07003856
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003857 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003858 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003859 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3860 // Id has not been allocated yet. Allocate a new id.
3861 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3862 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003863
Sunny Goyald478c832016-04-01 12:04:16 -07003864 // Also try to bind the widget. If the bind fails, the user will be shown
3865 // a click to setup UI, which will ask for the bind permission.
3866 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3867 pendingInfo.spanX = item.spanX;
3868 pendingInfo.spanY = item.spanY;
3869 pendingInfo.minSpanX = item.minSpanX;
3870 pendingInfo.minSpanY = item.minSpanY;
3871 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
3872 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3873 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003874
Sunny Goyald478c832016-04-01 12:04:16 -07003875 // Bind succeeded
3876 if (success) {
3877 // If the widget has a configure activity, it is still needs to set it up,
3878 // otherwise the widget is ready to go.
3879 item.restoreStatus = (appWidgetInfo.configure == null)
3880 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3881 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003882 }
Sunny Goyald478c832016-04-01 12:04:16 -07003883
3884 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003885 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003886 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003887 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003888 // The widget was marked as UI not ready, but there is no configure activity to
3889 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003890 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3891 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003892 }
Sunny Goyalff572272014-07-23 13:58:07 -07003893 }
3894
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003895 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003896 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003897 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3898 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003899 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003900
Sunny Goyal56c73602015-09-25 12:55:01 -07003901 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003902 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003903 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003904 deleteWidgetInfo(item);
3905 return;
3906 }
3907
Sunny Goyal233ee962015-08-03 13:05:01 -07003908 item.minSpanX = appWidgetInfo.minSpanX;
3909 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003910 addAppWidgetToWorkspace(
3911 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3912 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003913 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003914 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003915 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003916 view.updateAppWidget(null);
3917 view.setOnClickListener(this);
3918 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003919 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003920 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003921
Daniel Sandler843e8602010-06-07 14:59:01 -04003922 if (DEBUG_WIDGETS) {
3923 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3924 + (SystemClock.uptimeMillis()-start) + "ms");
3925 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003926 }
3927
Sunny Goyalff572272014-07-23 13:58:07 -07003928 /**
3929 * Restores a pending widget.
3930 *
3931 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003932 */
Sunny Goyald478c832016-04-01 12:04:16 -07003933 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003934 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3935 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3936 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003937 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003938 }
3939
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003940 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003941 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003942
3943 mWorkspace.reinflateWidgetsIfNecessary();
3944 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003945 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003946 }
3947
Adam Cohen1462de32012-07-24 22:34:36 -07003948 public void onPageBoundSynchronously(int page) {
3949 mSynchronouslyBoundPages.add(page);
3950 }
3951
Sunny Goyal527c7d32015-08-28 15:19:36 -07003952 @Override
3953 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3954 if (mPendingExecutor != null) {
3955 mPendingExecutor.markCompleted();
3956 }
3957 mPendingExecutor = executor;
3958 executor.attachTo(this);
3959 }
3960
3961 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3962 if (mPendingExecutor == executor) {
3963 mPendingExecutor = null;
3964 }
3965 }
3966
Joe Onorato9c1289c2009-08-17 11:03:03 -04003967 /**
3968 * Callback saying that there aren't any more items to bind.
3969 *
3970 * Implementation of the method from LauncherModel.Callbacks.
3971 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003972 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003973 Runnable r = new Runnable() {
3974 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003975 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003976 }
3977 };
3978 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003979 return;
3980 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003981 if (mSavedState != null) {
3982 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003983 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003984 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003985
Joe Onorato9c1289c2009-08-17 11:03:03 -04003986 mSavedState = null;
3987 }
3988
Adam Cohen1462de32012-07-24 22:34:36 -07003989 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003990
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003991 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003992
3993 // If we received the result of any pending adds while the loader was running (e.g. the
3994 // widget configuration forced an orientation change), process them now.
3995 if (sPendingAddItem != null) {
3996 final long screenId = completeAdd(sPendingAddItem);
3997
3998 // TODO: this moves the user to the page where the pending item was added. Ideally,
3999 // the screen would be guaranteed to exist after bind, and the page would be set through
4000 // the workspace restore process.
4001 mWorkspace.post(new Runnable() {
4002 @Override
4003 public void run() {
4004 mWorkspace.snapToScreenId(screenId);
4005 }
4006 });
4007 sPendingAddItem = null;
4008 }
4009
Sunny Goyal756adbc2015-04-16 15:20:51 -07004010 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004011
4012 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004013 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004014 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004015 }
4016
Winson Chunga0b7e862013-09-05 16:03:15 -07004017 public boolean isAllAppsButtonRank(int rank) {
4018 if (mHotseat != null) {
4019 return mHotseat.isAllAppsButtonRank(rank);
4020 }
4021 return false;
4022 }
4023
Winson Chunga2413752012-04-03 14:22:34 -07004024 private boolean canRunNewAppsAnimation() {
4025 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004026 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4027 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004028 }
4029
Winson Chungc9168342013-06-26 14:54:55 -07004030 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004031 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004032 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4033 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004034 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004035 return bounceAnim;
4036 }
4037
Adam Cohen27772732013-11-11 14:00:56 +00004038 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004039 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004040 }
4041
Tony Wickham3a3517f2015-10-06 11:27:04 -07004042 /** Returns the search bar bounds in pixels. */
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004043 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004044 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004045 }
4046
Tony Wickham55616cd2015-09-23 14:55:17 -07004047 public int getSearchBarHeight() {
4048 if (mLauncherCallbacks != null) {
4049 return mLauncherCallbacks.getSearchBarHeight();
4050 }
4051 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4052 }
4053
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004054 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004055 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4056 * multiple calls to bind the same list.)
4057 */
4058 @Thunk ArrayList<AppInfo> mTmpAppsList;
4059 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4060 public void run() {
4061 bindAllApplications(mTmpAppsList);
4062 mTmpAppsList = null;
4063 }
4064 };
4065
4066 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004067 * Add the icons for all apps.
4068 *
4069 * Implementation of the method from LauncherModel.Callbacks.
4070 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004071 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004072 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4073 mTmpAppsList = apps;
4074 return;
4075 }
4076
Winson Chungb745afb2015-03-02 11:51:23 -08004077 if (mAppsView != null) {
4078 mAppsView.setApps(apps);
4079 }
Adam Cohen9211d422014-10-07 18:14:53 -07004080 if (mLauncherCallbacks != null) {
4081 mLauncherCallbacks.bindAllApplications(apps);
4082 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004083 }
4084
4085 /**
4086 * A package was updated.
4087 *
4088 * Implementation of the method from LauncherModel.Callbacks.
4089 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004090 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004091 Runnable r = new Runnable() {
4092 public void run() {
4093 bindAppsUpdated(apps);
4094 }
4095 };
4096 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004097 return;
4098 }
4099
Winson Chungb745afb2015-03-02 11:51:23 -08004100 if (mAppsView != null) {
4101 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004102 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004103 }
4104
Sunny Goyal4390ace2014-10-13 11:33:11 -07004105 @Override
4106 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4107 Runnable r = new Runnable() {
4108 public void run() {
4109 bindWidgetsRestored(widgets);
4110 }
4111 };
4112 if (waitUntilResume(r)) {
4113 return;
4114 }
4115 mWorkspace.widgetsRestored(widgets);
4116 }
4117
Joe Onorato9c1289c2009-08-17 11:03:03 -04004118 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004119 * Some shortcuts were updated in the background.
4120 *
4121 * Implementation of the method from LauncherModel.Callbacks.
4122 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004123 @Override
4124 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4125 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004126 Runnable r = new Runnable() {
4127 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004128 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004129 }
4130 };
4131 if (waitUntilResume(r)) {
4132 return;
4133 }
4134
Sunny Goyal4390ace2014-10-13 11:33:11 -07004135 if (!updated.isEmpty()) {
4136 mWorkspace.updateShortcuts(updated);
4137 }
4138
4139 if (!removed.isEmpty()) {
4140 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4141 for (ShortcutInfo si : removed) {
4142 removedComponents.add(si.getTargetComponent());
4143 }
4144 mWorkspace.removeItemsByComponentName(removedComponents, user);
4145 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004146 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004147 }
4148 }
4149
4150 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004151 * Update the state of a package, typically related to install state.
4152 *
4153 * Implementation of the method from LauncherModel.Callbacks.
4154 */
Sunny Goyale755d462014-07-22 13:48:29 -07004155 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004156 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4157 Runnable r = new Runnable() {
4158 public void run() {
4159 bindRestoreItemsChange(updates);
4160 }
4161 };
4162 if (waitUntilResume(r)) {
4163 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004164 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004165
Sunny Goyal756adbc2015-04-16 15:20:51 -07004166 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004167 }
4168
4169 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004170 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004171 * in addition to specific applications to remove, the reason being that
4172 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004173 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004174 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004175 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004176 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004177 public void bindWorkspaceComponentsRemoved(
4178 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4179 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004180 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004181 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004182 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004183 }
Winson Chungd64d1762013-08-20 14:37:16 -07004184 };
4185 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004186 return;
4187 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004188 if (!packageNames.isEmpty()) {
4189 mWorkspace.removeItemsByPackageName(packageNames, user);
4190 }
4191 if (!components.isEmpty()) {
4192 mWorkspace.removeItemsByComponentName(components, user);
4193 }
4194 // Notify the drag controller
4195 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004196
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004197 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004198
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004199 @Override
4200 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4201 Runnable r = new Runnable() {
4202 public void run() {
4203 bindAppInfosRemoved(appInfos);
4204 }
4205 };
4206 if (waitUntilResume(r)) {
4207 return;
Joe Onorato36115782010-06-17 13:28:48 -04004208 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004209
Winson Chungdf95eb12013-10-16 14:57:07 -07004210 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004211 if (mAppsView != null) {
4212 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004213 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004214 }
Joe Onoratobe386092009-11-17 17:32:16 -08004215
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004216 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004217 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004218 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004219 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004220 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004221
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004222 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004223 public void bindWidgetsModel(WidgetsModel model) {
4224 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004225 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004226 return;
4227 }
4228
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004229 if (mWidgetsView != null && model != null) {
4230 mWidgetsView.addWidgets(model);
4231 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004232 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004233 }
4234
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004235 @Override
4236 public void notifyWidgetProvidersChanged() {
4237 if (mWorkspace.getState().shouldUpdateWidget) {
4238 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4239 }
4240 }
4241
Winson Chung400438b2011-01-16 17:53:48 -08004242 private int mapConfigurationOriActivityInfoOri(int configOri) {
4243 final Display d = getWindowManager().getDefaultDisplay();
4244 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4245 switch (d.getRotation()) {
4246 case Surface.ROTATION_0:
4247 case Surface.ROTATION_180:
4248 // We are currently in the same basic orientation as the natural orientation
4249 naturalOri = configOri;
4250 break;
4251 case Surface.ROTATION_90:
4252 case Surface.ROTATION_270:
4253 // We are currently in the other basic orientation to the natural orientation
4254 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4255 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4256 break;
4257 }
4258
4259 int[] oriMap = {
4260 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4261 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4262 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4263 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4264 };
4265 // Since the map starts at portrait, we need to offset if this device's natural orientation
4266 // is landscape.
4267 int indexOffset = 0;
4268 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4269 indexOffset = 1;
4270 }
4271 return oriMap[(d.getRotation() + indexOffset) % 4];
4272 }
Adam Cohen446e9402011-09-15 18:21:21 -07004273
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004274 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004275 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004276 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004277 if (Utilities.ATLEAST_JB_MR2) {
4278 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4279 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004280 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4281 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004282 }
Winson Chung4b919f82012-05-01 10:44:08 -07004283 }
4284 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004285
Winson Chung4b919f82012-05-01 10:44:08 -07004286 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004287 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004288 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004289 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004290 } else {
4291 mHandler.postDelayed(new Runnable() {
4292 public void run() {
4293 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4294 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004295 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004296 }
Winson Chung4b919f82012-05-01 10:44:08 -07004297 }
Winson Chung400438b2011-01-16 17:53:48 -08004298 }
4299
Adam Cohenea90f832014-05-21 15:03:34 -07004300 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004301 * To be overridden by subclasses to indicate that there is an activity to launch
4302 * before showing the standard launcher experience.
4303 */
4304 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004305 if (mLauncherCallbacks != null) {
4306 return mLauncherCallbacks.hasFirstRunActivity();
4307 }
Adam Cohen432609a2014-03-13 17:03:22 -07004308 return false;
4309 }
4310
4311 /**
4312 * To be overridden by subclasses to launch any first run activity
4313 */
4314 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004315 if (mLauncherCallbacks != null) {
4316 return mLauncherCallbacks.getFirstRunActivity();
4317 }
Adam Cohen432609a2014-03-13 17:03:22 -07004318 return null;
4319 }
4320
Winson Chunga6945242014-01-08 14:04:34 -08004321 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004322 return !ActivityManager.isRunningInTestHarness() &&
4323 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004324 }
4325
Adam Cohen4a9423d2014-03-28 14:22:31 -07004326 protected boolean hasRunFirstRunActivity() {
4327 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4328 }
4329
Adam Cohen432609a2014-03-13 17:03:22 -07004330 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004331 if (shouldRunFirstRunActivity() &&
4332 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004333 Intent firstRunIntent = getFirstRunActivity();
4334 if (firstRunIntent != null) {
4335 startActivity(firstRunIntent);
4336 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004337 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004338 }
4339 }
Adam Cohen432609a2014-03-13 17:03:22 -07004340 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004341 }
4342
4343 private void markFirstRunActivityShown() {
4344 SharedPreferences.Editor editor = mSharedPrefs.edit();
4345 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4346 editor.apply();
4347 }
4348
Adam Cohen432609a2014-03-13 17:03:22 -07004349 /**
4350 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4351 * screen that must be displayed and dismissed.
4352 */
4353 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004354 if (mLauncherCallbacks != null) {
4355 return mLauncherCallbacks.hasDismissableIntroScreen();
4356 }
Adam Cohen432609a2014-03-13 17:03:22 -07004357 return false;
4358 }
4359
4360 /**
4361 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4362 */
4363 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004364 if (mLauncherCallbacks != null) {
4365 return mLauncherCallbacks.getIntroScreen();
4366 }
Adam Cohen432609a2014-03-13 17:03:22 -07004367 return null;
4368 }
4369
4370 /**
4371 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4372 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4373 */
4374 private boolean shouldShowIntroScreen() {
4375 return hasDismissableIntroScreen() &&
4376 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4377 }
4378
4379 protected void showIntroScreen() {
4380 View introScreen = getIntroScreen();
4381 changeWallpaperVisiblity(false);
4382 if (introScreen != null) {
4383 mDragLayer.showOverlayView(introScreen);
4384 }
4385 }
4386
4387 public void dismissIntroScreen() {
4388 markIntroScreenDismissed();
4389 if (showFirstRunActivity()) {
4390 // We delay hiding the intro view until the first run activity is showing. This
4391 // avoids a blip.
4392 mWorkspace.postDelayed(new Runnable() {
4393 @Override
4394 public void run() {
4395 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004396 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004397 }
4398 }, ACTIVITY_START_DELAY);
4399 } else {
4400 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004401 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004402 }
4403 changeWallpaperVisiblity(true);
4404 }
4405
4406 private void markIntroScreenDismissed() {
4407 SharedPreferences.Editor editor = mSharedPrefs.edit();
4408 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4409 editor.apply();
4410 }
4411
Adam Cohen091440a2015-03-18 14:16:05 -07004412 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004413 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4414 // on the device, then we always show the first run cling experience (or if there is no
4415 // launcher2). Otherwise, we prompt the user upon started for migration
4416 LauncherClings launcherClings = new LauncherClings(this);
4417 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004418 mClings = launcherClings;
Sunny Goyalded0fdb2016-05-23 15:55:41 -07004419 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004420 }
4421 }
4422
Winson Chung5ffd51d2015-06-25 11:36:50 -07004423 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004424 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004425 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004426 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004427 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004428 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004429 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4430 if (userHandle != null) {
4431 user = UserHandleCompat.fromUser(userHandle);
4432 }
4433 }
4434 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004435 }
4436
4437 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004438 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004439 if (user == null) {
4440 user = UserHandleCompat.myUserHandle();
4441 }
4442
4443 // Called from search suggestion, add the profile extra to the intent to ensure that we
4444 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004445 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004446 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004447 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004448 return null;
4449 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004450 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004451 }
4452
Winson Chung5ffd51d2015-06-25 11:36:50 -07004453 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004454 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4455 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004456 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004457 UserHandleCompat.myUserHandle());
4458 }
4459
Winson Chung5ffd51d2015-06-25 11:36:50 -07004460 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004461 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4462 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004463 mWorkspace.onExternalDragStartedWithItem(dragView);
4464 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004465 }
4466
Winson Chung5ffd51d2015-06-25 11:36:50 -07004467 protected void moveWorkspaceToDefaultScreen() {
4468 mWorkspace.moveToDefaultScreen(false);
4469 }
4470
Winson Chung80baf5a2010-08-09 16:03:15 -07004471 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004472 * Returns a FastBitmapDrawable with the icon, accurately sized.
4473 */
4474 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4475 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4476 d.setFilterBitmap(true);
4477 resizeIconDrawable(d);
4478 return d;
4479 }
4480
4481 /**
4482 * Resizes an icon drawable to the correct icon size.
4483 */
Winson5fbe0742015-09-30 15:33:00 -07004484 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004485 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004486 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004487 }
4488
4489 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004490 * Prints out out state for debugging.
4491 */
4492 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004493 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004494 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004495 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4496 Log.d(TAG, "mRestoring=" + mRestoring);
4497 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004498 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004499 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004500
Daniel Sandler325dc232013-06-05 22:57:57 -04004501 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004502 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004503
4504 @Override
4505 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4506 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004507 // Dump workspace
4508 writer.println(prefix + "Workspace Items");
4509 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4510 writer.println(prefix + " Homescreen " + i);
4511
4512 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4513 for (int j = 0; j < layout.getChildCount(); j++) {
4514 Object tag = layout.getChildAt(j).getTag();
4515 if (tag != null) {
4516 writer.println(prefix + " " + tag.toString());
4517 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004518 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004519 }
Sunny Goyala1365452015-10-01 15:46:24 -07004520
4521 writer.println(prefix + " Hotseat");
4522 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4523 for (int j = 0; j < layout.getChildCount(); j++) {
4524 Object tag = layout.getChildAt(j).getTag();
4525 if (tag != null) {
4526 writer.println(prefix + " " + tag.toString());
4527 }
4528 }
4529
Sunny Goyal713edfc2016-05-06 09:58:34 -07004530 try {
4531 FileLog.flushAll(writer);
4532 } catch (Exception e) {
4533 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004534 }
4535
Adam Cohen9211d422014-10-07 18:14:53 -07004536 if (mLauncherCallbacks != null) {
4537 mLauncherCallbacks.dump(prefix, fd, writer, args);
4538 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004539 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004540
Adam Cohen59400422014-03-05 18:07:04 -08004541 public static CustomAppWidget getCustomAppWidget(String name) {
4542 return sCustomAppWidgets.get(name);
4543 }
4544
4545 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4546 return sCustomAppWidgets;
4547 }
4548
Sunny Goyal29538332016-02-18 09:10:19 -08004549 public static List<View> getFolderContents(View icon) {
4550 if (icon instanceof FolderIcon) {
4551 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4552 } else {
4553 return Collections.EMPTY_LIST;
4554 }
4555 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004556
4557 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4558
4559 @Override
4560 public void onSharedPreferenceChanged(
4561 SharedPreferences sharedPreferences, String key) {
4562 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4563 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4564 if (!waitUntilResume(this, true)) {
4565 run();
4566 }
4567 }
4568 }
4569
4570 @Override
4571 public void run() {
4572 setOrientation();
4573 }
4574 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004575}