blob: 0a8e1e65f12f55a2ecd55227879ecd7c7db78880 [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;
Winson Chunge43a1e72014-01-15 10:33:02 -080048import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070050import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070052import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070053import android.graphics.Bitmap;
54import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070055import android.graphics.Color;
Tony Wickhama3c74d12015-10-23 11:43:47 -070056import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070057import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070059import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070060import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020062import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020064import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080065import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070066import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070067import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040068import android.os.SystemClock;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070069import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.Selection;
71import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070072import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070074import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.MotionEvent;
80import android.view.Surface;
81import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070082import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080083import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.ViewGroup;
85import android.view.ViewTreeObserver;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080087import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070088import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070089import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080093import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070095
Sunny Goyal651077b2014-06-30 14:15:31 -070096import com.android.launcher3.DropTarget.DragObject;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070097import com.android.launcher3.allapps.AllAppsContainerView;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080098import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -070099import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100100import com.android.launcher3.compat.LauncherActivityInfoCompat;
101import com.android.launcher3.compat.LauncherAppsCompat;
102import com.android.launcher3.compat.UserHandleCompat;
103import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800104import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700105import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700106import com.android.launcher3.dragndrop.DragController;
107import com.android.launcher3.dragndrop.DragLayer;
108import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -0700109import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000110import com.android.launcher3.folder.Folder;
111import com.android.launcher3.folder.FolderIcon;
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800112import com.android.launcher3.logging.LoggerUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700113import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700114import com.android.launcher3.model.WidgetsModel;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800115import com.android.launcher3.userevent.nano.LauncherLogProto;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700116import com.android.launcher3.util.ComponentKey;
Sunny Goyal713edfc2016-05-06 09:58:34 -0700117import com.android.launcher3.logging.FileLog;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700118import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal322d5562015-06-25 19:35:49 -0700119import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700120import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700121import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700122import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700123import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700124import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700125
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700126import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700127import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700128import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700129import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800130import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700131import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700132import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700133import java.util.List;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800134import java.util.Locale;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700135
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136/**
137 * Default launcher application.
138 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100139public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700140 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
141 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700142 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700143 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700144 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
Daniel Sandlerf061f822013-06-27 13:59:36 -0400146 static final boolean PROFILE_STARTUP = false;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700147 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700148 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400149 static final boolean DEBUG_RESUME_TIME = false;
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700150 static final boolean DEBUG_LOGGING = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700151
Dan Sandlerd5024042014-01-09 15:01:33 -0500152 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
153
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700155 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700156 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700157 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158
Michael Jurka8b805b12012-04-18 14:23:14 -0700159 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700160 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700161
Sunny Goyal28c6b962015-10-12 11:42:05 -0700162 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
163
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700164 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
165 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
166 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
167
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700168 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
169
Mathew Inwood876a8462013-06-14 14:12:41 +0100170 /**
171 * IntentStarter uses request codes starting with this. This must be greater than all activity
172 * request codes used internally.
173 */
174 protected static final int REQUEST_LAST = 100;
175
Michael Jurka0a457bf2012-11-19 14:05:05 -0800176 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700177 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800178 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
Winson Chung2672ff92012-05-04 16:22:30 -0700180 // The Intent extra that defines whether to ignore the launch animation
181 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400182 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 // Type: int
185 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700186 // Type: int
187 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700188 // Type: Content Values / parcelable
189 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700190 // Type: parcelable
191 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000192 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800193 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194
Adam Cohen432609a2014-03-13 17:03:22 -0700195 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800196 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
197
Sunny Goyal594d76d2014-11-06 10:12:54 -0800198 private static final String QSB_WIDGET_ID = "qsb_widget_id";
199 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
200
Winson Chunga6945242014-01-08 14:04:34 -0800201 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
202
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700203 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700204 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
205 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700206
Adam Cohen091440a2015-03-18 14:16:05 -0700207 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700208 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700209
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700210 private boolean mIsSafeModeEnabled;
211
Joe Onorato9c1289c2009-08-17 11:03:03 -0400212 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800213 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700214 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700215 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
Winson Chunga2413752012-04-03 14:22:34 -0700217 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700218 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
219 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700220 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700221
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800222 private final BroadcastReceiver mCloseSystemDialogsReceiver
223 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800224
Adam Cohen091440a2015-03-18 14:16:05 -0700225 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700226 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800227 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700228 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800229 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700230
231 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700232
Sunny Goyalffe83f12014-08-14 17:39:34 -0700233 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700234 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700235
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700236 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800237 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800238 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700239
Michael Jurka0280c3b2010-09-17 15:00:07 -0700240 private int[] mTmpAddItemCellCoordinates = new int[2];
241
Sunny Goyal316490e2015-06-02 09:38:28 -0700242 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800243 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700244
Michael Jurka838a4ca2011-02-07 13:33:06 -0800245 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700246 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700247
Winson Chungc51db6a2011-10-05 11:44:49 -0700248 private SearchDropTargetBar mSearchDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -0700249 private AppInfoDropTargetBar mAppInfoDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700250
251 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700252 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700253
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700254 // Main container view and the model for the widget tray screen.
255 @Thunk WidgetsContainerView mWidgetsView;
256 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700257
Sunny Goyal594d76d2014-11-06 10:12:54 -0800258 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700261 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
262 // scroll issues (because the workspace may not have been measured yet) and extra work.
263 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
264 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265
266 private SpannableStringBuilder mDefaultKeySsb = null;
267
Adam Cohen091440a2015-03-18 14:16:05 -0700268 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400269
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800270 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 private boolean mRestoring;
272 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700273 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274
Michael Jurka1e2f4652013-07-08 18:03:46 -0700275 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700276 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700277 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700278
Joe Onorato9c1289c2009-08-17 11:03:03 -0400279 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800280 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700281 private ExtractedColors mExtractedColors;
Adam Cohen091440a2015-03-18 14:16:05 -0700282 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800283 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700284 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800285 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400286
Sunny Goyal639e9062015-08-19 19:17:06 -0700287 private LauncherClings mClings;
288
Adam Cohen61f560d2013-09-30 15:58:20 -0700289 private View.OnTouchListener mHapticFeedbackTouchListener;
290
Adam Cohended9f8d2010-11-03 13:25:16 -0700291 // Related to the auto-advancing of widgets
292 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700293 private static final int ADVANCE_INTERVAL = 20000;
294 private static final int ADVANCE_STAGGER = 250;
295
296 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700297 private long mAutoAdvanceSentTime;
298 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700299 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700300
Winson Chung400438b2011-01-16 17:53:48 -0800301 // Determines how long to wait after a rotation before restoring the screen orientation to
302 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700303 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800304
Adam Cohen091440a2015-03-18 14:16:05 -0700305 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700306
Adam Cohen1462de32012-07-24 22:34:36 -0700307 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700308 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700309
Winson Chung46353de2012-02-16 14:05:10 -0800310 // We only want to get the SharedPreferences once since it does an FS stat each time we get
311 // it from the context.
312 private SharedPreferences mSharedPrefs;
313
Winson Chungf0c6ae02012-03-21 16:10:31 -0700314 // Holds the page that we need to animate to, and the icon views that we need to animate up
315 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700316 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700317 private Bitmap mFolderIconBitmap;
318 private Canvas mFolderIconCanvas;
319 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700320
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700321 private DeviceProfile mDeviceProfile;
322
Adam Cohen4b66bf32015-09-18 12:15:19 -0700323 private boolean mMoveToDefaultScreenFromNewIntent;
324
Winson Chung13eb5272015-05-11 16:30:13 -0700325 // This is set to the view that launched the activity that navigated the user away from
326 // launcher. Since there is no callback for when the activity has finished launching, enable
327 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800328 private BubbleTextView mWaitingForResume;
329
Adam Cohen59400422014-03-05 18:07:04 -0800330 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
331 new HashMap<String, CustomAppWidget>();
332
Adam Cohen59400422014-03-05 18:07:04 -0800333 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700334 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
335 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800336 }
337 }
338
Adam Cohen091440a2015-03-18 14:16:05 -0700339 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700340 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700341 if (mWorkspace != null) {
342 mWorkspace.buildPageHardwareLayers();
343 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700344 }
345 };
346
Adam Cohendb364c32014-05-20 14:23:40 -0700347 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800348
Adam Cohen091440a2015-03-18 14:16:05 -0700349 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800350 int requestCode;
351 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700352 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700353 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800354 int cellX;
355 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700356 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800357 }
358
Hyunyoung Songaa953652016-04-19 18:30:24 -0700359 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800360
Adam Cohenf9c184a2016-01-15 16:47:43 -0800361 public FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700362 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400363
364 @Thunk void setOrientation() {
365 if (mRotationEnabled) {
366 unlockScreenOrientation(true);
367 } else {
368 setRequestedOrientation(
369 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700370 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400371 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700372
Sunny Goyal745bad92016-05-02 10:54:12 -0700373 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700374
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 @Override
376 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700377 if (DEBUG_STRICT_MODE) {
378 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
379 .detectDiskReads()
380 .detectDiskWrites()
381 .detectNetwork() // or .detectAll() for all detectable problems
382 .penaltyLog()
383 .build());
384 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
385 .detectLeakedSqlLiteObjects()
386 .detectLeakedClosableObjects()
387 .penaltyLog()
388 .penaltyDeath()
389 .build());
390 }
391
Adam Cohen9211d422014-10-07 18:14:53 -0700392 if (mLauncherCallbacks != null) {
393 mLauncherCallbacks.preOnCreate();
394 }
395
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400397
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400398 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700399
Adam Cohen2e6da152015-05-06 11:42:25 -0700400 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700401 mDeviceProfile = getResources().getConfiguration().orientation
402 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700403 app.getInvariantDeviceProfile().landscapeProfile
404 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700405
Sunny Goyalf7258242015-10-19 16:59:07 -0700406 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700407 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800408 mModel = app.setLauncher(this);
409 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700410
Joe Onorato41a12d22009-10-31 18:30:00 -0400411 mDragController = new DragController(this);
Winson Chung006ee262015-08-03 14:40:11 -0700412 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700413
Sunny Goyalffe83f12014-08-14 17:39:34 -0700414 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700415
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700416 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
417 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700418
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700419 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
420 // this also ensures that any synchronous binding below doesn't re-trigger another
421 // LauncherModel load.
422 mPaused = false;
423
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800424 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200425 android.os.Debug.startMethodTracing(
426 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800427 }
428
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800429 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700430
Tony Wickhameef44322015-10-20 13:24:36 -0700431 app.getInvariantDeviceProfile().landscapeProfile.setSearchBarHeight(getSearchBarHeight());
432 app.getInvariantDeviceProfile().portraitProfile.setSearchBarHeight(getSearchBarHeight());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700434 mDeviceProfile.layout(this);
Tony Wickham827cef22016-03-17 15:39:39 -0700435 mExtractedColors = new ExtractedColors();
436 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437
Tony Wickhame2217252016-03-22 16:34:23 -0700438 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
439 .addAccessibilityStateChangeListener(this);
440
Joe Onorato7c312c12009-08-13 21:36:53 -0700441 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700442
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 mSavedState = savedInstanceState;
444 restoreState(mSavedState);
445
446 if (PROFILE_STARTUP) {
447 android.os.Debug.stopMethodTracing();
448 }
449
450 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700451 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700452 // If the user leaves launcher, then we should just load items asynchronously when
453 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700454 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700455 } else {
456 // We only load the page synchronously if the user rotates (or triggers a
457 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700458 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700459 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 }
461
462 // For handling default keys
463 mDefaultKeySsb = new SpannableStringBuilder();
464 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800465
466 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
467 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800468
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800469 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700470 // In case we are on a device with locked rotation, we should look at preferences to check
471 // if the user has specifically allowed rotation.
472 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700473 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700474 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
475 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700476 }
477
478 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
479 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400480 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700481
Adam Cohen9211d422014-10-07 18:14:53 -0700482 if (mLauncherCallbacks != null) {
483 mLauncherCallbacks.onCreate(savedInstanceState);
484 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700485
486 if (shouldShowIntroScreen()) {
487 showIntroScreen();
488 } else {
489 showFirstRunActivity();
490 showFirstRunClings();
491 }
Adam Cohen9211d422014-10-07 18:14:53 -0700492 }
493
Sunny Goyal7779d622015-06-11 16:18:39 -0700494 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700495 public void onExtractedColorsChanged() {
496 loadExtractedColorsAndColorItems();
497 }
498
499 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700500 // TODO: do this in pre-N as well, once the extraction part is complete.
501 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700502 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700503 mHotseat.updateColor(mExtractedColors, !mPaused);
Tony Wickham827cef22016-03-17 15:39:39 -0700504 }
505 }
506
Adam Cohen9211d422014-10-07 18:14:53 -0700507 private LauncherCallbacks mLauncherCallbacks;
508
509 public void onPostCreate(Bundle savedInstanceState) {
510 super.onPostCreate(savedInstanceState);
511 if (mLauncherCallbacks != null) {
512 mLauncherCallbacks.onPostCreate(savedInstanceState);
513 }
514 }
515
Sunny Goyal32554d12015-12-03 15:31:25 -0800516 /**
517 * Call this after onCreate to set or clear overlay.
518 */
519 public void setLauncherOverlay(LauncherOverlay overlay) {
520 if (overlay != null) {
521 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
522 }
523 mWorkspace.setLauncherOverlay(overlay);
524 }
525
Adam Cohen9211d422014-10-07 18:14:53 -0700526 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
527 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700528 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700529 private boolean mWorkspaceImportanceStored = false;
530 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700531 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800532 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700533 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
534
535 @Override
536 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700537 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700538 return;
539 }
540 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700541 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700542 if (mWorkspace != null) {
543 mWorkspaceImportanceForAccessibility =
544 mWorkspace.getImportantForAccessibility();
545 mWorkspace.setImportantForAccessibility(
546 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
547 mWorkspaceImportanceStored = true;
548 }
549 if (mHotseat != null) {
550 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
551 mHotseat.setImportantForAccessibility(
552 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
553 mHotseatImportanceStored = true;
554 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700555 }
556
557 @Override
558 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700559 if (mWorkspaceImportanceStored && mWorkspace != null) {
560 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
561 }
562 if (mHotseatImportanceStored && mHotseat != null) {
563 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
564 }
565 mWorkspaceImportanceStored = false;
566 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700567 }
568 });
Adam Cohen9211d422014-10-07 18:14:53 -0700569 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700570 }
571
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700572 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700573 public void onLauncherProviderChange() {
574 if (mLauncherCallbacks != null) {
575 mLauncherCallbacks.onLauncherProviderChange();
576 }
577 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700578
Adam Cohen9211d422014-10-07 18:14:53 -0700579 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700580 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700581 if (mLauncherCallbacks != null) {
582 return mLauncherCallbacks.hasCustomContentToLeft();
583 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700584 return false;
585 }
586
Dave Hawkeya8881582013-09-17 15:55:33 -0600587 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500588 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600589 * {@link #addToCustomContentPage}. This will only be invoked if
590 * {@link #hasCustomContentToLeft()} is {@code true}.
591 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500592 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700593 if (mLauncherCallbacks != null) {
594 mLauncherCallbacks.populateCustomContentContainer();
595 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600596 }
597
598 /**
599 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
600 * ensure the custom content page is added or removed if necessary.
601 */
602 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600603 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600604 // Not bound yet, wait for bindScreens to be called.
605 return;
606 }
607
608 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
609 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500610 mWorkspace.createCustomContentContainer();
611 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600612 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
613 mWorkspace.removeCustomContentPage();
614 }
615 }
616
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800617 /**
618 * Logger object is a singleton and does not have to be coupled with the foreground activity.
619 * Since most user event logging is done on the UI, the object is retrieved from the
620 * callback for convenience.
621 */
Hyunyoung Songaa953652016-04-19 18:30:24 -0700622 private UserEventDispatcher createUserEventDispatcher() {
623 return new UserEventDispatcher() {
624 @Override
625 public void dispatchUserEvent(LauncherLogProto.LauncherEvent ev, Intent intent) {
626 if (!DEBUG_LOGGING) {
627 return;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800628 }
Hyunyoung Songaa953652016-04-19 18:30:24 -0700629 Log.d("UserEvent", String.format(Locale.US,
630 "action:%s\nchild:%s\nparent:%s\nelapsed container %d ms session %d ms",
631 LoggerUtils.getActionStr(ev.action),
632 LoggerUtils.getTargetStr(ev.srcTarget[0]),
633 LoggerUtils.getTargetStr(ev.srcTarget[1]),
634 ev.elapsedContainerMillis,
635 ev.elapsedSessionMillis));
636 }
637 };
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800638 }
639
Hyunyoung Songaa953652016-04-19 18:30:24 -0700640 public UserEventDispatcher getUserEventDispatcher() {
641 if (mLauncherCallbacks != null) {
642 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
643 if (dispatcher != null) {
644 return dispatcher;
645 }
646 }
647
648 if (mUserEventDispatcher == null) {
649 mUserEventDispatcher = createUserEventDispatcher();
650 }
651 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800652 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100653
Hyunyoung Song3f471442015-04-08 19:01:34 -0700654 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700655 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
656 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700657 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700658 }
659
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000660 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700661 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
662 // This cast is safe as long as the id < 0x00FFFFFF
663 // Since we jail all the dynamically generated views, there should be no clashes
664 // with any other views.
665 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000666 }
667
668 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700669 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
670 * a configuration step, this allows the proper animations to run after other transitions.
671 */
Adam Cohendb364c32014-05-20 14:23:40 -0700672 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700673 long screenId = args.screenId;
674 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
675 // When the screen id represents an actual screen (as opposed to a rank) we make sure
676 // that the drop page actually exists.
677 screenId = ensurePendingDropLayoutExists(args.screenId);
678 }
Adam Cohendb364c32014-05-20 14:23:40 -0700679
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800680 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800681 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700682 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700683 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700684 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800685 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700686 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700687 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700688 case REQUEST_RECONFIGURE_APPWIDGET:
689 completeRestoreAppWidget(args.appWidgetId);
690 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800691 }
Michael Jurka27614d22012-04-02 06:40:22 -0700692 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
693 // if you turned the screen off and then back while in All Apps, Launcher would not
694 // return to the workspace. Clearing mAddInfo.container here fixes this issue
695 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700696 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800697 }
698
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800699 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700700 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700701 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700702 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700703 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800704 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700705
Adam Cohenad4e15c2013-10-17 16:21:35 -0700706 Runnable exitSpringLoaded = new Runnable() {
707 @Override
708 public void run() {
709 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
710 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
711 }
712 };
713
Michael Jurka8b805b12012-04-18 14:23:14 -0700714 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700715 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700716 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700717 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
718 if (resultCode == RESULT_CANCELED) {
719 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700720 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700721 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700722 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800723 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700724 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Winsona1f79d32015-08-05 14:00:45 -0700725
726 // When the user has granted permission to bind widgets, we should check to see if
727 // we can inflate the default search bar widget.
728 getOrCreateQsbBar();
Michael Jurka8b805b12012-04-18 14:23:14 -0700729 }
730 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200731 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
732 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700733 // User could have free-scrolled between pages before picking a wallpaper; make sure
734 // we move to the closest one now.
735 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700736 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200737 }
738 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700739 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200740
Adam Cohened66b2b2012-01-23 17:28:51 -0800741 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700742 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700743
Adam Cohendb364c32014-05-20 14:23:40 -0700744 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800745 // We have special handling for widgets
746 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800747 final int appWidgetId;
748 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
749 : -1;
750 if (widgetId < 0) {
751 appWidgetId = pendingAddWidgetId;
752 } else {
753 appWidgetId = widgetId;
754 }
755
Adam Cohenad4e15c2013-10-17 16:21:35 -0700756 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800757 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700758 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
759 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700760 result = RESULT_CANCELED;
761 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700762 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700763 @Override
764 public void run() {
765 exitSpringLoadedDragModeDelayed(false, 0, null);
766 }
767 };
Adam Cohendb364c32014-05-20 14:23:40 -0700768 if (workspaceLocked) {
769 // No need to remove the empty screen if we're mid-binding, as the
770 // the bind will not add the empty screen.
771 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
772 } else {
773 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
774 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
775 }
Winson Chung5aaab772012-04-27 15:24:02 -0700776 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700777 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700778 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
779 // When the screen id represents an actual screen (as opposed to a rank)
780 // we make sure that the drop page actually exists.
781 mPendingAddInfo.screenId =
782 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
783 }
Adam Cohendb364c32014-05-20 14:23:40 -0700784 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
785
786 dropLayout.setDropPending(true);
787 final Runnable onComplete = new Runnable() {
788 @Override
789 public void run() {
790 completeTwoStageWidgetDrop(resultCode, appWidgetId);
791 dropLayout.setDropPending(false);
792 }
793 };
794 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
795 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
796 } else {
797 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
798 mPendingAddInfo);
799 sPendingAddItem = args;
800 }
Winson Chung5aaab772012-04-27 15:24:02 -0700801 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800802 return;
803 }
804
Sunny Goyalff572272014-07-23 13:58:07 -0700805 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
806 if (resultCode == RESULT_OK) {
807 // Update the widget view.
808 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
809 pendingAddWidgetId, mPendingAddInfo);
810 if (workspaceLocked) {
811 sPendingAddItem = args;
812 } else {
813 completeAdd(args);
814 }
815 }
816 // Leave the widget in the pending state if the user canceled the configure.
817 return;
818 }
819
Sunny Goyalaad90582015-07-09 14:22:50 -0700820 if (requestCode == REQUEST_CREATE_SHORTCUT) {
821 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
822 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
823 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
824 mPendingAddInfo);
825 if (isWorkspaceLocked()) {
826 sPendingAddItem = args;
827 } else {
828 completeAdd(args);
829 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
830 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
831 }
832 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700833 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
834 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800837 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800838
839 }
840
841 @Override
842 protected void onActivityResult(
843 final int requestCode, final int resultCode, final Intent data) {
844 handleActivityResult(requestCode, resultCode, data);
845 if (mLauncherCallbacks != null) {
846 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
847 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800848 }
849
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600850 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700851 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600852 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700853 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
854 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
855 View v = null;
856 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
857 if (layout != null) {
858 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
859 }
860 Intent intent = sPendingAddItem.intent;
861 sPendingAddItem = null;
862 if (grantResults.length > 0
863 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
864 startActivity(v, intent, null);
865 } else {
866 // TODO: Show a snack bar with link to settings
867 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
868 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
869 }
870 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600871 if (mLauncherCallbacks != null) {
872 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
873 grantResults);
874 }
875 }
876
Adam Cohendb364c32014-05-20 14:23:40 -0700877 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
878 appWidgetId, ItemInfo info) {
879 PendingAddArguments args = new PendingAddArguments();
880 args.requestCode = requestCode;
881 args.intent = data;
882 args.container = info.container;
883 args.screenId = info.screenId;
884 args.cellX = info.cellX;
885 args.cellY = info.cellY;
886 args.appWidgetId = appWidgetId;
887 return args;
888 }
889
890 /**
891 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
892 *
893 * @param screenId the screen id to check
894 * @return the new screen, or screenId if it exists
895 */
896 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800897 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700898 if (dropLayout == null) {
899 // it's possible that the add screen was removed because it was
900 // empty and a re-bind occurred
901 mWorkspace.addExtraEmptyScreen();
902 return mWorkspace.commitExtraEmptyScreen();
903 } else {
904 return screenId;
905 }
906 }
907
Adam Cohen091440a2015-03-18 14:16:05 -0700908 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800909 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800910 Runnable onCompleteRunnable = null;
911 int animationType = 0;
912
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700913 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800914 if (resultCode == RESULT_OK) {
915 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
916 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700917 mPendingAddWidgetInfo);
918 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800919 onCompleteRunnable = new Runnable() {
920 @Override
921 public void run() {
922 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700923 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700924 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
925 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800926 }
927 };
928 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800929 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800930 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800931 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700932 if (mDragLayer.getAnimatedView() != null) {
933 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
934 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
935 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700936 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700937 // The animated view may be null in the case of a rotation during widget configuration
938 onCompleteRunnable.run();
939 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 }
941
942 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700943 protected void onStop() {
944 super.onStop();
945 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700946
947 if (mLauncherCallbacks != null) {
948 mLauncherCallbacks.onStop();
949 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700950 }
951
952 @Override
953 protected void onStart() {
954 super.onStart();
955 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700956
957 if (mLauncherCallbacks != null) {
958 mLauncherCallbacks.onStart();
959 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700960 }
961
962 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200964 long startTime = 0;
965 if (DEBUG_RESUME_TIME) {
966 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400967 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200968 }
Adam Cohen9211d422014-10-07 18:14:53 -0700969
970 if (mLauncherCallbacks != null) {
971 mLauncherCallbacks.preOnResume();
972 }
973
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700975 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700976
Winson Chung4a2afa32012-07-19 14:53:05 -0700977 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700978 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700979 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800980 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700981 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700982 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700983 // view after launching an app, as they may be depending on the UI to be static to
984 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700985 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700986 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800987 } else if (mOnResumeState == State.WIDGETS) {
988 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700989 }
990 mOnResumeState = State.NONE;
991
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700992 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700993 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
994 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700995
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800996 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700997 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700998 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700999 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001000 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001001 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001003 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001004 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1005 // execute them here
1006 long startTimeCallbacks = 0;
1007 if (DEBUG_RESUME_TIME) {
1008 startTimeCallbacks = System.currentTimeMillis();
1009 }
1010
Michael Jurka1e2f4652013-07-08 18:03:46 -07001011 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1012 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001013 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001014 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001015 if (DEBUG_RESUME_TIME) {
1016 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1017 (System.currentTimeMillis() - startTimeCallbacks));
1018 }
Michael Jurka447bf842013-05-15 14:52:15 +02001019 }
Michael Jurka54554252013-08-01 12:52:23 +02001020 if (mOnResumeCallbacks.size() > 0) {
1021 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1022 mOnResumeCallbacks.get(i).run();
1023 }
1024 mOnResumeCallbacks.clear();
1025 }
Winson Chunge4e50662012-01-23 14:45:13 -08001026
1027 // Reset the pressed state of icons that were locked in the press state while activities
1028 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001029 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001030 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001031 mWaitingForResume.setStayPressed(false);
1032 }
Winson Chung82963d52013-10-09 11:20:57 -07001033
Adam Cohen06dff352012-06-01 17:17:08 -07001034 // It is possible that widgets can receive updates while launcher is not in the foreground.
1035 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1036 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1037 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001038 if (!isWorkspaceLoading()) {
1039 getWorkspace().reinflateWidgetsIfNecessary();
1040 }
Sunny Goyal22235bc2015-04-03 09:23:43 -07001041 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001042
Michael Jurka447bf842013-05-15 14:52:15 +02001043 if (DEBUG_RESUME_TIME) {
1044 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1045 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001046
Adam Cohen4b66bf32015-09-18 12:15:19 -07001047 // We want to suppress callbacks about CustomContent being shown if we have just received
1048 // onNewIntent while the user was present within launcher. In that case, we post a call
1049 // to move the user to the main screen (which will occur after onResume). We don't want to
1050 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1051 // suppress here.
1052 if (mWorkspace.getCustomContentCallbacks() != null
1053 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001054 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001055 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001056 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1057 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001058 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001059 }
1060 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001061 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001062 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001063 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001064
Sunny Goyal756adbc2015-04-16 15:20:51 -07001065 if (!isWorkspaceLoading()) {
1066 // Process any items that were added while Launcher was away.
1067 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1068 }
Adam Cohen9211d422014-10-07 18:14:53 -07001069
1070 if (mLauncherCallbacks != null) {
1071 mLauncherCallbacks.onResume();
1072 }
Adam Cohen06dff352012-06-01 17:17:08 -07001073 }
1074
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001075 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001076 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001077 // Ensure that items added to Launcher are queued until Launcher returns
1078 InstallShortcutReceiver.enableInstallQueue();
1079
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001080 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001081 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001082 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001083 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001084
1085 // We call onHide() aggressively. The custom content callbacks should be able to
1086 // debounce excess onHide calls.
1087 if (mWorkspace.getCustomContentCallbacks() != null) {
1088 mWorkspace.getCustomContentCallbacks().onHide();
1089 }
Romain Guycbb89e42009-06-08 15:52:54 -07001090
Adam Cohen9211d422014-10-07 18:14:53 -07001091 if (mLauncherCallbacks != null) {
1092 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001093 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001094 }
1095
1096 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001097 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1098 // by a onResume or by scrolling otherwise.
1099 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001100
1101 // Custom content is completely hidden
1102 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001103
1104 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1105 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001106
1107 // Indicates whether the user is allowed to scroll away from the custom content.
1108 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001109 }
1110
Adam Cohenc2d6e892014-10-16 09:49:24 -07001111 public interface LauncherOverlay {
1112
1113 /**
1114 * Touch interaction leading to overscroll has begun
1115 */
1116 public void onScrollInteractionBegin();
1117
1118 /**
1119 * Touch interaction related to overscroll has ended
1120 */
1121 public void onScrollInteractionEnd();
1122
1123 /**
1124 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1125 * screen (or in the case of RTL, the rightmost screen).
1126 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001127 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001128
1129 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001130 * Called when the launcher is ready to use the overlay
1131 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001132 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001133 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001134 }
1135
Jun Mukai8af0cd82015-05-12 12:32:12 -07001136 public interface LauncherSearchCallbacks {
1137 /**
1138 * Called when the search overlay is shown.
1139 */
1140 public void onSearchOverlayOpened();
1141
1142 /**
1143 * Called when the search overlay is dismissed.
1144 */
1145 public void onSearchOverlayClosed();
1146 }
1147
Adam Cohenc2d6e892014-10-16 09:49:24 -07001148 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001149 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001150 }
1151
1152 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1153
Sunny Goyalc86df472016-02-25 09:19:38 -08001154 public void onScrollChanged(float progress) {
1155 if (mWorkspace != null) {
1156 mWorkspace.onOverlayScrollChanged(progress);
1157 }
1158 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001159 }
1160
Jorim Jaggid017f882014-01-14 17:08:48 -08001161 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001162 if (mLauncherCallbacks != null) {
1163 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001164 } else {
1165 // On devices with a locked orientation, we will at least have the allow rotation
1166 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001167 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001168 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001169 }
1170
Adam Cohen9211d422014-10-07 18:14:53 -07001171 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001172 CustomContentCallbacks callbacks, String description) {
1173 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001174 }
1175
Adam Cohenedb40762013-07-18 16:45:45 -07001176 // The custom content needs to offset its content to account for the QSB
1177 public int getTopOffsetForCustomContent() {
1178 return mWorkspace.getPaddingTop();
1179 }
1180
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001181 @Override
1182 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001183 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001184 if (mModel.isCurrentCallbacks(this)) {
1185 mModel.stopLoader();
1186 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001187 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1188
Romain Guy13c2e7b2010-03-10 19:45:00 -08001189 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001190 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001191
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001192 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001193 @Override
1194 public void onWindowFocusChanged(boolean hasFocus) {
1195 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001196 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001197
1198 if (mLauncherCallbacks != null) {
1199 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1200 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001201 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001202
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 private boolean acceptFilter() {
1204 final InputMethodManager inputManager = (InputMethodManager)
1205 getSystemService(Context.INPUT_METHOD_SERVICE);
1206 return !inputManager.isFullscreenMode();
1207 }
1208
1209 @Override
1210 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001211 final int uniChar = event.getUnicodeChar();
1212 final boolean handled = super.onKeyDown(keyCode, event);
1213 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1214 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1216 keyCode, event);
1217 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001218 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001219 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001220 // showSearchDialog()
1221 // If there are multiple keystrokes before the search dialog takes focus,
1222 // onSearchRequested() will be called for every keystroke,
1223 // but it is idempotent, so it's fine.
1224 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 }
1226 }
1227
Joe Onorato8a9625e2010-01-28 15:55:35 -08001228 // Eat the long press event so the keyboard doesn't come up.
1229 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1230 return true;
1231 }
1232
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 return handled;
1234 }
1235
Winson46163472015-09-22 17:31:56 -07001236 @Override
1237 public boolean onKeyUp(int keyCode, KeyEvent event) {
1238 if (keyCode == KeyEvent.KEYCODE_MENU) {
1239 // Ignore the menu key if we are currently dragging or are on the custom content screen
1240 if (!isOnCustomContent() && !mDragController.isDragging()) {
1241 // Close any open folders
1242 closeFolder();
1243
1244 // Stop resizing any widgets
1245 mWorkspace.exitWidgetResizeMode();
1246
1247 // Show the overview mode if we are on the workspace
1248 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1249 !mWorkspace.isSwitchingState()) {
1250 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001251 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001252 }
1253 }
1254 return true;
1255 }
1256 return super.onKeyUp(keyCode, event);
1257 }
1258
Karl Rosaen138a0412009-04-23 19:00:21 -07001259 private String getTypedText() {
1260 return mDefaultKeySsb.toString();
1261 }
1262
1263 private void clearTypedText() {
1264 mDefaultKeySsb.clear();
1265 mDefaultKeySsb.clearSpans();
1266 Selection.setSelection(mDefaultKeySsb, 0);
1267 }
1268
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001270 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1271 * State
1272 */
1273 private static State intToState(int stateOrdinal) {
1274 State state = State.WORKSPACE;
1275 final State[] stateValues = State.values();
1276 for (int i = 0; i < stateValues.length; i++) {
1277 if (stateValues[i].ordinal() == stateOrdinal) {
1278 state = stateValues[i];
1279 break;
1280 }
1281 }
1282 return state;
1283 }
1284
1285 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 * Restores the previous state, if it exists.
1287 *
1288 * @param savedState The previous state.
1289 */
1290 private void restoreState(Bundle savedState) {
1291 if (savedState == null) {
1292 return;
1293 }
1294
Michael Jurka883f55b2010-10-21 15:47:14 -07001295 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001296 if (state == State.APPS || state == State.WIDGETS) {
1297 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001298 }
1299
Adam Cohen21cd0022013-10-09 18:57:02 -07001300 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1301 PagedView.INVALID_RESTORE_PAGE);
1302 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001303 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 }
1305
Sunny Goyal756cd262015-08-20 12:33:21 -07001306 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1307 if (itemValues != null) {
1308 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001309 AppWidgetProviderInfo info = savedState.getParcelable(
1310 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001311 mPendingAddWidgetInfo = info == null ?
1312 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1313
Adam Cohen4637b5a2013-11-04 18:21:24 -08001314 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001315 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 mRestoring = true;
1317 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 }
1319
1320 /**
1321 * Finds all the views we need and configure them properly.
1322 */
1323 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001324 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001325
Craig Mautner360310b2012-10-26 15:13:08 -07001326 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001327 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001328 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1329 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Winson Chunga6945242014-01-08 14:04:34 -08001330 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001331
Sunny Goyal784f9c32016-03-23 16:56:54 -07001332 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1333 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1334 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001335 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336
Winson Chung4c98d922011-05-31 16:50:48 -07001337 // Setup the drag layer
1338 mDragLayer.setup(this, dragController);
1339
Winson Chung3d503fb2011-07-13 17:25:49 -07001340 // Setup the hotseat
1341 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1342 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001343 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001344 }
1345
Winsone9f27272015-10-13 10:47:51 -07001346 // Setup the overview panel
1347 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001348
Winson Chung4c98d922011-05-31 16:50:48 -07001349 // Setup the workspace
1350 mWorkspace.setHapticFeedbackEnabled(false);
1351 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001352 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001353 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001354
Tony Wickham34d2c912015-09-11 09:27:58 -07001355 // Get the search/delete/uninstall bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001356 mSearchDropTargetBar = (SearchDropTargetBar)
1357 mDragLayer.findViewById(R.id.search_drop_target_bar);
Tony Wickham34d2c912015-09-11 09:27:58 -07001358 // Get the app info bar
1359 mAppInfoDropTargetBar = (AppInfoDropTargetBar)
1360 mDragLayer.findViewById(R.id.app_info_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001361
Winson Chungef7f8742015-06-04 17:18:17 -07001362 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001363 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001364 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001365 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1366 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1367 } else {
1368 mAppsView.setSearchBarController(new DefaultAppSearchController());
1369 }
Craig Mautner360310b2012-10-26 15:13:08 -07001370
Winson Chung3d503fb2011-07-13 17:25:49 -07001371 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001372 dragController.setDragScoller(mWorkspace);
Tony Wickham0f97b782015-12-02 17:55:07 -08001373 dragController.setScrollView(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -07001374 dragController.setMoveTarget(mWorkspace);
1375 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001376 if (mSearchDropTargetBar != null) {
1377 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001378 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001379 }
Tony Wickham34d2c912015-09-11 09:27:58 -07001380 if (mAppInfoDropTargetBar != null) {
1381 mAppInfoDropTargetBar.setup(this, dragController);
1382 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001383
Sunny Goyal322d5562015-06-25 19:35:49 -07001384 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1385 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001386 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 }
1388
Winsone9f27272015-10-13 10:47:51 -07001389 private void setupOverviewPanel() {
1390 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1391
1392 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1393 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1394 @Override
1395 public boolean onLongClick(View v) {
1396 return v.performClick();
1397 }
1398 };
1399
1400 // Bind wallpaper button actions
1401 View wallpaperButton = findViewById(R.id.wallpaper_button);
1402 wallpaperButton.setOnClickListener(new OnClickListener() {
1403 @Override
1404 public void onClick(View view) {
1405 if (!mWorkspace.isSwitchingState()) {
1406 onClickWallpaperPicker(view);
1407 }
1408 }
1409 });
1410 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1411 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1412
1413 // Bind widget button actions
1414 mWidgetsButton = findViewById(R.id.widget_button);
1415 mWidgetsButton.setOnClickListener(new OnClickListener() {
1416 @Override
1417 public void onClick(View view) {
1418 if (!mWorkspace.isSwitchingState()) {
1419 onClickAddWidgetButton(view);
1420 }
1421 }
1422 });
1423 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1424 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1425
1426 // Bind settings actions
1427 View settingsButton = findViewById(R.id.settings_button);
1428 boolean hasSettings = hasSettings();
1429 if (hasSettings) {
1430 settingsButton.setOnClickListener(new OnClickListener() {
1431 @Override
1432 public void onClick(View view) {
1433 if (!mWorkspace.isSwitchingState()) {
1434 onClickSettingsButton(view);
1435 }
1436 }
1437 });
1438 settingsButton.setOnLongClickListener(performClickOnLongClick);
1439 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1440 } else {
1441 settingsButton.setVisibility(View.GONE);
1442 }
1443
1444 mOverviewPanel.setAlpha(0f);
1445 }
1446
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001448 * Sets the all apps button. This method is called from {@link Hotseat}.
1449 */
1450 public void setAllAppsButton(View allAppsButton) {
1451 mAllAppsButton = allAppsButton;
1452 }
1453
1454 public View getAllAppsButton() {
1455 return mAllAppsButton;
1456 }
1457
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001458 public View getWidgetsButton() {
1459 return mWidgetsButton;
1460 }
1461
Anjali Koppal5ad44842014-03-10 20:34:39 -07001462 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 * Creates a view representing a shortcut.
1464 *
1465 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001467 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001468 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 }
1470
1471 /**
1472 * Creates a view representing a shortcut inflated from the specified resource.
1473 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 * @param parent The group the shortcut belongs to.
1475 * @param info The data structure describing the shortcut.
1476 *
1477 * @return A View inflated from layoutResId.
1478 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001479 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001480 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001481 parent, false);
1482 favorite.applyFromShortcutInfo(info, mIconCache);
1483 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001485 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 return favorite;
1487 }
1488
1489 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 * Add a shortcut to the workspace.
1491 *
1492 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001494 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001495 int cellY) {
1496 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001497 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001498
Sunny Goyal5c97f512015-05-19 16:03:28 -07001499 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001500 if (info == null) {
1501 return;
1502 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001503 final View view = createShortcut(info);
1504
Sunny Goyal5c97f512015-05-19 16:03:28 -07001505 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001506 // First we check if we already know the exact location where we want to add this item.
1507 if (cellX >= 0 && cellY >= 0) {
1508 cellXY[0] = cellX;
1509 cellXY[1] = cellY;
1510 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001511
1512 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001513 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001514 true, null,null)) {
1515 return;
1516 }
1517 DragObject dragObject = new DragObject();
1518 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001519 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1520 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001521 return;
1522 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001523 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001524 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001525 }
1526
1527 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001528 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001529 return;
1530 }
1531
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001532 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533
1534 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001535 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001536 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 }
1538 }
1539
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001541 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001543 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 */
Adam Cohen091440a2015-03-18 14:16:05 -07001545 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001546 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1547
1548 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001549 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001550 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001551 }
Romain Guycbb89e42009-06-08 15:52:54 -07001552
Adam Cohen59400422014-03-05 18:07:04 -08001553 if (appWidgetInfo.isCustomWidget) {
1554 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001555 }
1556
Adam Cohen59400422014-03-05 18:07:04 -08001557 LauncherAppWidgetInfo launcherInfo;
1558 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1559 launcherInfo.spanX = info.spanX;
1560 launcherInfo.spanY = info.spanY;
1561 launcherInfo.minSpanX = info.minSpanX;
1562 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001563 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001564
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001566 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567
1568 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001569 if (hostView == null) {
1570 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001571 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001572 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001573 hostView.setVisibility(View.VISIBLE);
1574 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001576 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 }
Romain Guycbb89e42009-06-08 15:52:54 -07001578
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001579 private void addAppWidgetToWorkspace(
1580 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001581 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001582 hostView.setTag(item);
1583 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001584
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001585 hostView.setFocusable(true);
1586 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001587
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001588 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001589 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1590
1591 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001592 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001593 }
1594 }
1595
Adam Cohended9f8d2010-11-03 13:25:16 -07001596 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1597 @Override
1598 public void onReceive(Context context, Intent intent) {
1599 final String action = intent.getAction();
1600 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1601 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001602 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001603 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001604
Winson Chungc100e8e2011-08-09 16:02:43 -07001605 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001606 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001607 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001608 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001609 if (!showWorkspace(false)) {
1610 // If we are already on the workspace, then manually reset all apps
1611 mAppsView.reset();
1612 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001613 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001614 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1615 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001616 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001617 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1618 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001619 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001620 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1621 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1622 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001623 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001624 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1625 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001626 }
1627 }
1628 };
1629
1630 @Override
1631 public void onAttachedToWindow() {
1632 super.onAttachedToWindow();
1633
1634 // Listen for broadcasts related to user-presence
1635 final IntentFilter filter = new IntentFilter();
1636 filter.addAction(Intent.ACTION_SCREEN_OFF);
1637 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001638 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001639 if (ENABLE_DEBUG_INTENTS) {
1640 filter.addAction(DebugIntents.DELETE_DATABASE);
1641 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1642 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001643 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001644 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001645 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001646 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001647
1648 if (mLauncherCallbacks != null) {
1649 mLauncherCallbacks.onAttachedToWindow();
1650 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001651 }
1652
1653 @Override
1654 public void onDetachedFromWindow() {
1655 super.onDetachedFromWindow();
1656 mVisible = false;
1657
Adam Cohend113e0c2010-11-11 10:48:05 -08001658 if (mAttached) {
1659 unregisterReceiver(mReceiver);
1660 mAttached = false;
1661 }
Winson Chungb745afb2015-03-02 11:51:23 -08001662 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001663
1664 if (mLauncherCallbacks != null) {
1665 mLauncherCallbacks.onDetachedFromWindow();
1666 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001667 }
1668
1669 public void onWindowVisibilityChanged(int visibility) {
1670 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001671 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001672 // The following code used to be in onResume, but it turns out onResume is called when
1673 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1674 // is a more appropriate event to handle
1675 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001676 if (!mWorkspaceLoading) {
1677 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001678 // We want to let Launcher draw itself at least once before we force it to build
1679 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001680 // apps is nice and speedy.
1681 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001682 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001683 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001684 if (mStarted) return;
1685 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001686 // We delay the layer building a bit in order to give
1687 // other message processing a time to run. In particular
1688 // this avoids a delay in hiding the IME if it was
1689 // currently shown, because doing that may involve
1690 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001691 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001692 final ViewTreeObserver.OnDrawListener listener = this;
1693 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001694 public void run() {
1695 if (mWorkspace != null &&
1696 mWorkspace.getViewTreeObserver() != null) {
1697 mWorkspace.getViewTreeObserver().
1698 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001699 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001700 }
1701 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001702 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001703 }
1704 });
1705 }
1706 clearTypedText();
1707 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001708 }
1709
Adam Cohen091440a2015-03-18 14:16:05 -07001710 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001711 mHandler.removeMessages(ADVANCE_MSG);
1712 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1713 mHandler.sendMessageDelayed(msg, delay);
1714 mAutoAdvanceSentTime = System.currentTimeMillis();
1715 }
1716
Adam Cohen091440a2015-03-18 14:16:05 -07001717 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001718 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1719 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1720 mAutoAdvanceRunning = autoAdvanceRunning;
1721 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001722 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001723 sendAdvanceMessage(delay);
1724 } else {
1725 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001726 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001727 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1728 }
1729 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001730 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001731 }
1732 }
1733 }
1734
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001735 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1736
Adam Cohended9f8d2010-11-03 13:25:16 -07001737 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001738 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001739 if (msg.what == ADVANCE_MSG) {
1740 int i = 0;
1741 for (View key: mWidgetsToAdvance.keySet()) {
1742 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001743 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001744 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001745 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001746 public void run() {
1747 ((Advanceable) v).advance();
1748 }
1749 }, delay);
1750 }
1751 i++;
1752 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001753 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001754 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001755 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001756 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001757 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001758
Winsonc0b52fe2015-09-09 16:38:15 -07001759 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001760 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001761 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1762 if (v instanceof Advanceable) {
1763 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001764 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001765 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001766 }
1767 }
1768
Winsonc0b52fe2015-09-09 16:38:15 -07001769 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001770 if (mWidgetsToAdvance.containsKey(hostView)) {
1771 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001772 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001773 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001774 }
1775
Hyunyoung Song3f471442015-04-08 19:01:34 -07001776 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001777 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1778 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001779 }
1780
Winson Chunga6945242014-01-08 14:04:34 -08001781 public DragLayer getDragLayer() {
1782 return mDragLayer;
1783 }
1784
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001785 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001786 return mAppsView;
1787 }
1788
Hyunyoung Song3f471442015-04-08 19:01:34 -07001789 public WidgetsContainerView getWidgetsView() {
1790 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001791 }
1792
Winson Chunga6945242014-01-08 14:04:34 -08001793 public Workspace getWorkspace() {
1794 return mWorkspace;
1795 }
1796
1797 public Hotseat getHotseat() {
1798 return mHotseat;
1799 }
1800
Jorim Jaggid017f882014-01-14 17:08:48 -08001801 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001802 return mOverviewPanel;
1803 }
1804
Winson Chung006ee262015-08-03 14:40:11 -07001805 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001806 return mSearchDropTargetBar;
1807 }
1808
Tony Wickham34d2c912015-09-11 09:27:58 -07001809 public AppInfoDropTargetBar getAppInfoDropTargetBar() {
1810 return mAppInfoDropTargetBar;
1811 }
1812
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001813 public LauncherAppWidgetHost getAppWidgetHost() {
1814 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 }
Romain Guycbb89e42009-06-08 15:52:54 -07001816
Winson Chunga9abd0e2010-10-27 17:18:37 -07001817 public LauncherModel getModel() {
1818 return mModel;
1819 }
1820
Adam Cohen79d90c52016-04-22 13:29:20 -07001821 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001822 return mSharedPrefs;
1823 }
1824
Adam Cohen2e6da152015-05-06 11:42:25 -07001825 public DeviceProfile getDeviceProfile() {
1826 return mDeviceProfile;
1827 }
1828
Bjorn Bringertc459e522013-06-07 19:36:01 +01001829 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001830 getWindow().closeAllPanels();
1831
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001832 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001833 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001834 }
1835
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 @Override
1837 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001838 long startTime = 0;
1839 if (DEBUG_RESUME_TIME) {
1840 startTime = System.currentTimeMillis();
1841 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001842 super.onNewIntent(intent);
1843
1844 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001845 Folder openFolder = mWorkspace.getOpenFolder();
1846 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1847 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1848 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1849 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1850 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001851 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001852 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001853
Adam Cohen6fecd412013-10-02 17:41:50 -07001854 if (mWorkspace == null) {
1855 // Can be cases where mWorkspace is null, this prevents a NPE
1856 return;
1857 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001858 // In all these cases, only animate if we're already on home
1859 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001860
Sunny Goyal935fca12015-10-13 10:19:01 -07001861 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001862 exitSpringLoadedDragMode();
1863
1864 // If we are already on home, then just animate back to the workspace,
1865 // otherwise, just wait until onResume to set the state back to Workspace
1866 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001867 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001868 } else {
1869 mOnResumeState = State.WORKSPACE;
1870 }
1871
1872 final View v = getWindow().peekDecorView();
1873 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001874 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001875 INPUT_METHOD_SERVICE);
1876 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1877 }
1878
Winson Chungb745afb2015-03-02 11:51:23 -08001879 // Reset the apps view
1880 if (!alreadyOnHome && mAppsView != null) {
1881 mAppsView.scrollToTop();
1882 }
1883
Hyunyoung Song3f471442015-04-08 19:01:34 -07001884 // Reset the widgets view
1885 if (!alreadyOnHome && mWidgetsView != null) {
1886 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001887 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001888
Adam Cohen9211d422014-10-07 18:14:53 -07001889 if (mLauncherCallbacks != null) {
1890 mLauncherCallbacks.onHomeIntent();
1891 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001892 }
Adam Cohened307df2013-10-02 09:37:31 -07001893
Adam Cohen9211d422014-10-07 18:14:53 -07001894 if (mLauncherCallbacks != null) {
1895 mLauncherCallbacks.onNewIntent(intent);
1896 }
Winson15f8b172015-08-19 11:02:39 -07001897
1898 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1899 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1900 // animation.
1901 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001902 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1903 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001904 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1905 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001906
1907 // We use this flag to suppress noisy callbacks above custom content state
1908 // from onResume.
1909 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001910 mWorkspace.post(new Runnable() {
1911 @Override
1912 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001913 if (mWorkspace != null) {
1914 mWorkspace.moveToDefaultScreen(true);
1915 }
Winson15f8b172015-08-19 11:02:39 -07001916 }
1917 });
1918 }
1919 }
1920
1921 if (DEBUG_RESUME_TIME) {
1922 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1923 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001924 }
1925
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001927 public void onRestoreInstanceState(Bundle state) {
1928 super.onRestoreInstanceState(state);
1929 for (int page: mSynchronouslyBoundPages) {
1930 mWorkspace.restoreInstanceStateForChild(page);
1931 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 }
1933
1934 @Override
1935 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001936 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001937 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1938 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001939 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001940 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941
Michael Jurka883f55b2010-10-21 15:47:14 -07001942 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001943 // We close any open folder since it will not be re-opened, and we need to make sure
1944 // this state is reflected.
1945 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1946 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947
Adam Cohendcd297f2013-06-18 13:13:40 -07001948 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001949 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001950 ContentValues itemValues = new ContentValues();
1951 mPendingAddInfo.writeToValues(itemValues);
1952 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001953 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001954 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955 }
1956
Adam Cohen9211d422014-10-07 18:14:53 -07001957 if (mLauncherCallbacks != null) {
1958 mLauncherCallbacks.onSaveInstanceState(outState);
1959 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 }
1961
1962 @Override
1963 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001965
Winson Chunge7a03942011-08-05 15:05:12 -07001966 // Remove all pending runnables
1967 mHandler.removeMessages(ADVANCE_MSG);
1968 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001969 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001970 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001971
Winson Chungcd2b0142011-06-08 16:02:26 -07001972 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001973 // It's possible to receive onDestroy after a new Launcher activity has
1974 // been created. In this case, don't interfere with the new Launcher.
1975 if (mModel.isCurrentCallbacks(this)) {
1976 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001977 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001978 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001979
Sunny Goyal745bad92016-05-02 10:54:12 -07001980 if (mRotationPrefChangeHandler != null) {
1981 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1982 }
1983
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001985 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001987 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001989 mAppWidgetHost = null;
1990
1991 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992
1993 TextKeyListener.getInstance().release();
1994
Tony Wickhame2217252016-03-22 16:34:23 -07001995 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1996 .removeAccessibilityStateChangeListener(this);
1997
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001998 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001999
Michael Jurka2ecf9952012-06-18 12:52:28 -07002000 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002001
2002 if (mLauncherCallbacks != null) {
2003 mLauncherCallbacks.onDestroy();
2004 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 }
2006
Adam Cohena9cf38f2011-05-02 15:36:58 -07002007 public DragController getDragController() {
2008 return mDragController;
2009 }
2010
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011 @Override
2012 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002013 onStartForResult(requestCode);
2014 super.startActivityForResult(intent, requestCode);
2015 }
2016
2017 @Override
2018 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2019 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2020 onStartForResult(requestCode);
2021 try {
2022 super.startIntentSenderForResult(intent, requestCode,
2023 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2024 } catch (IntentSender.SendIntentException e) {
2025 throw new ActivityNotFoundException();
2026 }
2027 }
2028
2029 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002030 if (requestCode >= 0) {
2031 setWaitingForResult(true);
2032 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002033 }
2034
Winson Chung70d72102011-08-12 11:24:35 -07002035 /**
2036 * Indicates that we want global search for this activity by setting the globalSearch
2037 * argument for {@link #startSearch} to true.
2038 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002040 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002042
Karl Rosaen138a0412009-04-23 19:00:21 -07002043 if (initialQuery == null) {
2044 // Use any text typed in the launcher as the initial query
2045 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002046 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 if (appSearchData == null) {
2048 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002049 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002050 }
Winson Chungadf0c182012-08-23 14:59:07 -07002051 Rect sourceBounds = new Rect();
2052 if (mSearchDropTargetBar != null) {
2053 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2054 }
Romain Guycbb89e42009-06-08 15:52:54 -07002055
Ian Parkinson047036e2014-09-01 15:40:53 +01002056 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002057 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002058 if (clearTextImmediately) {
2059 clearTypedText();
2060 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002061
2062 // We need to show the workspace after starting the search
2063 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002064 }
2065
Ian Parkinson047036e2014-09-01 15:40:53 +01002066 /**
2067 * Start a text search.
2068 *
2069 * @return {@code true} if the search will start immediately, so any further keypresses
2070 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2071 * to buffer keypresses.
2072 */
2073 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002074 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002075 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2076 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2077 sourceBounds);
2078 }
2079
Michael Jurkaa33411c2012-06-14 16:18:21 -07002080 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002081 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002082 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002083 }
2084
2085 /**
2086 * Starts the global search activity. This code is a copied from SearchManager
2087 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002088 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002089 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002090 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002091 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2092 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2093 if (globalSearchActivity == null) {
2094 Log.w(TAG, "No global search activity found.");
2095 return;
2096 }
2097 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2098 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2099 intent.setComponent(globalSearchActivity);
2100 // Make sure that we have a Bundle to put source in
2101 if (appSearchData == null) {
2102 appSearchData = new Bundle();
2103 } else {
2104 appSearchData = new Bundle(appSearchData);
2105 }
Adam Cohen9211d422014-10-07 18:14:53 -07002106 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002107 if (!appSearchData.containsKey("source")) {
2108 appSearchData.putString("source", getPackageName());
2109 }
2110 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2111 if (!TextUtils.isEmpty(initialQuery)) {
2112 intent.putExtra(SearchManager.QUERY, initialQuery);
2113 }
2114 if (selectInitialQuery) {
2115 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2116 }
2117 intent.setSourceBounds(sourceBounds);
2118 try {
2119 startActivity(intent);
2120 } catch (ActivityNotFoundException ex) {
2121 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2122 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123 }
2124
Yura4f93ec62014-02-04 14:15:21 +00002125 public boolean isOnCustomContent() {
2126 return mWorkspace.isOnOrMovingToCustomContent();
2127 }
2128
Winson Chung70d72102011-08-12 11:24:35 -07002129 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002130 public boolean onPrepareOptionsMenu(Menu menu) {
2131 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002132 if (mLauncherCallbacks != null) {
2133 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2134 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002135 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002136 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002137
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002138 @Override
2139 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002140 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002141 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002142 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002143 }
2144
Joe Onorato9c1289c2009-08-17 11:03:03 -04002145 public boolean isWorkspaceLocked() {
2146 return mWorkspaceLoading || mWaitingForResult;
2147 }
2148
Adam Cohen517a7f52014-03-01 12:12:59 -08002149 public boolean isWorkspaceLoading() {
2150 return mWorkspaceLoading;
2151 }
2152
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002153 private void setWorkspaceLoading(boolean value) {
2154 boolean isLocked = isWorkspaceLocked();
2155 mWorkspaceLoading = value;
2156 if (isLocked != isWorkspaceLocked()) {
2157 onWorkspaceLockedChanged();
2158 }
2159 }
2160
2161 private void setWaitingForResult(boolean value) {
2162 boolean isLocked = isWorkspaceLocked();
2163 mWaitingForResult = value;
2164 if (isLocked != isWorkspaceLocked()) {
2165 onWorkspaceLockedChanged();
2166 }
2167 }
2168
Adam Cohen9211d422014-10-07 18:14:53 -07002169 protected void onWorkspaceLockedChanged() {
2170 if (mLauncherCallbacks != null) {
2171 mLauncherCallbacks.onWorkspaceLockedChanged();
2172 }
2173 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002174
Michael Jurka0280c3b2010-09-17 15:00:07 -07002175 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002176 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002177 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002178 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2179 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002180 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002181 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002182
Tony Wickhama0628cc2015-10-14 15:23:04 -07002183 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002184 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002185 if (LOGD) {
2186 Log.d(TAG, "Adding widget from drop");
2187 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002188 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2189 }
2190
Sunny Goyale6b63a32015-01-16 16:14:29 -08002191 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002192 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2193 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002194 if (appWidgetInfo.configure != null) {
2195 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002196 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002197
Bjorn Bringert7984c942009-12-09 15:38:25 +00002198 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002199 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2200 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2201
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002202 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002203 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002204 Runnable onComplete = new Runnable() {
2205 @Override
2206 public void run() {
2207 // Exit spring loaded mode if necessary after adding the widget
2208 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2209 null);
2210 }
2211 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002212 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002213 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002214 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002215 }
2216 }
Romain Guycbb89e42009-06-08 15:52:54 -07002217
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002218 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002219 // Close any folders that may be open.
2220 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002221 mWorkspace.moveToCustomContentScreen(animate);
2222 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002223
2224 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2225 int[] cell, int spanX, int spanY) {
2226 switch (info.itemType) {
2227 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2228 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2229 int span[] = new int[2];
2230 span[0] = spanX;
2231 span[1] = spanY;
2232 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2233 container, screenId, cell, span);
2234 break;
2235 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2236 processShortcutFromDrop(info.componentName, container, screenId, cell);
2237 break;
2238 default:
2239 throw new IllegalStateException("Unknown item type: " + info.itemType);
2240 }
2241 }
2242
Adam Cohenfbba09b2011-07-18 21:43:05 -07002243 /**
2244 * Process a shortcut drop.
2245 *
2246 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002247 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002248 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002249 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002250 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2251 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002252 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002253 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002254 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002255
2256 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002257 mPendingAddInfo.cellX = cell[0];
2258 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002259 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002260
2261 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2262 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002263 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002264 }
2265
Adam Cohenfbba09b2011-07-18 21:43:05 -07002266 /**
2267 * Process a widget drop.
2268 *
2269 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002270 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002271 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002272 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002273 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2274 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002275 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002276 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002277 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002278 mPendingAddInfo.minSpanX = info.minSpanX;
2279 mPendingAddInfo.minSpanY = info.minSpanY;
2280
Adam Cohenfbba09b2011-07-18 21:43:05 -07002281 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002282 mPendingAddInfo.cellX = cell[0];
2283 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002284 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002285 if (span != null) {
2286 mPendingAddInfo.spanX = span[0];
2287 mPendingAddInfo.spanY = span[1];
2288 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002289
Adam Cohened66b2b2012-01-23 17:28:51 -08002290 AppWidgetHostView hostView = info.boundWidget;
2291 int appWidgetId;
2292 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002293 // In the case where we've prebound the widget, we remove it from the DragLayer
2294 if (LOGD) {
2295 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2296 }
2297 getDragLayer().removeView(hostView);
2298
Adam Cohened66b2b2012-01-23 17:28:51 -08002299 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002300 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002301
2302 // Clear the boundWidget so that it doesn't get destroyed.
2303 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002304 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002305 // In this case, we either need to start an activity to get permission to bind
2306 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002307 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002308 Bundle options = info.bindOptions;
2309
Sunny Goyalffe83f12014-08-14 17:39:34 -07002310 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2311 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002312 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002313 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002314 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002315 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002316 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2317 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2318 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002319 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2320 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002321 // TODO: we need to make sure that this accounts for the options bundle.
2322 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002323 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2324 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002325 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002326 }
2327
Adam Cohendcd297f2013-06-18 13:13:40 -07002328 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002329 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002330 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002331 folderInfo.title = getText(R.string.folder_name);
2332
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002333 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002334 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2335 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002336
2337 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002338 FolderIcon newFolder =
2339 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002340 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002341 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002342 // Force measure the new folder icon
2343 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2344 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002345 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002346 }
Romain Guycbb89e42009-06-08 15:52:54 -07002347
Winsonc0b52fe2015-09-09 16:38:15 -07002348 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002349 * Unbinds the view for the specified item, and removes the item and all its children.
2350 *
2351 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002352 * @param itemInfo the {@link ItemInfo} for this view.
2353 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002354 */
Winson2949fb52015-09-24 09:56:11 -07002355 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002356 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002357 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002358 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2359 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002360 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002361 } else {
2362 mWorkspace.removeWorkspaceItem(v);
2363 }
Winsonc0b52fe2015-09-09 16:38:15 -07002364 if (deleteFromDb) {
2365 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2366 }
2367 } else if (itemInfo instanceof FolderInfo) {
2368 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002369 if (v instanceof FolderIcon) {
2370 ((FolderIcon) v).removeListeners();
2371 }
Winsonc0b52fe2015-09-09 16:38:15 -07002372 mWorkspace.removeWorkspaceItem(v);
2373 if (deleteFromDb) {
2374 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2375 }
2376 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2377 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002378 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002379 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002380 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002381 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002382 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002383
Winsonc0b52fe2015-09-09 16:38:15 -07002384 } else {
2385 return false;
2386 }
2387 return true;
2388 }
2389
2390 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002391 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002392 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002393 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002394 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyal56c73602015-09-25 12:55:01 -07002395 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdValid()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002396 // Deleting an app widget ID is a void call but writes to disk before returning
2397 // to the caller...
2398 new AsyncTask<Void, Void, Void>() {
2399 public Void doInBackground(Void ... args) {
2400 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2401 return null;
2402 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002403 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002404 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002405 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002406 }
2407
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002408 @Override
2409 public boolean dispatchKeyEvent(KeyEvent event) {
2410 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2411 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002412 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002413 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002414 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002415 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002416 dumpState();
2417 return true;
2418 }
2419 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002420 }
2421 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2422 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002423 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002424 return true;
2425 }
2426 }
2427
2428 return super.dispatchKeyEvent(event);
2429 }
2430
Joe Onorato88ec0992009-11-19 13:16:06 -08002431 @Override
2432 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002433 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2434 return;
2435 }
2436
Sunny Goyal45478022015-06-08 16:52:41 -07002437 if (mDragController.isDragging()) {
2438 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002439 return;
2440 }
2441
Winson Chungb745afb2015-03-02 11:51:23 -08002442 if (isAppsViewVisible()) {
2443 showWorkspace(true);
2444 } else if (isWidgetsViewVisible()) {
2445 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002446 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002447 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002448 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002449 Folder openFolder = mWorkspace.getOpenFolder();
2450 if (openFolder.isEditingName()) {
2451 openFolder.dismissEditingName();
2452 } else {
2453 closeFolder();
2454 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002455 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002456 mWorkspace.exitWidgetResizeMode();
2457
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002458 // Back button is a no-op here, but give at least some feedback for the button press
2459 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002460 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002461 }
2462
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002463 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002464 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002465 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002466 @Override
2467 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002468 if (mAppWidgetHost != null) {
2469 mAppWidgetHost.startListening();
2470 }
Sunny Goyal655daae2015-12-08 09:28:23 -08002471
2472 // Recreate the QSB, as the widget has been reset.
2473 bindSearchProviderChanged();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002474 }
2475
2476 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002477 * Launches the intent referred by the clicked shortcut.
2478 *
2479 * @param v The view representing the clicked shortcut.
2480 */
2481 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002482 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2483 // view has detached (it's possible for this to happen if the view is removed mid touch).
2484 if (v.getWindowToken() == null) {
2485 return;
2486 }
2487
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002488 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002489 return;
2490 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002491
Adam Cohen1697b792013-09-17 19:08:21 -07002492 if (v instanceof Workspace) {
2493 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002494 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002495 }
2496 return;
2497 }
2498
2499 if (v instanceof CellLayout) {
2500 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002501 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2502 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002503 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002504 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002505 }
2506
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002507 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002508 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002509 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002510 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002511 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002512 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002513 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002514 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002515 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002516 } else if (tag instanceof AppInfo) {
2517 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002518 } else if (tag instanceof LauncherAppWidgetInfo) {
2519 if (v instanceof PendingAppWidgetHostView) {
2520 onClickPendingWidget((PendingAppWidgetHostView) v);
2521 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002522 }
2523 }
2524
Sunny Goyal70660032015-05-14 00:07:08 -07002525 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002526 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002527 return false;
2528 }
2529
Michael Jurkaaf442092010-06-10 17:01:57 -07002530 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002531 * Event handler for the app widget view which has not fully restored.
2532 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002533 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002534 if (mIsSafeModeEnabled) {
2535 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2536 return;
2537 }
2538
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002539 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002540 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002541 int widgetId = info.appWidgetId;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002542 LauncherAppWidgetProviderInfo appWidgetInfo =
2543 mAppWidgetManager.getLauncherAppWidgetInfo(widgetId);
Sunny Goyalff572272014-07-23 13:58:07 -07002544 if (appWidgetInfo != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002545 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002546 mPendingAddInfo.copyFrom(info);
2547 mPendingAddWidgetId = widgetId;
2548
Sunny Goyalffe83f12014-08-14 17:39:34 -07002549 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2550 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002551 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002552 } else if (info.installProgress < 0) {
2553 // The install has not been queued
2554 final String packageName = info.providerName.getPackageName();
2555 showBrokenAppInstallDialog(packageName,
2556 new DialogInterface.OnClickListener() {
2557 public void onClick(DialogInterface dialog, int id) {
2558 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2559 }
2560 });
2561 } else {
2562 // Download has started.
2563 final String packageName = info.providerName.getPackageName();
2564 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002565 }
2566 }
2567
2568 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002569 * Event handler for the "grid" button that appears on the home screen, which
2570 * enters all apps mode.
2571 *
2572 * @param v The view that was clicked.
2573 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002574 protected void onClickAllAppsButton(View v) {
2575 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002576 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002577 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002578 true /* updatePredictedApps */, false /* focusSearchBar */);
2579 }
2580 }
2581
2582 protected void onLongClickAllAppsButton(View v) {
2583 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2584 if (!isAppsViewVisible()) {
2585 showAppsView(true /* animated */, false /* resetListToTop */,
2586 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002587 }
2588 }
2589
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002590 private void showBrokenAppInstallDialog(final String packageName,
2591 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002592 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002593 .setTitle(R.string.abandoned_promises_title)
2594 .setMessage(R.string.abandoned_promise_explanation)
2595 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2596 .setNeutralButton(R.string.abandoned_clean_this,
2597 new DialogInterface.OnClickListener() {
2598 public void onClick(DialogInterface dialog, int id) {
2599 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2600 mWorkspace.removeAbandonedPromise(packageName, user);
2601 }
2602 })
2603 .create().show();
2604 return;
2605 }
2606
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002607 /**
2608 * Event handler for an app shortcut click.
2609 *
2610 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2611 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002612 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002613 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2614 Object tag = v.getTag();
2615 if (!(tag instanceof ShortcutInfo)) {
2616 throw new IllegalArgumentException("Input must be a Shortcut");
2617 }
2618
2619 // Open shortcut
2620 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002621
2622 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002623 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2624 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002625 // Launch activity anyway, framework will tell the user why the app is suspended.
2626 } else {
2627 int error = R.string.activity_not_available;
2628 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2629 error = R.string.safemode_shortcut_error;
2630 }
2631 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2632 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002633 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002634 }
2635
Chris Wren40c5ed32014-06-24 18:24:23 -04002636 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002637 if ((v instanceof BubbleTextView)
2638 && shortcut.isPromise()
2639 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002640 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002641 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002642 new DialogInterface.OnClickListener() {
2643 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002644 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002645 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002646 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002647 return;
2648 }
2649
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002650 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002651 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002652 }
2653
Adam Cohen091440a2015-03-18 14:16:05 -07002654 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002655 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002656 final ShortcutInfo shortcut;
2657 final Intent intent;
2658 if (tag instanceof ShortcutInfo) {
2659 shortcut = (ShortcutInfo) tag;
2660 intent = shortcut.intent;
2661 int[] pos = new int[2];
2662 v.getLocationOnScreen(pos);
2663 intent.setSourceBounds(new Rect(pos[0], pos[1],
2664 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002665
Sunny Goyal508da152014-08-14 10:53:27 -07002666 } else if (tag instanceof AppInfo) {
2667 shortcut = null;
2668 intent = ((AppInfo) tag).intent;
2669 } else {
2670 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2671 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002672
2673 boolean success = startActivitySafely(v, intent, tag);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002674 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002675
2676 if (success && v instanceof BubbleTextView) {
2677 mWaitingForResume = (BubbleTextView) v;
2678 mWaitingForResume.setStayPressed(true);
2679 }
2680 }
2681
2682 /**
2683 * Event handler for a folder icon click.
2684 *
2685 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2686 */
2687 protected void onClickFolderIcon(View v) {
2688 if (LOGD) Log.d(TAG, "onClickFolder");
2689 if (!(v instanceof FolderIcon)){
2690 throw new IllegalArgumentException("Input must be a FolderIcon");
2691 }
2692
2693 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002694 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002695 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002696 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002697 }
Michael Jurka5130e402011-10-13 04:55:35 -07002698 }
2699
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002700 /**
2701 * Event handler for the (Add) Widgets button that appears after a long press
2702 * on the home screen.
2703 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002704 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002705 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002706 if (mIsSafeModeEnabled) {
2707 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2708 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002709 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002710 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002711 }
2712
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002713 /**
2714 * Event handler for the wallpaper picker button that appears after a long press
2715 * on the home screen.
2716 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002717 protected void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002718 if (!Utilities.isWallapaperAllowed(this)) {
2719 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2720 return;
2721 }
2722
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002723 String pickerPackage = getString(R.string.wallpaper_picker_package);
2724 if (TextUtils.isEmpty(pickerPackage)) {
2725 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2726 }
2727
Tony Wickham785f7a52015-08-31 17:28:32 -07002728 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2729 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002730 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER)
2731 .setPackage(pickerPackage)
2732 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset),
2733 REQUEST_PICK_WALLPAPER);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002734 }
2735
2736 /**
2737 * Event handler for a click on the settings button that appears after a long press
2738 * on the home screen.
2739 */
Sunny Goyal745bad92016-05-02 10:54:12 -07002740 private void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002741 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002742 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2743 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002744 }
2745
Adam Cohen61f560d2013-09-30 15:58:20 -07002746 public View.OnTouchListener getHapticFeedbackTouchListener() {
2747 if (mHapticFeedbackTouchListener == null) {
2748 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002749 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002750 @Override
2751 public boolean onTouch(View v, MotionEvent event) {
2752 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2753 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2754 }
2755 return false;
2756 }
2757 };
2758 }
2759 return mHapticFeedbackTouchListener;
2760 }
2761
Tony Wickhame2217252016-03-22 16:34:23 -07002762 @Override
2763 public void onAccessibilityStateChanged(boolean enabled) {
2764 mDragLayer.onAccessibilityStateChanged(enabled);
2765 }
2766
Adam Cohen9211d422014-10-07 18:14:53 -07002767 public void onDragStarted(View view) {
2768 if (isOnCustomContent()) {
2769 // Custom content screen doesn't participate in drag and drop. If on custom
2770 // content screen, move to default.
2771 moveWorkspaceToDefaultScreen();
2772 }
Adam Cohen9211d422014-10-07 18:14:53 -07002773 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002774
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002775 /**
2776 * Called when the user stops interacting with the launcher.
2777 * This implies that the user is now on the homescreen and is not doing housekeeping.
2778 */
Adam Cohen9211d422014-10-07 18:14:53 -07002779 protected void onInteractionEnd() {
2780 if (mLauncherCallbacks != null) {
2781 mLauncherCallbacks.onInteractionEnd();
2782 }
2783 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002784
2785 /**
2786 * Called when the user starts interacting with the launcher.
2787 * The possible interactions are:
2788 * - open all apps
2789 * - reorder an app shortcut, or a widget
2790 * - open the overview mode.
2791 * This is a good time to stop doing things that only make sense
2792 * when the user is on the homescreen and not doing housekeeping.
2793 */
Adam Cohen9211d422014-10-07 18:14:53 -07002794 protected void onInteractionBegin() {
2795 if (mLauncherCallbacks != null) {
2796 mLauncherCallbacks.onInteractionBegin();
2797 }
2798 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002799
Winson Chungcd99cd32015-04-29 11:03:24 -07002800 /** Updates the interaction state. */
2801 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002802 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002803 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002804 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2805 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002806 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002807 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002808 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002809 onInteractionEnd();
2810 }
2811 }
2812
Winson Chung8f1eff72015-05-28 17:33:40 -07002813 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002814 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002815 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002816 // Only launch using the new animation if the shortcut has not opted out (this is a
2817 // private contract between launcher and may be ignored in the future).
2818 boolean useLaunchAnimation = (v != null) &&
2819 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002820 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2821 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002822
Kenny Guy1317e2d2014-05-08 18:52:50 +01002823 UserHandleCompat user = null;
2824 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2825 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2826 user = userManager.getUserForSerialNumber(serialNumber);
2827 }
2828
2829 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002830 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002831 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002832 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002833 int left = 0, top = 0;
2834 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2835 if (v instanceof TextView) {
2836 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002837 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2838 if (icon != null) {
2839 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002840 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002841 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002842 width = bounds.width();
2843 height = bounds.height();
2844 }
2845 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002846 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2847 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002848 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002849 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002850 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002851 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002852 // On L devices, we use the device default slide-up transition.
2853 // On L MR1 devices, we a custom version of the slide-up transition which
2854 // doesn't have the delay present in the device default.
2855 opts = ActivityOptions.makeCustomAnimation(this,
2856 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002857 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002858 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002859 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002860
2861 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2862 // Could be launching some bookkeeping activity
2863 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002864 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002865 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002866 launcherApps.startActivityForProfile(intent.getComponent(), user,
2867 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002868 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002869 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002870 } catch (SecurityException e) {
Sunny Goyal28c6b962015-10-12 11:42:05 -07002871 if (Utilities.ATLEAST_MARSHMALLOW && tag instanceof ItemInfo) {
2872 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2873 // corresponding permission. Show the appropriate permission prompt if that
2874 // is the case.
2875 if (intent.getComponent() == null
2876 && Intent.ACTION_CALL.equals(intent.getAction())
2877 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2878 PackageManager.PERMISSION_GRANTED) {
2879 // TODO: Rename sPendingAddItem to a generic name.
2880 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2881 0, (ItemInfo) tag);
2882 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2883 REQUEST_PERMISSION_CALL_PHONE);
2884 return false;
2885 }
2886 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002887 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002888 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002889 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002890 "or use the exported attribute for this activity. "
2891 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002892 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002893 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002894 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002895
Winson Chungbedf9232015-07-10 12:38:30 -07002896 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002897 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002898 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2899 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2900 return false;
2901 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002902 try {
2903 success = startActivity(v, intent, tag);
2904 } catch (ActivityNotFoundException e) {
2905 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2906 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2907 }
2908 return success;
2909 }
2910
Adam Cohen268c4752012-06-06 17:47:33 -07002911 /**
2912 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2913 * in the DragLayer in the exact absolute location of the original FolderIcon.
2914 */
2915 private void copyFolderIconToImage(FolderIcon fi) {
2916 final int width = fi.getMeasuredWidth();
2917 final int height = fi.getMeasuredHeight();
2918
2919 // Lazy load ImageView, Bitmap and Canvas
2920 if (mFolderIconImageView == null) {
2921 mFolderIconImageView = new ImageView(this);
2922 }
2923 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2924 mFolderIconBitmap.getHeight() != height) {
2925 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2926 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2927 }
2928
2929 DragLayer.LayoutParams lp;
2930 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2931 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2932 } else {
2933 lp = new DragLayer.LayoutParams(width, height);
2934 }
2935
Adam Cohen307fe232012-08-16 17:55:58 -07002936 // The layout from which the folder is being opened may be scaled, adjust the starting
2937 // view size by this scale factor.
2938 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002939 lp.customPosition = true;
2940 lp.x = mRectForFolderAnimation.left;
2941 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002942 lp.width = (int) (scale * width);
2943 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002944
2945 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2946 fi.draw(mFolderIconCanvas);
2947 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002948 if (fi.getFolder() != null) {
2949 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2950 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002951 }
Adam Cohen268c4752012-06-06 17:47:33 -07002952 // Just in case this image view is still in the drag layer from a previous animation,
2953 // we remove it and re-add it.
2954 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2955 mDragLayer.removeView(mFolderIconImageView);
2956 }
2957 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002958 if (fi.getFolder() != null) {
2959 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002960 }
Adam Cohen268c4752012-06-06 17:47:33 -07002961 }
2962
Adam Cohen37b2a492016-04-06 18:36:06 -07002963 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002964 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002965 FolderInfo info = (FolderInfo) fi.getTag();
2966 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2967 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002968 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2969 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002970 }
2971
Adam Cohen268c4752012-06-06 17:47:33 -07002972 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2973 copyFolderIconToImage(fi);
2974 fi.setVisibility(View.INVISIBLE);
2975
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002976 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2977 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002978 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002979 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2980 }
2981 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002982 oa.start();
2983 }
2984
Sunny Goyal935fca12015-10-13 10:19:01 -07002985 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002986 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002987 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002988
Adam Cohen268c4752012-06-06 17:47:33 -07002989 // We remove and re-draw the FolderIcon in-case it has changed
2990 mDragLayer.removeView(mFolderIconImageView);
2991 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08002992
2993 if (cl != null) {
2994 cl.clearFolderLeaveBehind();
2995 }
2996
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002997 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002998 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002999 oa.addListener(new AnimatorListenerAdapter() {
3000 @Override
3001 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003002 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003003 // Remove the ImageView copy of the FolderIcon and make the original visible.
3004 mDragLayer.removeView(mFolderIconImageView);
3005 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003006 }
3007 }
3008 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003009 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003010 if (!animate) {
3011 oa.end();
3012 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003013 }
3014
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003015 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003016 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003017 * is animated relative to the specified View. If the View is null, no animation
3018 * is played.
3019 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003020 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003021 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003022 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003023
Adam Cohenfb91f302012-06-11 15:45:18 -07003024 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003025 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3026 if (openFolder != null && openFolder != folder) {
3027 // Close any open folder before opening a folder.
3028 closeFolder();
3029 }
3030
Adam Cohena9cf38f2011-05-02 15:36:58 -07003031 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003032
Adam Cohena9cf38f2011-05-02 15:36:58 -07003033 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003034
Sunny Goyalf4066152015-04-15 09:42:19 -07003035 // While the folder is open, the position of the icon cannot change.
3036 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3037
Adam Cohen4554ee12011-08-03 16:13:21 -07003038 // Just verify that the folder hasn't already been added to the DragLayer.
3039 // There was a one-off crash where the folder had a parent already.
3040 if (folder.getParent() == null) {
3041 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003042 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003043 } else {
3044 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3045 folder.getParent() + ").");
3046 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003047 folder.animateOpen();
3048
3049 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003050
3051 // Notify the accessibility manager that this folder "window" has appeared and occluded
3052 // the workspace items
3053 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3054 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003055 }
3056
3057 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003058 closeFolder(true);
3059 }
3060
3061 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003062 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003063 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003064 if (folder.isEditingName()) {
3065 folder.dismissEditingName();
3066 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003067 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003068 }
3069 }
3070
Sunny Goyal935fca12015-10-13 10:19:01 -07003071 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003072 animate &= !Utilities.isPowerSaverOn(this);
3073
Adam Cohen4554ee12011-08-03 16:13:21 -07003074 folder.getInfo().opened = false;
3075
3076 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3077 if (parent != null) {
3078 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003079 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003080 if (fi != null) {
3081 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3082 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003083 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003084 if (animate) {
3085 folder.animateClosed();
3086 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003087 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003088 }
Winson Chung83ca4802013-04-12 15:10:52 -07003089
Sunny Goyal935fca12015-10-13 10:19:01 -07003090 // Notify the accessibility manager that this folder "window" has disappeared and no
3091 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003092 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003093 }
3094
Tony Wickhamdadb3042016-02-24 11:07:00 -08003095 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003096 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003097 if (!isDraggingEnabled()) return false;
3098 if (isWorkspaceLocked()) return false;
3099 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003100
Winson Chung76648c52015-07-10 14:33:23 -07003101 if (v == mAllAppsButton) {
3102 onLongClickAllAppsButton(v);
3103 return true;
3104 }
3105
Adam Cohen1697b792013-09-17 19:08:21 -07003106 if (v instanceof Workspace) {
3107 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003108 if (!mWorkspace.isTouchActive()) {
3109 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003110 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3111 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3112 return true;
3113 } else {
3114 return false;
3115 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003116 } else {
3117 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003118 }
Adam Cohen1697b792013-09-17 19:08:21 -07003119 }
3120
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003121 CellLayout.CellInfo longClickCellInfo = null;
3122 View itemUnderLongClick = null;
3123 if (v.getTag() instanceof ItemInfo) {
3124 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003125 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003126 itemUnderLongClick = longClickCellInfo.cell;
3127 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003128 }
3129
Winson Chung3d503fb2011-07-13 17:25:49 -07003130 // The hotseat touch handling does not go through Workspace, and we always allow long press
3131 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003132 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003133 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003134 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003135 // User long pressed on empty space
3136 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3137 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003138 if (mWorkspace.isInOverviewMode()) {
3139 mWorkspace.startReordering(v);
3140 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003141 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003142 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003143 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003144 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3145 mHotseat.getOrderInHotseat(
3146 longClickCellInfo.cellX,
3147 longClickCellInfo.cellY));
3148 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003149 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003150 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003151 }
3152 }
3153 }
3154 return true;
3155 }
3156
Winson Chung3d503fb2011-07-13 17:25:49 -07003157 boolean isHotseatLayout(View layout) {
3158 return mHotseat != null && layout != null &&
3159 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3160 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003161
Winson Chung3d503fb2011-07-13 17:25:49 -07003162 /**
3163 * Returns the CellLayout of the specified container at the specified screen.
3164 */
Sunny Goyal92820592015-03-02 11:31:35 -08003165 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003166 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3167 if (mHotseat != null) {
3168 return mHotseat.getLayout();
3169 } else {
3170 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003171 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003172 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003173 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003174 }
3175 }
3176
Winson Chungb745afb2015-03-02 11:51:23 -08003177 /**
3178 * For overridden classes.
3179 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003180 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003181 return isAppsViewVisible();
3182 }
3183
3184 public boolean isAppsViewVisible() {
3185 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3186 }
3187
3188 public boolean isWidgetsViewVisible() {
3189 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003190 }
3191
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003192 private void setWorkspaceBackground(int background) {
3193 switch (background) {
3194 case WORKSPACE_BACKGROUND_TRANSPARENT:
3195 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3196 break;
3197 case WORKSPACE_BACKGROUND_BLACK:
3198 getWindow().setBackgroundDrawable(null);
3199 break;
3200 default:
3201 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3202 }
Craig Mautner360310b2012-10-26 15:13:08 -07003203 }
3204
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003205 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003206 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3207 int curflags = getWindow().getAttributes().flags
3208 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3209 if (wpflags != curflags) {
3210 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3211 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003212 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003213 }
3214
Michael Jurkae326f182011-11-21 14:05:46 -08003215 @Override
3216 public void onTrimMemory(int level) {
3217 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003218 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3219 // The widget preview db can result in holding onto over
3220 // 3MB of memory for caching which isn't necessary.
3221 SQLiteDatabase.releaseMemory();
3222
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003223 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003224 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003225 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003226 if (mLauncherCallbacks != null) {
3227 mLauncherCallbacks.onTrimMemory(level);
3228 }
Michael Jurkae326f182011-11-21 14:05:46 -08003229 }
3230
Winson5c6bdbb2015-09-03 11:36:19 -07003231 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003232 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003233 }
3234
Winson5c6bdbb2015-09-03 11:36:19 -07003235 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003236 boolean changed = mState != State.WORKSPACE ||
3237 mWorkspace.getState() != Workspace.State.NORMAL;
3238 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003239 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003240 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003241 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003242
Michael Jurkab3e22d92011-10-31 15:58:33 -07003243 // Set focus to the AppsCustomize button
3244 if (mAllAppsButton != null) {
3245 mAllAppsButton.requestFocus();
3246 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003247 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003248
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003249 // Change the state *after* we've called all the transition code
3250 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003251
Winson Chung5fb63472011-02-02 17:03:37 -08003252 // Resume the auto-advance of widgets
3253 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003254 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003255
Winson Chung0f785722015-04-08 10:27:49 -07003256 if (changed) {
3257 // Send an accessibility event to announce the context change
3258 getWindow().getDecorView()
3259 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003260 }
Winson5c6bdbb2015-09-03 11:36:19 -07003261 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003262 }
3263
Winsone9f27272015-10-13 10:47:51 -07003264 /**
3265 * Shows the overview button.
3266 */
Adam Cohened307df2013-10-02 09:37:31 -07003267 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003268 showOverviewMode(animated, false);
3269 }
3270
3271 /**
3272 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3273 * onto one of the overview panel buttons.
3274 */
3275 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3276 Runnable postAnimRunnable = null;
3277 if (requestButtonFocus) {
3278 postAnimRunnable = new Runnable() {
3279 @Override
3280 public void run() {
3281 // Hitting the menu button when in touch mode does not trigger touch mode to
3282 // be disabled, so if requested, force focus on one of the overview panel
3283 // buttons.
3284 mOverviewPanel.requestFocusFromTouch();
3285 }
3286 };
3287 }
Adam Cohened307df2013-10-02 09:37:31 -07003288 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003289 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003290 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003291 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003292 }
3293
Winson Chungb745afb2015-03-02 11:51:23 -08003294 /**
3295 * Shows the apps view.
3296 */
Winson Chung76648c52015-07-10 14:33:23 -07003297 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3298 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003299 if (resetListToTop) {
3300 mAppsView.scrollToTop();
3301 }
Winson Chung4ac30062015-05-08 17:34:17 -07003302 if (updatePredictedApps) {
3303 tryAndUpdatePredictedApps();
3304 }
Winson Chung76648c52015-07-10 14:33:23 -07003305 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003306 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003307
Winson Chungb745afb2015-03-02 11:51:23 -08003308 /**
3309 * Shows the widgets view.
3310 */
3311 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003312 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003313 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003314 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003315 }
Winson Chung76648c52015-07-10 14:33:23 -07003316 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003317
3318 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003319 @Override
3320 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003321 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003322 }
3323 });
Winson Chungb745afb2015-03-02 11:51:23 -08003324 }
3325
3326 /**
3327 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003328 *
3329 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003330 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003331 // TODO: calling method should use the return value so that when {@code false} is returned
3332 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003333 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003334 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3335 mState != State.WIDGETS_SPRING_LOADED) {
3336 return false;
3337 }
3338 if (toState != State.APPS && toState != State.WIDGETS) {
3339 return false;
3340 }
Winson Chungb745afb2015-03-02 11:51:23 -08003341
3342 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003343 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3344 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003345 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003346 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003347 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003348
Michael Jurkab3e22d92011-10-31 15:58:33 -07003349 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003350 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003351
3352 // Pause the auto-advance of widgets until we are out of AllApps
3353 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003354 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003355 closeFolder();
3356
3357 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003358 getWindow().getDecorView()
3359 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003360 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003361 }
3362
Winson Chungcd99cd32015-04-29 11:03:24 -07003363 /**
3364 * Updates the workspace and interaction state on state change, and return the animation to this
3365 * new state.
3366 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003367 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003368 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003369 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003370 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003371 updateInteraction(fromState, toState);
3372 return anim;
3373 }
3374
Jun Mukaif0033da2015-08-04 18:34:30 -07003375 public void onLauncherClingShown() {
3376 // When a launcher cling appears, it should cover the underlying layers, so their focus
3377 // should be blocked.
3378 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3379 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3380 }
3381 }
3382
3383 public void onLauncherClingDismissed() {
3384 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3385 }
3386
Hyunyoung Song3f471442015-04-08 19:01:34 -07003387 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003388 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003389 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003390 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003391 }
Winson Chungb745afb2015-03-02 11:51:23 -08003392
Winson Chung006ee262015-08-03 14:40:11 -07003393 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003394 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003395 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003396
3397 if (isAppsViewVisible()) {
3398 mState = State.APPS_SPRING_LOADED;
3399 } else if (isWidgetsViewVisible()) {
3400 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003401 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003402 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003403 } else {
3404 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003405 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003406 }
Adam Cohen7777d962011-08-18 18:58:38 -07003407
Hyunyoung Song3f471442015-04-08 19:01:34 -07003408 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003409 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003410 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003411
Winson Chunge7a03942011-08-05 15:05:12 -07003412 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003413 @Override
3414 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003415 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003416 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3417 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003418 // Before we show workspace, hide all apps again because
3419 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3420 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003421 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003422 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003423 } else {
3424 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003425 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003426 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003427 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003428 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003429
Tony Wickhame0c33232016-02-08 11:37:04 -08003430 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003431 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3432 || mState == State.WIDGETS_SPRING_LOADED;
3433 }
3434
Michael Jurkad3ef3062010-11-23 16:23:58 -08003435 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003436 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003437 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003438 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003439 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003440 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003441 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3442 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003443 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003444 }
3445
Winson Chung4ac30062015-05-08 17:34:17 -07003446 /**
3447 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3448 * resumed.
3449 */
3450 private void tryAndUpdatePredictedApps() {
3451 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003452 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003453 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003454 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003455 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003456 }
3457 }
3458 }
3459
Michael Jurkab3e22d92011-10-31 15:58:33 -07003460 void lockAllApps() {
3461 // TODO
3462 }
3463
3464 void unlockAllApps() {
3465 // TODO
3466 }
3467
Winsonf768d932015-09-25 16:12:35 -07003468 public boolean launcherCallbacksProvidesSearch() {
3469 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3470 }
3471
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003472 public View getOrCreateQsbBar() {
Winsonf768d932015-09-25 16:12:35 -07003473 if (launcherCallbacksProvidesSearch()) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003474 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003475 }
3476
Adam Cohen24ce0b32014-01-14 16:18:14 -08003477 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003478 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3479 if (searchProvider == null) {
3480 return null;
3481 }
3482
3483 Bundle opts = new Bundle();
3484 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003485 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003486
Tony Wickham3a3517f2015-10-06 11:27:04 -07003487 // Determine the min and max dimensions of the widget.
3488 LauncherAppState app = LauncherAppState.getInstance();
3489 DeviceProfile portraitProfile = app.getInvariantDeviceProfile().portraitProfile;
3490 DeviceProfile landscapeProfile = app.getInvariantDeviceProfile().landscapeProfile;
3491 float density = getResources().getDisplayMetrics().density;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003492 Point searchDimens = portraitProfile.getSearchBarDimensForWidgetOpts(getResources());
3493 int maxHeight = (int) (searchDimens.y / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003494 int minHeight = maxHeight;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003495 int maxWidth = (int) (searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003496 int minWidth = maxWidth;
3497 if (!landscapeProfile.isVerticalBarLayout()) {
Tony Wickhama3c74d12015-10-23 11:43:47 -07003498 searchDimens = landscapeProfile.getSearchBarDimensForWidgetOpts(getResources());
3499 maxHeight = (int) Math.max(maxHeight, searchDimens.y / density);
3500 minHeight = (int) Math.min(minHeight, searchDimens.y / density);
3501 maxWidth = (int) Math.max(maxWidth, searchDimens.x / density);
3502 minWidth = (int) Math.min(minWidth, searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003503 }
3504 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, maxHeight);
3505 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, minHeight);
3506 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, maxWidth);
3507 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, minWidth);
Tony Wickhameef44322015-10-20 13:24:36 -07003508 if (LOGD) {
3509 Log.d(TAG, "QSB widget options: maxHeight=" + maxHeight + " minHeight=" + minHeight
3510 + " maxWidth=" + maxWidth + " minWidth=" + minWidth);
3511 }
Tony Wickham3a3517f2015-10-06 11:27:04 -07003512
Tony Wickham775455c2015-10-16 09:49:32 -07003513 if (mLauncherCallbacks != null) {
3514 opts.putAll(mLauncherCallbacks.getAdditionalSearchWidgetOptions());
3515 }
3516
Sunny Goyalf7258242015-10-19 16:59:07 -07003517 int widgetId = mSharedPrefs.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003518 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003519 if (!searchProvider.provider.flattenToString().equals(
Sunny Goyalf7258242015-10-19 16:59:07 -07003520 mSharedPrefs.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003521 || (widgetInfo == null)
3522 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003523 // A valid widget is not already bound.
3524 if (widgetId > -1) {
3525 mAppWidgetHost.deleteAppWidgetId(widgetId);
3526 widgetId = -1;
3527 }
3528
3529 // Try to bind a new widget
3530 widgetId = mAppWidgetHost.allocateAppWidgetId();
3531
3532 if (!AppWidgetManagerCompat.getInstance(this)
3533 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3534 mAppWidgetHost.deleteAppWidgetId(widgetId);
3535 widgetId = -1;
3536 }
3537
Sunny Goyalf7258242015-10-19 16:59:07 -07003538 mSharedPrefs.edit()
Sunny Goyal594d76d2014-11-06 10:12:54 -08003539 .putInt(QSB_WIDGET_ID, widgetId)
3540 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
Sunny Goyald8043982015-12-17 22:23:02 -08003541 .apply();
Sunny Goyal594d76d2014-11-06 10:12:54 -08003542 }
3543
Sunny Goyal8d595092015-07-06 12:22:14 -07003544 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003545 if (widgetId != -1) {
3546 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
Sunny Goyalebfae6e2015-08-27 15:30:25 -07003547 mQsb.setId(R.id.qsb_widget);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003548 mQsb.updateAppWidgetOptions(opts);
3549 mQsb.setPadding(0, 0, 0, 0);
3550 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003551 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003552 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003553 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003554 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003555 }
3556
Sunny Goyal22235bc2015-04-03 09:23:43 -07003557 private void reinflateQSBIfNecessary() {
3558 if (mQsb instanceof LauncherAppWidgetHostView &&
3559 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3560 mSearchDropTargetBar.removeView(mQsb);
3561 mQsb = null;
3562 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3563 }
3564 }
3565
Michael Jurkad7c28052012-04-27 15:43:36 -07003566 @Override
3567 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003568 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003569 final List<CharSequence> text = event.getText();
3570 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003571 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003572 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003573 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003574 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003575 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003576 } else if (mWorkspace != null) {
3577 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003578 } else {
3579 text.add(getString(R.string.all_apps_home_button_label));
3580 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003581 return result;
3582 }
3583
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003584 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003585 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003586 */
Adam Cohen091440a2015-03-18 14:16:05 -07003587 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003588 @Override
3589 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003590 closeSystemDialogs();
3591 }
3592 }
3593
3594 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003595 * If the activity is currently paused, signal that we need to run the passed Runnable
3596 * in onResume.
3597 *
3598 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003599 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3600 * wrong when we're not running, and if the activity comes back to what the configuration was
3601 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003602 *
3603 * Implementation of the method from LauncherModel.Callbacks.
3604 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003605 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003606 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003607 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003608 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003609 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003610 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003611 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003612 }
3613 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003614 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003615 return true;
3616 } else {
3617 return false;
3618 }
3619 }
3620
Michael Jurkac402cd92013-05-20 15:49:32 +02003621 private boolean waitUntilResume(Runnable run) {
3622 return waitUntilResume(run, false);
3623 }
3624
Michael Jurka1e2f4652013-07-08 18:03:46 -07003625 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003626 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003627 }
3628
Michael Jurka7607c2f2013-04-03 14:33:19 -07003629 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003630 * If the activity is currently paused, signal that we need to re-run the loader
3631 * in onResume.
3632 *
3633 * This needs to be called from incoming places where resources might have been loaded
3634 * while we are paused. That is becaues the Configuration might be wrong
3635 * when we're not running, and if it comes back to what it was when we
3636 * were paused, we are not restarted.
3637 *
3638 * Implementation of the method from LauncherModel.Callbacks.
3639 *
3640 * @return true if we are currently paused. The caller might be able to
3641 * skip some work in that case since we will come back again.
3642 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003643 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003644 public boolean setLoadOnResume() {
3645 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003646 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003647 mOnResumeNeedsLoad = true;
3648 return true;
3649 } else {
3650 return false;
3651 }
3652 }
3653
3654 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003655 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003656 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003657 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003658 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003659 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003660 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003661 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003662 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003663 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003664 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003665
Joe Onorato9c1289c2009-08-17 11:03:03 -04003666 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003667 * Clear any pending bind callbacks. This is called when is loader is planning to
3668 * perform a full rebind from scratch.
3669 */
3670 @Override
3671 public void clearPendingBinds() {
3672 mBindOnResumeCallbacks.clear();
3673 if (mPendingExecutor != null) {
3674 mPendingExecutor.markCompleted();
3675 mPendingExecutor = null;
3676 }
3677 }
3678
3679 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003680 * Refreshes the shortcuts shown on the workspace.
3681 *
3682 * Implementation of the method from LauncherModel.Callbacks.
3683 */
3684 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003685 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003686
Winson Chungd64d1762013-08-20 14:37:16 -07003687 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003688 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003689 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003690
Michael Jurka05bf644e2011-11-30 20:28:53 -08003691 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003692 if (mHotseat != null) {
3693 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003694 }
3695 }
3696
Adam Cohendcd297f2013-06-18 13:13:40 -07003697 @Override
3698 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003699 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003700
Adam Cohen5084cba2013-09-03 12:01:16 -07003701 // If there are no screens, we need to have an empty screen
3702 if (orderedScreenIds.size() == 0) {
3703 mWorkspace.addExtraEmptyScreen();
3704 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003705
3706 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003707 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003708 if (hasCustomContentToLeft()) {
3709 mWorkspace.createCustomContentContainer();
3710 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003711 }
Winson Chung64359a52013-07-08 17:17:08 -07003712 }
3713
3714 @Override
3715 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003716 int count = orderedScreenIds.size();
3717 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003718 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003719 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003720 }
3721
Winson Chungd64d1762013-08-20 14:37:16 -07003722 public void bindAppsAdded(final ArrayList<Long> newScreens,
3723 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003724 final ArrayList<ItemInfo> addAnimated,
3725 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003726 Runnable r = new Runnable() {
3727 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003728 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003729 }
3730 };
3731 if (waitUntilResume(r)) {
3732 return;
3733 }
3734
Winson Chungd64d1762013-08-20 14:37:16 -07003735 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003736 if (newScreens != null) {
3737 bindAddScreens(newScreens);
3738 }
Winson Chungd64d1762013-08-20 14:37:16 -07003739
3740 // We add the items without animation on non-visible pages, and with
3741 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003742 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003743 bindItems(addNotAnimated, 0,
3744 addNotAnimated.size(), false);
3745 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003746 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003747 bindItems(addAnimated, 0,
3748 addAnimated.size(), true);
3749 }
Winson Chungc58497e2013-09-03 17:48:37 -07003750
Winson Chung87412982013-10-03 18:34:14 -07003751 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003752 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003753
Winson Chungb745afb2015-03-02 11:51:23 -08003754 if (addedApps != null && mAppsView != null) {
3755 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003756 }
Winson Chungd64d1762013-08-20 14:37:16 -07003757 }
3758
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003759 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003760 * Bind the items start-end from the list.
3761 *
3762 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003763 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003764 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003765 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3766 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003767 Runnable r = new Runnable() {
3768 public void run() {
3769 bindItems(shortcuts, start, end, forceAnimateIcons);
3770 }
3771 };
3772 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003773 return;
3774 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003775
Winson Chungf0c6ae02012-03-21 16:10:31 -07003776 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003777 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3778 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003779 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003780 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003781 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003782 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003783 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003784
3785 // Short circuit if we are loading dock items for a configuration which has no dock
3786 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3787 mHotseat == null) {
3788 continue;
3789 }
3790
Sunny Goyal639e9062015-08-19 19:17:06 -07003791 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003792 switch (item.itemType) {
3793 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3794 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003795 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003796 view = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003797
Winson Chung64359a52013-07-08 17:17:08 -07003798 /*
3799 * TODO: FIX collision case
3800 */
Winson Chungce376632013-07-11 16:12:41 -07003801 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3802 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3803 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003804 View v = cl.getChildAt(item.cellX, item.cellY);
3805 Object tag = v.getTag();
3806 String desc = "Collision while binding workspace item: " + item
3807 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003808 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003809 throw (new RuntimeException(desc));
3810 } else {
3811 Log.d(TAG, desc);
3812 }
Winson Chungce376632013-07-11 16:12:41 -07003813 }
Winson Chung64359a52013-07-08 17:17:08 -07003814 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003815 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003816 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003817 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003818 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003819 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003820 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003821 default:
3822 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003823 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003824
3825 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3826 item.cellY, 1, 1);
3827 if (animateIcons) {
3828 // Animate all the applications up now
3829 view.setAlpha(0f);
3830 view.setScaleX(0f);
3831 view.setScaleY(0f);
3832 bounceAnims.add(createNewAppBounceAnimation(view, i));
3833 newShortcutsScreenId = item.screenId;
3834 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003835 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003836
Winson Chung997a9232013-07-24 15:33:46 -07003837 if (animateIcons) {
3838 // Animate to the correct page
3839 if (newShortcutsScreenId > -1) {
3840 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003841 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003842 final Runnable startBounceAnimRunnable = new Runnable() {
3843 public void run() {
3844 anim.playTogether(bounceAnims);
3845 anim.start();
3846 }
3847 };
Winson Chung997a9232013-07-24 15:33:46 -07003848 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003849 // We post the animation slightly delayed to prevent slowdowns
3850 // when we are loading right after we return to launcher.
3851 mWorkspace.postDelayed(new Runnable() {
3852 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003853 if (mWorkspace != null) {
3854 mWorkspace.snapToPage(newScreenIndex);
3855 mWorkspace.postDelayed(startBounceAnimRunnable,
3856 NEW_APPS_ANIMATION_DELAY);
3857 }
Winson Chung94d67682013-09-25 16:29:40 -07003858 }
3859 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003860 } else {
3861 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003862 }
3863 }
Winson Chung64359a52013-07-08 17:17:08 -07003864 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003865 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003866 }
3867
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003868 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3869 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3870 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003871 view.updateAppWidget(null);
3872 view.setOnClickListener(this);
3873 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003874 mWorkspace.requestLayout();
3875 }
3876
Joe Onorato9c1289c2009-08-17 11:03:03 -04003877 /**
3878 * Add the views for a widget to the workspace.
3879 *
3880 * Implementation of the method from LauncherModel.Callbacks.
3881 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003882 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003883 Runnable r = new Runnable() {
3884 public void run() {
3885 bindAppWidget(item);
3886 }
3887 };
3888 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003889 return;
3890 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003891
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003892 if (mIsSafeModeEnabled) {
3893 bindSafeModeWidget(item);
3894 return;
3895 }
3896
Daniel Sandler843e8602010-06-07 14:59:01 -04003897 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3898 if (DEBUG_WIDGETS) {
3899 Log.d(TAG, "bindAppWidget: " + item);
3900 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003901
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003902 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003903
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003904 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3905 // If the provider is not ready, bind as a pending widget.
3906 appWidgetInfo = null;
3907 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3908 // The widget id is not valid. Try to find the widget based on the provider info.
3909 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3910 } else {
3911 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3912 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003913
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003914 // If the provider is ready, but the width is not yet restored, try to restore it.
3915 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3916 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003917 if (appWidgetInfo == null) {
3918 if (DEBUG_WIDGETS) {
3919 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3920 + " belongs to component " + item.providerName
3921 + ", as the povider is null");
3922 }
3923 LauncherModel.deleteItemFromDatabase(this, item);
3924 return;
3925 }
Sunny Goyalff572272014-07-23 13:58:07 -07003926
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003927 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003928 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003929 // Note: This assumes that the id remap broadcast is received before this step.
3930 // If that is not the case, the id remap will be ignored and user may see the
3931 // click to setup view.
Sunny Goyal16466f12016-03-10 05:34:30 -08003932 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003933 pendingInfo.spanX = item.spanX;
3934 pendingInfo.spanY = item.spanY;
3935 pendingInfo.minSpanX = item.minSpanX;
3936 pendingInfo.minSpanY = item.minSpanY;
Sunny Goyalb740f592015-12-17 23:22:42 -08003937 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003938
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003939 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
3940 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3941 newWidgetId, appWidgetInfo, options);
3942
3943 // TODO consider showing a permission dialog when the widget is clicked.
3944 if (!success) {
3945 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3946 if (DEBUG_WIDGETS) {
3947 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3948 + " belongs to component " + item.providerName
3949 + ", as the launcher is unable to bing a new widget id");
3950 }
3951 LauncherModel.deleteItemFromDatabase(this, item);
3952 return;
Sunny Goyalff572272014-07-23 13:58:07 -07003953 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003954
3955 item.appWidgetId = newWidgetId;
3956
3957 // If the widget has a configure activity, it is still needs to set it up, otherwise
3958 // the widget is ready to go.
3959 item.restoreStatus = (appWidgetInfo.configure == null)
3960 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3961 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3962
3963 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003964 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003965 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003966 // The widget was marked as UI not ready, but there is no configure activity to
3967 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003968 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3969 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003970 }
Sunny Goyalff572272014-07-23 13:58:07 -07003971 }
3972
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003973 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003974 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003975 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3976 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003977 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003978
Sunny Goyal56c73602015-09-25 12:55:01 -07003979 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003980 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003981 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003982 deleteWidgetInfo(item);
3983 return;
3984 }
3985
Sunny Goyal233ee962015-08-03 13:05:01 -07003986 item.minSpanX = appWidgetInfo.minSpanX;
3987 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003988 addAppWidgetToWorkspace(
3989 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3990 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003991 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003992 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003993 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003994 view.updateAppWidget(null);
3995 view.setOnClickListener(this);
3996 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003997 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003998 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003999
Daniel Sandler843e8602010-06-07 14:59:01 -04004000 if (DEBUG_WIDGETS) {
4001 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4002 + (SystemClock.uptimeMillis()-start) + "ms");
4003 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004004 }
4005
Sunny Goyalff572272014-07-23 13:58:07 -07004006 /**
4007 * Restores a pending widget.
4008 *
4009 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07004010 */
4011 private void completeRestoreAppWidget(final int appWidgetId) {
4012 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4013 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4014 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4015 return;
4016 }
4017
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004018 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004019 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4020
4021 mWorkspace.reinflateWidgetsIfNecessary();
4022 LauncherModel.updateItemInDatabase(this, info);
4023 }
4024
Adam Cohen1462de32012-07-24 22:34:36 -07004025 public void onPageBoundSynchronously(int page) {
4026 mSynchronouslyBoundPages.add(page);
4027 }
4028
Sunny Goyal527c7d32015-08-28 15:19:36 -07004029 @Override
4030 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
4031 if (mPendingExecutor != null) {
4032 mPendingExecutor.markCompleted();
4033 }
4034 mPendingExecutor = executor;
4035 executor.attachTo(this);
4036 }
4037
4038 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4039 if (mPendingExecutor == executor) {
4040 mPendingExecutor = null;
4041 }
4042 }
4043
Joe Onorato9c1289c2009-08-17 11:03:03 -04004044 /**
4045 * Callback saying that there aren't any more items to bind.
4046 *
4047 * Implementation of the method from LauncherModel.Callbacks.
4048 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004049 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004050 Runnable r = new Runnable() {
4051 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004052 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004053 }
4054 };
4055 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004056 return;
4057 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004058 if (mSavedState != null) {
4059 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004060 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004061 }
Sunny Goyal08442b82015-10-21 17:15:08 -07004062
Joe Onorato9c1289c2009-08-17 11:03:03 -04004063 mSavedState = null;
4064 }
4065
Adam Cohen1462de32012-07-24 22:34:36 -07004066 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004067
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004068 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07004069
4070 // If we received the result of any pending adds while the loader was running (e.g. the
4071 // widget configuration forced an orientation change), process them now.
4072 if (sPendingAddItem != null) {
4073 final long screenId = completeAdd(sPendingAddItem);
4074
4075 // TODO: this moves the user to the page where the pending item was added. Ideally,
4076 // the screen would be guaranteed to exist after bind, and the page would be set through
4077 // the workspace restore process.
4078 mWorkspace.post(new Runnable() {
4079 @Override
4080 public void run() {
4081 mWorkspace.snapToScreenId(screenId);
4082 }
4083 });
4084 sPendingAddItem = null;
4085 }
4086
Sunny Goyal756adbc2015-04-16 15:20:51 -07004087 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004088
4089 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004090 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004091 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004092 }
4093
Winson Chunga0b7e862013-09-05 16:03:15 -07004094 public boolean isAllAppsButtonRank(int rank) {
4095 if (mHotseat != null) {
4096 return mHotseat.isAllAppsButtonRank(rank);
4097 }
4098 return false;
4099 }
4100
Winson Chunga2413752012-04-03 14:22:34 -07004101 private boolean canRunNewAppsAnimation() {
4102 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004103 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4104 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004105 }
4106
Winson Chungc9168342013-06-26 14:54:55 -07004107 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004108 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004109 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4110 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004111 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004112 return bounceAnim;
4113 }
4114
Adam Cohen27772732013-11-11 14:00:56 +00004115 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004116 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004117 }
4118
Tony Wickham3a3517f2015-10-06 11:27:04 -07004119 /** Returns the search bar bounds in pixels. */
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004120 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004121 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004122 }
4123
Tony Wickham55616cd2015-09-23 14:55:17 -07004124 public int getSearchBarHeight() {
4125 if (mLauncherCallbacks != null) {
4126 return mLauncherCallbacks.getSearchBarHeight();
4127 }
4128 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4129 }
4130
Winson Chung88fa7412015-08-03 14:25:28 -07004131 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004132 if (mSearchDropTargetBar == null) {
4133 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004134 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004135 if (mQsb != null) {
4136 mSearchDropTargetBar.removeView(mQsb);
4137 mQsb = null;
4138 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004139 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004140 }
4141
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004142 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004143 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4144 * multiple calls to bind the same list.)
4145 */
4146 @Thunk ArrayList<AppInfo> mTmpAppsList;
4147 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4148 public void run() {
4149 bindAllApplications(mTmpAppsList);
4150 mTmpAppsList = null;
4151 }
4152 };
4153
4154 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004155 * Add the icons for all apps.
4156 *
4157 * Implementation of the method from LauncherModel.Callbacks.
4158 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004159 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004160 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4161 mTmpAppsList = apps;
4162 return;
4163 }
4164
Winson Chungb745afb2015-03-02 11:51:23 -08004165 if (mAppsView != null) {
4166 mAppsView.setApps(apps);
4167 }
Adam Cohen9211d422014-10-07 18:14:53 -07004168 if (mLauncherCallbacks != null) {
4169 mLauncherCallbacks.bindAllApplications(apps);
4170 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004171 }
4172
4173 /**
4174 * A package was updated.
4175 *
4176 * Implementation of the method from LauncherModel.Callbacks.
4177 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004178 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004179 Runnable r = new Runnable() {
4180 public void run() {
4181 bindAppsUpdated(apps);
4182 }
4183 };
4184 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004185 return;
4186 }
4187
Winson Chungb745afb2015-03-02 11:51:23 -08004188 if (mAppsView != null) {
4189 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004190 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004191 }
4192
Sunny Goyal4390ace2014-10-13 11:33:11 -07004193 @Override
4194 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4195 Runnable r = new Runnable() {
4196 public void run() {
4197 bindWidgetsRestored(widgets);
4198 }
4199 };
4200 if (waitUntilResume(r)) {
4201 return;
4202 }
4203 mWorkspace.widgetsRestored(widgets);
4204 }
4205
Joe Onorato9c1289c2009-08-17 11:03:03 -04004206 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004207 * Some shortcuts were updated in the background.
4208 *
4209 * Implementation of the method from LauncherModel.Callbacks.
4210 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004211 @Override
4212 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4213 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004214 Runnable r = new Runnable() {
4215 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004216 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004217 }
4218 };
4219 if (waitUntilResume(r)) {
4220 return;
4221 }
4222
Sunny Goyal4390ace2014-10-13 11:33:11 -07004223 if (!updated.isEmpty()) {
4224 mWorkspace.updateShortcuts(updated);
4225 }
4226
4227 if (!removed.isEmpty()) {
4228 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4229 for (ShortcutInfo si : removed) {
4230 removedComponents.add(si.getTargetComponent());
4231 }
4232 mWorkspace.removeItemsByComponentName(removedComponents, user);
4233 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004234 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004235 }
4236 }
4237
4238 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004239 * Update the state of a package, typically related to install state.
4240 *
4241 * Implementation of the method from LauncherModel.Callbacks.
4242 */
Sunny Goyale755d462014-07-22 13:48:29 -07004243 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004244 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4245 Runnable r = new Runnable() {
4246 public void run() {
4247 bindRestoreItemsChange(updates);
4248 }
4249 };
4250 if (waitUntilResume(r)) {
4251 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004252 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004253
Sunny Goyal756adbc2015-04-16 15:20:51 -07004254 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004255 }
4256
4257 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004258 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004259 * in addition to specific applications to remove, the reason being that
4260 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004261 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004262 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004263 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004264 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004265 public void bindWorkspaceComponentsRemoved(
4266 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4267 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004268 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004269 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004270 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004271 }
Winson Chungd64d1762013-08-20 14:37:16 -07004272 };
4273 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004274 return;
4275 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004276 if (!packageNames.isEmpty()) {
4277 mWorkspace.removeItemsByPackageName(packageNames, user);
4278 }
4279 if (!components.isEmpty()) {
4280 mWorkspace.removeItemsByComponentName(components, user);
4281 }
4282 // Notify the drag controller
4283 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004284
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004285 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004286
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004287 @Override
4288 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4289 Runnable r = new Runnable() {
4290 public void run() {
4291 bindAppInfosRemoved(appInfos);
4292 }
4293 };
4294 if (waitUntilResume(r)) {
4295 return;
Joe Onorato36115782010-06-17 13:28:48 -04004296 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004297
Winson Chungdf95eb12013-10-16 14:57:07 -07004298 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004299 if (mAppsView != null) {
4300 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004301 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004302 }
Joe Onoratobe386092009-11-17 17:32:16 -08004303
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004304 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004305 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004306 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004307 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004308 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004309
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004310 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004311 public void bindWidgetsModel(WidgetsModel model) {
4312 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004313 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004314 return;
4315 }
4316
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004317 if (mWidgetsView != null && model != null) {
4318 mWidgetsView.addWidgets(model);
4319 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004320 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004321 }
4322
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004323 @Override
4324 public void notifyWidgetProvidersChanged() {
4325 if (mWorkspace.getState().shouldUpdateWidget) {
4326 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4327 }
4328 }
4329
Winson Chung400438b2011-01-16 17:53:48 -08004330 private int mapConfigurationOriActivityInfoOri(int configOri) {
4331 final Display d = getWindowManager().getDefaultDisplay();
4332 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4333 switch (d.getRotation()) {
4334 case Surface.ROTATION_0:
4335 case Surface.ROTATION_180:
4336 // We are currently in the same basic orientation as the natural orientation
4337 naturalOri = configOri;
4338 break;
4339 case Surface.ROTATION_90:
4340 case Surface.ROTATION_270:
4341 // We are currently in the other basic orientation to the natural orientation
4342 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4343 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4344 break;
4345 }
4346
4347 int[] oriMap = {
4348 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4349 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4350 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4351 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4352 };
4353 // Since the map starts at portrait, we need to offset if this device's natural orientation
4354 // is landscape.
4355 int indexOffset = 0;
4356 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4357 indexOffset = 1;
4358 }
4359 return oriMap[(d.getRotation() + indexOffset) % 4];
4360 }
Adam Cohen446e9402011-09-15 18:21:21 -07004361
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004362 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004363 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004364 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004365 if (Utilities.ATLEAST_JB_MR2) {
4366 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4367 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004368 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4369 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004370 }
Winson Chung4b919f82012-05-01 10:44:08 -07004371 }
4372 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004373
Winson Chung4b919f82012-05-01 10:44:08 -07004374 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004375 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004376 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004377 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004378 } else {
4379 mHandler.postDelayed(new Runnable() {
4380 public void run() {
4381 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4382 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004383 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004384 }
Winson Chung4b919f82012-05-01 10:44:08 -07004385 }
Winson Chung400438b2011-01-16 17:53:48 -08004386 }
4387
Winson Chunge43a1e72014-01-15 10:33:02 -08004388 protected boolean isLauncherPreinstalled() {
4389 PackageManager pm = getPackageManager();
4390 try {
4391 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4392 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4393 return true;
4394 } else {
4395 return false;
4396 }
4397 } catch (NameNotFoundException e) {
4398 e.printStackTrace();
4399 return false;
4400 }
4401 }
4402
Adam Cohenea90f832014-05-21 15:03:34 -07004403 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004404 * To be overridden by subclasses to indicate that there is an activity to launch
4405 * before showing the standard launcher experience.
4406 */
4407 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004408 if (mLauncherCallbacks != null) {
4409 return mLauncherCallbacks.hasFirstRunActivity();
4410 }
Adam Cohen432609a2014-03-13 17:03:22 -07004411 return false;
4412 }
4413
4414 /**
4415 * To be overridden by subclasses to launch any first run activity
4416 */
4417 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004418 if (mLauncherCallbacks != null) {
4419 return mLauncherCallbacks.getFirstRunActivity();
4420 }
Adam Cohen432609a2014-03-13 17:03:22 -07004421 return null;
4422 }
4423
Winson Chunga6945242014-01-08 14:04:34 -08004424 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004425 return !ActivityManager.isRunningInTestHarness() &&
4426 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004427 }
4428
Adam Cohen4a9423d2014-03-28 14:22:31 -07004429 protected boolean hasRunFirstRunActivity() {
4430 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4431 }
4432
Adam Cohen432609a2014-03-13 17:03:22 -07004433 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004434 if (shouldRunFirstRunActivity() &&
4435 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004436 Intent firstRunIntent = getFirstRunActivity();
4437 if (firstRunIntent != null) {
4438 startActivity(firstRunIntent);
4439 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004440 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004441 }
4442 }
Adam Cohen432609a2014-03-13 17:03:22 -07004443 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004444 }
4445
4446 private void markFirstRunActivityShown() {
4447 SharedPreferences.Editor editor = mSharedPrefs.edit();
4448 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4449 editor.apply();
4450 }
4451
Adam Cohen432609a2014-03-13 17:03:22 -07004452 /**
4453 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4454 * screen that must be displayed and dismissed.
4455 */
4456 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004457 if (mLauncherCallbacks != null) {
4458 return mLauncherCallbacks.hasDismissableIntroScreen();
4459 }
Adam Cohen432609a2014-03-13 17:03:22 -07004460 return false;
4461 }
4462
4463 /**
4464 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4465 */
4466 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004467 if (mLauncherCallbacks != null) {
4468 return mLauncherCallbacks.getIntroScreen();
4469 }
Adam Cohen432609a2014-03-13 17:03:22 -07004470 return null;
4471 }
4472
4473 /**
4474 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4475 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4476 */
4477 private boolean shouldShowIntroScreen() {
4478 return hasDismissableIntroScreen() &&
4479 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4480 }
4481
4482 protected void showIntroScreen() {
4483 View introScreen = getIntroScreen();
4484 changeWallpaperVisiblity(false);
4485 if (introScreen != null) {
4486 mDragLayer.showOverlayView(introScreen);
4487 }
4488 }
4489
4490 public void dismissIntroScreen() {
4491 markIntroScreenDismissed();
4492 if (showFirstRunActivity()) {
4493 // We delay hiding the intro view until the first run activity is showing. This
4494 // avoids a blip.
4495 mWorkspace.postDelayed(new Runnable() {
4496 @Override
4497 public void run() {
4498 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004499 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004500 }
4501 }, ACTIVITY_START_DELAY);
4502 } else {
4503 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004504 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004505 }
4506 changeWallpaperVisiblity(true);
4507 }
4508
4509 private void markIntroScreenDismissed() {
4510 SharedPreferences.Editor editor = mSharedPrefs.edit();
4511 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4512 editor.apply();
4513 }
4514
Adam Cohen091440a2015-03-18 14:16:05 -07004515 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004516 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4517 // on the device, then we always show the first run cling experience (or if there is no
4518 // launcher2). Otherwise, we prompt the user upon started for migration
4519 LauncherClings launcherClings = new LauncherClings(this);
4520 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004521 mClings = launcherClings;
Sunny Goyal88d60f12014-09-08 03:47:29 -07004522 if (mModel.canMigrateFromOldLauncherDb(this)) {
4523 launcherClings.showMigrationCling();
4524 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004525 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004526 }
4527 }
4528 }
4529
Winson Chunga6945242014-01-08 14:04:34 -08004530 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004531 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4532 if (mHotseat != null) mHotseat.setAlpha(1f);
4533 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004534 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4535 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004536 }
4537
4538 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004539 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4540 if (mHotseat != null) mHotseat.setAlpha(0f);
4541 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004542 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4543 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004544 }
4545
Winson Chung5ffd51d2015-06-25 11:36:50 -07004546 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004547 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004548 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004549 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004550 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004551 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004552 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4553 if (userHandle != null) {
4554 user = UserHandleCompat.fromUser(userHandle);
4555 }
4556 }
4557 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004558 }
4559
4560 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004561 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004562 if (user == null) {
4563 user = UserHandleCompat.myUserHandle();
4564 }
4565
4566 // Called from search suggestion, add the profile extra to the intent to ensure that we
4567 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004568 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004569 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004570 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004571 return null;
4572 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004573 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004574 }
4575
Winson Chung5ffd51d2015-06-25 11:36:50 -07004576 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004577 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4578 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004579 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004580 UserHandleCompat.myUserHandle());
4581 }
4582
Winson Chung5ffd51d2015-06-25 11:36:50 -07004583 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004584 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4585 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004586 mWorkspace.onExternalDragStartedWithItem(dragView);
4587 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004588 }
4589
Winson Chung5ffd51d2015-06-25 11:36:50 -07004590 protected void moveWorkspaceToDefaultScreen() {
4591 mWorkspace.moveToDefaultScreen(false);
4592 }
4593
Winson Chung80baf5a2010-08-09 16:03:15 -07004594 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004595 * Returns a FastBitmapDrawable with the icon, accurately sized.
4596 */
4597 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4598 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4599 d.setFilterBitmap(true);
4600 resizeIconDrawable(d);
4601 return d;
4602 }
4603
4604 /**
4605 * Resizes an icon drawable to the correct icon size.
4606 */
Winson5fbe0742015-09-30 15:33:00 -07004607 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004608 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004609 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004610 }
4611
4612 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004613 * Prints out out state for debugging.
4614 */
4615 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004616 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004617 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004618 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4619 Log.d(TAG, "mRestoring=" + mRestoring);
4620 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004621 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004622 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004623
Daniel Sandler325dc232013-06-05 22:57:57 -04004624 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004625 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004626
4627 @Override
4628 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4629 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004630 // Dump workspace
4631 writer.println(prefix + "Workspace Items");
4632 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4633 writer.println(prefix + " Homescreen " + i);
4634
4635 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4636 for (int j = 0; j < layout.getChildCount(); j++) {
4637 Object tag = layout.getChildAt(j).getTag();
4638 if (tag != null) {
4639 writer.println(prefix + " " + tag.toString());
4640 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004641 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004642 }
Sunny Goyala1365452015-10-01 15:46:24 -07004643
4644 writer.println(prefix + " Hotseat");
4645 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4646 for (int j = 0; j < layout.getChildCount(); j++) {
4647 Object tag = layout.getChildAt(j).getTag();
4648 if (tag != null) {
4649 writer.println(prefix + " " + tag.toString());
4650 }
4651 }
4652
Sunny Goyal713edfc2016-05-06 09:58:34 -07004653 try {
4654 FileLog.flushAll(writer);
4655 } catch (Exception e) {
4656 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004657 }
4658
Adam Cohen9211d422014-10-07 18:14:53 -07004659 if (mLauncherCallbacks != null) {
4660 mLauncherCallbacks.dump(prefix, fd, writer, args);
4661 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004662 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004663
Adam Cohen59400422014-03-05 18:07:04 -08004664 public static CustomAppWidget getCustomAppWidget(String name) {
4665 return sCustomAppWidgets.get(name);
4666 }
4667
4668 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4669 return sCustomAppWidgets;
4670 }
4671
Sunny Goyal29538332016-02-18 09:10:19 -08004672 public static List<View> getFolderContents(View icon) {
4673 if (icon instanceof FolderIcon) {
4674 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4675 } else {
4676 return Collections.EMPTY_LIST;
4677 }
4678 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004679
4680 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4681
4682 @Override
4683 public void onSharedPreferenceChanged(
4684 SharedPreferences sharedPreferences, String key) {
4685 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4686 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4687 if (!waitUntilResume(this, true)) {
4688 run();
4689 }
4690 }
4691 }
4692
4693 @Override
4694 public void run() {
4695 setOrientation();
4696 }
4697 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004698}
Michael Jurka2763be32011-02-24 11:19:57 -08004699
Dan Sandlerd5024042014-01-09 15:01:33 -05004700interface DebugIntents {
4701 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4702 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004703}