blob: ec09cf149e22d36b2b8e93615e8110602f137343 [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;
Sunny Goyal1acb9e92016-05-16 12:41:09 -070061import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070062import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020063import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020065import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080066import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070067import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070068import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040069import android.os.SystemClock;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070070import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.Selection;
72import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070073import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070075import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070077import android.view.HapticFeedbackConstants;
78import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.MotionEvent;
81import android.view.Surface;
82import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070083import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080084import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewGroup;
86import android.view.ViewTreeObserver;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080088import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070089import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070090import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070093import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080094import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070095import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070096
Sunny Goyal651077b2014-06-30 14:15:31 -070097import com.android.launcher3.DropTarget.DragObject;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070098import com.android.launcher3.allapps.AllAppsContainerView;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080099import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700100import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100101import com.android.launcher3.compat.LauncherActivityInfoCompat;
102import com.android.launcher3.compat.LauncherAppsCompat;
103import com.android.launcher3.compat.UserHandleCompat;
104import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800105import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700106import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700107import com.android.launcher3.dragndrop.DragController;
108import com.android.launcher3.dragndrop.DragLayer;
109import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -0700110import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000111import com.android.launcher3.folder.Folder;
112import com.android.launcher3.folder.FolderIcon;
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800113import com.android.launcher3.logging.LoggerUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700114import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700115import com.android.launcher3.model.WidgetsModel;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700116import com.android.launcher3.pageindicators.PageIndicator;
117import com.android.launcher3.pageindicators.PageIndicatorLine;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800118import com.android.launcher3.userevent.nano.LauncherLogProto;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700119import com.android.launcher3.util.ComponentKey;
Sunny Goyal713edfc2016-05-06 09:58:34 -0700120import com.android.launcher3.logging.FileLog;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700121import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal322d5562015-06-25 19:35:49 -0700122import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700123import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700124import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700125import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700126import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700127import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700128
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700129import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700130import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700131import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700132import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800133import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700134import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700135import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700136import java.util.List;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800137import java.util.Locale;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700138
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139/**
140 * Default launcher application.
141 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100142public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700143 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
144 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700145 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700146 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700147 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Daniel Sandlerf061f822013-06-27 13:59:36 -0400149 static final boolean PROFILE_STARTUP = false;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700150 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700151 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400152 static final boolean DEBUG_RESUME_TIME = false;
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700153 static final boolean DEBUG_LOGGING = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700156 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700157 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700158 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159
Michael Jurka8b805b12012-04-18 14:23:14 -0700160 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700161 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700162
Sunny Goyal28c6b962015-10-12 11:42:05 -0700163 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
164
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700165 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
166 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
167 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
168
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700169 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
170
Mathew Inwood876a8462013-06-14 14:12:41 +0100171 /**
172 * IntentStarter uses request codes starting with this. This must be greater than all activity
173 * request codes used internally.
174 */
175 protected static final int REQUEST_LAST = 100;
176
Michael Jurka0a457bf2012-11-19 14:05:05 -0800177 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700178 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800179 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180
Winson Chung2672ff92012-05-04 16:22:30 -0700181 // The Intent extra that defines whether to ignore the launch animation
182 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400183 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700184
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 // Type: int
186 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700187 // Type: int
188 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700189 // Type: Content Values / parcelable
190 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700191 // Type: parcelable
192 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000193 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800194 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195
Adam Cohen432609a2014-03-13 17:03:22 -0700196 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800197 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
198
Sunny Goyal594d76d2014-11-06 10:12:54 -0800199 private static final String QSB_WIDGET_ID = "qsb_widget_id";
200 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
201
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700202 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700203 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
204 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700205
Adam Cohen091440a2015-03-18 14:16:05 -0700206 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700207 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700208
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700209 private boolean mIsSafeModeEnabled;
210
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800212 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700213 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700214 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215
Winson Chunga2413752012-04-03 14:22:34 -0700216 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700217 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
218 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700219 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700220
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800221 private final BroadcastReceiver mCloseSystemDialogsReceiver
222 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800223
Adam Cohen091440a2015-03-18 14:16:05 -0700224 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700225 private View mLauncherView;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700226 private PageIndicatorLine mPageIndicator;
Adam Cohen091440a2015-03-18 14:16:05 -0700227 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800228 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700229
230 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700231
Sunny Goyalffe83f12014-08-14 17:39:34 -0700232 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700233 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700234
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700235 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800236 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800237 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700238
Michael Jurka0280c3b2010-09-17 15:00:07 -0700239 private int[] mTmpAddItemCellCoordinates = new int[2];
240
Sunny Goyal316490e2015-06-02 09:38:28 -0700241 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800242 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700243
Michael Jurka838a4ca2011-02-07 13:33:06 -0800244 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700245 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700246
Winson Chungc51db6a2011-10-05 11:44:49 -0700247 private SearchDropTargetBar mSearchDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -0700248 private AppInfoDropTargetBar mAppInfoDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700249
250 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700251 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700252
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700253 // Main container view and the model for the widget tray screen.
254 @Thunk WidgetsContainerView mWidgetsView;
255 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700256
Sunny Goyal594d76d2014-11-06 10:12:54 -0800257 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700260 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
261 // scroll issues (because the workspace may not have been measured yet) and extra work.
262 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
263 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264
265 private SpannableStringBuilder mDefaultKeySsb = null;
266
Adam Cohen091440a2015-03-18 14:16:05 -0700267 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400268
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800269 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270 private boolean mRestoring;
271 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700272 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273
Michael Jurka1e2f4652013-07-08 18:03:46 -0700274 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700275 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700276 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700277
Joe Onorato9c1289c2009-08-17 11:03:03 -0400278 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800279 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700280 private ExtractedColors mExtractedColors;
Adam Cohen091440a2015-03-18 14:16:05 -0700281 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800282 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700283 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800284 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400285
Sunny Goyal639e9062015-08-19 19:17:06 -0700286 private LauncherClings mClings;
287
Adam Cohen61f560d2013-09-30 15:58:20 -0700288 private View.OnTouchListener mHapticFeedbackTouchListener;
289
Adam Cohended9f8d2010-11-03 13:25:16 -0700290 // Related to the auto-advancing of widgets
291 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700292 private static final int ADVANCE_INTERVAL = 20000;
293 private static final int ADVANCE_STAGGER = 250;
294
295 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700296 private long mAutoAdvanceSentTime;
297 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700298 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700299
Winson Chung400438b2011-01-16 17:53:48 -0800300 // Determines how long to wait after a rotation before restoring the screen orientation to
301 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700302 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800303
Adam Cohen091440a2015-03-18 14:16:05 -0700304 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700305
Adam Cohen1462de32012-07-24 22:34:36 -0700306 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700307 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700308
Winson Chung46353de2012-02-16 14:05:10 -0800309 // We only want to get the SharedPreferences once since it does an FS stat each time we get
310 // it from the context.
311 private SharedPreferences mSharedPrefs;
312
Winson Chungf0c6ae02012-03-21 16:10:31 -0700313 // Holds the page that we need to animate to, and the icon views that we need to animate up
314 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700315 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700316 private Bitmap mFolderIconBitmap;
317 private Canvas mFolderIconCanvas;
318 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700319
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700320 private DeviceProfile mDeviceProfile;
321
Adam Cohen4b66bf32015-09-18 12:15:19 -0700322 private boolean mMoveToDefaultScreenFromNewIntent;
323
Winson Chung13eb5272015-05-11 16:30:13 -0700324 // This is set to the view that launched the activity that navigated the user away from
325 // launcher. Since there is no callback for when the activity has finished launching, enable
326 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800327 private BubbleTextView mWaitingForResume;
328
Adam Cohen59400422014-03-05 18:07:04 -0800329 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
330 new HashMap<String, CustomAppWidget>();
331
Adam Cohen59400422014-03-05 18:07:04 -0800332 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700333 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
334 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800335 }
336 }
337
Adam Cohen091440a2015-03-18 14:16:05 -0700338 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700339 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700340 if (mWorkspace != null) {
341 mWorkspace.buildPageHardwareLayers();
342 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700343 }
344 };
345
Adam Cohendb364c32014-05-20 14:23:40 -0700346 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800347
Adam Cohen091440a2015-03-18 14:16:05 -0700348 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800349 int requestCode;
350 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700351 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700352 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800353 int cellX;
354 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700355 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800356 }
357
Hyunyoung Songaa953652016-04-19 18:30:24 -0700358 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800359
Adam Cohenf9c184a2016-01-15 16:47:43 -0800360 public FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700361 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400362
363 @Thunk void setOrientation() {
364 if (mRotationEnabled) {
365 unlockScreenOrientation(true);
366 } else {
367 setRequestedOrientation(
368 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700369 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400370 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700371
Sunny Goyal745bad92016-05-02 10:54:12 -0700372 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700373
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 @Override
375 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700376 if (DEBUG_STRICT_MODE) {
377 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
378 .detectDiskReads()
379 .detectDiskWrites()
380 .detectNetwork() // or .detectAll() for all detectable problems
381 .penaltyLog()
382 .build());
383 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
384 .detectLeakedSqlLiteObjects()
385 .detectLeakedClosableObjects()
386 .penaltyLog()
387 .penaltyDeath()
388 .build());
389 }
390
Adam Cohen9211d422014-10-07 18:14:53 -0700391 if (mLauncherCallbacks != null) {
392 mLauncherCallbacks.preOnCreate();
393 }
394
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400396
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400397 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700398
Adam Cohen2e6da152015-05-06 11:42:25 -0700399 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700400 mDeviceProfile = getResources().getConfiguration().orientation
401 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700402 app.getInvariantDeviceProfile().landscapeProfile
403 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700404
Sunny Goyalf7258242015-10-19 16:59:07 -0700405 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700406 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800407 mModel = app.setLauncher(this);
408 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700409
Joe Onorato41a12d22009-10-31 18:30:00 -0400410 mDragController = new DragController(this);
Winson Chung006ee262015-08-03 14:40:11 -0700411 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700412
Sunny Goyalffe83f12014-08-14 17:39:34 -0700413 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700414
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700415 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
416 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700417
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700418 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
419 // this also ensures that any synchronous binding below doesn't re-trigger another
420 // LauncherModel load.
421 mPaused = false;
422
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200424 android.os.Debug.startMethodTracing(
425 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800426 }
427
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700429
Tony Wickhameef44322015-10-20 13:24:36 -0700430 app.getInvariantDeviceProfile().landscapeProfile.setSearchBarHeight(getSearchBarHeight());
431 app.getInvariantDeviceProfile().portraitProfile.setSearchBarHeight(getSearchBarHeight());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700433 mDeviceProfile.layout(this);
Tony Wickham827cef22016-03-17 15:39:39 -0700434 mExtractedColors = new ExtractedColors();
435 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436
Tony Wickhame2217252016-03-22 16:34:23 -0700437 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
438 .addAccessibilityStateChangeListener(this);
439
Joe Onorato7c312c12009-08-13 21:36:53 -0700440 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700441
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442 mSavedState = savedInstanceState;
443 restoreState(mSavedState);
444
445 if (PROFILE_STARTUP) {
446 android.os.Debug.stopMethodTracing();
447 }
448
449 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700450 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700451 // If the user leaves launcher, then we should just load items asynchronously when
452 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700453 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700454 } else {
455 // We only load the page synchronously if the user rotates (or triggers a
456 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700457 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700458 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800459 }
460
461 // For handling default keys
462 mDefaultKeySsb = new SpannableStringBuilder();
463 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800464
465 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
466 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800467
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800468 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700469 // In case we are on a device with locked rotation, we should look at preferences to check
470 // if the user has specifically allowed rotation.
471 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700472 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700473 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
474 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700475 }
476
477 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
478 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400479 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700480
Adam Cohen9211d422014-10-07 18:14:53 -0700481 if (mLauncherCallbacks != null) {
482 mLauncherCallbacks.onCreate(savedInstanceState);
483 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700484
485 if (shouldShowIntroScreen()) {
486 showIntroScreen();
487 } else {
488 showFirstRunActivity();
489 showFirstRunClings();
490 }
Adam Cohen9211d422014-10-07 18:14:53 -0700491 }
492
Sunny Goyal7779d622015-06-11 16:18:39 -0700493 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700494 public void onExtractedColorsChanged() {
495 loadExtractedColorsAndColorItems();
496 }
497
498 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700499 // TODO: do this in pre-N as well, once the extraction part is complete.
500 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700501 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700502 mHotseat.updateColor(mExtractedColors, !mPaused);
Tony Wickham770217c2016-05-18 15:16:40 -0700503 if (mPageIndicator != null) {
504 mPageIndicator.updateColor(mExtractedColors);
505 }
Tony Wickham827cef22016-03-17 15:39:39 -0700506 }
507 }
508
Adam Cohen9211d422014-10-07 18:14:53 -0700509 private LauncherCallbacks mLauncherCallbacks;
510
511 public void onPostCreate(Bundle savedInstanceState) {
512 super.onPostCreate(savedInstanceState);
513 if (mLauncherCallbacks != null) {
514 mLauncherCallbacks.onPostCreate(savedInstanceState);
515 }
516 }
517
Sunny Goyal32554d12015-12-03 15:31:25 -0800518 /**
519 * Call this after onCreate to set or clear overlay.
520 */
521 public void setLauncherOverlay(LauncherOverlay overlay) {
522 if (overlay != null) {
523 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
524 }
525 mWorkspace.setLauncherOverlay(overlay);
526 }
527
Adam Cohen9211d422014-10-07 18:14:53 -0700528 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
529 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700530 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700531 private boolean mWorkspaceImportanceStored = false;
532 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700533 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800534 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700535 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
536
537 @Override
538 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700539 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700540 return;
541 }
542 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700543 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700544 if (mWorkspace != null) {
545 mWorkspaceImportanceForAccessibility =
546 mWorkspace.getImportantForAccessibility();
547 mWorkspace.setImportantForAccessibility(
548 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
549 mWorkspaceImportanceStored = true;
550 }
551 if (mHotseat != null) {
552 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
553 mHotseat.setImportantForAccessibility(
554 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
555 mHotseatImportanceStored = true;
556 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700557 }
558
559 @Override
560 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700561 if (mWorkspaceImportanceStored && mWorkspace != null) {
562 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
563 }
564 if (mHotseatImportanceStored && mHotseat != null) {
565 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
566 }
567 mWorkspaceImportanceStored = false;
568 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700569 }
570 });
Adam Cohen9211d422014-10-07 18:14:53 -0700571 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700572 }
573
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700574 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700575 public void onLauncherProviderChange() {
576 if (mLauncherCallbacks != null) {
577 mLauncherCallbacks.onLauncherProviderChange();
578 }
579 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700580
Adam Cohen9211d422014-10-07 18:14:53 -0700581 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700582 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700583 if (mLauncherCallbacks != null) {
584 return mLauncherCallbacks.hasCustomContentToLeft();
585 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700586 return false;
587 }
588
Dave Hawkeya8881582013-09-17 15:55:33 -0600589 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500590 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600591 * {@link #addToCustomContentPage}. This will only be invoked if
592 * {@link #hasCustomContentToLeft()} is {@code true}.
593 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500594 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700595 if (mLauncherCallbacks != null) {
596 mLauncherCallbacks.populateCustomContentContainer();
597 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600598 }
599
600 /**
601 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
602 * ensure the custom content page is added or removed if necessary.
603 */
604 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600605 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600606 // Not bound yet, wait for bindScreens to be called.
607 return;
608 }
609
610 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
611 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500612 mWorkspace.createCustomContentContainer();
613 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600614 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
615 mWorkspace.removeCustomContentPage();
616 }
617 }
618
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800619 /**
620 * Logger object is a singleton and does not have to be coupled with the foreground activity.
621 * Since most user event logging is done on the UI, the object is retrieved from the
622 * callback for convenience.
623 */
Hyunyoung Songaa953652016-04-19 18:30:24 -0700624 private UserEventDispatcher createUserEventDispatcher() {
625 return new UserEventDispatcher() {
626 @Override
627 public void dispatchUserEvent(LauncherLogProto.LauncherEvent ev, Intent intent) {
628 if (!DEBUG_LOGGING) {
629 return;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800630 }
Hyunyoung Songaa953652016-04-19 18:30:24 -0700631 Log.d("UserEvent", String.format(Locale.US,
632 "action:%s\nchild:%s\nparent:%s\nelapsed container %d ms session %d ms",
633 LoggerUtils.getActionStr(ev.action),
634 LoggerUtils.getTargetStr(ev.srcTarget[0]),
635 LoggerUtils.getTargetStr(ev.srcTarget[1]),
636 ev.elapsedContainerMillis,
637 ev.elapsedSessionMillis));
638 }
639 };
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800640 }
641
Hyunyoung Songaa953652016-04-19 18:30:24 -0700642 public UserEventDispatcher getUserEventDispatcher() {
643 if (mLauncherCallbacks != null) {
644 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
645 if (dispatcher != null) {
646 return dispatcher;
647 }
648 }
649
650 if (mUserEventDispatcher == null) {
651 mUserEventDispatcher = createUserEventDispatcher();
652 }
653 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800654 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100655
Hyunyoung Song3f471442015-04-08 19:01:34 -0700656 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700657 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
658 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700659 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700660 }
661
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000662 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700663 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
664 // This cast is safe as long as the id < 0x00FFFFFF
665 // Since we jail all the dynamically generated views, there should be no clashes
666 // with any other views.
667 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000668 }
669
670 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700671 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
672 * a configuration step, this allows the proper animations to run after other transitions.
673 */
Adam Cohendb364c32014-05-20 14:23:40 -0700674 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700675 long screenId = args.screenId;
676 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
677 // When the screen id represents an actual screen (as opposed to a rank) we make sure
678 // that the drop page actually exists.
679 screenId = ensurePendingDropLayoutExists(args.screenId);
680 }
Adam Cohendb364c32014-05-20 14:23:40 -0700681
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800682 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800683 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700684 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700685 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700686 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800687 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700688 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700689 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700690 case REQUEST_RECONFIGURE_APPWIDGET:
691 completeRestoreAppWidget(args.appWidgetId);
692 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800693 }
Michael Jurka27614d22012-04-02 06:40:22 -0700694 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
695 // if you turned the screen off and then back while in All Apps, Launcher would not
696 // return to the workspace. Clearing mAddInfo.container here fixes this issue
697 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700698 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800699 }
700
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800701 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700702 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700703 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700704 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700705 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800706 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700707
Adam Cohenad4e15c2013-10-17 16:21:35 -0700708 Runnable exitSpringLoaded = new Runnable() {
709 @Override
710 public void run() {
711 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
712 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
713 }
714 };
715
Michael Jurka8b805b12012-04-18 14:23:14 -0700716 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700717 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700718 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700719 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
720 if (resultCode == RESULT_CANCELED) {
721 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700722 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700723 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700724 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800725 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700726 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Winsona1f79d32015-08-05 14:00:45 -0700727
728 // When the user has granted permission to bind widgets, we should check to see if
729 // we can inflate the default search bar widget.
730 getOrCreateQsbBar();
Michael Jurka8b805b12012-04-18 14:23:14 -0700731 }
732 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200733 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
734 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700735 // User could have free-scrolled between pages before picking a wallpaper; make sure
736 // we move to the closest one now.
737 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700738 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200739 }
740 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700741 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200742
Adam Cohened66b2b2012-01-23 17:28:51 -0800743 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700744 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700745
Adam Cohendb364c32014-05-20 14:23:40 -0700746 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800747 // We have special handling for widgets
748 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800749 final int appWidgetId;
750 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
751 : -1;
752 if (widgetId < 0) {
753 appWidgetId = pendingAddWidgetId;
754 } else {
755 appWidgetId = widgetId;
756 }
757
Adam Cohenad4e15c2013-10-17 16:21:35 -0700758 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800759 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700760 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
761 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700762 result = RESULT_CANCELED;
763 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700764 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700765 @Override
766 public void run() {
767 exitSpringLoadedDragModeDelayed(false, 0, null);
768 }
769 };
Adam Cohendb364c32014-05-20 14:23:40 -0700770 if (workspaceLocked) {
771 // No need to remove the empty screen if we're mid-binding, as the
772 // the bind will not add the empty screen.
773 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
774 } else {
775 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
776 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
777 }
Winson Chung5aaab772012-04-27 15:24:02 -0700778 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700779 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700780 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
781 // When the screen id represents an actual screen (as opposed to a rank)
782 // we make sure that the drop page actually exists.
783 mPendingAddInfo.screenId =
784 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
785 }
Adam Cohendb364c32014-05-20 14:23:40 -0700786 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
787
788 dropLayout.setDropPending(true);
789 final Runnable onComplete = new Runnable() {
790 @Override
791 public void run() {
792 completeTwoStageWidgetDrop(resultCode, appWidgetId);
793 dropLayout.setDropPending(false);
794 }
795 };
796 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
797 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
798 } else {
799 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
800 mPendingAddInfo);
801 sPendingAddItem = args;
802 }
Winson Chung5aaab772012-04-27 15:24:02 -0700803 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800804 return;
805 }
806
Sunny Goyalff572272014-07-23 13:58:07 -0700807 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
808 if (resultCode == RESULT_OK) {
809 // Update the widget view.
810 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
811 pendingAddWidgetId, mPendingAddInfo);
812 if (workspaceLocked) {
813 sPendingAddItem = args;
814 } else {
815 completeAdd(args);
816 }
817 }
818 // Leave the widget in the pending state if the user canceled the configure.
819 return;
820 }
821
Sunny Goyalaad90582015-07-09 14:22:50 -0700822 if (requestCode == REQUEST_CREATE_SHORTCUT) {
823 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
824 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
825 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
826 mPendingAddInfo);
827 if (isWorkspaceLocked()) {
828 sPendingAddItem = args;
829 } else {
830 completeAdd(args);
831 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
832 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
833 }
834 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700835 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
836 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800839 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800840
841 }
842
843 @Override
844 protected void onActivityResult(
845 final int requestCode, final int resultCode, final Intent data) {
846 handleActivityResult(requestCode, resultCode, data);
847 if (mLauncherCallbacks != null) {
848 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
849 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800850 }
851
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600852 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700853 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600854 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700855 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
856 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
857 View v = null;
858 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
859 if (layout != null) {
860 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
861 }
862 Intent intent = sPendingAddItem.intent;
863 sPendingAddItem = null;
864 if (grantResults.length > 0
865 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
866 startActivity(v, intent, null);
867 } else {
868 // TODO: Show a snack bar with link to settings
869 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
870 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
871 }
872 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600873 if (mLauncherCallbacks != null) {
874 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
875 grantResults);
876 }
877 }
878
Adam Cohendb364c32014-05-20 14:23:40 -0700879 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
880 appWidgetId, ItemInfo info) {
881 PendingAddArguments args = new PendingAddArguments();
882 args.requestCode = requestCode;
883 args.intent = data;
884 args.container = info.container;
885 args.screenId = info.screenId;
886 args.cellX = info.cellX;
887 args.cellY = info.cellY;
888 args.appWidgetId = appWidgetId;
889 return args;
890 }
891
892 /**
893 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
894 *
895 * @param screenId the screen id to check
896 * @return the new screen, or screenId if it exists
897 */
898 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800899 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700900 if (dropLayout == null) {
901 // it's possible that the add screen was removed because it was
902 // empty and a re-bind occurred
903 mWorkspace.addExtraEmptyScreen();
904 return mWorkspace.commitExtraEmptyScreen();
905 } else {
906 return screenId;
907 }
908 }
909
Adam Cohen091440a2015-03-18 14:16:05 -0700910 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800911 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800912 Runnable onCompleteRunnable = null;
913 int animationType = 0;
914
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700915 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800916 if (resultCode == RESULT_OK) {
917 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
918 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700919 mPendingAddWidgetInfo);
920 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800921 onCompleteRunnable = new Runnable() {
922 @Override
923 public void run() {
924 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700925 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700926 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
927 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800928 }
929 };
930 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800931 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800932 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800933 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700934 if (mDragLayer.getAnimatedView() != null) {
935 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
936 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
937 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700938 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700939 // The animated view may be null in the case of a rotation during widget configuration
940 onCompleteRunnable.run();
941 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 }
943
944 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700945 protected void onStop() {
946 super.onStop();
947 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700948
949 if (mLauncherCallbacks != null) {
950 mLauncherCallbacks.onStop();
951 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700952 }
953
954 @Override
955 protected void onStart() {
956 super.onStart();
957 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700958
959 if (mLauncherCallbacks != null) {
960 mLauncherCallbacks.onStart();
961 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700962 }
963
964 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200966 long startTime = 0;
967 if (DEBUG_RESUME_TIME) {
968 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400969 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200970 }
Adam Cohen9211d422014-10-07 18:14:53 -0700971
972 if (mLauncherCallbacks != null) {
973 mLauncherCallbacks.preOnResume();
974 }
975
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700977 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700978
Winson Chung4a2afa32012-07-19 14:53:05 -0700979 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700980 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700981 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800982 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700983 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700984 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700985 // view after launching an app, as they may be depending on the UI to be static to
986 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700987 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700988 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800989 } else if (mOnResumeState == State.WIDGETS) {
990 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700991 }
992 mOnResumeState = State.NONE;
993
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700994 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700995 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
996 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700997
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800998 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700999 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001000 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001001 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001002 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001003 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001005 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001006 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1007 // execute them here
1008 long startTimeCallbacks = 0;
1009 if (DEBUG_RESUME_TIME) {
1010 startTimeCallbacks = System.currentTimeMillis();
1011 }
1012
Michael Jurka1e2f4652013-07-08 18:03:46 -07001013 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1014 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001015 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001016 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001017 if (DEBUG_RESUME_TIME) {
1018 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1019 (System.currentTimeMillis() - startTimeCallbacks));
1020 }
Michael Jurka447bf842013-05-15 14:52:15 +02001021 }
Michael Jurka54554252013-08-01 12:52:23 +02001022 if (mOnResumeCallbacks.size() > 0) {
1023 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1024 mOnResumeCallbacks.get(i).run();
1025 }
1026 mOnResumeCallbacks.clear();
1027 }
Winson Chunge4e50662012-01-23 14:45:13 -08001028
1029 // Reset the pressed state of icons that were locked in the press state while activities
1030 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001031 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001032 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001033 mWaitingForResume.setStayPressed(false);
1034 }
Winson Chung82963d52013-10-09 11:20:57 -07001035
Adam Cohen06dff352012-06-01 17:17:08 -07001036 // It is possible that widgets can receive updates while launcher is not in the foreground.
1037 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1038 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1039 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001040 if (!isWorkspaceLoading()) {
1041 getWorkspace().reinflateWidgetsIfNecessary();
1042 }
Sunny Goyal22235bc2015-04-03 09:23:43 -07001043 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001044
Michael Jurka447bf842013-05-15 14:52:15 +02001045 if (DEBUG_RESUME_TIME) {
1046 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1047 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001048
Adam Cohen4b66bf32015-09-18 12:15:19 -07001049 // We want to suppress callbacks about CustomContent being shown if we have just received
1050 // onNewIntent while the user was present within launcher. In that case, we post a call
1051 // to move the user to the main screen (which will occur after onResume). We don't want to
1052 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1053 // suppress here.
1054 if (mWorkspace.getCustomContentCallbacks() != null
1055 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001056 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001057 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001058 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1059 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001060 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001061 }
1062 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001063 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001064 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001065 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001066
Sunny Goyal756adbc2015-04-16 15:20:51 -07001067 if (!isWorkspaceLoading()) {
1068 // Process any items that were added while Launcher was away.
1069 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1070 }
Adam Cohen9211d422014-10-07 18:14:53 -07001071
1072 if (mLauncherCallbacks != null) {
1073 mLauncherCallbacks.onResume();
1074 }
Adam Cohen06dff352012-06-01 17:17:08 -07001075 }
1076
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001078 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001079 // Ensure that items added to Launcher are queued until Launcher returns
1080 InstallShortcutReceiver.enableInstallQueue();
1081
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001082 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001083 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001084 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001085 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001086
1087 // We call onHide() aggressively. The custom content callbacks should be able to
1088 // debounce excess onHide calls.
1089 if (mWorkspace.getCustomContentCallbacks() != null) {
1090 mWorkspace.getCustomContentCallbacks().onHide();
1091 }
Romain Guycbb89e42009-06-08 15:52:54 -07001092
Adam Cohen9211d422014-10-07 18:14:53 -07001093 if (mLauncherCallbacks != null) {
1094 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001095 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001096 }
1097
1098 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001099 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1100 // by a onResume or by scrolling otherwise.
1101 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001102
1103 // Custom content is completely hidden
1104 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001105
1106 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1107 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001108
1109 // Indicates whether the user is allowed to scroll away from the custom content.
1110 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001111 }
1112
Adam Cohenc2d6e892014-10-16 09:49:24 -07001113 public interface LauncherOverlay {
1114
1115 /**
1116 * Touch interaction leading to overscroll has begun
1117 */
1118 public void onScrollInteractionBegin();
1119
1120 /**
1121 * Touch interaction related to overscroll has ended
1122 */
1123 public void onScrollInteractionEnd();
1124
1125 /**
1126 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1127 * screen (or in the case of RTL, the rightmost screen).
1128 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001129 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001130
1131 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001132 * Called when the launcher is ready to use the overlay
1133 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001134 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001135 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001136 }
1137
Jun Mukai8af0cd82015-05-12 12:32:12 -07001138 public interface LauncherSearchCallbacks {
1139 /**
1140 * Called when the search overlay is shown.
1141 */
1142 public void onSearchOverlayOpened();
1143
1144 /**
1145 * Called when the search overlay is dismissed.
1146 */
1147 public void onSearchOverlayClosed();
1148 }
1149
Adam Cohenc2d6e892014-10-16 09:49:24 -07001150 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001151 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001152 }
1153
1154 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1155
Sunny Goyalc86df472016-02-25 09:19:38 -08001156 public void onScrollChanged(float progress) {
1157 if (mWorkspace != null) {
1158 mWorkspace.onOverlayScrollChanged(progress);
1159 }
1160 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001161 }
1162
Jorim Jaggid017f882014-01-14 17:08:48 -08001163 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001164 if (mLauncherCallbacks != null) {
1165 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001166 } else {
1167 // On devices with a locked orientation, we will at least have the allow rotation
1168 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001169 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001170 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001171 }
1172
Adam Cohen9211d422014-10-07 18:14:53 -07001173 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001174 CustomContentCallbacks callbacks, String description) {
1175 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001176 }
1177
Adam Cohenedb40762013-07-18 16:45:45 -07001178 // The custom content needs to offset its content to account for the QSB
1179 public int getTopOffsetForCustomContent() {
1180 return mWorkspace.getPaddingTop();
1181 }
1182
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001183 @Override
1184 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001185 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001186 if (mModel.isCurrentCallbacks(this)) {
1187 mModel.stopLoader();
1188 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001189 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1190
Romain Guy13c2e7b2010-03-10 19:45:00 -08001191 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001192 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001193
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001194 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001195 @Override
1196 public void onWindowFocusChanged(boolean hasFocus) {
1197 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001198 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001199
1200 if (mLauncherCallbacks != null) {
1201 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1202 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001203 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001204
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 private boolean acceptFilter() {
1206 final InputMethodManager inputManager = (InputMethodManager)
1207 getSystemService(Context.INPUT_METHOD_SERVICE);
1208 return !inputManager.isFullscreenMode();
1209 }
1210
1211 @Override
1212 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001213 final int uniChar = event.getUnicodeChar();
1214 final boolean handled = super.onKeyDown(keyCode, event);
1215 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1216 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1218 keyCode, event);
1219 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001220 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001221 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001222 // showSearchDialog()
1223 // If there are multiple keystrokes before the search dialog takes focus,
1224 // onSearchRequested() will be called for every keystroke,
1225 // but it is idempotent, so it's fine.
1226 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 }
1228 }
1229
Joe Onorato8a9625e2010-01-28 15:55:35 -08001230 // Eat the long press event so the keyboard doesn't come up.
1231 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1232 return true;
1233 }
1234
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 return handled;
1236 }
1237
Winson46163472015-09-22 17:31:56 -07001238 @Override
1239 public boolean onKeyUp(int keyCode, KeyEvent event) {
1240 if (keyCode == KeyEvent.KEYCODE_MENU) {
1241 // Ignore the menu key if we are currently dragging or are on the custom content screen
1242 if (!isOnCustomContent() && !mDragController.isDragging()) {
1243 // Close any open folders
1244 closeFolder();
1245
1246 // Stop resizing any widgets
1247 mWorkspace.exitWidgetResizeMode();
1248
1249 // Show the overview mode if we are on the workspace
1250 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1251 !mWorkspace.isSwitchingState()) {
1252 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001253 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001254 }
1255 }
1256 return true;
1257 }
1258 return super.onKeyUp(keyCode, event);
1259 }
1260
Karl Rosaen138a0412009-04-23 19:00:21 -07001261 private String getTypedText() {
1262 return mDefaultKeySsb.toString();
1263 }
1264
1265 private void clearTypedText() {
1266 mDefaultKeySsb.clear();
1267 mDefaultKeySsb.clearSpans();
1268 Selection.setSelection(mDefaultKeySsb, 0);
1269 }
1270
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001272 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1273 * State
1274 */
1275 private static State intToState(int stateOrdinal) {
1276 State state = State.WORKSPACE;
1277 final State[] stateValues = State.values();
1278 for (int i = 0; i < stateValues.length; i++) {
1279 if (stateValues[i].ordinal() == stateOrdinal) {
1280 state = stateValues[i];
1281 break;
1282 }
1283 }
1284 return state;
1285 }
1286
1287 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 * Restores the previous state, if it exists.
1289 *
1290 * @param savedState The previous state.
1291 */
1292 private void restoreState(Bundle savedState) {
1293 if (savedState == null) {
1294 return;
1295 }
1296
Michael Jurka883f55b2010-10-21 15:47:14 -07001297 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001298 if (state == State.APPS || state == State.WIDGETS) {
1299 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001300 }
1301
Adam Cohen21cd0022013-10-09 18:57:02 -07001302 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1303 PagedView.INVALID_RESTORE_PAGE);
1304 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001305 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 }
1307
Sunny Goyal756cd262015-08-20 12:33:21 -07001308 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1309 if (itemValues != null) {
1310 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001311 AppWidgetProviderInfo info = savedState.getParcelable(
1312 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001313 mPendingAddWidgetInfo = info == null ?
1314 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1315
Adam Cohen4637b5a2013-11-04 18:21:24 -08001316 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001317 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 mRestoring = true;
1319 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001320 }
1321
1322 /**
1323 * Finds all the views we need and configure them properly.
1324 */
1325 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001326 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001327
Craig Mautner360310b2012-10-26 15:13:08 -07001328 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001329 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001330 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1331 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Tony Wickhamf549dab2016-05-16 09:54:06 -07001332 mPageIndicator = (PageIndicatorLine) mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001333
Sunny Goyal784f9c32016-03-23 16:56:54 -07001334 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1335 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1336 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001337 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338
Winson Chung4c98d922011-05-31 16:50:48 -07001339 // Setup the drag layer
1340 mDragLayer.setup(this, dragController);
1341
Winson Chung3d503fb2011-07-13 17:25:49 -07001342 // Setup the hotseat
1343 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1344 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001345 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001346 }
1347
Winsone9f27272015-10-13 10:47:51 -07001348 // Setup the overview panel
1349 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001350
Winson Chung4c98d922011-05-31 16:50:48 -07001351 // Setup the workspace
1352 mWorkspace.setHapticFeedbackEnabled(false);
1353 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001354 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001355 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001356
Tony Wickham34d2c912015-09-11 09:27:58 -07001357 // Get the search/delete/uninstall bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001358 mSearchDropTargetBar = (SearchDropTargetBar)
1359 mDragLayer.findViewById(R.id.search_drop_target_bar);
Tony Wickham34d2c912015-09-11 09:27:58 -07001360 // Get the app info bar
1361 mAppInfoDropTargetBar = (AppInfoDropTargetBar)
1362 mDragLayer.findViewById(R.id.app_info_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001363
Winson Chungef7f8742015-06-04 17:18:17 -07001364 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001365 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001366 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001367 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1368 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1369 } else {
1370 mAppsView.setSearchBarController(new DefaultAppSearchController());
1371 }
Craig Mautner360310b2012-10-26 15:13:08 -07001372
Winson Chung3d503fb2011-07-13 17:25:49 -07001373 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001374 dragController.setDragScoller(mWorkspace);
Tony Wickham0f97b782015-12-02 17:55:07 -08001375 dragController.setScrollView(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -07001376 dragController.setMoveTarget(mWorkspace);
1377 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001378 if (mSearchDropTargetBar != null) {
1379 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001380 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001381 }
Tony Wickham34d2c912015-09-11 09:27:58 -07001382 if (mAppInfoDropTargetBar != null) {
1383 mAppInfoDropTargetBar.setup(this, dragController);
1384 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001385
Sunny Goyal322d5562015-06-25 19:35:49 -07001386 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1387 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001388 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 }
1390
Winsone9f27272015-10-13 10:47:51 -07001391 private void setupOverviewPanel() {
1392 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1393
1394 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1395 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1396 @Override
1397 public boolean onLongClick(View v) {
1398 return v.performClick();
1399 }
1400 };
1401
1402 // Bind wallpaper button actions
1403 View wallpaperButton = findViewById(R.id.wallpaper_button);
1404 wallpaperButton.setOnClickListener(new OnClickListener() {
1405 @Override
1406 public void onClick(View view) {
1407 if (!mWorkspace.isSwitchingState()) {
1408 onClickWallpaperPicker(view);
1409 }
1410 }
1411 });
1412 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1413 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1414
1415 // Bind widget button actions
1416 mWidgetsButton = findViewById(R.id.widget_button);
1417 mWidgetsButton.setOnClickListener(new OnClickListener() {
1418 @Override
1419 public void onClick(View view) {
1420 if (!mWorkspace.isSwitchingState()) {
1421 onClickAddWidgetButton(view);
1422 }
1423 }
1424 });
1425 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1426 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1427
1428 // Bind settings actions
1429 View settingsButton = findViewById(R.id.settings_button);
1430 boolean hasSettings = hasSettings();
1431 if (hasSettings) {
1432 settingsButton.setOnClickListener(new OnClickListener() {
1433 @Override
1434 public void onClick(View view) {
1435 if (!mWorkspace.isSwitchingState()) {
1436 onClickSettingsButton(view);
1437 }
1438 }
1439 });
1440 settingsButton.setOnLongClickListener(performClickOnLongClick);
1441 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1442 } else {
1443 settingsButton.setVisibility(View.GONE);
1444 }
1445
1446 mOverviewPanel.setAlpha(0f);
1447 }
1448
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001450 * Sets the all apps button. This method is called from {@link Hotseat}.
1451 */
1452 public void setAllAppsButton(View allAppsButton) {
1453 mAllAppsButton = allAppsButton;
1454 }
1455
1456 public View getAllAppsButton() {
1457 return mAllAppsButton;
1458 }
1459
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001460 public View getWidgetsButton() {
1461 return mWidgetsButton;
1462 }
1463
Anjali Koppal5ad44842014-03-10 20:34:39 -07001464 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 * Creates a view representing a shortcut.
1466 *
1467 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001469 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001470 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 }
1472
1473 /**
1474 * Creates a view representing a shortcut inflated from the specified resource.
1475 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 * @param parent The group the shortcut belongs to.
1477 * @param info The data structure describing the shortcut.
1478 *
1479 * @return A View inflated from layoutResId.
1480 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001481 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001482 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001483 parent, false);
1484 favorite.applyFromShortcutInfo(info, mIconCache);
1485 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001487 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 return favorite;
1489 }
1490
1491 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 * Add a shortcut to the workspace.
1493 *
1494 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001496 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001497 int cellY) {
1498 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001499 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001500
Sunny Goyal5c97f512015-05-19 16:03:28 -07001501 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001502 if (info == null) {
1503 return;
1504 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001505 final View view = createShortcut(info);
1506
Sunny Goyal5c97f512015-05-19 16:03:28 -07001507 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001508 // First we check if we already know the exact location where we want to add this item.
1509 if (cellX >= 0 && cellY >= 0) {
1510 cellXY[0] = cellX;
1511 cellXY[1] = cellY;
1512 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001513
1514 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001515 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001516 true, null,null)) {
1517 return;
1518 }
1519 DragObject dragObject = new DragObject();
1520 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001521 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1522 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001523 return;
1524 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001525 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001526 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001527 }
1528
1529 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001530 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001531 return;
1532 }
1533
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001534 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535
1536 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001537 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001538 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 }
1540 }
1541
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001543 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001545 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 */
Adam Cohen091440a2015-03-18 14:16:05 -07001547 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001548 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1549
1550 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001551 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001552 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001553 }
Romain Guycbb89e42009-06-08 15:52:54 -07001554
Adam Cohen59400422014-03-05 18:07:04 -08001555 if (appWidgetInfo.isCustomWidget) {
1556 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001557 }
1558
Adam Cohen59400422014-03-05 18:07:04 -08001559 LauncherAppWidgetInfo launcherInfo;
1560 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1561 launcherInfo.spanX = info.spanX;
1562 launcherInfo.spanY = info.spanY;
1563 launcherInfo.minSpanX = info.minSpanX;
1564 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001565 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001566
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001568 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569
1570 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001571 if (hostView == null) {
1572 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001573 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001574 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001575 hostView.setVisibility(View.VISIBLE);
1576 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001578 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579 }
Romain Guycbb89e42009-06-08 15:52:54 -07001580
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001581 private void addAppWidgetToWorkspace(
1582 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001583 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001584 hostView.setTag(item);
1585 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001586
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001587 hostView.setFocusable(true);
1588 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001589
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001590 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001591 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1592
1593 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001594 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001595 }
1596 }
1597
Adam Cohended9f8d2010-11-03 13:25:16 -07001598 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1599 @Override
1600 public void onReceive(Context context, Intent intent) {
1601 final String action = intent.getAction();
1602 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1603 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001604 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001605 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001606
Winson Chungc100e8e2011-08-09 16:02:43 -07001607 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001608 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001609 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001610 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001611 if (!showWorkspace(false)) {
1612 // If we are already on the workspace, then manually reset all apps
1613 mAppsView.reset();
1614 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001615 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001616 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1617 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001618 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001619 }
1620 }
1621 };
1622
1623 @Override
1624 public void onAttachedToWindow() {
1625 super.onAttachedToWindow();
1626
1627 // Listen for broadcasts related to user-presence
1628 final IntentFilter filter = new IntentFilter();
1629 filter.addAction(Intent.ACTION_SCREEN_OFF);
1630 filter.addAction(Intent.ACTION_USER_PRESENT);
1631 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001632 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001633 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001634 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001635
1636 if (mLauncherCallbacks != null) {
1637 mLauncherCallbacks.onAttachedToWindow();
1638 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001639 }
1640
1641 @Override
1642 public void onDetachedFromWindow() {
1643 super.onDetachedFromWindow();
1644 mVisible = false;
1645
Adam Cohend113e0c2010-11-11 10:48:05 -08001646 if (mAttached) {
1647 unregisterReceiver(mReceiver);
1648 mAttached = false;
1649 }
Winson Chungb745afb2015-03-02 11:51:23 -08001650 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001651
1652 if (mLauncherCallbacks != null) {
1653 mLauncherCallbacks.onDetachedFromWindow();
1654 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001655 }
1656
1657 public void onWindowVisibilityChanged(int visibility) {
1658 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001659 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001660 // The following code used to be in onResume, but it turns out onResume is called when
1661 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1662 // is a more appropriate event to handle
1663 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001664 if (!mWorkspaceLoading) {
1665 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001666 // We want to let Launcher draw itself at least once before we force it to build
1667 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001668 // apps is nice and speedy.
1669 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001670 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001671 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001672 if (mStarted) return;
1673 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001674 // We delay the layer building a bit in order to give
1675 // other message processing a time to run. In particular
1676 // this avoids a delay in hiding the IME if it was
1677 // currently shown, because doing that may involve
1678 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001679 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001680 final ViewTreeObserver.OnDrawListener listener = this;
1681 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001682 public void run() {
1683 if (mWorkspace != null &&
1684 mWorkspace.getViewTreeObserver() != null) {
1685 mWorkspace.getViewTreeObserver().
1686 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001687 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001688 }
1689 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001690 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001691 }
1692 });
1693 }
1694 clearTypedText();
1695 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001696 }
1697
Adam Cohen091440a2015-03-18 14:16:05 -07001698 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001699 mHandler.removeMessages(ADVANCE_MSG);
1700 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1701 mHandler.sendMessageDelayed(msg, delay);
1702 mAutoAdvanceSentTime = System.currentTimeMillis();
1703 }
1704
Adam Cohen091440a2015-03-18 14:16:05 -07001705 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001706 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1707 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1708 mAutoAdvanceRunning = autoAdvanceRunning;
1709 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001710 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001711 sendAdvanceMessage(delay);
1712 } else {
1713 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001714 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001715 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1716 }
1717 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001718 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001719 }
1720 }
1721 }
1722
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001723 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1724
Adam Cohended9f8d2010-11-03 13:25:16 -07001725 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001726 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001727 if (msg.what == ADVANCE_MSG) {
1728 int i = 0;
1729 for (View key: mWidgetsToAdvance.keySet()) {
1730 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001731 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001732 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001733 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001734 public void run() {
1735 ((Advanceable) v).advance();
1736 }
1737 }, delay);
1738 }
1739 i++;
1740 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001741 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001742 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001743 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001744 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001745 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001746
Winsonc0b52fe2015-09-09 16:38:15 -07001747 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001748 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001749 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1750 if (v instanceof Advanceable) {
1751 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001752 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001753 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001754 }
1755 }
1756
Winsonc0b52fe2015-09-09 16:38:15 -07001757 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001758 if (mWidgetsToAdvance.containsKey(hostView)) {
1759 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001760 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001761 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001762 }
1763
Hyunyoung Song3f471442015-04-08 19:01:34 -07001764 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001765 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1766 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001767 }
1768
Winson Chunga6945242014-01-08 14:04:34 -08001769 public DragLayer getDragLayer() {
1770 return mDragLayer;
1771 }
1772
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001773 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001774 return mAppsView;
1775 }
1776
Hyunyoung Song3f471442015-04-08 19:01:34 -07001777 public WidgetsContainerView getWidgetsView() {
1778 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001779 }
1780
Winson Chunga6945242014-01-08 14:04:34 -08001781 public Workspace getWorkspace() {
1782 return mWorkspace;
1783 }
1784
1785 public Hotseat getHotseat() {
1786 return mHotseat;
1787 }
1788
Jorim Jaggid017f882014-01-14 17:08:48 -08001789 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001790 return mOverviewPanel;
1791 }
1792
Winson Chung006ee262015-08-03 14:40:11 -07001793 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001794 return mSearchDropTargetBar;
1795 }
1796
Tony Wickham34d2c912015-09-11 09:27:58 -07001797 public AppInfoDropTargetBar getAppInfoDropTargetBar() {
1798 return mAppInfoDropTargetBar;
1799 }
1800
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001801 public LauncherAppWidgetHost getAppWidgetHost() {
1802 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803 }
Romain Guycbb89e42009-06-08 15:52:54 -07001804
Winson Chunga9abd0e2010-10-27 17:18:37 -07001805 public LauncherModel getModel() {
1806 return mModel;
1807 }
1808
Adam Cohen79d90c52016-04-22 13:29:20 -07001809 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001810 return mSharedPrefs;
1811 }
1812
Adam Cohen2e6da152015-05-06 11:42:25 -07001813 public DeviceProfile getDeviceProfile() {
1814 return mDeviceProfile;
1815 }
1816
Bjorn Bringertc459e522013-06-07 19:36:01 +01001817 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001818 getWindow().closeAllPanels();
1819
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001820 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001821 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001822 }
1823
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824 @Override
1825 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001826 long startTime = 0;
1827 if (DEBUG_RESUME_TIME) {
1828 startTime = System.currentTimeMillis();
1829 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001830 super.onNewIntent(intent);
1831
1832 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001833 Folder openFolder = mWorkspace.getOpenFolder();
1834 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1835 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1836 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1837 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1838 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001839 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001840 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001841
Adam Cohen6fecd412013-10-02 17:41:50 -07001842 if (mWorkspace == null) {
1843 // Can be cases where mWorkspace is null, this prevents a NPE
1844 return;
1845 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001846 // In all these cases, only animate if we're already on home
1847 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001848
Sunny Goyal935fca12015-10-13 10:19:01 -07001849 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001850 exitSpringLoadedDragMode();
1851
1852 // If we are already on home, then just animate back to the workspace,
1853 // otherwise, just wait until onResume to set the state back to Workspace
1854 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001855 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001856 } else {
1857 mOnResumeState = State.WORKSPACE;
1858 }
1859
1860 final View v = getWindow().peekDecorView();
1861 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001862 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001863 INPUT_METHOD_SERVICE);
1864 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1865 }
1866
Winson Chungb745afb2015-03-02 11:51:23 -08001867 // Reset the apps view
1868 if (!alreadyOnHome && mAppsView != null) {
1869 mAppsView.scrollToTop();
1870 }
1871
Hyunyoung Song3f471442015-04-08 19:01:34 -07001872 // Reset the widgets view
1873 if (!alreadyOnHome && mWidgetsView != null) {
1874 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001875 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001876
Adam Cohen9211d422014-10-07 18:14:53 -07001877 if (mLauncherCallbacks != null) {
1878 mLauncherCallbacks.onHomeIntent();
1879 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 }
Adam Cohened307df2013-10-02 09:37:31 -07001881
Adam Cohen9211d422014-10-07 18:14:53 -07001882 if (mLauncherCallbacks != null) {
1883 mLauncherCallbacks.onNewIntent(intent);
1884 }
Winson15f8b172015-08-19 11:02:39 -07001885
1886 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1887 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1888 // animation.
1889 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001890 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1891 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001892 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1893 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001894
1895 // We use this flag to suppress noisy callbacks above custom content state
1896 // from onResume.
1897 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001898 mWorkspace.post(new Runnable() {
1899 @Override
1900 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001901 if (mWorkspace != null) {
1902 mWorkspace.moveToDefaultScreen(true);
1903 }
Winson15f8b172015-08-19 11:02:39 -07001904 }
1905 });
1906 }
1907 }
1908
1909 if (DEBUG_RESUME_TIME) {
1910 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1911 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001912 }
1913
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001915 public void onRestoreInstanceState(Bundle state) {
1916 super.onRestoreInstanceState(state);
1917 for (int page: mSynchronouslyBoundPages) {
1918 mWorkspace.restoreInstanceStateForChild(page);
1919 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 }
1921
1922 @Override
1923 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001924 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001925 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1926 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001927 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001928 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929
Michael Jurka883f55b2010-10-21 15:47:14 -07001930 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001931 // We close any open folder since it will not be re-opened, and we need to make sure
1932 // this state is reflected.
1933 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1934 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935
Adam Cohendcd297f2013-06-18 13:13:40 -07001936 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001937 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001938 ContentValues itemValues = new ContentValues();
1939 mPendingAddInfo.writeToValues(itemValues);
1940 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001941 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001942 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943 }
1944
Adam Cohen9211d422014-10-07 18:14:53 -07001945 if (mLauncherCallbacks != null) {
1946 mLauncherCallbacks.onSaveInstanceState(outState);
1947 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 }
1949
1950 @Override
1951 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001953
Winson Chunge7a03942011-08-05 15:05:12 -07001954 // Remove all pending runnables
1955 mHandler.removeMessages(ADVANCE_MSG);
1956 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001957 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001958 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001959
Winson Chungcd2b0142011-06-08 16:02:26 -07001960 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001961 // It's possible to receive onDestroy after a new Launcher activity has
1962 // been created. In this case, don't interfere with the new Launcher.
1963 if (mModel.isCurrentCallbacks(this)) {
1964 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001965 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001966 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001967
Sunny Goyal745bad92016-05-02 10:54:12 -07001968 if (mRotationPrefChangeHandler != null) {
1969 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1970 }
1971
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001973 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001975 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001977 mAppWidgetHost = null;
1978
1979 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980
1981 TextKeyListener.getInstance().release();
1982
Tony Wickhame2217252016-03-22 16:34:23 -07001983 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1984 .removeAccessibilityStateChangeListener(this);
1985
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001986 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001987
Michael Jurka2ecf9952012-06-18 12:52:28 -07001988 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001989
1990 if (mLauncherCallbacks != null) {
1991 mLauncherCallbacks.onDestroy();
1992 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 }
1994
Adam Cohena9cf38f2011-05-02 15:36:58 -07001995 public DragController getDragController() {
1996 return mDragController;
1997 }
1998
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 @Override
2000 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002001 onStartForResult(requestCode);
2002 super.startActivityForResult(intent, requestCode);
2003 }
2004
2005 @Override
2006 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2007 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2008 onStartForResult(requestCode);
2009 try {
2010 super.startIntentSenderForResult(intent, requestCode,
2011 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2012 } catch (IntentSender.SendIntentException e) {
2013 throw new ActivityNotFoundException();
2014 }
2015 }
2016
2017 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002018 if (requestCode >= 0) {
2019 setWaitingForResult(true);
2020 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 }
2022
Winson Chung70d72102011-08-12 11:24:35 -07002023 /**
2024 * Indicates that we want global search for this activity by setting the globalSearch
2025 * argument for {@link #startSearch} to true.
2026 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002028 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002030
Karl Rosaen138a0412009-04-23 19:00:21 -07002031 if (initialQuery == null) {
2032 // Use any text typed in the launcher as the initial query
2033 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002034 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035 if (appSearchData == null) {
2036 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002037 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 }
Winson Chungadf0c182012-08-23 14:59:07 -07002039 Rect sourceBounds = new Rect();
2040 if (mSearchDropTargetBar != null) {
2041 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2042 }
Romain Guycbb89e42009-06-08 15:52:54 -07002043
Ian Parkinson047036e2014-09-01 15:40:53 +01002044 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002045 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002046 if (clearTextImmediately) {
2047 clearTypedText();
2048 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002049
2050 // We need to show the workspace after starting the search
2051 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002052 }
2053
Ian Parkinson047036e2014-09-01 15:40:53 +01002054 /**
2055 * Start a text search.
2056 *
2057 * @return {@code true} if the search will start immediately, so any further keypresses
2058 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2059 * to buffer keypresses.
2060 */
2061 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002062 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002063 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2064 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2065 sourceBounds);
2066 }
2067
Michael Jurkaa33411c2012-06-14 16:18:21 -07002068 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002069 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002070 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002071 }
2072
2073 /**
2074 * Starts the global search activity. This code is a copied from SearchManager
2075 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002076 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002077 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002078 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002079 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2080 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2081 if (globalSearchActivity == null) {
2082 Log.w(TAG, "No global search activity found.");
2083 return;
2084 }
2085 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2086 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2087 intent.setComponent(globalSearchActivity);
2088 // Make sure that we have a Bundle to put source in
2089 if (appSearchData == null) {
2090 appSearchData = new Bundle();
2091 } else {
2092 appSearchData = new Bundle(appSearchData);
2093 }
Adam Cohen9211d422014-10-07 18:14:53 -07002094 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002095 if (!appSearchData.containsKey("source")) {
2096 appSearchData.putString("source", getPackageName());
2097 }
2098 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2099 if (!TextUtils.isEmpty(initialQuery)) {
2100 intent.putExtra(SearchManager.QUERY, initialQuery);
2101 }
2102 if (selectInitialQuery) {
2103 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2104 }
2105 intent.setSourceBounds(sourceBounds);
2106 try {
2107 startActivity(intent);
2108 } catch (ActivityNotFoundException ex) {
2109 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2110 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002111 }
2112
Yura4f93ec62014-02-04 14:15:21 +00002113 public boolean isOnCustomContent() {
2114 return mWorkspace.isOnOrMovingToCustomContent();
2115 }
2116
Winson Chung70d72102011-08-12 11:24:35 -07002117 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002118 public boolean onPrepareOptionsMenu(Menu menu) {
2119 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002120 if (mLauncherCallbacks != null) {
2121 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2122 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002123 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002124 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002125
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002126 @Override
2127 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002128 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002129 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002130 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002131 }
2132
Joe Onorato9c1289c2009-08-17 11:03:03 -04002133 public boolean isWorkspaceLocked() {
2134 return mWorkspaceLoading || mWaitingForResult;
2135 }
2136
Adam Cohen517a7f52014-03-01 12:12:59 -08002137 public boolean isWorkspaceLoading() {
2138 return mWorkspaceLoading;
2139 }
2140
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002141 private void setWorkspaceLoading(boolean value) {
2142 boolean isLocked = isWorkspaceLocked();
2143 mWorkspaceLoading = value;
2144 if (isLocked != isWorkspaceLocked()) {
2145 onWorkspaceLockedChanged();
2146 }
2147 }
2148
2149 private void setWaitingForResult(boolean value) {
2150 boolean isLocked = isWorkspaceLocked();
2151 mWaitingForResult = value;
2152 if (isLocked != isWorkspaceLocked()) {
2153 onWorkspaceLockedChanged();
2154 }
2155 }
2156
Adam Cohen9211d422014-10-07 18:14:53 -07002157 protected void onWorkspaceLockedChanged() {
2158 if (mLauncherCallbacks != null) {
2159 mLauncherCallbacks.onWorkspaceLockedChanged();
2160 }
2161 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002162
Michael Jurka0280c3b2010-09-17 15:00:07 -07002163 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002164 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002165 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002166 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2167 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002168 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002169 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002170
Tony Wickhama0628cc2015-10-14 15:23:04 -07002171 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002172 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002173 if (LOGD) {
2174 Log.d(TAG, "Adding widget from drop");
2175 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002176 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2177 }
2178
Sunny Goyale6b63a32015-01-16 16:14:29 -08002179 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002180 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2181 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002182 if (appWidgetInfo.configure != null) {
2183 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002184 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002185
Bjorn Bringert7984c942009-12-09 15:38:25 +00002186 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002187 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2188 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2189
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002190 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002191 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002192 Runnable onComplete = new Runnable() {
2193 @Override
2194 public void run() {
2195 // Exit spring loaded mode if necessary after adding the widget
2196 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2197 null);
2198 }
2199 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002200 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002201 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002202 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002203 }
2204 }
Romain Guycbb89e42009-06-08 15:52:54 -07002205
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002206 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002207 // Close any folders that may be open.
2208 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002209 mWorkspace.moveToCustomContentScreen(animate);
2210 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002211
2212 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2213 int[] cell, int spanX, int spanY) {
2214 switch (info.itemType) {
2215 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2216 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2217 int span[] = new int[2];
2218 span[0] = spanX;
2219 span[1] = spanY;
2220 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2221 container, screenId, cell, span);
2222 break;
2223 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2224 processShortcutFromDrop(info.componentName, container, screenId, cell);
2225 break;
2226 default:
2227 throw new IllegalStateException("Unknown item type: " + info.itemType);
2228 }
2229 }
2230
Adam Cohenfbba09b2011-07-18 21:43:05 -07002231 /**
2232 * Process a shortcut drop.
2233 *
2234 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002235 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002236 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002237 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002238 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2239 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002240 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002241 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002242 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002243
2244 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002245 mPendingAddInfo.cellX = cell[0];
2246 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002247 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002248
2249 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2250 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002251 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002252 }
2253
Adam Cohenfbba09b2011-07-18 21:43:05 -07002254 /**
2255 * Process a widget drop.
2256 *
2257 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002258 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002259 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002260 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002261 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2262 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002263 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002264 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002265 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002266 mPendingAddInfo.minSpanX = info.minSpanX;
2267 mPendingAddInfo.minSpanY = info.minSpanY;
2268
Adam Cohenfbba09b2011-07-18 21:43:05 -07002269 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002270 mPendingAddInfo.cellX = cell[0];
2271 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002272 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002273 if (span != null) {
2274 mPendingAddInfo.spanX = span[0];
2275 mPendingAddInfo.spanY = span[1];
2276 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002277
Adam Cohened66b2b2012-01-23 17:28:51 -08002278 AppWidgetHostView hostView = info.boundWidget;
2279 int appWidgetId;
2280 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002281 // In the case where we've prebound the widget, we remove it from the DragLayer
2282 if (LOGD) {
2283 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2284 }
2285 getDragLayer().removeView(hostView);
2286
Adam Cohened66b2b2012-01-23 17:28:51 -08002287 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002288 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002289
2290 // Clear the boundWidget so that it doesn't get destroyed.
2291 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002292 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002293 // In this case, we either need to start an activity to get permission to bind
2294 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002295 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002296 Bundle options = info.bindOptions;
2297
Sunny Goyalffe83f12014-08-14 17:39:34 -07002298 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2299 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002300 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002301 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002302 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002303 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002304 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2305 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2306 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002307 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2308 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002309 // TODO: we need to make sure that this accounts for the options bundle.
2310 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002311 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2312 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002313 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002314 }
2315
Adam Cohendcd297f2013-06-18 13:13:40 -07002316 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002317 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002318 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002319 folderInfo.title = getText(R.string.folder_name);
2320
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002321 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002322 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2323 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002324
2325 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002326 FolderIcon newFolder =
2327 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002328 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002329 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002330 // Force measure the new folder icon
2331 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2332 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002333 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002334 }
Romain Guycbb89e42009-06-08 15:52:54 -07002335
Winsonc0b52fe2015-09-09 16:38:15 -07002336 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002337 * Unbinds the view for the specified item, and removes the item and all its children.
2338 *
2339 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002340 * @param itemInfo the {@link ItemInfo} for this view.
2341 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002342 */
Winson2949fb52015-09-24 09:56:11 -07002343 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002344 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002345 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002346 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2347 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002348 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002349 } else {
2350 mWorkspace.removeWorkspaceItem(v);
2351 }
Winsonc0b52fe2015-09-09 16:38:15 -07002352 if (deleteFromDb) {
2353 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2354 }
2355 } else if (itemInfo instanceof FolderInfo) {
2356 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002357 if (v instanceof FolderIcon) {
2358 ((FolderIcon) v).removeListeners();
2359 }
Winsonc0b52fe2015-09-09 16:38:15 -07002360 mWorkspace.removeWorkspaceItem(v);
2361 if (deleteFromDb) {
2362 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2363 }
2364 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2365 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002366 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002367 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002368 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002369 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002370 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002371
Winsonc0b52fe2015-09-09 16:38:15 -07002372 } else {
2373 return false;
2374 }
2375 return true;
2376 }
2377
2378 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002379 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002380 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002381 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002382 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyal56c73602015-09-25 12:55:01 -07002383 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdValid()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002384 // Deleting an app widget ID is a void call but writes to disk before returning
2385 // to the caller...
2386 new AsyncTask<Void, Void, Void>() {
2387 public Void doInBackground(Void ... args) {
2388 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2389 return null;
2390 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002391 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002392 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002393 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002394 }
2395
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002396 @Override
2397 public boolean dispatchKeyEvent(KeyEvent event) {
2398 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2399 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002400 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002401 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002402 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002403 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002404 dumpState();
2405 return true;
2406 }
2407 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002408 }
2409 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2410 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002411 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002412 return true;
2413 }
2414 }
2415
2416 return super.dispatchKeyEvent(event);
2417 }
2418
Joe Onorato88ec0992009-11-19 13:16:06 -08002419 @Override
2420 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002421 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2422 return;
2423 }
2424
Sunny Goyal45478022015-06-08 16:52:41 -07002425 if (mDragController.isDragging()) {
2426 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002427 return;
2428 }
2429
Winson Chungb745afb2015-03-02 11:51:23 -08002430 if (isAppsViewVisible()) {
2431 showWorkspace(true);
2432 } else if (isWidgetsViewVisible()) {
2433 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002434 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002435 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002436 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002437 Folder openFolder = mWorkspace.getOpenFolder();
2438 if (openFolder.isEditingName()) {
2439 openFolder.dismissEditingName();
2440 } else {
2441 closeFolder();
2442 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002443 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002444 mWorkspace.exitWidgetResizeMode();
2445
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002446 // Back button is a no-op here, but give at least some feedback for the button press
2447 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002448 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002449 }
2450
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002451 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002452 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002453 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002454 @Override
2455 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002456 if (mAppWidgetHost != null) {
2457 mAppWidgetHost.startListening();
2458 }
Sunny Goyal655daae2015-12-08 09:28:23 -08002459
2460 // Recreate the QSB, as the widget has been reset.
2461 bindSearchProviderChanged();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002462 }
2463
2464 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002465 * Launches the intent referred by the clicked shortcut.
2466 *
2467 * @param v The view representing the clicked shortcut.
2468 */
2469 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002470 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2471 // view has detached (it's possible for this to happen if the view is removed mid touch).
2472 if (v.getWindowToken() == null) {
2473 return;
2474 }
2475
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002476 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002477 return;
2478 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002479
Adam Cohen1697b792013-09-17 19:08:21 -07002480 if (v instanceof Workspace) {
2481 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002482 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002483 }
2484 return;
2485 }
2486
2487 if (v instanceof CellLayout) {
2488 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002489 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2490 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002491 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002492 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002493 }
2494
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002495 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002496 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002497 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002498 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002499 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002500 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002501 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002502 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002503 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002504 } else if (tag instanceof AppInfo) {
2505 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002506 } else if (tag instanceof LauncherAppWidgetInfo) {
2507 if (v instanceof PendingAppWidgetHostView) {
2508 onClickPendingWidget((PendingAppWidgetHostView) v);
2509 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002510 }
2511 }
2512
Sunny Goyal70660032015-05-14 00:07:08 -07002513 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002514 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002515 return false;
2516 }
2517
Michael Jurkaaf442092010-06-10 17:01:57 -07002518 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002519 * Event handler for the app widget view which has not fully restored.
2520 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002521 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002522 if (mIsSafeModeEnabled) {
2523 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2524 return;
2525 }
2526
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002527 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002528 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002529 int widgetId = info.appWidgetId;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002530 LauncherAppWidgetProviderInfo appWidgetInfo =
2531 mAppWidgetManager.getLauncherAppWidgetInfo(widgetId);
Sunny Goyalff572272014-07-23 13:58:07 -07002532 if (appWidgetInfo != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002533 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002534 mPendingAddInfo.copyFrom(info);
2535 mPendingAddWidgetId = widgetId;
2536
Sunny Goyalffe83f12014-08-14 17:39:34 -07002537 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2538 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002539 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002540 } else if (info.installProgress < 0) {
2541 // The install has not been queued
2542 final String packageName = info.providerName.getPackageName();
2543 showBrokenAppInstallDialog(packageName,
2544 new DialogInterface.OnClickListener() {
2545 public void onClick(DialogInterface dialog, int id) {
2546 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2547 }
2548 });
2549 } else {
2550 // Download has started.
2551 final String packageName = info.providerName.getPackageName();
2552 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002553 }
2554 }
2555
2556 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002557 * Event handler for the "grid" button that appears on the home screen, which
2558 * enters all apps mode.
2559 *
2560 * @param v The view that was clicked.
2561 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002562 protected void onClickAllAppsButton(View v) {
2563 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002564 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002565 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002566 true /* updatePredictedApps */, false /* focusSearchBar */);
2567 }
2568 }
2569
2570 protected void onLongClickAllAppsButton(View v) {
2571 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2572 if (!isAppsViewVisible()) {
2573 showAppsView(true /* animated */, false /* resetListToTop */,
2574 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002575 }
2576 }
2577
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002578 private void showBrokenAppInstallDialog(final String packageName,
2579 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002580 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002581 .setTitle(R.string.abandoned_promises_title)
2582 .setMessage(R.string.abandoned_promise_explanation)
2583 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2584 .setNeutralButton(R.string.abandoned_clean_this,
2585 new DialogInterface.OnClickListener() {
2586 public void onClick(DialogInterface dialog, int id) {
2587 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2588 mWorkspace.removeAbandonedPromise(packageName, user);
2589 }
2590 })
2591 .create().show();
2592 return;
2593 }
2594
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002595 /**
2596 * Event handler for an app shortcut click.
2597 *
2598 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2599 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002600 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002601 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2602 Object tag = v.getTag();
2603 if (!(tag instanceof ShortcutInfo)) {
2604 throw new IllegalArgumentException("Input must be a Shortcut");
2605 }
2606
2607 // Open shortcut
2608 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002609
2610 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002611 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2612 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002613 // Launch activity anyway, framework will tell the user why the app is suspended.
2614 } else {
2615 int error = R.string.activity_not_available;
2616 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2617 error = R.string.safemode_shortcut_error;
2618 }
2619 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2620 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002621 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002622 }
2623
Chris Wren40c5ed32014-06-24 18:24:23 -04002624 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002625 if ((v instanceof BubbleTextView)
2626 && shortcut.isPromise()
2627 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002628 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002629 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002630 new DialogInterface.OnClickListener() {
2631 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002632 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002633 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002634 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002635 return;
2636 }
2637
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002638 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002639 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002640 }
2641
Adam Cohen091440a2015-03-18 14:16:05 -07002642 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002643 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002644 final ShortcutInfo shortcut;
2645 final Intent intent;
2646 if (tag instanceof ShortcutInfo) {
2647 shortcut = (ShortcutInfo) tag;
2648 intent = shortcut.intent;
2649 int[] pos = new int[2];
2650 v.getLocationOnScreen(pos);
2651 intent.setSourceBounds(new Rect(pos[0], pos[1],
2652 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002653
Sunny Goyal508da152014-08-14 10:53:27 -07002654 } else if (tag instanceof AppInfo) {
2655 shortcut = null;
2656 intent = ((AppInfo) tag).intent;
2657 } else {
2658 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2659 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002660
2661 boolean success = startActivitySafely(v, intent, tag);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002662 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002663
2664 if (success && v instanceof BubbleTextView) {
2665 mWaitingForResume = (BubbleTextView) v;
2666 mWaitingForResume.setStayPressed(true);
2667 }
2668 }
2669
2670 /**
2671 * Event handler for a folder icon click.
2672 *
2673 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2674 */
2675 protected void onClickFolderIcon(View v) {
2676 if (LOGD) Log.d(TAG, "onClickFolder");
2677 if (!(v instanceof FolderIcon)){
2678 throw new IllegalArgumentException("Input must be a FolderIcon");
2679 }
2680
2681 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002682 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002683 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002684 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002685 }
Michael Jurka5130e402011-10-13 04:55:35 -07002686 }
2687
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002688 /**
2689 * Event handler for the (Add) Widgets button that appears after a long press
2690 * on the home screen.
2691 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002692 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002693 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002694 if (mIsSafeModeEnabled) {
2695 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2696 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002697 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002698 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002699 }
2700
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002701 /**
2702 * Event handler for the wallpaper picker button that appears after a long press
2703 * on the home screen.
2704 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002705 protected void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002706 if (!Utilities.isWallapaperAllowed(this)) {
2707 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2708 return;
2709 }
2710
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002711 String pickerPackage = getString(R.string.wallpaper_picker_package);
2712 if (TextUtils.isEmpty(pickerPackage)) {
2713 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2714 }
2715
Tony Wickham785f7a52015-08-31 17:28:32 -07002716 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2717 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002718 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER)
2719 .setPackage(pickerPackage)
2720 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset),
2721 REQUEST_PICK_WALLPAPER);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002722 }
2723
2724 /**
2725 * Event handler for a click on the settings button that appears after a long press
2726 * on the home screen.
2727 */
Sunny Goyal745bad92016-05-02 10:54:12 -07002728 private void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002729 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002730 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2731 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002732 }
2733
Adam Cohen61f560d2013-09-30 15:58:20 -07002734 public View.OnTouchListener getHapticFeedbackTouchListener() {
2735 if (mHapticFeedbackTouchListener == null) {
2736 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002737 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002738 @Override
2739 public boolean onTouch(View v, MotionEvent event) {
2740 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2741 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2742 }
2743 return false;
2744 }
2745 };
2746 }
2747 return mHapticFeedbackTouchListener;
2748 }
2749
Tony Wickhame2217252016-03-22 16:34:23 -07002750 @Override
2751 public void onAccessibilityStateChanged(boolean enabled) {
2752 mDragLayer.onAccessibilityStateChanged(enabled);
2753 }
2754
Adam Cohen9211d422014-10-07 18:14:53 -07002755 public void onDragStarted(View view) {
2756 if (isOnCustomContent()) {
2757 // Custom content screen doesn't participate in drag and drop. If on custom
2758 // content screen, move to default.
2759 moveWorkspaceToDefaultScreen();
2760 }
Adam Cohen9211d422014-10-07 18:14:53 -07002761 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002762
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002763 /**
2764 * Called when the user stops interacting with the launcher.
2765 * This implies that the user is now on the homescreen and is not doing housekeeping.
2766 */
Adam Cohen9211d422014-10-07 18:14:53 -07002767 protected void onInteractionEnd() {
2768 if (mLauncherCallbacks != null) {
2769 mLauncherCallbacks.onInteractionEnd();
2770 }
2771 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002772
2773 /**
2774 * Called when the user starts interacting with the launcher.
2775 * The possible interactions are:
2776 * - open all apps
2777 * - reorder an app shortcut, or a widget
2778 * - open the overview mode.
2779 * This is a good time to stop doing things that only make sense
2780 * when the user is on the homescreen and not doing housekeeping.
2781 */
Adam Cohen9211d422014-10-07 18:14:53 -07002782 protected void onInteractionBegin() {
2783 if (mLauncherCallbacks != null) {
2784 mLauncherCallbacks.onInteractionBegin();
2785 }
2786 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002787
Winson Chungcd99cd32015-04-29 11:03:24 -07002788 /** Updates the interaction state. */
2789 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002790 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002791 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002792 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2793 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002794 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002795 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002796 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002797 onInteractionEnd();
2798 }
2799 }
2800
Winson Chung8f1eff72015-05-28 17:33:40 -07002801 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002802 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002803 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002804 // Only launch using the new animation if the shortcut has not opted out (this is a
2805 // private contract between launcher and may be ignored in the future).
2806 boolean useLaunchAnimation = (v != null) &&
2807 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002808 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2809 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002810
Kenny Guy1317e2d2014-05-08 18:52:50 +01002811 UserHandleCompat user = null;
2812 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2813 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2814 user = userManager.getUserForSerialNumber(serialNumber);
2815 }
2816
2817 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002818 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002819 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002820 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002821 int left = 0, top = 0;
2822 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2823 if (v instanceof TextView) {
2824 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002825 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2826 if (icon != null) {
2827 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002828 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002829 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002830 width = bounds.width();
2831 height = bounds.height();
2832 }
2833 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002834 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2835 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002836 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002837 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002838 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002839 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002840 // On L devices, we use the device default slide-up transition.
2841 // On L MR1 devices, we a custom version of the slide-up transition which
2842 // doesn't have the delay present in the device default.
2843 opts = ActivityOptions.makeCustomAnimation(this,
2844 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002845 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002846 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002847 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002848
2849 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2850 // Could be launching some bookkeeping activity
2851 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002852 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002853 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002854 launcherApps.startActivityForProfile(intent.getComponent(), user,
2855 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002856 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002857 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002858 } catch (SecurityException e) {
Sunny Goyal28c6b962015-10-12 11:42:05 -07002859 if (Utilities.ATLEAST_MARSHMALLOW && tag instanceof ItemInfo) {
2860 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2861 // corresponding permission. Show the appropriate permission prompt if that
2862 // is the case.
2863 if (intent.getComponent() == null
2864 && Intent.ACTION_CALL.equals(intent.getAction())
2865 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2866 PackageManager.PERMISSION_GRANTED) {
2867 // TODO: Rename sPendingAddItem to a generic name.
2868 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2869 0, (ItemInfo) tag);
2870 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2871 REQUEST_PERMISSION_CALL_PHONE);
2872 return false;
2873 }
2874 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002875 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002876 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002877 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002878 "or use the exported attribute for this activity. "
2879 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002880 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002881 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002882 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002883
Winson Chungbedf9232015-07-10 12:38:30 -07002884 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002885 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002886 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2887 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2888 return false;
2889 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002890 try {
2891 success = startActivity(v, intent, tag);
2892 } catch (ActivityNotFoundException e) {
2893 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2894 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2895 }
2896 return success;
2897 }
2898
Adam Cohen268c4752012-06-06 17:47:33 -07002899 /**
2900 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2901 * in the DragLayer in the exact absolute location of the original FolderIcon.
2902 */
2903 private void copyFolderIconToImage(FolderIcon fi) {
2904 final int width = fi.getMeasuredWidth();
2905 final int height = fi.getMeasuredHeight();
2906
2907 // Lazy load ImageView, Bitmap and Canvas
2908 if (mFolderIconImageView == null) {
2909 mFolderIconImageView = new ImageView(this);
2910 }
2911 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2912 mFolderIconBitmap.getHeight() != height) {
2913 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2914 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2915 }
2916
2917 DragLayer.LayoutParams lp;
2918 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2919 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2920 } else {
2921 lp = new DragLayer.LayoutParams(width, height);
2922 }
2923
Adam Cohen307fe232012-08-16 17:55:58 -07002924 // The layout from which the folder is being opened may be scaled, adjust the starting
2925 // view size by this scale factor.
2926 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002927 lp.customPosition = true;
2928 lp.x = mRectForFolderAnimation.left;
2929 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002930 lp.width = (int) (scale * width);
2931 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002932
2933 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2934 fi.draw(mFolderIconCanvas);
2935 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002936 if (fi.getFolder() != null) {
2937 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2938 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002939 }
Adam Cohen268c4752012-06-06 17:47:33 -07002940 // Just in case this image view is still in the drag layer from a previous animation,
2941 // we remove it and re-add it.
2942 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2943 mDragLayer.removeView(mFolderIconImageView);
2944 }
2945 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002946 if (fi.getFolder() != null) {
2947 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002948 }
Adam Cohen268c4752012-06-06 17:47:33 -07002949 }
2950
Adam Cohen37b2a492016-04-06 18:36:06 -07002951 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002952 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002953 FolderInfo info = (FolderInfo) fi.getTag();
2954 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2955 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002956 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2957 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002958 }
2959
Adam Cohen268c4752012-06-06 17:47:33 -07002960 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2961 copyFolderIconToImage(fi);
2962 fi.setVisibility(View.INVISIBLE);
2963
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002964 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2965 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002966 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002967 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2968 }
2969 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002970 oa.start();
2971 }
2972
Sunny Goyal935fca12015-10-13 10:19:01 -07002973 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002974 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002975 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002976
Adam Cohen268c4752012-06-06 17:47:33 -07002977 // We remove and re-draw the FolderIcon in-case it has changed
2978 mDragLayer.removeView(mFolderIconImageView);
2979 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08002980
2981 if (cl != null) {
2982 cl.clearFolderLeaveBehind();
2983 }
2984
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002985 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002986 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002987 oa.addListener(new AnimatorListenerAdapter() {
2988 @Override
2989 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002990 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07002991 // Remove the ImageView copy of the FolderIcon and make the original visible.
2992 mDragLayer.removeView(mFolderIconImageView);
2993 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002994 }
2995 }
2996 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002997 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07002998 if (!animate) {
2999 oa.end();
3000 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003001 }
3002
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003003 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003004 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003005 * is animated relative to the specified View. If the View is null, no animation
3006 * is played.
3007 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003008 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003009 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003010 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003011
Adam Cohenfb91f302012-06-11 15:45:18 -07003012 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003013 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3014 if (openFolder != null && openFolder != folder) {
3015 // Close any open folder before opening a folder.
3016 closeFolder();
3017 }
3018
Adam Cohena9cf38f2011-05-02 15:36:58 -07003019 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003020
Adam Cohena9cf38f2011-05-02 15:36:58 -07003021 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003022
Sunny Goyalf4066152015-04-15 09:42:19 -07003023 // While the folder is open, the position of the icon cannot change.
3024 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3025
Adam Cohen4554ee12011-08-03 16:13:21 -07003026 // Just verify that the folder hasn't already been added to the DragLayer.
3027 // There was a one-off crash where the folder had a parent already.
3028 if (folder.getParent() == null) {
3029 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003030 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003031 } else {
3032 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3033 folder.getParent() + ").");
3034 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003035 folder.animateOpen();
3036
3037 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003038
3039 // Notify the accessibility manager that this folder "window" has appeared and occluded
3040 // the workspace items
3041 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3042 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003043 }
3044
3045 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003046 closeFolder(true);
3047 }
3048
3049 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003050 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003051 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003052 if (folder.isEditingName()) {
3053 folder.dismissEditingName();
3054 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003055 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003056 }
3057 }
3058
Sunny Goyal935fca12015-10-13 10:19:01 -07003059 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003060 animate &= !Utilities.isPowerSaverOn(this);
3061
Adam Cohen4554ee12011-08-03 16:13:21 -07003062 folder.getInfo().opened = false;
3063
3064 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3065 if (parent != null) {
3066 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003067 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003068 if (fi != null) {
3069 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3070 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003071 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003072 if (animate) {
3073 folder.animateClosed();
3074 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003075 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003076 }
Winson Chung83ca4802013-04-12 15:10:52 -07003077
Sunny Goyal935fca12015-10-13 10:19:01 -07003078 // Notify the accessibility manager that this folder "window" has disappeared and no
3079 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003080 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003081 }
3082
Tony Wickhamdadb3042016-02-24 11:07:00 -08003083 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003084 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003085 if (!isDraggingEnabled()) return false;
3086 if (isWorkspaceLocked()) return false;
3087 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003088
Winson Chung76648c52015-07-10 14:33:23 -07003089 if (v == mAllAppsButton) {
3090 onLongClickAllAppsButton(v);
3091 return true;
3092 }
3093
Adam Cohen1697b792013-09-17 19:08:21 -07003094 if (v instanceof Workspace) {
3095 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003096 if (!mWorkspace.isTouchActive()) {
3097 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003098 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3099 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3100 return true;
3101 } else {
3102 return false;
3103 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003104 } else {
3105 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003106 }
Adam Cohen1697b792013-09-17 19:08:21 -07003107 }
3108
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003109 CellLayout.CellInfo longClickCellInfo = null;
3110 View itemUnderLongClick = null;
3111 if (v.getTag() instanceof ItemInfo) {
3112 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003113 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003114 itemUnderLongClick = longClickCellInfo.cell;
3115 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003116 }
3117
Winson Chung3d503fb2011-07-13 17:25:49 -07003118 // The hotseat touch handling does not go through Workspace, and we always allow long press
3119 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003120 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003121 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003122 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003123 // User long pressed on empty space
3124 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3125 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003126 if (mWorkspace.isInOverviewMode()) {
3127 mWorkspace.startReordering(v);
3128 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003129 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003130 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003131 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003132 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3133 mHotseat.getOrderInHotseat(
3134 longClickCellInfo.cellX,
3135 longClickCellInfo.cellY));
3136 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003137 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003138 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003139 }
3140 }
3141 }
3142 return true;
3143 }
3144
Winson Chung3d503fb2011-07-13 17:25:49 -07003145 boolean isHotseatLayout(View layout) {
3146 return mHotseat != null && layout != null &&
3147 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3148 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003149
Winson Chung3d503fb2011-07-13 17:25:49 -07003150 /**
3151 * Returns the CellLayout of the specified container at the specified screen.
3152 */
Sunny Goyal92820592015-03-02 11:31:35 -08003153 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003154 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3155 if (mHotseat != null) {
3156 return mHotseat.getLayout();
3157 } else {
3158 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003159 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003160 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003161 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003162 }
3163 }
3164
Winson Chungb745afb2015-03-02 11:51:23 -08003165 /**
3166 * For overridden classes.
3167 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003168 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003169 return isAppsViewVisible();
3170 }
3171
3172 public boolean isAppsViewVisible() {
3173 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3174 }
3175
3176 public boolean isWidgetsViewVisible() {
3177 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003178 }
3179
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003180 private void setWorkspaceBackground(int background) {
3181 switch (background) {
3182 case WORKSPACE_BACKGROUND_TRANSPARENT:
3183 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3184 break;
3185 case WORKSPACE_BACKGROUND_BLACK:
3186 getWindow().setBackgroundDrawable(null);
3187 break;
3188 default:
3189 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3190 }
Craig Mautner360310b2012-10-26 15:13:08 -07003191 }
3192
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003193 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003194 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3195 int curflags = getWindow().getAttributes().flags
3196 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3197 if (wpflags != curflags) {
3198 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3199 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003200 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003201 }
3202
Michael Jurkae326f182011-11-21 14:05:46 -08003203 @Override
3204 public void onTrimMemory(int level) {
3205 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003206 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3207 // The widget preview db can result in holding onto over
3208 // 3MB of memory for caching which isn't necessary.
3209 SQLiteDatabase.releaseMemory();
3210
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003211 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003212 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003213 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003214 if (mLauncherCallbacks != null) {
3215 mLauncherCallbacks.onTrimMemory(level);
3216 }
Michael Jurkae326f182011-11-21 14:05:46 -08003217 }
3218
Winson5c6bdbb2015-09-03 11:36:19 -07003219 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003220 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003221 }
3222
Winson5c6bdbb2015-09-03 11:36:19 -07003223 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003224 boolean changed = mState != State.WORKSPACE ||
3225 mWorkspace.getState() != Workspace.State.NORMAL;
3226 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003227 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003228 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003229 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003230
Michael Jurkab3e22d92011-10-31 15:58:33 -07003231 // Set focus to the AppsCustomize button
3232 if (mAllAppsButton != null) {
3233 mAllAppsButton.requestFocus();
3234 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003235 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003236
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003237 // Change the state *after* we've called all the transition code
3238 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003239
Winson Chung5fb63472011-02-02 17:03:37 -08003240 // Resume the auto-advance of widgets
3241 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003242 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003243
Winson Chung0f785722015-04-08 10:27:49 -07003244 if (changed) {
3245 // Send an accessibility event to announce the context change
3246 getWindow().getDecorView()
3247 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003248 }
Winson5c6bdbb2015-09-03 11:36:19 -07003249 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003250 }
3251
Winsone9f27272015-10-13 10:47:51 -07003252 /**
3253 * Shows the overview button.
3254 */
Adam Cohened307df2013-10-02 09:37:31 -07003255 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003256 showOverviewMode(animated, false);
3257 }
3258
3259 /**
3260 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3261 * onto one of the overview panel buttons.
3262 */
3263 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3264 Runnable postAnimRunnable = null;
3265 if (requestButtonFocus) {
3266 postAnimRunnable = new Runnable() {
3267 @Override
3268 public void run() {
3269 // Hitting the menu button when in touch mode does not trigger touch mode to
3270 // be disabled, so if requested, force focus on one of the overview panel
3271 // buttons.
3272 mOverviewPanel.requestFocusFromTouch();
3273 }
3274 };
3275 }
Adam Cohened307df2013-10-02 09:37:31 -07003276 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003277 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003278 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003279 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003280 }
3281
Winson Chungb745afb2015-03-02 11:51:23 -08003282 /**
3283 * Shows the apps view.
3284 */
Winson Chung76648c52015-07-10 14:33:23 -07003285 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3286 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003287 if (resetListToTop) {
3288 mAppsView.scrollToTop();
3289 }
Winson Chung4ac30062015-05-08 17:34:17 -07003290 if (updatePredictedApps) {
3291 tryAndUpdatePredictedApps();
3292 }
Winson Chung76648c52015-07-10 14:33:23 -07003293 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003294 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003295
Winson Chungb745afb2015-03-02 11:51:23 -08003296 /**
3297 * Shows the widgets view.
3298 */
3299 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003300 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003301 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003302 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003303 }
Winson Chung76648c52015-07-10 14:33:23 -07003304 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003305
3306 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003307 @Override
3308 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003309 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003310 }
3311 });
Winson Chungb745afb2015-03-02 11:51:23 -08003312 }
3313
3314 /**
3315 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003316 *
3317 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003318 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003319 // TODO: calling method should use the return value so that when {@code false} is returned
3320 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003321 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003322 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3323 mState != State.WIDGETS_SPRING_LOADED) {
3324 return false;
3325 }
3326 if (toState != State.APPS && toState != State.WIDGETS) {
3327 return false;
3328 }
Winson Chungb745afb2015-03-02 11:51:23 -08003329
3330 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003331 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3332 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003333 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003334 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003335 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003336
Michael Jurkab3e22d92011-10-31 15:58:33 -07003337 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003338 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003339
3340 // Pause the auto-advance of widgets until we are out of AllApps
3341 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003342 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003343 closeFolder();
3344
3345 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003346 getWindow().getDecorView()
3347 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003348 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003349 }
3350
Winson Chungcd99cd32015-04-29 11:03:24 -07003351 /**
3352 * Updates the workspace and interaction state on state change, and return the animation to this
3353 * new state.
3354 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003355 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003356 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003357 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003358 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003359 updateInteraction(fromState, toState);
3360 return anim;
3361 }
3362
Jun Mukaif0033da2015-08-04 18:34:30 -07003363 public void onLauncherClingShown() {
3364 // When a launcher cling appears, it should cover the underlying layers, so their focus
3365 // should be blocked.
3366 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3367 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3368 }
3369 }
3370
3371 public void onLauncherClingDismissed() {
3372 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3373 }
3374
Hyunyoung Song3f471442015-04-08 19:01:34 -07003375 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003376 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003377 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003378 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003379 }
Winson Chungb745afb2015-03-02 11:51:23 -08003380
Winson Chung006ee262015-08-03 14:40:11 -07003381 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003382 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003383 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003384
3385 if (isAppsViewVisible()) {
3386 mState = State.APPS_SPRING_LOADED;
3387 } else if (isWidgetsViewVisible()) {
3388 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003389 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003390 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003391 } else {
3392 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003393 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003394 }
Adam Cohen7777d962011-08-18 18:58:38 -07003395
Hyunyoung Song3f471442015-04-08 19:01:34 -07003396 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003397 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003398 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003399
Winson Chunge7a03942011-08-05 15:05:12 -07003400 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003401 @Override
3402 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003403 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003404 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3405 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003406 // Before we show workspace, hide all apps again because
3407 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3408 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003409 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003410 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003411 } else {
3412 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003413 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003414 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003415 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003416 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003417
Tony Wickhame0c33232016-02-08 11:37:04 -08003418 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003419 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3420 || mState == State.WIDGETS_SPRING_LOADED;
3421 }
3422
Michael Jurkad3ef3062010-11-23 16:23:58 -08003423 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003424 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003425 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003426 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003427 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003428 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003429 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3430 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003431 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003432 }
3433
Winson Chung4ac30062015-05-08 17:34:17 -07003434 /**
3435 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3436 * resumed.
3437 */
3438 private void tryAndUpdatePredictedApps() {
3439 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003440 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003441 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003442 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003443 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003444 }
3445 }
3446 }
3447
Michael Jurkab3e22d92011-10-31 15:58:33 -07003448 void lockAllApps() {
3449 // TODO
3450 }
3451
3452 void unlockAllApps() {
3453 // TODO
3454 }
3455
Winsonf768d932015-09-25 16:12:35 -07003456 public boolean launcherCallbacksProvidesSearch() {
3457 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3458 }
3459
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003460 public View getOrCreateQsbBar() {
Winsonf768d932015-09-25 16:12:35 -07003461 if (launcherCallbacksProvidesSearch()) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003462 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003463 }
3464
Adam Cohen24ce0b32014-01-14 16:18:14 -08003465 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003466 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3467 if (searchProvider == null) {
3468 return null;
3469 }
3470
3471 Bundle opts = new Bundle();
3472 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003473 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003474
Tony Wickham3a3517f2015-10-06 11:27:04 -07003475 // Determine the min and max dimensions of the widget.
3476 LauncherAppState app = LauncherAppState.getInstance();
3477 DeviceProfile portraitProfile = app.getInvariantDeviceProfile().portraitProfile;
3478 DeviceProfile landscapeProfile = app.getInvariantDeviceProfile().landscapeProfile;
3479 float density = getResources().getDisplayMetrics().density;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003480 Point searchDimens = portraitProfile.getSearchBarDimensForWidgetOpts(getResources());
3481 int maxHeight = (int) (searchDimens.y / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003482 int minHeight = maxHeight;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003483 int maxWidth = (int) (searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003484 int minWidth = maxWidth;
3485 if (!landscapeProfile.isVerticalBarLayout()) {
Tony Wickhama3c74d12015-10-23 11:43:47 -07003486 searchDimens = landscapeProfile.getSearchBarDimensForWidgetOpts(getResources());
3487 maxHeight = (int) Math.max(maxHeight, searchDimens.y / density);
3488 minHeight = (int) Math.min(minHeight, searchDimens.y / density);
3489 maxWidth = (int) Math.max(maxWidth, searchDimens.x / density);
3490 minWidth = (int) Math.min(minWidth, searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003491 }
3492 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, maxHeight);
3493 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, minHeight);
3494 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, maxWidth);
3495 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, minWidth);
Tony Wickhameef44322015-10-20 13:24:36 -07003496 if (LOGD) {
3497 Log.d(TAG, "QSB widget options: maxHeight=" + maxHeight + " minHeight=" + minHeight
3498 + " maxWidth=" + maxWidth + " minWidth=" + minWidth);
3499 }
Tony Wickham3a3517f2015-10-06 11:27:04 -07003500
Tony Wickham775455c2015-10-16 09:49:32 -07003501 if (mLauncherCallbacks != null) {
3502 opts.putAll(mLauncherCallbacks.getAdditionalSearchWidgetOptions());
3503 }
3504
Sunny Goyalf7258242015-10-19 16:59:07 -07003505 int widgetId = mSharedPrefs.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003506 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003507 if (!searchProvider.provider.flattenToString().equals(
Sunny Goyalf7258242015-10-19 16:59:07 -07003508 mSharedPrefs.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003509 || (widgetInfo == null)
3510 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003511 // A valid widget is not already bound.
3512 if (widgetId > -1) {
3513 mAppWidgetHost.deleteAppWidgetId(widgetId);
3514 widgetId = -1;
3515 }
3516
3517 // Try to bind a new widget
3518 widgetId = mAppWidgetHost.allocateAppWidgetId();
3519
3520 if (!AppWidgetManagerCompat.getInstance(this)
3521 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3522 mAppWidgetHost.deleteAppWidgetId(widgetId);
3523 widgetId = -1;
3524 }
3525
Sunny Goyalf7258242015-10-19 16:59:07 -07003526 mSharedPrefs.edit()
Sunny Goyal594d76d2014-11-06 10:12:54 -08003527 .putInt(QSB_WIDGET_ID, widgetId)
3528 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
Sunny Goyald8043982015-12-17 22:23:02 -08003529 .apply();
Sunny Goyal594d76d2014-11-06 10:12:54 -08003530 }
3531
Sunny Goyal8d595092015-07-06 12:22:14 -07003532 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003533 if (widgetId != -1) {
3534 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
Sunny Goyalebfae6e2015-08-27 15:30:25 -07003535 mQsb.setId(R.id.qsb_widget);
Sunny Goyal3f732192016-05-13 13:48:44 -07003536 if (!Utilities.containsAll(
3537 AppWidgetManager.getInstance(this).getAppWidgetOptions(widgetId), opts)) {
3538 // Launcher should not be updating the options often.
3539 FileLog.d(TAG, "Options for QSB were not same");
3540 mQsb.updateAppWidgetOptions(opts);
3541 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08003542 mQsb.setPadding(0, 0, 0, 0);
3543 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003544 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003545 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003546 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003547 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003548 }
3549
Sunny Goyal22235bc2015-04-03 09:23:43 -07003550 private void reinflateQSBIfNecessary() {
3551 if (mQsb instanceof LauncherAppWidgetHostView &&
3552 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3553 mSearchDropTargetBar.removeView(mQsb);
3554 mQsb = null;
3555 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3556 }
3557 }
3558
Michael Jurkad7c28052012-04-27 15:43:36 -07003559 @Override
3560 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003561 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003562 final List<CharSequence> text = event.getText();
3563 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003564 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003565 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003566 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003567 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003568 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003569 } else if (mWorkspace != null) {
3570 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003571 } else {
3572 text.add(getString(R.string.all_apps_home_button_label));
3573 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003574 return result;
3575 }
3576
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003577 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003578 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003579 */
Adam Cohen091440a2015-03-18 14:16:05 -07003580 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003581 @Override
3582 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003583 closeSystemDialogs();
3584 }
3585 }
3586
3587 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003588 * If the activity is currently paused, signal that we need to run the passed Runnable
3589 * in onResume.
3590 *
3591 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003592 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3593 * wrong when we're not running, and if the activity comes back to what the configuration was
3594 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003595 *
3596 * Implementation of the method from LauncherModel.Callbacks.
3597 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003598 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003599 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003600 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003601 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003602 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003603 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003604 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003605 }
3606 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003607 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003608 return true;
3609 } else {
3610 return false;
3611 }
3612 }
3613
Michael Jurkac402cd92013-05-20 15:49:32 +02003614 private boolean waitUntilResume(Runnable run) {
3615 return waitUntilResume(run, false);
3616 }
3617
Michael Jurka1e2f4652013-07-08 18:03:46 -07003618 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003619 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003620 }
3621
Michael Jurka7607c2f2013-04-03 14:33:19 -07003622 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003623 * If the activity is currently paused, signal that we need to re-run the loader
3624 * in onResume.
3625 *
3626 * This needs to be called from incoming places where resources might have been loaded
3627 * while we are paused. That is becaues the Configuration might be wrong
3628 * when we're not running, and if it comes back to what it was when we
3629 * were paused, we are not restarted.
3630 *
3631 * Implementation of the method from LauncherModel.Callbacks.
3632 *
3633 * @return true if we are currently paused. The caller might be able to
3634 * skip some work in that case since we will come back again.
3635 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003636 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003637 public boolean setLoadOnResume() {
3638 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003639 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003640 mOnResumeNeedsLoad = true;
3641 return true;
3642 } else {
3643 return false;
3644 }
3645 }
3646
3647 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003648 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003649 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003650 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003651 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003652 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003653 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003654 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003655 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003656 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003657 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003658
Joe Onorato9c1289c2009-08-17 11:03:03 -04003659 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003660 * Clear any pending bind callbacks. This is called when is loader is planning to
3661 * perform a full rebind from scratch.
3662 */
3663 @Override
3664 public void clearPendingBinds() {
3665 mBindOnResumeCallbacks.clear();
3666 if (mPendingExecutor != null) {
3667 mPendingExecutor.markCompleted();
3668 mPendingExecutor = null;
3669 }
3670 }
3671
3672 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003673 * Refreshes the shortcuts shown on the workspace.
3674 *
3675 * Implementation of the method from LauncherModel.Callbacks.
3676 */
3677 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003678 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003679
Winson Chungd64d1762013-08-20 14:37:16 -07003680 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003681 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003682 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003683
Michael Jurka05bf644e2011-11-30 20:28:53 -08003684 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003685 if (mHotseat != null) {
3686 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003687 }
3688 }
3689
Adam Cohendcd297f2013-06-18 13:13:40 -07003690 @Override
3691 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003692 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003693
Adam Cohen5084cba2013-09-03 12:01:16 -07003694 // If there are no screens, we need to have an empty screen
3695 if (orderedScreenIds.size() == 0) {
3696 mWorkspace.addExtraEmptyScreen();
3697 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003698
3699 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003700 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003701 if (hasCustomContentToLeft()) {
3702 mWorkspace.createCustomContentContainer();
3703 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003704 }
Winson Chung64359a52013-07-08 17:17:08 -07003705 }
3706
3707 @Override
3708 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003709 int count = orderedScreenIds.size();
3710 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003711 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003712 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003713 }
3714
Winson Chungd64d1762013-08-20 14:37:16 -07003715 public void bindAppsAdded(final ArrayList<Long> newScreens,
3716 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003717 final ArrayList<ItemInfo> addAnimated,
3718 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003719 Runnable r = new Runnable() {
3720 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003721 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003722 }
3723 };
3724 if (waitUntilResume(r)) {
3725 return;
3726 }
3727
Winson Chungd64d1762013-08-20 14:37:16 -07003728 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003729 if (newScreens != null) {
3730 bindAddScreens(newScreens);
3731 }
Winson Chungd64d1762013-08-20 14:37:16 -07003732
3733 // We add the items without animation on non-visible pages, and with
3734 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003735 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003736 bindItems(addNotAnimated, 0,
3737 addNotAnimated.size(), false);
3738 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003739 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003740 bindItems(addAnimated, 0,
3741 addAnimated.size(), true);
3742 }
Winson Chungc58497e2013-09-03 17:48:37 -07003743
Winson Chung87412982013-10-03 18:34:14 -07003744 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003745 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003746
Winson Chungb745afb2015-03-02 11:51:23 -08003747 if (addedApps != null && mAppsView != null) {
3748 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003749 }
Winson Chungd64d1762013-08-20 14:37:16 -07003750 }
3751
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003752 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003753 * Bind the items start-end from the list.
3754 *
3755 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003756 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003757 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003758 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3759 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003760 Runnable r = new Runnable() {
3761 public void run() {
3762 bindItems(shortcuts, start, end, forceAnimateIcons);
3763 }
3764 };
3765 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003766 return;
3767 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003768
Winson Chungf0c6ae02012-03-21 16:10:31 -07003769 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003770 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3771 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003772 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003773 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003774 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003775 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003776 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003777
3778 // Short circuit if we are loading dock items for a configuration which has no dock
3779 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3780 mHotseat == null) {
3781 continue;
3782 }
3783
Sunny Goyal639e9062015-08-19 19:17:06 -07003784 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003785 switch (item.itemType) {
3786 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3787 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003788 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003789 view = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003790
Winson Chung64359a52013-07-08 17:17:08 -07003791 /*
3792 * TODO: FIX collision case
3793 */
Winson Chungce376632013-07-11 16:12:41 -07003794 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3795 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3796 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003797 View v = cl.getChildAt(item.cellX, item.cellY);
3798 Object tag = v.getTag();
3799 String desc = "Collision while binding workspace item: " + item
3800 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003801 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003802 throw (new RuntimeException(desc));
3803 } else {
3804 Log.d(TAG, desc);
3805 }
Winson Chungce376632013-07-11 16:12:41 -07003806 }
Winson Chung64359a52013-07-08 17:17:08 -07003807 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003808 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003809 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003810 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003811 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003812 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003813 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003814 default:
3815 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003816 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003817
3818 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3819 item.cellY, 1, 1);
3820 if (animateIcons) {
3821 // Animate all the applications up now
3822 view.setAlpha(0f);
3823 view.setScaleX(0f);
3824 view.setScaleY(0f);
3825 bounceAnims.add(createNewAppBounceAnimation(view, i));
3826 newShortcutsScreenId = item.screenId;
3827 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003828 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003829
Winson Chung997a9232013-07-24 15:33:46 -07003830 if (animateIcons) {
3831 // Animate to the correct page
3832 if (newShortcutsScreenId > -1) {
3833 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003834 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003835 final Runnable startBounceAnimRunnable = new Runnable() {
3836 public void run() {
3837 anim.playTogether(bounceAnims);
3838 anim.start();
3839 }
3840 };
Winson Chung997a9232013-07-24 15:33:46 -07003841 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003842 // We post the animation slightly delayed to prevent slowdowns
3843 // when we are loading right after we return to launcher.
3844 mWorkspace.postDelayed(new Runnable() {
3845 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003846 if (mWorkspace != null) {
3847 mWorkspace.snapToPage(newScreenIndex);
3848 mWorkspace.postDelayed(startBounceAnimRunnable,
3849 NEW_APPS_ANIMATION_DELAY);
3850 }
Winson Chung94d67682013-09-25 16:29:40 -07003851 }
3852 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003853 } else {
3854 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003855 }
3856 }
Winson Chung64359a52013-07-08 17:17:08 -07003857 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003858 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003859 }
3860
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003861 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3862 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3863 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003864 view.updateAppWidget(null);
3865 view.setOnClickListener(this);
3866 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003867 mWorkspace.requestLayout();
3868 }
3869
Joe Onorato9c1289c2009-08-17 11:03:03 -04003870 /**
3871 * Add the views for a widget to the workspace.
3872 *
3873 * Implementation of the method from LauncherModel.Callbacks.
3874 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003875 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003876 Runnable r = new Runnable() {
3877 public void run() {
3878 bindAppWidget(item);
3879 }
3880 };
3881 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003882 return;
3883 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003884
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003885 if (mIsSafeModeEnabled) {
3886 bindSafeModeWidget(item);
3887 return;
3888 }
3889
Daniel Sandler843e8602010-06-07 14:59:01 -04003890 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3891 if (DEBUG_WIDGETS) {
3892 Log.d(TAG, "bindAppWidget: " + item);
3893 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003894
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003895 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003896
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003897 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3898 // If the provider is not ready, bind as a pending widget.
3899 appWidgetInfo = null;
3900 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3901 // The widget id is not valid. Try to find the widget based on the provider info.
3902 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3903 } else {
3904 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3905 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003906
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003907 // If the provider is ready, but the width is not yet restored, try to restore it.
3908 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3909 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003910 if (appWidgetInfo == null) {
3911 if (DEBUG_WIDGETS) {
3912 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3913 + " belongs to component " + item.providerName
3914 + ", as the povider is null");
3915 }
3916 LauncherModel.deleteItemFromDatabase(this, item);
3917 return;
3918 }
Sunny Goyalff572272014-07-23 13:58:07 -07003919
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003920 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003921 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003922 // Note: This assumes that the id remap broadcast is received before this step.
3923 // If that is not the case, the id remap will be ignored and user may see the
3924 // click to setup view.
Sunny Goyal16466f12016-03-10 05:34:30 -08003925 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003926 pendingInfo.spanX = item.spanX;
3927 pendingInfo.spanY = item.spanY;
3928 pendingInfo.minSpanX = item.minSpanX;
3929 pendingInfo.minSpanY = item.minSpanY;
Sunny Goyalb740f592015-12-17 23:22:42 -08003930 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003931
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003932 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
3933 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3934 newWidgetId, appWidgetInfo, options);
3935
3936 // TODO consider showing a permission dialog when the widget is clicked.
3937 if (!success) {
3938 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3939 if (DEBUG_WIDGETS) {
3940 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3941 + " belongs to component " + item.providerName
3942 + ", as the launcher is unable to bing a new widget id");
3943 }
3944 LauncherModel.deleteItemFromDatabase(this, item);
3945 return;
Sunny Goyalff572272014-07-23 13:58:07 -07003946 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003947
3948 item.appWidgetId = newWidgetId;
3949
3950 // If the widget has a configure activity, it is still needs to set it up, otherwise
3951 // the widget is ready to go.
3952 item.restoreStatus = (appWidgetInfo.configure == null)
3953 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3954 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3955
3956 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003957 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003958 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003959 // The widget was marked as UI not ready, but there is no configure activity to
3960 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003961 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3962 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003963 }
Sunny Goyalff572272014-07-23 13:58:07 -07003964 }
3965
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003966 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003967 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003968 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3969 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003970 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003971
Sunny Goyal56c73602015-09-25 12:55:01 -07003972 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003973 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003974 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003975 deleteWidgetInfo(item);
3976 return;
3977 }
3978
Sunny Goyal233ee962015-08-03 13:05:01 -07003979 item.minSpanX = appWidgetInfo.minSpanX;
3980 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003981 addAppWidgetToWorkspace(
3982 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3983 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003984 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003985 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003986 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003987 view.updateAppWidget(null);
3988 view.setOnClickListener(this);
3989 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003990 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003991 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003992
Daniel Sandler843e8602010-06-07 14:59:01 -04003993 if (DEBUG_WIDGETS) {
3994 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3995 + (SystemClock.uptimeMillis()-start) + "ms");
3996 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003997 }
3998
Sunny Goyalff572272014-07-23 13:58:07 -07003999 /**
4000 * Restores a pending widget.
4001 *
4002 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07004003 */
4004 private void completeRestoreAppWidget(final int appWidgetId) {
4005 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4006 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4007 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4008 return;
4009 }
4010
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004011 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004012 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4013
4014 mWorkspace.reinflateWidgetsIfNecessary();
4015 LauncherModel.updateItemInDatabase(this, info);
4016 }
4017
Adam Cohen1462de32012-07-24 22:34:36 -07004018 public void onPageBoundSynchronously(int page) {
4019 mSynchronouslyBoundPages.add(page);
4020 }
4021
Sunny Goyal527c7d32015-08-28 15:19:36 -07004022 @Override
4023 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
4024 if (mPendingExecutor != null) {
4025 mPendingExecutor.markCompleted();
4026 }
4027 mPendingExecutor = executor;
4028 executor.attachTo(this);
4029 }
4030
4031 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4032 if (mPendingExecutor == executor) {
4033 mPendingExecutor = null;
4034 }
4035 }
4036
Joe Onorato9c1289c2009-08-17 11:03:03 -04004037 /**
4038 * Callback saying that there aren't any more items to bind.
4039 *
4040 * Implementation of the method from LauncherModel.Callbacks.
4041 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004042 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004043 Runnable r = new Runnable() {
4044 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004045 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004046 }
4047 };
4048 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004049 return;
4050 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004051 if (mSavedState != null) {
4052 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004053 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004054 }
Sunny Goyal08442b82015-10-21 17:15:08 -07004055
Joe Onorato9c1289c2009-08-17 11:03:03 -04004056 mSavedState = null;
4057 }
4058
Adam Cohen1462de32012-07-24 22:34:36 -07004059 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004060
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004061 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07004062
4063 // If we received the result of any pending adds while the loader was running (e.g. the
4064 // widget configuration forced an orientation change), process them now.
4065 if (sPendingAddItem != null) {
4066 final long screenId = completeAdd(sPendingAddItem);
4067
4068 // TODO: this moves the user to the page where the pending item was added. Ideally,
4069 // the screen would be guaranteed to exist after bind, and the page would be set through
4070 // the workspace restore process.
4071 mWorkspace.post(new Runnable() {
4072 @Override
4073 public void run() {
4074 mWorkspace.snapToScreenId(screenId);
4075 }
4076 });
4077 sPendingAddItem = null;
4078 }
4079
Sunny Goyal756adbc2015-04-16 15:20:51 -07004080 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004081
4082 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004083 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004084 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004085 }
4086
Winson Chunga0b7e862013-09-05 16:03:15 -07004087 public boolean isAllAppsButtonRank(int rank) {
4088 if (mHotseat != null) {
4089 return mHotseat.isAllAppsButtonRank(rank);
4090 }
4091 return false;
4092 }
4093
Winson Chunga2413752012-04-03 14:22:34 -07004094 private boolean canRunNewAppsAnimation() {
4095 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004096 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4097 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004098 }
4099
Winson Chungc9168342013-06-26 14:54:55 -07004100 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004101 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004102 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4103 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004104 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004105 return bounceAnim;
4106 }
4107
Adam Cohen27772732013-11-11 14:00:56 +00004108 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004109 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004110 }
4111
Tony Wickham3a3517f2015-10-06 11:27:04 -07004112 /** Returns the search bar bounds in pixels. */
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004113 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004114 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004115 }
4116
Tony Wickham55616cd2015-09-23 14:55:17 -07004117 public int getSearchBarHeight() {
4118 if (mLauncherCallbacks != null) {
4119 return mLauncherCallbacks.getSearchBarHeight();
4120 }
4121 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4122 }
4123
Winson Chung88fa7412015-08-03 14:25:28 -07004124 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004125 if (mSearchDropTargetBar == null) {
4126 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004127 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004128 if (mQsb != null) {
4129 mSearchDropTargetBar.removeView(mQsb);
4130 mQsb = null;
4131 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004132 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004133 }
4134
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004135 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004136 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4137 * multiple calls to bind the same list.)
4138 */
4139 @Thunk ArrayList<AppInfo> mTmpAppsList;
4140 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4141 public void run() {
4142 bindAllApplications(mTmpAppsList);
4143 mTmpAppsList = null;
4144 }
4145 };
4146
4147 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004148 * Add the icons for all apps.
4149 *
4150 * Implementation of the method from LauncherModel.Callbacks.
4151 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004152 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004153 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4154 mTmpAppsList = apps;
4155 return;
4156 }
4157
Winson Chungb745afb2015-03-02 11:51:23 -08004158 if (mAppsView != null) {
4159 mAppsView.setApps(apps);
4160 }
Adam Cohen9211d422014-10-07 18:14:53 -07004161 if (mLauncherCallbacks != null) {
4162 mLauncherCallbacks.bindAllApplications(apps);
4163 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004164 }
4165
4166 /**
4167 * A package was updated.
4168 *
4169 * Implementation of the method from LauncherModel.Callbacks.
4170 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004171 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004172 Runnable r = new Runnable() {
4173 public void run() {
4174 bindAppsUpdated(apps);
4175 }
4176 };
4177 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004178 return;
4179 }
4180
Winson Chungb745afb2015-03-02 11:51:23 -08004181 if (mAppsView != null) {
4182 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004183 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004184 }
4185
Sunny Goyal4390ace2014-10-13 11:33:11 -07004186 @Override
4187 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4188 Runnable r = new Runnable() {
4189 public void run() {
4190 bindWidgetsRestored(widgets);
4191 }
4192 };
4193 if (waitUntilResume(r)) {
4194 return;
4195 }
4196 mWorkspace.widgetsRestored(widgets);
4197 }
4198
Joe Onorato9c1289c2009-08-17 11:03:03 -04004199 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004200 * Some shortcuts were updated in the background.
4201 *
4202 * Implementation of the method from LauncherModel.Callbacks.
4203 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004204 @Override
4205 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4206 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004207 Runnable r = new Runnable() {
4208 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004209 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004210 }
4211 };
4212 if (waitUntilResume(r)) {
4213 return;
4214 }
4215
Sunny Goyal4390ace2014-10-13 11:33:11 -07004216 if (!updated.isEmpty()) {
4217 mWorkspace.updateShortcuts(updated);
4218 }
4219
4220 if (!removed.isEmpty()) {
4221 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4222 for (ShortcutInfo si : removed) {
4223 removedComponents.add(si.getTargetComponent());
4224 }
4225 mWorkspace.removeItemsByComponentName(removedComponents, user);
4226 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004227 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004228 }
4229 }
4230
4231 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004232 * Update the state of a package, typically related to install state.
4233 *
4234 * Implementation of the method from LauncherModel.Callbacks.
4235 */
Sunny Goyale755d462014-07-22 13:48:29 -07004236 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004237 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4238 Runnable r = new Runnable() {
4239 public void run() {
4240 bindRestoreItemsChange(updates);
4241 }
4242 };
4243 if (waitUntilResume(r)) {
4244 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004245 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004246
Sunny Goyal756adbc2015-04-16 15:20:51 -07004247 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004248 }
4249
4250 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004251 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004252 * in addition to specific applications to remove, the reason being that
4253 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004254 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004255 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004256 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004257 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004258 public void bindWorkspaceComponentsRemoved(
4259 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4260 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004261 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004262 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004263 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004264 }
Winson Chungd64d1762013-08-20 14:37:16 -07004265 };
4266 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004267 return;
4268 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004269 if (!packageNames.isEmpty()) {
4270 mWorkspace.removeItemsByPackageName(packageNames, user);
4271 }
4272 if (!components.isEmpty()) {
4273 mWorkspace.removeItemsByComponentName(components, user);
4274 }
4275 // Notify the drag controller
4276 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004277
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004278 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004279
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004280 @Override
4281 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4282 Runnable r = new Runnable() {
4283 public void run() {
4284 bindAppInfosRemoved(appInfos);
4285 }
4286 };
4287 if (waitUntilResume(r)) {
4288 return;
Joe Onorato36115782010-06-17 13:28:48 -04004289 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004290
Winson Chungdf95eb12013-10-16 14:57:07 -07004291 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004292 if (mAppsView != null) {
4293 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004294 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004295 }
Joe Onoratobe386092009-11-17 17:32:16 -08004296
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004297 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004298 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004299 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004300 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004301 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004302
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004303 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004304 public void bindWidgetsModel(WidgetsModel model) {
4305 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004306 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004307 return;
4308 }
4309
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004310 if (mWidgetsView != null && model != null) {
4311 mWidgetsView.addWidgets(model);
4312 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004313 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004314 }
4315
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004316 @Override
4317 public void notifyWidgetProvidersChanged() {
4318 if (mWorkspace.getState().shouldUpdateWidget) {
4319 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4320 }
4321 }
4322
Winson Chung400438b2011-01-16 17:53:48 -08004323 private int mapConfigurationOriActivityInfoOri(int configOri) {
4324 final Display d = getWindowManager().getDefaultDisplay();
4325 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4326 switch (d.getRotation()) {
4327 case Surface.ROTATION_0:
4328 case Surface.ROTATION_180:
4329 // We are currently in the same basic orientation as the natural orientation
4330 naturalOri = configOri;
4331 break;
4332 case Surface.ROTATION_90:
4333 case Surface.ROTATION_270:
4334 // We are currently in the other basic orientation to the natural orientation
4335 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4336 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4337 break;
4338 }
4339
4340 int[] oriMap = {
4341 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4342 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4343 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4344 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4345 };
4346 // Since the map starts at portrait, we need to offset if this device's natural orientation
4347 // is landscape.
4348 int indexOffset = 0;
4349 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4350 indexOffset = 1;
4351 }
4352 return oriMap[(d.getRotation() + indexOffset) % 4];
4353 }
Adam Cohen446e9402011-09-15 18:21:21 -07004354
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004355 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004356 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004357 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004358 if (Utilities.ATLEAST_JB_MR2) {
4359 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4360 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004361 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4362 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004363 }
Winson Chung4b919f82012-05-01 10:44:08 -07004364 }
4365 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004366
Winson Chung4b919f82012-05-01 10:44:08 -07004367 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004368 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004369 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004370 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004371 } else {
4372 mHandler.postDelayed(new Runnable() {
4373 public void run() {
4374 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4375 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004376 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004377 }
Winson Chung4b919f82012-05-01 10:44:08 -07004378 }
Winson Chung400438b2011-01-16 17:53:48 -08004379 }
4380
Adam Cohenea90f832014-05-21 15:03:34 -07004381 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004382 * To be overridden by subclasses to indicate that there is an activity to launch
4383 * before showing the standard launcher experience.
4384 */
4385 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004386 if (mLauncherCallbacks != null) {
4387 return mLauncherCallbacks.hasFirstRunActivity();
4388 }
Adam Cohen432609a2014-03-13 17:03:22 -07004389 return false;
4390 }
4391
4392 /**
4393 * To be overridden by subclasses to launch any first run activity
4394 */
4395 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004396 if (mLauncherCallbacks != null) {
4397 return mLauncherCallbacks.getFirstRunActivity();
4398 }
Adam Cohen432609a2014-03-13 17:03:22 -07004399 return null;
4400 }
4401
Winson Chunga6945242014-01-08 14:04:34 -08004402 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004403 return !ActivityManager.isRunningInTestHarness() &&
4404 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004405 }
4406
Adam Cohen4a9423d2014-03-28 14:22:31 -07004407 protected boolean hasRunFirstRunActivity() {
4408 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4409 }
4410
Adam Cohen432609a2014-03-13 17:03:22 -07004411 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004412 if (shouldRunFirstRunActivity() &&
4413 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004414 Intent firstRunIntent = getFirstRunActivity();
4415 if (firstRunIntent != null) {
4416 startActivity(firstRunIntent);
4417 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004418 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004419 }
4420 }
Adam Cohen432609a2014-03-13 17:03:22 -07004421 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004422 }
4423
4424 private void markFirstRunActivityShown() {
4425 SharedPreferences.Editor editor = mSharedPrefs.edit();
4426 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4427 editor.apply();
4428 }
4429
Adam Cohen432609a2014-03-13 17:03:22 -07004430 /**
4431 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4432 * screen that must be displayed and dismissed.
4433 */
4434 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004435 if (mLauncherCallbacks != null) {
4436 return mLauncherCallbacks.hasDismissableIntroScreen();
4437 }
Adam Cohen432609a2014-03-13 17:03:22 -07004438 return false;
4439 }
4440
4441 /**
4442 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4443 */
4444 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004445 if (mLauncherCallbacks != null) {
4446 return mLauncherCallbacks.getIntroScreen();
4447 }
Adam Cohen432609a2014-03-13 17:03:22 -07004448 return null;
4449 }
4450
4451 /**
4452 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4453 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4454 */
4455 private boolean shouldShowIntroScreen() {
4456 return hasDismissableIntroScreen() &&
4457 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4458 }
4459
4460 protected void showIntroScreen() {
4461 View introScreen = getIntroScreen();
4462 changeWallpaperVisiblity(false);
4463 if (introScreen != null) {
4464 mDragLayer.showOverlayView(introScreen);
4465 }
4466 }
4467
4468 public void dismissIntroScreen() {
4469 markIntroScreenDismissed();
4470 if (showFirstRunActivity()) {
4471 // We delay hiding the intro view until the first run activity is showing. This
4472 // avoids a blip.
4473 mWorkspace.postDelayed(new Runnable() {
4474 @Override
4475 public void run() {
4476 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004477 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004478 }
4479 }, ACTIVITY_START_DELAY);
4480 } else {
4481 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004482 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004483 }
4484 changeWallpaperVisiblity(true);
4485 }
4486
4487 private void markIntroScreenDismissed() {
4488 SharedPreferences.Editor editor = mSharedPrefs.edit();
4489 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4490 editor.apply();
4491 }
4492
Adam Cohen091440a2015-03-18 14:16:05 -07004493 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004494 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4495 // on the device, then we always show the first run cling experience (or if there is no
4496 // launcher2). Otherwise, we prompt the user upon started for migration
4497 LauncherClings launcherClings = new LauncherClings(this);
4498 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004499 mClings = launcherClings;
Sunny Goyalded0fdb2016-05-23 15:55:41 -07004500 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004501 }
4502 }
4503
Winson Chung5ffd51d2015-06-25 11:36:50 -07004504 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004505 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004506 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004507 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004508 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004509 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004510 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4511 if (userHandle != null) {
4512 user = UserHandleCompat.fromUser(userHandle);
4513 }
4514 }
4515 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004516 }
4517
4518 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004519 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004520 if (user == null) {
4521 user = UserHandleCompat.myUserHandle();
4522 }
4523
4524 // Called from search suggestion, add the profile extra to the intent to ensure that we
4525 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004526 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004527 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004528 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004529 return null;
4530 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004531 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004532 }
4533
Winson Chung5ffd51d2015-06-25 11:36:50 -07004534 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004535 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4536 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004537 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004538 UserHandleCompat.myUserHandle());
4539 }
4540
Winson Chung5ffd51d2015-06-25 11:36:50 -07004541 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004542 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4543 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004544 mWorkspace.onExternalDragStartedWithItem(dragView);
4545 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004546 }
4547
Winson Chung5ffd51d2015-06-25 11:36:50 -07004548 protected void moveWorkspaceToDefaultScreen() {
4549 mWorkspace.moveToDefaultScreen(false);
4550 }
4551
Winson Chung80baf5a2010-08-09 16:03:15 -07004552 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004553 * Returns a FastBitmapDrawable with the icon, accurately sized.
4554 */
4555 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4556 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4557 d.setFilterBitmap(true);
4558 resizeIconDrawable(d);
4559 return d;
4560 }
4561
4562 /**
4563 * Resizes an icon drawable to the correct icon size.
4564 */
Winson5fbe0742015-09-30 15:33:00 -07004565 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004566 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004567 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004568 }
4569
4570 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004571 * Prints out out state for debugging.
4572 */
4573 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004574 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004575 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004576 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4577 Log.d(TAG, "mRestoring=" + mRestoring);
4578 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004579 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004580 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004581
Daniel Sandler325dc232013-06-05 22:57:57 -04004582 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004583 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004584
4585 @Override
4586 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4587 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004588 // Dump workspace
4589 writer.println(prefix + "Workspace Items");
4590 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4591 writer.println(prefix + " Homescreen " + i);
4592
4593 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4594 for (int j = 0; j < layout.getChildCount(); j++) {
4595 Object tag = layout.getChildAt(j).getTag();
4596 if (tag != null) {
4597 writer.println(prefix + " " + tag.toString());
4598 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004599 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004600 }
Sunny Goyala1365452015-10-01 15:46:24 -07004601
4602 writer.println(prefix + " Hotseat");
4603 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4604 for (int j = 0; j < layout.getChildCount(); j++) {
4605 Object tag = layout.getChildAt(j).getTag();
4606 if (tag != null) {
4607 writer.println(prefix + " " + tag.toString());
4608 }
4609 }
4610
Sunny Goyal713edfc2016-05-06 09:58:34 -07004611 try {
4612 FileLog.flushAll(writer);
4613 } catch (Exception e) {
4614 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004615 }
4616
Adam Cohen9211d422014-10-07 18:14:53 -07004617 if (mLauncherCallbacks != null) {
4618 mLauncherCallbacks.dump(prefix, fd, writer, args);
4619 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004620 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004621
Adam Cohen59400422014-03-05 18:07:04 -08004622 public static CustomAppWidget getCustomAppWidget(String name) {
4623 return sCustomAppWidgets.get(name);
4624 }
4625
4626 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4627 return sCustomAppWidgets;
4628 }
4629
Sunny Goyal29538332016-02-18 09:10:19 -08004630 public static List<View> getFolderContents(View icon) {
4631 if (icon instanceof FolderIcon) {
4632 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4633 } else {
4634 return Collections.EMPTY_LIST;
4635 }
4636 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004637
4638 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4639
4640 @Override
4641 public void onSharedPreferenceChanged(
4642 SharedPreferences sharedPreferences, String key) {
4643 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4644 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4645 if (!waitUntilResume(this, true)) {
4646 run();
4647 }
4648 }
4649 }
4650
4651 @Override
4652 public void run() {
4653 setOrientation();
4654 }
4655 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004656}