blob: 691136019423804a5d3b5c4626652b5ec66f5c45 [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;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070041import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040042import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070044import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070045import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070046import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070047import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070048import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.pm.PackageManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070051import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070052import android.graphics.Bitmap;
53import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070054import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070055import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070057import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020060import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080062import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070063import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070064import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070066import android.os.Trace;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070067import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.HapticFeedbackConstants;
75import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070077import android.view.MotionEvent;
78import android.view.Surface;
79import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070080import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080081import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.view.ViewGroup;
83import android.view.ViewTreeObserver;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080085import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070086import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070087import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080091import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070093
Sunny Goyal651077b2014-06-30 14:15:31 -070094import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070095import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyalae502842016-06-17 08:43:56 -070096import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070097import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070098import com.android.launcher3.allapps.AllAppsTransitionController;
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 Goyal3333b0c2016-05-09 20:43:21 -0700113import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700114import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700115import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700116import com.android.launcher3.model.WidgetsModel;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700117import com.android.launcher3.pageindicators.PageIndicator;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700118import com.android.launcher3.shortcuts.DeepShortcutManager;
Tony Wickham49c8d292016-07-01 11:44:34 -0700119import com.android.launcher3.shortcuts.DeepShortcutsContainer;
Hyunyoung Song5aa27142016-07-21 11:48:37 -0700120import com.android.launcher3.userevent.nano.LauncherLogProto;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700121import com.android.launcher3.util.ComponentKey;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700122import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700123import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal322d5562015-06-25 19:35:49 -0700124import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700125import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700126import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700127import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700128import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700129import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700130
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700131import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700132import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700133import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700134import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800135import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700136import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700137import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700138import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700139
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140/**
141 * Default launcher application.
142 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100143public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700144 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
145 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700146 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700147 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700148 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
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;
Romain Guy6fefcf12009-06-11 13:07:43 -0700153
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700155 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700156 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700157 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158
Michael Jurka8b805b12012-04-18 14:23:14 -0700159 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700160 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
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
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700185 public static final String ACTION_APPWIDGET_HOST_RESET =
186 "com.android.launcher3.intent.ACTION_APPWIDGET_HOST_RESET";
187
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 // Type: int
189 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700190 // Type: int
191 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700192 // Type: Content Values / parcelable
193 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700194 // Type: parcelable
195 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000196 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800197 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198
Adam Cohen432609a2014-03-13 17:03:22 -0700199 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800200 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700201 static final String APPS_VIEW_SHOWN = "launcher.apps_view_shown";
Adam Cohenb54a5982014-01-08 15:21:04 -0800202
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700203 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700204 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
205 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700206
Adam Cohen091440a2015-03-18 14:16:05 -0700207 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700208 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700209
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700210 private boolean mIsSafeModeEnabled;
211
Joe Onorato9c1289c2009-08-17 11:03:03 -0400212 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800213 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700214 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700215 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
Winson Chunga2413752012-04-03 14:22:34 -0700217 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700218 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
219 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700220 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700221
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700222 private final BroadcastReceiver mUiBroadcastReceiver = new BroadcastReceiver() {
223
224 @Override
225 public void onReceive(Context context, Intent intent) {
226 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
227 closeSystemDialogs();
228 } else if (ACTION_APPWIDGET_HOST_RESET.equals(intent.getAction())) {
229 if (mAppWidgetHost != null) {
230 mAppWidgetHost.startListening();
231 }
232 }
233 }
234 };
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800235
Adam Cohen091440a2015-03-18 14:16:05 -0700236 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700237 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700238 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800239 private DragController mDragController;
Sunny Goyal6178f132016-07-11 17:30:03 -0700240 private View mQsbContainer;
Sunny Goyal322d5562015-06-25 19:35:49 -0700241
242 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700243
Sunny Goyalffe83f12014-08-14 17:39:34 -0700244 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700245 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700246
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700247 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800248 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800249 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700250
Michael Jurka0280c3b2010-09-17 15:00:07 -0700251 private int[] mTmpAddItemCellCoordinates = new int[2];
252
Sunny Goyal316490e2015-06-02 09:38:28 -0700253 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800254 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700255
Michael Jurka838a4ca2011-02-07 13:33:06 -0800256 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700257 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700258
Sunny Goyal47328fd2016-05-25 18:56:41 -0700259 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700260
261 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700262 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700263 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700264
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700265 // Main container view and the model for the widget tray screen.
266 @Thunk WidgetsContainerView mWidgetsView;
267 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700268
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700270 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
271 // scroll issues (because the workspace may not have been measured yet) and extra work.
272 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
273 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274
275 private SpannableStringBuilder mDefaultKeySsb = null;
276
Adam Cohen091440a2015-03-18 14:16:05 -0700277 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400278
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800279 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280 private boolean mRestoring;
281 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700282 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283
Michael Jurka1e2f4652013-07-08 18:03:46 -0700284 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700285 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700286 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700287
Joe Onorato9c1289c2009-08-17 11:03:03 -0400288 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800289 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700290 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700291 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700292 private boolean mIsResumeFromActionScreenOff;
Adam Cohen091440a2015-03-18 14:16:05 -0700293 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800294 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700295 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800296 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400297
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700298 /** Maps launcher activity components to their list of shortcut ids. */
299 private MultiHashMap<ComponentKey, String> mDeepShortcutMap = new MultiHashMap<>();
300
Adam Cohen61f560d2013-09-30 15:58:20 -0700301 private View.OnTouchListener mHapticFeedbackTouchListener;
302
Adam Cohended9f8d2010-11-03 13:25:16 -0700303 // Related to the auto-advancing of widgets
304 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700305 private static final int ADVANCE_INTERVAL = 20000;
306 private static final int ADVANCE_STAGGER = 250;
307
308 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700309 private long mAutoAdvanceSentTime;
310 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700311 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700312
Winson Chung400438b2011-01-16 17:53:48 -0800313 // Determines how long to wait after a rotation before restoring the screen orientation to
314 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700315 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800316
Adam Cohen091440a2015-03-18 14:16:05 -0700317 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700318
Adam Cohen1462de32012-07-24 22:34:36 -0700319 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700320 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700321
Winson Chung46353de2012-02-16 14:05:10 -0800322 // We only want to get the SharedPreferences once since it does an FS stat each time we get
323 // it from the context.
324 private SharedPreferences mSharedPrefs;
325
Winson Chungf0c6ae02012-03-21 16:10:31 -0700326 // Holds the page that we need to animate to, and the icon views that we need to animate up
327 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700328 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700329 private Bitmap mFolderIconBitmap;
330 private Canvas mFolderIconCanvas;
331 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700332
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700333 private DeviceProfile mDeviceProfile;
334
Adam Cohen4b66bf32015-09-18 12:15:19 -0700335 private boolean mMoveToDefaultScreenFromNewIntent;
336
Winson Chung13eb5272015-05-11 16:30:13 -0700337 // This is set to the view that launched the activity that navigated the user away from
338 // launcher. Since there is no callback for when the activity has finished launching, enable
339 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800340 private BubbleTextView mWaitingForResume;
341
Adam Cohen59400422014-03-05 18:07:04 -0800342 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
343 new HashMap<String, CustomAppWidget>();
344
Adam Cohen59400422014-03-05 18:07:04 -0800345 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700346 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
347 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800348 }
349 }
350
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700351 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
352 // state transition. If another state transition happened during this delay,
353 // simply unregister this runnable.
354 private Runnable mExitSpringLoadedModeRunnable;
355
Adam Cohen091440a2015-03-18 14:16:05 -0700356 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700357 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700358 if (mWorkspace != null) {
359 mWorkspace.buildPageHardwareLayers();
360 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700361 }
362 };
363
Adam Cohendb364c32014-05-20 14:23:40 -0700364 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800365
Adam Cohen091440a2015-03-18 14:16:05 -0700366 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800367 int requestCode;
368 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700369 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700370 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800371 int cellX;
372 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700373 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800374 }
375
Hyunyoung Songaa953652016-04-19 18:30:24 -0700376 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800377
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700378 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700379 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400380
381 @Thunk void setOrientation() {
382 if (mRotationEnabled) {
383 unlockScreenOrientation(true);
384 } else {
385 setRequestedOrientation(
386 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700387 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400388 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700389
Sunny Goyal745bad92016-05-02 10:54:12 -0700390 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700391
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 @Override
393 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700394 if (DEBUG_STRICT_MODE) {
395 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
396 .detectDiskReads()
397 .detectDiskWrites()
398 .detectNetwork() // or .detectAll() for all detectable problems
399 .penaltyLog()
400 .build());
401 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
402 .detectLeakedSqlLiteObjects()
403 .detectLeakedClosableObjects()
404 .penaltyLog()
405 .penaltyDeath()
406 .build());
407 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700408 if (LauncherAppState.PROFILE_STARTUP) {
409 Trace.beginSection("Launcher-onCreate");
410 }
Winson Chunga2413752012-04-03 14:22:34 -0700411
Adam Cohen9211d422014-10-07 18:14:53 -0700412 if (mLauncherCallbacks != null) {
413 mLauncherCallbacks.preOnCreate();
414 }
415
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800416 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400417
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400418 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700419
Adam Cohen2e6da152015-05-06 11:42:25 -0700420 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700421 mDeviceProfile = getResources().getConfiguration().orientation
422 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700423 app.getInvariantDeviceProfile().landscapeProfile
424 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700425
Sunny Goyalf7258242015-10-19 16:59:07 -0700426 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700427 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800428 mModel = app.setLauncher(this);
429 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700430 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700431
Joe Onorato41a12d22009-10-31 18:30:00 -0400432 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700433 mAllAppsController = new AllAppsTransitionController(this);
434 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700435
Sunny Goyalffe83f12014-08-14 17:39:34 -0700436 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700437
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700438 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
439 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700440
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700441 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
442 // this also ensures that any synchronous binding below doesn't re-trigger another
443 // LauncherModel load.
444 mPaused = false;
445
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700447
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 setupViews();
Winson1f064272016-07-18 17:18:02 -0700449 mDeviceProfile.layout(this, false /* notifyListeners */);
Tony Wickham827cef22016-03-17 15:39:39 -0700450 mExtractedColors = new ExtractedColors();
451 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452
Tony Wickhame2217252016-03-22 16:34:23 -0700453 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
454 .addAccessibilityStateChangeListener(this);
455
Joe Onorato7c312c12009-08-13 21:36:53 -0700456 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700457
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458 mSavedState = savedInstanceState;
459 restoreState(mSavedState);
460
Sunny Goyale26d1002016-06-20 14:52:14 -0700461 if (LauncherAppState.PROFILE_STARTUP) {
462 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800463 }
464
465 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700466 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700467 // If the user leaves launcher, then we should just load items asynchronously when
468 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700469 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700470 } else {
471 // We only load the page synchronously if the user rotates (or triggers a
472 // configuration change) while launcher is in the foreground
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700473 if (!mModel.startLoader(mWorkspace.getRestorePage())) {
474 // If we are not binding synchronously, show a fade in animation when
475 // the first page bind completes.
476 mDragLayer.setAlpha(0);
477 }
Winson Chung4a2afa32012-07-19 14:53:05 -0700478 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 }
480
481 // For handling default keys
482 mDefaultKeySsb = new SpannableStringBuilder();
483 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800484
485 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700486 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
487 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800488
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800489 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700490 // In case we are on a device with locked rotation, we should look at preferences to check
491 // if the user has specifically allowed rotation.
492 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700493 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700494 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
495 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700496 }
497
498 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
499 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400500 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700501
Adam Cohen9211d422014-10-07 18:14:53 -0700502 if (mLauncherCallbacks != null) {
503 mLauncherCallbacks.onCreate(savedInstanceState);
504 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700505
506 if (shouldShowIntroScreen()) {
507 showIntroScreen();
508 } else {
509 showFirstRunActivity();
Adam Cohenc3e12c72014-10-31 16:15:36 -0700510 }
Adam Cohen9211d422014-10-07 18:14:53 -0700511 }
512
Sunny Goyal7779d622015-06-11 16:18:39 -0700513 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700514 public void onExtractedColorsChanged() {
515 loadExtractedColorsAndColorItems();
516 }
517
518 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700519 // TODO: do this in pre-N as well, once the extraction part is complete.
520 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700521 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700522 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700523 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham827cef22016-03-17 15:39:39 -0700524 }
525 }
526
Adam Cohen9211d422014-10-07 18:14:53 -0700527 private LauncherCallbacks mLauncherCallbacks;
528
529 public void onPostCreate(Bundle savedInstanceState) {
530 super.onPostCreate(savedInstanceState);
531 if (mLauncherCallbacks != null) {
532 mLauncherCallbacks.onPostCreate(savedInstanceState);
533 }
534 }
535
Winson1f064272016-07-18 17:18:02 -0700536 public void onInsetsChanged(Rect insets) {
537 mDeviceProfile.updateInsets(insets);
538 mDeviceProfile.layout(this, true /* notifyListeners */);
539 }
540
Sunny Goyal32554d12015-12-03 15:31:25 -0800541 /**
542 * Call this after onCreate to set or clear overlay.
543 */
544 public void setLauncherOverlay(LauncherOverlay overlay) {
545 if (overlay != null) {
546 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
547 }
548 mWorkspace.setLauncherOverlay(overlay);
549 }
550
Adam Cohen9211d422014-10-07 18:14:53 -0700551 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
552 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700553 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700554 private boolean mWorkspaceImportanceStored = false;
555 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700556 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800557 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700558 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
559
560 @Override
561 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700562 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700563 return;
564 }
565 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700566 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700567 if (mWorkspace != null) {
568 mWorkspaceImportanceForAccessibility =
569 mWorkspace.getImportantForAccessibility();
570 mWorkspace.setImportantForAccessibility(
571 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
572 mWorkspaceImportanceStored = true;
573 }
574 if (mHotseat != null) {
575 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
576 mHotseat.setImportantForAccessibility(
577 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
578 mHotseatImportanceStored = true;
579 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700580 }
581
582 @Override
583 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700584 if (mWorkspaceImportanceStored && mWorkspace != null) {
585 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
586 }
587 if (mHotseatImportanceStored && mHotseat != null) {
588 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
589 }
590 mWorkspaceImportanceStored = false;
591 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700592 }
593 });
Adam Cohen9211d422014-10-07 18:14:53 -0700594 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700595 }
596
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700597 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700598 public void onLauncherProviderChange() {
599 if (mLauncherCallbacks != null) {
600 mLauncherCallbacks.onLauncherProviderChange();
601 }
602 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700603
Adam Cohen9211d422014-10-07 18:14:53 -0700604 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700605 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700606 if (mLauncherCallbacks != null) {
607 return mLauncherCallbacks.hasCustomContentToLeft();
608 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700609 return false;
610 }
611
Dave Hawkeya8881582013-09-17 15:55:33 -0600612 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500613 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600614 * {@link #addToCustomContentPage}. This will only be invoked if
615 * {@link #hasCustomContentToLeft()} is {@code true}.
616 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500617 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700618 if (mLauncherCallbacks != null) {
619 mLauncherCallbacks.populateCustomContentContainer();
620 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600621 }
622
623 /**
624 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
625 * ensure the custom content page is added or removed if necessary.
626 */
627 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600628 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600629 // Not bound yet, wait for bindScreens to be called.
630 return;
631 }
632
633 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
634 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500635 mWorkspace.createCustomContentContainer();
636 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600637 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
638 mWorkspace.removeCustomContentPage();
639 }
640 }
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
Sunny Goyal64976d52016-06-20 14:56:28 -0700650 // Logger object is a singleton and does not have to be coupled with the foreground
651 // activity. Since most user event logging is done on the UI, the object is retrieved
652 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700653 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700654 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700655 }
656 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800657 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100658
Hyunyoung Song3f471442015-04-08 19:01:34 -0700659 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700660 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
661 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700662 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700663 }
664
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000665 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700666 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
667 // This cast is safe as long as the id < 0x00FFFFFF
668 // Since we jail all the dynamically generated views, there should be no clashes
669 // with any other views.
670 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000671 }
672
673 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700674 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
675 * a configuration step, this allows the proper animations to run after other transitions.
676 */
Adam Cohendb364c32014-05-20 14:23:40 -0700677 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700678 long screenId = args.screenId;
679 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
680 // When the screen id represents an actual screen (as opposed to a rank) we make sure
681 // that the drop page actually exists.
682 screenId = ensurePendingDropLayoutExists(args.screenId);
683 }
Adam Cohendb364c32014-05-20 14:23:40 -0700684
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800685 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800686 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700687 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700688 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700689 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800690 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700691 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700692 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700693 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyald478c832016-04-01 12:04:16 -0700694 completeRestoreAppWidget(args.appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700695 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700696 case REQUEST_BIND_PENDING_APPWIDGET: {
697 int widgetId = args.appWidgetId;
698 LauncherAppWidgetInfo info =
699 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
700 if (info != null) {
701 // Since the view was just bound, also launch the configure activity if needed
702 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
703 .getLauncherAppWidgetInfo(widgetId);
704 if (provider != null && provider.configure != null) {
705 startRestoredWidgetReconfigActivity(provider, info);
706 }
707 }
708 break;
709 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800710 }
Michael Jurka27614d22012-04-02 06:40:22 -0700711 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
712 // if you turned the screen off and then back while in All Apps, Launcher would not
713 // return to the workspace. Clearing mAddInfo.container here fixes this issue
714 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700715 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800716 }
717
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800718 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700719 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700720 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700721 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700722 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800723 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700724
Adam Cohenad4e15c2013-10-17 16:21:35 -0700725 Runnable exitSpringLoaded = new Runnable() {
726 @Override
727 public void run() {
728 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
729 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
730 }
731 };
732
Michael Jurka8b805b12012-04-18 14:23:14 -0700733 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700734 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700735 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700736 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
737 if (resultCode == RESULT_CANCELED) {
738 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700739 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700740 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700741 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800742 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700743 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700744 }
745 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200746 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
747 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700748 // User could have free-scrolled between pages before picking a wallpaper; make sure
749 // we move to the closest one now.
750 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700751 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200752 }
753 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700754 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200755
Adam Cohened66b2b2012-01-23 17:28:51 -0800756 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700757 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700758
Adam Cohendb364c32014-05-20 14:23:40 -0700759 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800760 // We have special handling for widgets
761 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800762 final int appWidgetId;
763 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
764 : -1;
765 if (widgetId < 0) {
766 appWidgetId = pendingAddWidgetId;
767 } else {
768 appWidgetId = widgetId;
769 }
770
Adam Cohenad4e15c2013-10-17 16:21:35 -0700771 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800772 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700773 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
774 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700775 result = RESULT_CANCELED;
776 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700777 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700778 @Override
779 public void run() {
780 exitSpringLoadedDragModeDelayed(false, 0, null);
781 }
782 };
Adam Cohendb364c32014-05-20 14:23:40 -0700783 if (workspaceLocked) {
784 // No need to remove the empty screen if we're mid-binding, as the
785 // the bind will not add the empty screen.
786 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
787 } else {
788 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
789 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
790 }
Winson Chung5aaab772012-04-27 15:24:02 -0700791 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700792 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700793 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
794 // When the screen id represents an actual screen (as opposed to a rank)
795 // we make sure that the drop page actually exists.
796 mPendingAddInfo.screenId =
797 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
798 }
Adam Cohendb364c32014-05-20 14:23:40 -0700799 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
800
801 dropLayout.setDropPending(true);
802 final Runnable onComplete = new Runnable() {
803 @Override
804 public void run() {
805 completeTwoStageWidgetDrop(resultCode, appWidgetId);
806 dropLayout.setDropPending(false);
807 }
808 };
809 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
810 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
811 } else {
812 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
813 mPendingAddInfo);
814 sPendingAddItem = args;
815 }
Winson Chung5aaab772012-04-27 15:24:02 -0700816 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800817 return;
818 }
819
Sunny Goyald478c832016-04-01 12:04:16 -0700820 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
821 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700822 if (resultCode == RESULT_OK) {
823 // Update the widget view.
824 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
825 pendingAddWidgetId, mPendingAddInfo);
826 if (workspaceLocked) {
827 sPendingAddItem = args;
828 } else {
829 completeAdd(args);
830 }
831 }
832 // Leave the widget in the pending state if the user canceled the configure.
833 return;
834 }
835
Sunny Goyalaad90582015-07-09 14:22:50 -0700836 if (requestCode == REQUEST_CREATE_SHORTCUT) {
837 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
838 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
839 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
840 mPendingAddInfo);
841 if (isWorkspaceLocked()) {
842 sPendingAddItem = args;
843 } else {
844 completeAdd(args);
845 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
846 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
847 }
848 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700849 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
850 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800851 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800852 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800853 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800854
855 }
856
857 @Override
858 protected void onActivityResult(
859 final int requestCode, final int resultCode, final Intent data) {
860 handleActivityResult(requestCode, resultCode, data);
861 if (mLauncherCallbacks != null) {
862 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
863 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800864 }
865
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600866 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700867 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600868 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700869 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
870 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
871 View v = null;
872 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
873 if (layout != null) {
874 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
875 }
876 Intent intent = sPendingAddItem.intent;
877 sPendingAddItem = null;
878 if (grantResults.length > 0
879 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700880 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700881 } else {
882 // TODO: Show a snack bar with link to settings
883 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
884 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
885 }
886 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600887 if (mLauncherCallbacks != null) {
888 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
889 grantResults);
890 }
891 }
892
Adam Cohendb364c32014-05-20 14:23:40 -0700893 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
894 appWidgetId, ItemInfo info) {
895 PendingAddArguments args = new PendingAddArguments();
896 args.requestCode = requestCode;
897 args.intent = data;
898 args.container = info.container;
899 args.screenId = info.screenId;
900 args.cellX = info.cellX;
901 args.cellY = info.cellY;
902 args.appWidgetId = appWidgetId;
903 return args;
904 }
905
906 /**
907 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
908 *
909 * @param screenId the screen id to check
910 * @return the new screen, or screenId if it exists
911 */
912 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800913 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700914 if (dropLayout == null) {
915 // it's possible that the add screen was removed because it was
916 // empty and a re-bind occurred
917 mWorkspace.addExtraEmptyScreen();
918 return mWorkspace.commitExtraEmptyScreen();
919 } else {
920 return screenId;
921 }
922 }
923
Adam Cohen091440a2015-03-18 14:16:05 -0700924 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800925 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800926 Runnable onCompleteRunnable = null;
927 int animationType = 0;
928
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700929 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800930 if (resultCode == RESULT_OK) {
931 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
932 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700933 mPendingAddWidgetInfo);
934 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800935 onCompleteRunnable = new Runnable() {
936 @Override
937 public void run() {
938 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700939 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700940 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
941 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800942 }
943 };
944 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800945 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800946 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800947 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700948 if (mDragLayer.getAnimatedView() != null) {
949 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
950 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
951 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700952 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700953 // The animated view may be null in the case of a rotation during widget configuration
954 onCompleteRunnable.run();
955 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 }
957
958 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700959 protected void onStop() {
960 super.onStop();
961 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700962
963 if (mLauncherCallbacks != null) {
964 mLauncherCallbacks.onStop();
965 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700966
967 if (Utilities.isNycMR1OrAbove()) {
968 mAppWidgetHost.stopListening();
969 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700970 }
971
972 @Override
973 protected void onStart() {
974 super.onStart();
975 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700976
977 if (mLauncherCallbacks != null) {
978 mLauncherCallbacks.onStart();
979 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700980
981 if (Utilities.isNycMR1OrAbove()) {
982 mAppWidgetHost.startListening();
983 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700984 }
985
986 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200988 long startTime = 0;
989 if (DEBUG_RESUME_TIME) {
990 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400991 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200992 }
Adam Cohen9211d422014-10-07 18:14:53 -0700993
994 if (mLauncherCallbacks != null) {
995 mLauncherCallbacks.preOnResume();
996 }
997
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700999 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001000
Winson Chung4a2afa32012-07-19 14:53:05 -07001001 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001002 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001003 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001004 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -07001005 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -07001006 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -07001007 // view after launching an app, as they may be depending on the UI to be static to
1008 // switch to another app, otherwise, if it was
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07001009 showAppsView(false /* animated */, !launchedFromApp /* updatePredictedApps */,
1010 false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08001011 } else if (mOnResumeState == State.WIDGETS) {
1012 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001013 }
1014 mOnResumeState = State.NONE;
1015
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001016 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001017 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1018 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001019
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001020 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001021 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001022 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001023 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001024 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001025 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001027 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001028 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1029 // execute them here
1030 long startTimeCallbacks = 0;
1031 if (DEBUG_RESUME_TIME) {
1032 startTimeCallbacks = System.currentTimeMillis();
1033 }
1034
Michael Jurka1e2f4652013-07-08 18:03:46 -07001035 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1036 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001037 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001038 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001039 if (DEBUG_RESUME_TIME) {
1040 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1041 (System.currentTimeMillis() - startTimeCallbacks));
1042 }
Michael Jurka447bf842013-05-15 14:52:15 +02001043 }
Michael Jurka54554252013-08-01 12:52:23 +02001044 if (mOnResumeCallbacks.size() > 0) {
1045 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1046 mOnResumeCallbacks.get(i).run();
1047 }
1048 mOnResumeCallbacks.clear();
1049 }
Winson Chunge4e50662012-01-23 14:45:13 -08001050
1051 // Reset the pressed state of icons that were locked in the press state while activities
1052 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001053 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001054 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001055 mWaitingForResume.setStayPressed(false);
1056 }
Winson Chung82963d52013-10-09 11:20:57 -07001057
Adam Cohen06dff352012-06-01 17:17:08 -07001058 // It is possible that widgets can receive updates while launcher is not in the foreground.
1059 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1060 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1061 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001062 if (!isWorkspaceLoading()) {
1063 getWorkspace().reinflateWidgetsIfNecessary();
1064 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001065
Michael Jurka447bf842013-05-15 14:52:15 +02001066 if (DEBUG_RESUME_TIME) {
1067 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1068 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001069
Adam Cohen4b66bf32015-09-18 12:15:19 -07001070 // We want to suppress callbacks about CustomContent being shown if we have just received
1071 // onNewIntent while the user was present within launcher. In that case, we post a call
1072 // to move the user to the main screen (which will occur after onResume). We don't want to
1073 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1074 // suppress here.
1075 if (mWorkspace.getCustomContentCallbacks() != null
1076 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001077 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001078 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001079 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1080 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001081 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001082 }
1083 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001084 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001085 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001086 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001087
Sunny Goyal756adbc2015-04-16 15:20:51 -07001088 if (!isWorkspaceLoading()) {
1089 // Process any items that were added while Launcher was away.
1090 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1091 }
Adam Cohen9211d422014-10-07 18:14:53 -07001092
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001093 if (shouldShowDiscoveryBounce()) {
1094 mAllAppsController.showDiscoveryBounce();
1095 }
1096 mIsResumeFromActionScreenOff = false;
Adam Cohen9211d422014-10-07 18:14:53 -07001097 if (mLauncherCallbacks != null) {
1098 mLauncherCallbacks.onResume();
1099 }
Adam Cohen06dff352012-06-01 17:17:08 -07001100 }
1101
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001103 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001104 // Ensure that items added to Launcher are queued until Launcher returns
1105 InstallShortcutReceiver.enableInstallQueue();
1106
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001107 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001108 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001109 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001110 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001111
1112 // We call onHide() aggressively. The custom content callbacks should be able to
1113 // debounce excess onHide calls.
1114 if (mWorkspace.getCustomContentCallbacks() != null) {
1115 mWorkspace.getCustomContentCallbacks().onHide();
1116 }
Romain Guycbb89e42009-06-08 15:52:54 -07001117
Adam Cohen9211d422014-10-07 18:14:53 -07001118 if (mLauncherCallbacks != null) {
1119 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001120 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001121 }
1122
1123 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001124 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1125 // by a onResume or by scrolling otherwise.
1126 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001127
1128 // Custom content is completely hidden
1129 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001130
1131 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1132 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001133
1134 // Indicates whether the user is allowed to scroll away from the custom content.
1135 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001136 }
1137
Adam Cohenc2d6e892014-10-16 09:49:24 -07001138 public interface LauncherOverlay {
1139
1140 /**
1141 * Touch interaction leading to overscroll has begun
1142 */
1143 public void onScrollInteractionBegin();
1144
1145 /**
1146 * Touch interaction related to overscroll has ended
1147 */
1148 public void onScrollInteractionEnd();
1149
1150 /**
1151 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1152 * screen (or in the case of RTL, the rightmost screen).
1153 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001154 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001155
1156 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001157 * Called when the launcher is ready to use the overlay
1158 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001159 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001160 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001161 }
1162
Jun Mukai8af0cd82015-05-12 12:32:12 -07001163 public interface LauncherSearchCallbacks {
1164 /**
1165 * Called when the search overlay is shown.
1166 */
1167 public void onSearchOverlayOpened();
1168
1169 /**
1170 * Called when the search overlay is dismissed.
1171 */
1172 public void onSearchOverlayClosed();
1173 }
1174
Adam Cohenc2d6e892014-10-16 09:49:24 -07001175 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001176 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001177 }
1178
1179 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1180
Sunny Goyalc86df472016-02-25 09:19:38 -08001181 public void onScrollChanged(float progress) {
1182 if (mWorkspace != null) {
1183 mWorkspace.onOverlayScrollChanged(progress);
1184 }
1185 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001186 }
1187
Jorim Jaggid017f882014-01-14 17:08:48 -08001188 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001189 if (mLauncherCallbacks != null) {
1190 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001191 } else {
1192 // On devices with a locked orientation, we will at least have the allow rotation
1193 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001194 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001195 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001196 }
1197
Adam Cohen9211d422014-10-07 18:14:53 -07001198 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001199 CustomContentCallbacks callbacks, String description) {
1200 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001201 }
1202
Adam Cohenedb40762013-07-18 16:45:45 -07001203 // The custom content needs to offset its content to account for the QSB
1204 public int getTopOffsetForCustomContent() {
1205 return mWorkspace.getPaddingTop();
1206 }
1207
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001208 @Override
1209 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001210 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001211 if (mModel.isCurrentCallbacks(this)) {
1212 mModel.stopLoader();
1213 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001214 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1215
Romain Guy13c2e7b2010-03-10 19:45:00 -08001216 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001217 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001218
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001219 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001220 @Override
1221 public void onWindowFocusChanged(boolean hasFocus) {
1222 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001223 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001224
1225 if (mLauncherCallbacks != null) {
1226 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1227 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001228 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001229
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 private boolean acceptFilter() {
1231 final InputMethodManager inputManager = (InputMethodManager)
1232 getSystemService(Context.INPUT_METHOD_SERVICE);
1233 return !inputManager.isFullscreenMode();
1234 }
1235
1236 @Override
1237 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001238 final int uniChar = event.getUnicodeChar();
1239 final boolean handled = super.onKeyDown(keyCode, event);
1240 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1241 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1243 keyCode, event);
1244 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001245 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001246 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001247 // showSearchDialog()
1248 // If there are multiple keystrokes before the search dialog takes focus,
1249 // onSearchRequested() will be called for every keystroke,
1250 // but it is idempotent, so it's fine.
1251 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 }
1253 }
1254
Joe Onorato8a9625e2010-01-28 15:55:35 -08001255 // Eat the long press event so the keyboard doesn't come up.
1256 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1257 return true;
1258 }
1259
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 return handled;
1261 }
1262
Winson46163472015-09-22 17:31:56 -07001263 @Override
1264 public boolean onKeyUp(int keyCode, KeyEvent event) {
1265 if (keyCode == KeyEvent.KEYCODE_MENU) {
1266 // Ignore the menu key if we are currently dragging or are on the custom content screen
1267 if (!isOnCustomContent() && !mDragController.isDragging()) {
1268 // Close any open folders
1269 closeFolder();
1270
Tony Wickham49c8d292016-07-01 11:44:34 -07001271 // Close any shortcuts containers
1272 closeShortcutsContainer();
1273
Winson46163472015-09-22 17:31:56 -07001274 // Stop resizing any widgets
1275 mWorkspace.exitWidgetResizeMode();
1276
1277 // Show the overview mode if we are on the workspace
1278 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1279 !mWorkspace.isSwitchingState()) {
1280 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001281 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001282 }
1283 }
1284 return true;
1285 }
1286 return super.onKeyUp(keyCode, event);
1287 }
1288
Karl Rosaen138a0412009-04-23 19:00:21 -07001289 private String getTypedText() {
1290 return mDefaultKeySsb.toString();
1291 }
1292
1293 private void clearTypedText() {
1294 mDefaultKeySsb.clear();
1295 mDefaultKeySsb.clearSpans();
1296 Selection.setSelection(mDefaultKeySsb, 0);
1297 }
1298
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001300 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1301 * State
1302 */
1303 private static State intToState(int stateOrdinal) {
1304 State state = State.WORKSPACE;
1305 final State[] stateValues = State.values();
1306 for (int i = 0; i < stateValues.length; i++) {
1307 if (stateValues[i].ordinal() == stateOrdinal) {
1308 state = stateValues[i];
1309 break;
1310 }
1311 }
1312 return state;
1313 }
1314
1315 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 * Restores the previous state, if it exists.
1317 *
1318 * @param savedState The previous state.
1319 */
1320 private void restoreState(Bundle savedState) {
1321 if (savedState == null) {
1322 return;
1323 }
1324
Michael Jurka883f55b2010-10-21 15:47:14 -07001325 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001326 if (state == State.APPS || state == State.WIDGETS) {
1327 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001328 }
1329
Adam Cohen21cd0022013-10-09 18:57:02 -07001330 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1331 PagedView.INVALID_RESTORE_PAGE);
1332 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001333 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 }
1335
Sunny Goyal756cd262015-08-20 12:33:21 -07001336 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1337 if (itemValues != null) {
1338 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001339 AppWidgetProviderInfo info = savedState.getParcelable(
1340 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001341 mPendingAddWidgetInfo = info == null ?
1342 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1343
Adam Cohen4637b5a2013-11-04 18:21:24 -08001344 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001345 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 mRestoring = true;
1347 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 }
1349
1350 /**
1351 * Finds all the views we need and configure them properly.
1352 */
1353 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001354 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001355 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001356 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001357 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyal6178f132016-07-11 17:30:03 -07001358 mQsbContainer = mDragLayer.findViewById(mDeviceProfile.isVerticalBarLayout()
1359 ? R.id.workspace_blocked_row : R.id.qsb_container);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001360 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001361
Sunny Goyal784f9c32016-03-23 16:56:54 -07001362 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1363 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1364 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001365 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366
Winson Chung4c98d922011-05-31 16:50:48 -07001367 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001368
1369 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001370
Winson Chung3d503fb2011-07-13 17:25:49 -07001371 // Setup the hotseat
1372 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1373 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001374 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001375 }
1376
Winsone9f27272015-10-13 10:47:51 -07001377 // Setup the overview panel
1378 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001379
Winson Chung4c98d922011-05-31 16:50:48 -07001380 // Setup the workspace
1381 mWorkspace.setHapticFeedbackEnabled(false);
1382 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001383 mWorkspace.setup(mDragController);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001384 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001385 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001386
Tony Wickham34d2c912015-09-11 09:27:58 -07001387 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001388 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001389
Winson Chungef7f8742015-06-04 17:18:17 -07001390 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001391 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001392 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001393 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1394 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1395 } else {
1396 mAppsView.setSearchBarController(new DefaultAppSearchController());
1397 }
Craig Mautner360310b2012-10-26 15:13:08 -07001398
Winson Chung3d503fb2011-07-13 17:25:49 -07001399 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001400 mDragController.setDragScoller(mWorkspace);
1401 mDragController.setScrollView(mDragLayer);
1402 mDragController.setMoveTarget(mWorkspace);
1403 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001404 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001405
Peter Schiller310a9882016-06-30 13:52:36 -07001406 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1407 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1408 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001409
Sunny Goyal322d5562015-06-25 19:35:49 -07001410 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1411 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001412 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001413 }
1414
Winsone9f27272015-10-13 10:47:51 -07001415 private void setupOverviewPanel() {
1416 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1417
1418 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1419 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1420 @Override
1421 public boolean onLongClick(View v) {
1422 return v.performClick();
1423 }
1424 };
1425
1426 // Bind wallpaper button actions
1427 View wallpaperButton = findViewById(R.id.wallpaper_button);
1428 wallpaperButton.setOnClickListener(new OnClickListener() {
1429 @Override
1430 public void onClick(View view) {
1431 if (!mWorkspace.isSwitchingState()) {
1432 onClickWallpaperPicker(view);
1433 }
1434 }
1435 });
1436 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1437 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1438
1439 // Bind widget button actions
1440 mWidgetsButton = findViewById(R.id.widget_button);
1441 mWidgetsButton.setOnClickListener(new OnClickListener() {
1442 @Override
1443 public void onClick(View view) {
1444 if (!mWorkspace.isSwitchingState()) {
1445 onClickAddWidgetButton(view);
1446 }
1447 }
1448 });
1449 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1450 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1451
1452 // Bind settings actions
1453 View settingsButton = findViewById(R.id.settings_button);
1454 boolean hasSettings = hasSettings();
1455 if (hasSettings) {
1456 settingsButton.setOnClickListener(new OnClickListener() {
1457 @Override
1458 public void onClick(View view) {
1459 if (!mWorkspace.isSwitchingState()) {
1460 onClickSettingsButton(view);
1461 }
1462 }
1463 });
1464 settingsButton.setOnLongClickListener(performClickOnLongClick);
1465 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1466 } else {
1467 settingsButton.setVisibility(View.GONE);
1468 }
1469
1470 mOverviewPanel.setAlpha(0f);
1471 }
1472
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001474 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001475 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001476 */
1477 public void setAllAppsButton(View allAppsButton) {
1478 mAllAppsButton = allAppsButton;
1479 }
1480
Sunny Goyalbb011da2016-06-15 15:42:29 -07001481 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001482 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001483 }
1484
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001485 public View getWidgetsButton() {
1486 return mWidgetsButton;
1487 }
1488
Anjali Koppal5ad44842014-03-10 20:34:39 -07001489 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 * Creates a view representing a shortcut.
1491 *
1492 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001494 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001495 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 }
1497
1498 /**
1499 * Creates a view representing a shortcut inflated from the specified resource.
1500 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 * @param parent The group the shortcut belongs to.
1502 * @param info The data structure describing the shortcut.
1503 *
1504 * @return A View inflated from layoutResId.
1505 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001506 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001507 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001508 parent, false);
1509 favorite.applyFromShortcutInfo(info, mIconCache);
1510 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001512 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 return favorite;
1514 }
1515
1516 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 * Add a shortcut to the workspace.
1518 *
1519 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001521 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001522 int cellY) {
1523 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001524 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001525
Sunny Goyal5c97f512015-05-19 16:03:28 -07001526 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001527 if (info == null) {
1528 return;
1529 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001530 final View view = createShortcut(info);
1531
Sunny Goyal5c97f512015-05-19 16:03:28 -07001532 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001533 // First we check if we already know the exact location where we want to add this item.
1534 if (cellX >= 0 && cellY >= 0) {
1535 cellXY[0] = cellX;
1536 cellXY[1] = cellY;
1537 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001538
1539 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001540 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001541 true, null,null)) {
1542 return;
1543 }
1544 DragObject dragObject = new DragObject();
1545 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001546 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1547 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001548 return;
1549 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001550 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001551 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001552 }
1553
1554 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001555 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001556 return;
1557 }
1558
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001559 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560
1561 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001562 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001563 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 }
1565 }
1566
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001568 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001570 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 */
Adam Cohen091440a2015-03-18 14:16:05 -07001572 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001573 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1574
1575 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001576 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001577 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001578 }
Romain Guycbb89e42009-06-08 15:52:54 -07001579
Adam Cohen59400422014-03-05 18:07:04 -08001580 if (appWidgetInfo.isCustomWidget) {
1581 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001582 }
1583
Adam Cohen59400422014-03-05 18:07:04 -08001584 LauncherAppWidgetInfo launcherInfo;
1585 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1586 launcherInfo.spanX = info.spanX;
1587 launcherInfo.spanY = info.spanY;
1588 launcherInfo.minSpanX = info.minSpanX;
1589 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001590 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001591
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001593 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594
1595 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001596 if (hostView == null) {
1597 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001598 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001599 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001600 hostView.setVisibility(View.VISIBLE);
1601 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001603 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001604 }
Romain Guycbb89e42009-06-08 15:52:54 -07001605
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001606 private void addAppWidgetToWorkspace(
1607 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001608 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001609 hostView.setTag(item);
1610 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001611
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001612 hostView.setFocusable(true);
1613 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001614
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001615 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001616 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1617
1618 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001619 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001620 }
1621 }
1622
Adam Cohended9f8d2010-11-03 13:25:16 -07001623 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1624 @Override
1625 public void onReceive(Context context, Intent intent) {
1626 final String action = intent.getAction();
1627 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1628 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001629 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001630 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001631
Winson Chungc100e8e2011-08-09 16:02:43 -07001632 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001633 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001634 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001635 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001636 if (!showWorkspace(false)) {
1637 // If we are already on the workspace, then manually reset all apps
1638 mAppsView.reset();
1639 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001640 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001641 mIsResumeFromActionScreenOff = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001642 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1643 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001644 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001645 }
1646 }
1647 };
1648
1649 @Override
1650 public void onAttachedToWindow() {
1651 super.onAttachedToWindow();
1652
1653 // Listen for broadcasts related to user-presence
1654 final IntentFilter filter = new IntentFilter();
1655 filter.addAction(Intent.ACTION_SCREEN_OFF);
1656 filter.addAction(Intent.ACTION_USER_PRESENT);
1657 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001658 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001659 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001660 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001661
1662 if (mLauncherCallbacks != null) {
1663 mLauncherCallbacks.onAttachedToWindow();
1664 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001665 }
1666
1667 @Override
1668 public void onDetachedFromWindow() {
1669 super.onDetachedFromWindow();
1670 mVisible = false;
1671
Adam Cohend113e0c2010-11-11 10:48:05 -08001672 if (mAttached) {
1673 unregisterReceiver(mReceiver);
1674 mAttached = false;
1675 }
Winson Chungb745afb2015-03-02 11:51:23 -08001676 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001677
1678 if (mLauncherCallbacks != null) {
1679 mLauncherCallbacks.onDetachedFromWindow();
1680 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001681 }
1682
1683 public void onWindowVisibilityChanged(int visibility) {
1684 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001685 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001686 // The following code used to be in onResume, but it turns out onResume is called when
1687 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1688 // is a more appropriate event to handle
1689 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001690 if (!mWorkspaceLoading) {
1691 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001692 // We want to let Launcher draw itself at least once before we force it to build
1693 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001694 // apps is nice and speedy.
1695 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001696 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001697 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001698 if (mStarted) return;
1699 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001700 // We delay the layer building a bit in order to give
1701 // other message processing a time to run. In particular
1702 // this avoids a delay in hiding the IME if it was
1703 // currently shown, because doing that may involve
1704 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001705 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001706 final ViewTreeObserver.OnDrawListener listener = this;
1707 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001708 public void run() {
1709 if (mWorkspace != null &&
1710 mWorkspace.getViewTreeObserver() != null) {
1711 mWorkspace.getViewTreeObserver().
1712 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001713 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001714 }
1715 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001716 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001717 }
1718 });
1719 }
1720 clearTypedText();
1721 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001722 }
1723
Adam Cohen091440a2015-03-18 14:16:05 -07001724 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001725 mHandler.removeMessages(ADVANCE_MSG);
1726 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1727 mHandler.sendMessageDelayed(msg, delay);
1728 mAutoAdvanceSentTime = System.currentTimeMillis();
1729 }
1730
Adam Cohen091440a2015-03-18 14:16:05 -07001731 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001732 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1733 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1734 mAutoAdvanceRunning = autoAdvanceRunning;
1735 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001736 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001737 sendAdvanceMessage(delay);
1738 } else {
1739 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001740 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001741 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1742 }
1743 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001744 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001745 }
1746 }
1747 }
1748
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001749 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1750
Adam Cohended9f8d2010-11-03 13:25:16 -07001751 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001752 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001753 if (msg.what == ADVANCE_MSG) {
1754 int i = 0;
1755 for (View key: mWidgetsToAdvance.keySet()) {
1756 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001757 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001758 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001759 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001760 public void run() {
1761 ((Advanceable) v).advance();
1762 }
1763 }, delay);
1764 }
1765 i++;
1766 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001767 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001768 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001769 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001770 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001771 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001772
Winsonc0b52fe2015-09-09 16:38:15 -07001773 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001774 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001775 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1776 if (v instanceof Advanceable) {
1777 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001778 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001779 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001780 }
1781 }
1782
Winsonc0b52fe2015-09-09 16:38:15 -07001783 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001784 if (mWidgetsToAdvance.containsKey(hostView)) {
1785 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001786 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001787 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001788 }
1789
Hyunyoung Song3f471442015-04-08 19:01:34 -07001790 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001791 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1792 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001793 }
1794
Winson Chunga6945242014-01-08 14:04:34 -08001795 public DragLayer getDragLayer() {
1796 return mDragLayer;
1797 }
1798
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001799 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001800 return mAppsView;
1801 }
1802
Hyunyoung Song3f471442015-04-08 19:01:34 -07001803 public WidgetsContainerView getWidgetsView() {
1804 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001805 }
1806
Winson Chunga6945242014-01-08 14:04:34 -08001807 public Workspace getWorkspace() {
1808 return mWorkspace;
1809 }
1810
Sunny Goyal6178f132016-07-11 17:30:03 -07001811 public View getQsbContainer() {
1812 return mQsbContainer;
1813 }
1814
Winson Chunga6945242014-01-08 14:04:34 -08001815 public Hotseat getHotseat() {
1816 return mHotseat;
1817 }
1818
Jorim Jaggid017f882014-01-14 17:08:48 -08001819 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001820 return mOverviewPanel;
1821 }
1822
Sunny Goyal47328fd2016-05-25 18:56:41 -07001823 public DropTargetBar getDropTargetBar() {
1824 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001825 }
1826
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001827 public LauncherAppWidgetHost getAppWidgetHost() {
1828 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001829 }
Romain Guycbb89e42009-06-08 15:52:54 -07001830
Winson Chunga9abd0e2010-10-27 17:18:37 -07001831 public LauncherModel getModel() {
1832 return mModel;
1833 }
1834
Adam Cohen79d90c52016-04-22 13:29:20 -07001835 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001836 return mSharedPrefs;
1837 }
1838
Adam Cohen2e6da152015-05-06 11:42:25 -07001839 public DeviceProfile getDeviceProfile() {
1840 return mDeviceProfile;
1841 }
1842
Bjorn Bringertc459e522013-06-07 19:36:01 +01001843 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001844 getWindow().closeAllPanels();
1845
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001846 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001847 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001848 }
1849
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 @Override
1851 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001852 long startTime = 0;
1853 if (DEBUG_RESUME_TIME) {
1854 startTime = System.currentTimeMillis();
1855 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001856 super.onNewIntent(intent);
1857
1858 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001859 Folder openFolder = mWorkspace.getOpenFolder();
1860 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1861 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1862 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1863 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1864 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001865 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001866 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001867
Adam Cohen6fecd412013-10-02 17:41:50 -07001868 if (mWorkspace == null) {
1869 // Can be cases where mWorkspace is null, this prevents a NPE
1870 return;
1871 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001872 // In all these cases, only animate if we're already on home
1873 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001874
Sunny Goyal935fca12015-10-13 10:19:01 -07001875 closeFolder(alreadyOnHome);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001876 closeShortcutsContainer(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001877 exitSpringLoadedDragMode();
1878
1879 // If we are already on home, then just animate back to the workspace,
1880 // otherwise, just wait until onResume to set the state back to Workspace
1881 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001882 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001883 } else {
1884 mOnResumeState = State.WORKSPACE;
1885 }
1886
1887 final View v = getWindow().peekDecorView();
1888 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001889 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001890 INPUT_METHOD_SERVICE);
1891 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1892 }
1893
Winson Chungb745afb2015-03-02 11:51:23 -08001894 // Reset the apps view
1895 if (!alreadyOnHome && mAppsView != null) {
1896 mAppsView.scrollToTop();
1897 }
1898
Hyunyoung Song3f471442015-04-08 19:01:34 -07001899 // Reset the widgets view
1900 if (!alreadyOnHome && mWidgetsView != null) {
1901 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001902 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001903
Adam Cohen9211d422014-10-07 18:14:53 -07001904 if (mLauncherCallbacks != null) {
1905 mLauncherCallbacks.onHomeIntent();
1906 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001907 }
Adam Cohened307df2013-10-02 09:37:31 -07001908
Adam Cohen9211d422014-10-07 18:14:53 -07001909 if (mLauncherCallbacks != null) {
1910 mLauncherCallbacks.onNewIntent(intent);
1911 }
Winson15f8b172015-08-19 11:02:39 -07001912
1913 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1914 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1915 // animation.
1916 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001917 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1918 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001919 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1920 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001921
1922 // We use this flag to suppress noisy callbacks above custom content state
1923 // from onResume.
1924 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001925 mWorkspace.post(new Runnable() {
1926 @Override
1927 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001928 if (mWorkspace != null) {
1929 mWorkspace.moveToDefaultScreen(true);
1930 }
Winson15f8b172015-08-19 11:02:39 -07001931 }
1932 });
1933 }
1934 }
1935
1936 if (DEBUG_RESUME_TIME) {
1937 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1938 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001939 }
1940
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001942 public void onRestoreInstanceState(Bundle state) {
1943 super.onRestoreInstanceState(state);
1944 for (int page: mSynchronouslyBoundPages) {
1945 mWorkspace.restoreInstanceStateForChild(page);
1946 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947 }
1948
1949 @Override
1950 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001951 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001952 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1953 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001954
Adam Cohen21cd0022013-10-09 18:57:02 -07001955 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001956 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957
Michael Jurka883f55b2010-10-21 15:47:14 -07001958 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001959 // We close any open folder since it will not be re-opened, and we need to make sure
1960 // this state is reflected.
1961 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1962 closeFolder(false);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001963 closeShortcutsContainer(false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001964
Adam Cohendcd297f2013-06-18 13:13:40 -07001965 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001966 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001967 ContentValues itemValues = new ContentValues();
1968 mPendingAddInfo.writeToValues(itemValues);
1969 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001970 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001971 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 }
1973
Adam Cohen9211d422014-10-07 18:14:53 -07001974 if (mLauncherCallbacks != null) {
1975 mLauncherCallbacks.onSaveInstanceState(outState);
1976 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 }
1978
1979 @Override
1980 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001982
Winson Chunge7a03942011-08-05 15:05:12 -07001983 // Remove all pending runnables
1984 mHandler.removeMessages(ADVANCE_MSG);
1985 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001986 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001987 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001988
Winson Chungcd2b0142011-06-08 16:02:26 -07001989 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001990 // It's possible to receive onDestroy after a new Launcher activity has
1991 // been created. In this case, don't interfere with the new Launcher.
1992 if (mModel.isCurrentCallbacks(this)) {
1993 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001994 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001995 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001996
Sunny Goyal745bad92016-05-02 10:54:12 -07001997 if (mRotationPrefChangeHandler != null) {
1998 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1999 }
2000
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002002 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002004 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002006 mAppWidgetHost = null;
2007
2008 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009
2010 TextKeyListener.getInstance().release();
2011
Tony Wickhame2217252016-03-22 16:34:23 -07002012 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
2013 .removeAccessibilityStateChangeListener(this);
2014
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002015 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002016
Michael Jurka2ecf9952012-06-18 12:52:28 -07002017 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002018
2019 if (mLauncherCallbacks != null) {
2020 mLauncherCallbacks.onDestroy();
2021 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 }
2023
Sunny Goyalae502842016-06-17 08:43:56 -07002024 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
2025 return mAccessibilityDelegate;
2026 }
2027
Adam Cohena9cf38f2011-05-02 15:36:58 -07002028 public DragController getDragController() {
2029 return mDragController;
2030 }
2031
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002033 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07002034 onStartForResult(requestCode);
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002035 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07002036 }
2037
2038 @Override
2039 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2040 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2041 onStartForResult(requestCode);
2042 try {
2043 super.startIntentSenderForResult(intent, requestCode,
2044 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2045 } catch (IntentSender.SendIntentException e) {
2046 throw new ActivityNotFoundException();
2047 }
2048 }
2049
2050 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002051 if (requestCode >= 0) {
2052 setWaitingForResult(true);
2053 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 }
2055
Winson Chung70d72102011-08-12 11:24:35 -07002056 /**
2057 * Indicates that we want global search for this activity by setting the globalSearch
2058 * argument for {@link #startSearch} to true.
2059 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002061 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002063
Karl Rosaen138a0412009-04-23 19:00:21 -07002064 if (initialQuery == null) {
2065 // Use any text typed in the launcher as the initial query
2066 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002067 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068 if (appSearchData == null) {
2069 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002070 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002072
2073 // TODO send proper bounds.
2074 Rect sourceBounds = null;
Romain Guycbb89e42009-06-08 15:52:54 -07002075
Ian Parkinson047036e2014-09-01 15:40:53 +01002076 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002077 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002078 if (clearTextImmediately) {
2079 clearTypedText();
2080 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002081
2082 // We need to show the workspace after starting the search
2083 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002084 }
2085
Ian Parkinson047036e2014-09-01 15:40:53 +01002086 /**
2087 * Start a text search.
2088 *
2089 * @return {@code true} if the search will start immediately, so any further keypresses
2090 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2091 * to buffer keypresses.
2092 */
2093 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002094 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002095 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2096 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2097 sourceBounds);
2098 }
2099
Michael Jurkaa33411c2012-06-14 16:18:21 -07002100 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002101 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002102 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002103 }
2104
2105 /**
2106 * Starts the global search activity. This code is a copied from SearchManager
2107 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002108 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002109 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002110 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002111 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2112 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2113 if (globalSearchActivity == null) {
2114 Log.w(TAG, "No global search activity found.");
2115 return;
2116 }
2117 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2118 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2119 intent.setComponent(globalSearchActivity);
2120 // Make sure that we have a Bundle to put source in
2121 if (appSearchData == null) {
2122 appSearchData = new Bundle();
2123 } else {
2124 appSearchData = new Bundle(appSearchData);
2125 }
Adam Cohen9211d422014-10-07 18:14:53 -07002126 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002127 if (!appSearchData.containsKey("source")) {
2128 appSearchData.putString("source", getPackageName());
2129 }
2130 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2131 if (!TextUtils.isEmpty(initialQuery)) {
2132 intent.putExtra(SearchManager.QUERY, initialQuery);
2133 }
2134 if (selectInitialQuery) {
2135 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2136 }
2137 intent.setSourceBounds(sourceBounds);
2138 try {
2139 startActivity(intent);
2140 } catch (ActivityNotFoundException ex) {
2141 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2142 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002143 }
2144
Yura4f93ec62014-02-04 14:15:21 +00002145 public boolean isOnCustomContent() {
2146 return mWorkspace.isOnOrMovingToCustomContent();
2147 }
2148
Winson Chung70d72102011-08-12 11:24:35 -07002149 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002150 public boolean onPrepareOptionsMenu(Menu menu) {
2151 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002152 if (mLauncherCallbacks != null) {
2153 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2154 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002155 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002156 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002157
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002158 @Override
2159 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002160 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002161 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002162 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002163 }
2164
Joe Onorato9c1289c2009-08-17 11:03:03 -04002165 public boolean isWorkspaceLocked() {
2166 return mWorkspaceLoading || mWaitingForResult;
2167 }
2168
Adam Cohen517a7f52014-03-01 12:12:59 -08002169 public boolean isWorkspaceLoading() {
2170 return mWorkspaceLoading;
2171 }
2172
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002173 private void setWorkspaceLoading(boolean value) {
2174 boolean isLocked = isWorkspaceLocked();
2175 mWorkspaceLoading = value;
2176 if (isLocked != isWorkspaceLocked()) {
2177 onWorkspaceLockedChanged();
2178 }
2179 }
2180
2181 private void setWaitingForResult(boolean value) {
2182 boolean isLocked = isWorkspaceLocked();
2183 mWaitingForResult = value;
2184 if (isLocked != isWorkspaceLocked()) {
2185 onWorkspaceLockedChanged();
2186 }
2187 }
2188
Adam Cohen9211d422014-10-07 18:14:53 -07002189 protected void onWorkspaceLockedChanged() {
2190 if (mLauncherCallbacks != null) {
2191 mLauncherCallbacks.onWorkspaceLockedChanged();
2192 }
2193 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002194
Michael Jurka0280c3b2010-09-17 15:00:07 -07002195 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002196 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002197 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002198 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2199 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002200 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002201 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002202
Tony Wickhama0628cc2015-10-14 15:23:04 -07002203 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002204 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002205 if (LOGD) {
2206 Log.d(TAG, "Adding widget from drop");
2207 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002208 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2209 }
2210
Sunny Goyale6b63a32015-01-16 16:14:29 -08002211 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002212 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2213 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002214 if (appWidgetInfo.configure != null) {
2215 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002216 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002217
Bjorn Bringert7984c942009-12-09 15:38:25 +00002218 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002219 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2220 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2221
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002222 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002223 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002224 Runnable onComplete = new Runnable() {
2225 @Override
2226 public void run() {
2227 // Exit spring loaded mode if necessary after adding the widget
2228 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2229 null);
2230 }
2231 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002232 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002233 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002234 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002235 }
2236 }
Romain Guycbb89e42009-06-08 15:52:54 -07002237
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002238 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002239 // Close any folders that may be open.
2240 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002241 mWorkspace.moveToCustomContentScreen(animate);
2242 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002243
2244 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2245 int[] cell, int spanX, int spanY) {
2246 switch (info.itemType) {
2247 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2248 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2249 int span[] = new int[2];
2250 span[0] = spanX;
2251 span[1] = spanY;
2252 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2253 container, screenId, cell, span);
2254 break;
2255 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2256 processShortcutFromDrop(info.componentName, container, screenId, cell);
2257 break;
2258 default:
2259 throw new IllegalStateException("Unknown item type: " + info.itemType);
2260 }
2261 }
2262
Adam Cohenfbba09b2011-07-18 21:43:05 -07002263 /**
2264 * Process a shortcut drop.
2265 *
2266 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002267 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002268 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002269 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002270 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2271 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002272 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002273 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002274 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002275
2276 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002277 mPendingAddInfo.cellX = cell[0];
2278 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002279 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002280
2281 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2282 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002283 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002284 }
2285
Adam Cohenfbba09b2011-07-18 21:43:05 -07002286 /**
2287 * Process a widget drop.
2288 *
2289 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002290 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002291 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002292 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002293 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2294 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002295 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002296 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002297 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002298 mPendingAddInfo.minSpanX = info.minSpanX;
2299 mPendingAddInfo.minSpanY = info.minSpanY;
2300
Adam Cohenfbba09b2011-07-18 21:43:05 -07002301 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002302 mPendingAddInfo.cellX = cell[0];
2303 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002304 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002305 if (span != null) {
2306 mPendingAddInfo.spanX = span[0];
2307 mPendingAddInfo.spanY = span[1];
2308 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002309
Adam Cohened66b2b2012-01-23 17:28:51 -08002310 AppWidgetHostView hostView = info.boundWidget;
2311 int appWidgetId;
2312 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002313 // In the case where we've prebound the widget, we remove it from the DragLayer
2314 if (LOGD) {
2315 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2316 }
2317 getDragLayer().removeView(hostView);
2318
Adam Cohened66b2b2012-01-23 17:28:51 -08002319 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002320 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002321
2322 // Clear the boundWidget so that it doesn't get destroyed.
2323 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002324 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002325 // In this case, we either need to start an activity to get permission to bind
2326 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002327 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002328 Bundle options = info.bindOptions;
2329
Sunny Goyalffe83f12014-08-14 17:39:34 -07002330 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2331 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002332 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002333 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002334 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002335 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002336 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2337 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2338 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002339 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2340 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002341 // TODO: we need to make sure that this accounts for the options bundle.
2342 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002343 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2344 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002345 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002346 }
2347
Adam Cohendcd297f2013-06-18 13:13:40 -07002348 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002349 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002350 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002351 folderInfo.title = getText(R.string.folder_name);
2352
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002353 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002354 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2355 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002356
2357 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002358 FolderIcon newFolder =
2359 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002360 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002361 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002362 // Force measure the new folder icon
2363 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2364 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002365 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002366 }
Romain Guycbb89e42009-06-08 15:52:54 -07002367
Winsonc0b52fe2015-09-09 16:38:15 -07002368 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002369 * Unbinds the view for the specified item, and removes the item and all its children.
2370 *
2371 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002372 * @param itemInfo the {@link ItemInfo} for this view.
2373 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002374 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002375 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002376 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002377 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002378 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2379 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002380 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002381 } else {
2382 mWorkspace.removeWorkspaceItem(v);
2383 }
Winsonc0b52fe2015-09-09 16:38:15 -07002384 if (deleteFromDb) {
2385 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2386 }
2387 } else if (itemInfo instanceof FolderInfo) {
2388 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002389 if (v instanceof FolderIcon) {
2390 ((FolderIcon) v).removeListeners();
2391 }
Winsonc0b52fe2015-09-09 16:38:15 -07002392 mWorkspace.removeWorkspaceItem(v);
2393 if (deleteFromDb) {
2394 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2395 }
2396 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2397 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002398 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002399 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002400 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002401 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002402 }
2403 } else {
2404 return false;
2405 }
2406 return true;
2407 }
2408
2409 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002410 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002411 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002412 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002413 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002414 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002415 // Deleting an app widget ID is a void call but writes to disk before returning
2416 // to the caller...
2417 new AsyncTask<Void, Void, Void>() {
2418 public Void doInBackground(Void ... args) {
2419 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2420 return null;
2421 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002422 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002423 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002424 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002425 }
2426
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002427 @Override
2428 public boolean dispatchKeyEvent(KeyEvent event) {
2429 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2430 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002431 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002432 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002433 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002434 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002435 dumpState();
2436 return true;
2437 }
2438 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002439 }
2440 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2441 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002442 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002443 return true;
2444 }
2445 }
2446
2447 return super.dispatchKeyEvent(event);
2448 }
2449
Joe Onorato88ec0992009-11-19 13:16:06 -08002450 @Override
2451 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002452 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2453 return;
2454 }
2455
Sunny Goyal45478022015-06-08 16:52:41 -07002456 if (mDragController.isDragging()) {
2457 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002458 return;
2459 }
2460
Tony Wickham49c8d292016-07-01 11:44:34 -07002461 if (getOpenShortcutsContainer() != null) {
2462 closeShortcutsContainer();
2463 } else if (isAppsViewVisible()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002464 showWorkspace(true);
2465 } else if (isWidgetsViewVisible()) {
2466 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002467 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002468 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002469 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002470 Folder openFolder = mWorkspace.getOpenFolder();
2471 if (openFolder.isEditingName()) {
2472 openFolder.dismissEditingName();
2473 } else {
2474 closeFolder();
2475 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002476 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002477 mWorkspace.exitWidgetResizeMode();
2478
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002479 // Back button is a no-op here, but give at least some feedback for the button press
2480 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002481 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002482 }
2483
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002484 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002485 * Launches the intent referred by the clicked shortcut.
2486 *
2487 * @param v The view representing the clicked shortcut.
2488 */
2489 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002490 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2491 // view has detached (it's possible for this to happen if the view is removed mid touch).
2492 if (v.getWindowToken() == null) {
2493 return;
2494 }
2495
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002496 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002497 return;
2498 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002499
Adam Cohen1697b792013-09-17 19:08:21 -07002500 if (v instanceof Workspace) {
2501 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002502 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002503 }
2504 return;
2505 }
2506
2507 if (v instanceof CellLayout) {
2508 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002509 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2510 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002511 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002512 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002513 }
2514
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002515 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002516 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002517 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002518 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002519 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002520 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002521 }
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002522 } else if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2523 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002524 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002525 } else if (tag instanceof AppInfo) {
2526 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002527 } else if (tag instanceof LauncherAppWidgetInfo) {
2528 if (v instanceof PendingAppWidgetHostView) {
2529 onClickPendingWidget((PendingAppWidgetHostView) v);
2530 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002531 }
2532 }
2533
Sunny Goyal70660032015-05-14 00:07:08 -07002534 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002535 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002536 return false;
2537 }
2538
Michael Jurkaaf442092010-06-10 17:01:57 -07002539 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002540 * Event handler for the app widget view which has not fully restored.
2541 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002542 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002543 if (mIsSafeModeEnabled) {
2544 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2545 return;
2546 }
2547
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002548 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002549 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002550 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2551 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2552 // This should not happen, as we make sure that an Id is allocated during bind.
2553 return;
2554 }
2555 LauncherAppWidgetProviderInfo appWidgetInfo =
2556 mAppWidgetManager.findProvider(info.providerName, info.user);
2557 if (appWidgetInfo != null) {
2558 mPendingAddWidgetId = info.appWidgetId;
2559 mPendingAddInfo.copyFrom(info);
2560 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002561
Sunny Goyald478c832016-04-01 12:04:16 -07002562 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2563 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingAddWidgetId);
2564 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
2565 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2566 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2567 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2568 }
2569 } else {
2570 LauncherAppWidgetProviderInfo appWidgetInfo =
2571 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2572 if (appWidgetInfo != null) {
2573 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2574 }
Sunny Goyalff572272014-07-23 13:58:07 -07002575 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002576 } else if (info.installProgress < 0) {
2577 // The install has not been queued
2578 final String packageName = info.providerName.getPackageName();
2579 showBrokenAppInstallDialog(packageName,
2580 new DialogInterface.OnClickListener() {
2581 public void onClick(DialogInterface dialog, int id) {
2582 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2583 }
2584 });
2585 } else {
2586 // Download has started.
2587 final String packageName = info.providerName.getPackageName();
2588 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002589 }
2590 }
2591
Sunny Goyald478c832016-04-01 12:04:16 -07002592 private void startRestoredWidgetReconfigActivity(
2593 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
2594 mPendingAddWidgetInfo = provider;
2595 mPendingAddInfo.copyFrom(info);
2596 mPendingAddWidgetId = info.appWidgetId;
2597 mAppWidgetManager.startConfigActivity(provider,
2598 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2599 }
2600
Sunny Goyalff572272014-07-23 13:58:07 -07002601 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002602 * Event handler for the "grid" button that appears on the home screen, which
2603 * enters all apps mode.
2604 *
2605 * @param v The view that was clicked.
2606 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002607 protected void onClickAllAppsButton(View v) {
2608 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002609 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002610 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.TAP,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002611 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002612 showAppsView(true /* animated */, true /* updatePredictedApps */,
2613 false /* focusSearchBar */);
Winson Chung76648c52015-07-10 14:33:23 -07002614 }
2615 }
2616
2617 protected void onLongClickAllAppsButton(View v) {
2618 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2619 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002620 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.LONGPRESS,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002621 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002622 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07002623 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002624 }
2625 }
2626
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002627 private void showBrokenAppInstallDialog(final String packageName,
2628 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002629 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002630 .setTitle(R.string.abandoned_promises_title)
2631 .setMessage(R.string.abandoned_promise_explanation)
2632 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2633 .setNeutralButton(R.string.abandoned_clean_this,
2634 new DialogInterface.OnClickListener() {
2635 public void onClick(DialogInterface dialog, int id) {
2636 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2637 mWorkspace.removeAbandonedPromise(packageName, user);
2638 }
2639 })
2640 .create().show();
2641 return;
2642 }
2643
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002644 /**
2645 * Event handler for an app shortcut click.
2646 *
2647 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2648 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002649 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002650 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2651 Object tag = v.getTag();
2652 if (!(tag instanceof ShortcutInfo)) {
2653 throw new IllegalArgumentException("Input must be a Shortcut");
2654 }
2655
2656 // Open shortcut
2657 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002658
2659 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002660 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2661 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002662 // Launch activity anyway, framework will tell the user why the app is suspended.
2663 } else {
2664 int error = R.string.activity_not_available;
2665 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2666 error = R.string.safemode_shortcut_error;
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002667 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0) {
2668 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002669 }
2670 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2671 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002672 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002673 }
2674
Chris Wren40c5ed32014-06-24 18:24:23 -04002675 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002676 if ((v instanceof BubbleTextView)
2677 && shortcut.isPromise()
2678 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002679 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002680 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002681 new DialogInterface.OnClickListener() {
2682 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002683 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002684 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002685 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002686 return;
2687 }
2688
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002689 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002690 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002691 }
2692
Sunny Goyala7ce1662016-05-31 15:01:35 -07002693 private void startAppShortcutOrInfoActivity(View v) {
2694 ItemInfo item = (ItemInfo) v.getTag();
2695 Intent intent = item.getIntent();
2696 if (intent == null) {
2697 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002698 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002699 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002700 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002701
2702 if (success && v instanceof BubbleTextView) {
2703 mWaitingForResume = (BubbleTextView) v;
2704 mWaitingForResume.setStayPressed(true);
2705 }
2706 }
2707
2708 /**
2709 * Event handler for a folder icon click.
2710 *
2711 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2712 */
2713 protected void onClickFolderIcon(View v) {
2714 if (LOGD) Log.d(TAG, "onClickFolder");
2715 if (!(v instanceof FolderIcon)){
2716 throw new IllegalArgumentException("Input must be a FolderIcon");
2717 }
2718
2719 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002720 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002721 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002722 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002723 }
Michael Jurka5130e402011-10-13 04:55:35 -07002724 }
2725
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002726 /**
2727 * Event handler for the (Add) Widgets button that appears after a long press
2728 * on the home screen.
2729 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002730 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002731 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002732 if (mIsSafeModeEnabled) {
2733 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2734 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002735 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002736 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002737 }
2738
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002739 /**
2740 * Event handler for the wallpaper picker button that appears after a long press
2741 * on the home screen.
2742 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002743 public void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002744 if (!Utilities.isWallapaperAllowed(this)) {
2745 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2746 return;
2747 }
2748
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002749 String pickerPackage = getString(R.string.wallpaper_picker_package);
2750 if (TextUtils.isEmpty(pickerPackage)) {
2751 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2752 }
2753
Tony Wickham785f7a52015-08-31 17:28:32 -07002754 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2755 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002756
2757 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002758 .setPackage(pickerPackage)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002759 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
2760 intent.setSourceBounds(getViewBounds(v));
2761 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002762 }
2763
2764 /**
2765 * Event handler for a click on the settings button that appears after a long press
2766 * on the home screen.
2767 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002768 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002769 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002770 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2771 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002772 }
2773
Adam Cohen61f560d2013-09-30 15:58:20 -07002774 public View.OnTouchListener getHapticFeedbackTouchListener() {
2775 if (mHapticFeedbackTouchListener == null) {
2776 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002777 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002778 @Override
2779 public boolean onTouch(View v, MotionEvent event) {
2780 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2781 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2782 }
2783 return false;
2784 }
2785 };
2786 }
2787 return mHapticFeedbackTouchListener;
2788 }
2789
Tony Wickhame2217252016-03-22 16:34:23 -07002790 @Override
2791 public void onAccessibilityStateChanged(boolean enabled) {
2792 mDragLayer.onAccessibilityStateChanged(enabled);
2793 }
2794
Adam Cohen9211d422014-10-07 18:14:53 -07002795 public void onDragStarted(View view) {
2796 if (isOnCustomContent()) {
2797 // Custom content screen doesn't participate in drag and drop. If on custom
2798 // content screen, move to default.
2799 moveWorkspaceToDefaultScreen();
2800 }
Adam Cohen9211d422014-10-07 18:14:53 -07002801 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002802
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002803 /**
2804 * Called when the user stops interacting with the launcher.
2805 * This implies that the user is now on the homescreen and is not doing housekeeping.
2806 */
Adam Cohen9211d422014-10-07 18:14:53 -07002807 protected void onInteractionEnd() {
2808 if (mLauncherCallbacks != null) {
2809 mLauncherCallbacks.onInteractionEnd();
2810 }
2811 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002812
2813 /**
2814 * Called when the user starts interacting with the launcher.
2815 * The possible interactions are:
2816 * - open all apps
2817 * - reorder an app shortcut, or a widget
2818 * - open the overview mode.
2819 * This is a good time to stop doing things that only make sense
2820 * when the user is on the homescreen and not doing housekeeping.
2821 */
Adam Cohen9211d422014-10-07 18:14:53 -07002822 protected void onInteractionBegin() {
2823 if (mLauncherCallbacks != null) {
2824 mLauncherCallbacks.onInteractionBegin();
2825 }
2826 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002827
Winson Chungcd99cd32015-04-29 11:03:24 -07002828 /** Updates the interaction state. */
2829 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002830 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002831 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002832 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2833 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002834 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002835 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002836 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002837 onInteractionEnd();
2838 }
2839 }
2840
Sunny Goyala7ce1662016-05-31 15:01:35 -07002841 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002842 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002843 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2844 try {
2845 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2846 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2847 // is enabled by default on NYC.
2848 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2849 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002850
2851 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2852 String id = ((ShortcutInfo) info).getDeepShortcutId();
2853 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002854 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2855 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002856 } else {
2857 // Could be launching some bookkeeping activity
2858 startActivity(intent, optsBundle);
2859 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002860 } finally {
2861 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002862 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002863 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002864 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2865 // corresponding permission. Show the appropriate permission prompt if that
2866 // is the case.
2867 if (intent.getComponent() == null
2868 && Intent.ACTION_CALL.equals(intent.getAction())
2869 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2870 PackageManager.PERMISSION_GRANTED) {
2871 // TODO: Rename sPendingAddItem to a generic name.
2872 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2873 0, info);
2874 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2875 REQUEST_PERMISSION_CALL_PHONE);
2876 } else {
2877 // No idea why this was thrown.
2878 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002879 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002880 }
2881 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002882
Sunny Goyala7ce1662016-05-31 15:01:35 -07002883 private Bundle getActivityLaunchOptions(View v) {
2884 if (Utilities.ATLEAST_MARSHMALLOW) {
2885 int left = 0, top = 0;
2886 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2887 if (v instanceof TextView) {
2888 // Launch from center of icon, not entire view
2889 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2890 if (icon != null) {
2891 Rect bounds = icon.getBounds();
2892 left = (width - bounds.width()) / 2;
2893 top = v.getPaddingTop();
2894 width = bounds.width();
2895 height = bounds.height();
2896 }
2897 }
2898 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2899 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2900 // On L devices, we use the device default slide-up transition.
2901 // On L MR1 devices, we use a custom version of the slide-up transition which
2902 // doesn't have the delay present in the device default.
2903 return ActivityOptions.makeCustomAnimation(
2904 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2905 }
2906 return null;
2907 }
2908
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002909 private Rect getViewBounds(View v) {
2910 int[] pos = new int[2];
2911 v.getLocationOnScreen(pos);
2912 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2913 }
2914
Sunny Goyala7ce1662016-05-31 15:01:35 -07002915 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002916 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2917 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2918 return false;
2919 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002920 // Only launch using the new animation if the shortcut has not opted out (this is a
2921 // private contract between launcher and may be ignored in the future).
2922 boolean useLaunchAnimation = (v != null) &&
2923 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2924 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2925
2926 UserHandleCompat user = null;
2927 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2928 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2929 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002930 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002931
2932 // Prepare intent
2933 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2934 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002935 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002936 }
2937 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002938 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2939 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
2940 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002941 // Shortcuts need some special checks due to legacy reasons.
2942 startShortcutIntentSafely(intent, optsBundle, item);
2943 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2944 // Could be launching some bookkeeping activity
2945 startActivity(intent, optsBundle);
2946 } else {
2947 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2948 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2949 }
2950 return true;
2951 } catch (ActivityNotFoundException|SecurityException e) {
2952 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2953 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2954 }
2955 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002956 }
2957
Adam Cohen268c4752012-06-06 17:47:33 -07002958 /**
2959 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2960 * in the DragLayer in the exact absolute location of the original FolderIcon.
2961 */
2962 private void copyFolderIconToImage(FolderIcon fi) {
2963 final int width = fi.getMeasuredWidth();
2964 final int height = fi.getMeasuredHeight();
2965
2966 // Lazy load ImageView, Bitmap and Canvas
2967 if (mFolderIconImageView == null) {
2968 mFolderIconImageView = new ImageView(this);
2969 }
2970 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2971 mFolderIconBitmap.getHeight() != height) {
2972 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2973 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2974 }
2975
2976 DragLayer.LayoutParams lp;
2977 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2978 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2979 } else {
2980 lp = new DragLayer.LayoutParams(width, height);
2981 }
2982
Adam Cohen307fe232012-08-16 17:55:58 -07002983 // The layout from which the folder is being opened may be scaled, adjust the starting
2984 // view size by this scale factor.
2985 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002986 lp.customPosition = true;
2987 lp.x = mRectForFolderAnimation.left;
2988 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002989 lp.width = (int) (scale * width);
2990 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002991
2992 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2993 fi.draw(mFolderIconCanvas);
2994 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002995 if (fi.getFolder() != null) {
2996 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2997 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002998 }
Adam Cohen268c4752012-06-06 17:47:33 -07002999 // Just in case this image view is still in the drag layer from a previous animation,
3000 // we remove it and re-add it.
3001 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3002 mDragLayer.removeView(mFolderIconImageView);
3003 }
3004 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003005 if (fi.getFolder() != null) {
3006 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003007 }
Adam Cohen268c4752012-06-06 17:47:33 -07003008 }
3009
Adam Cohen37b2a492016-04-06 18:36:06 -07003010 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003011 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07003012 FolderInfo info = (FolderInfo) fi.getTag();
3013 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3014 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003015 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3016 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003017 }
3018
Adam Cohen268c4752012-06-06 17:47:33 -07003019 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3020 copyFolderIconToImage(fi);
3021 fi.setVisibility(View.INVISIBLE);
3022
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003023 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
3024 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003025 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003026 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3027 }
3028 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003029 oa.start();
3030 }
3031
Sunny Goyal935fca12015-10-13 10:19:01 -07003032 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003033 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003034 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003035
Adam Cohen268c4752012-06-06 17:47:33 -07003036 // We remove and re-draw the FolderIcon in-case it has changed
3037 mDragLayer.removeView(mFolderIconImageView);
3038 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08003039
3040 if (cl != null) {
3041 cl.clearFolderLeaveBehind();
3042 }
3043
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003044 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003045 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003046 oa.addListener(new AnimatorListenerAdapter() {
3047 @Override
3048 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003049 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003050 // Remove the ImageView copy of the FolderIcon and make the original visible.
3051 mDragLayer.removeView(mFolderIconImageView);
3052 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003053 }
3054 }
3055 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003056 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003057 if (!animate) {
3058 oa.end();
3059 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003060 }
3061
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003062 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003063 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003064 * is animated relative to the specified View. If the View is null, no animation
3065 * is played.
3066 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003067 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003068 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003069 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003070
Adam Cohenfb91f302012-06-11 15:45:18 -07003071 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003072 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3073 if (openFolder != null && openFolder != folder) {
3074 // Close any open folder before opening a folder.
3075 closeFolder();
3076 }
3077
Adam Cohena9cf38f2011-05-02 15:36:58 -07003078 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003079
Adam Cohena9cf38f2011-05-02 15:36:58 -07003080 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003081
Sunny Goyalf4066152015-04-15 09:42:19 -07003082 // While the folder is open, the position of the icon cannot change.
3083 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3084
Adam Cohen4554ee12011-08-03 16:13:21 -07003085 // Just verify that the folder hasn't already been added to the DragLayer.
3086 // There was a one-off crash where the folder had a parent already.
3087 if (folder.getParent() == null) {
3088 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003089 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003090 } else {
3091 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3092 folder.getParent() + ").");
3093 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003094 folder.animateOpen();
3095
3096 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003097
3098 // Notify the accessibility manager that this folder "window" has appeared and occluded
3099 // the workspace items
3100 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3101 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003102 }
3103
3104 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003105 closeFolder(true);
3106 }
3107
3108 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003109 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003110 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003111 if (folder.isEditingName()) {
3112 folder.dismissEditingName();
3113 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003114 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003115 }
3116 }
3117
Sunny Goyal935fca12015-10-13 10:19:01 -07003118 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003119 animate &= !Utilities.isPowerSaverOn(this);
3120
Adam Cohen4554ee12011-08-03 16:13:21 -07003121 folder.getInfo().opened = false;
3122
3123 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3124 if (parent != null) {
3125 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003126 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003127 if (fi != null) {
3128 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3129 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003130 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003131 if (animate) {
3132 folder.animateClosed();
3133 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003134 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003135 }
Winson Chung83ca4802013-04-12 15:10:52 -07003136
Sunny Goyal935fca12015-10-13 10:19:01 -07003137 // Notify the accessibility manager that this folder "window" has disappeared and no
3138 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003139 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003140 }
3141
Tony Wickham49c8d292016-07-01 11:44:34 -07003142 public void closeShortcutsContainer() {
Sunny Goyala2454ad2016-07-22 10:50:11 -07003143 closeShortcutsContainer(true);
3144 }
3145
3146 public void closeShortcutsContainer(boolean animate) {
Tony Wickham49c8d292016-07-01 11:44:34 -07003147 DeepShortcutsContainer deepShortcutsContainer = getOpenShortcutsContainer();
3148 if (deepShortcutsContainer != null) {
Sunny Goyala2454ad2016-07-22 10:50:11 -07003149 if (animate) {
3150 deepShortcutsContainer.animateClose();
3151 } else {
3152 deepShortcutsContainer.close();
3153 }
Tony Wickham49c8d292016-07-01 11:44:34 -07003154 }
3155 }
3156
3157 /**
3158 * @return The open shortcuts container, or null if there is none
3159 */
3160 public DeepShortcutsContainer getOpenShortcutsContainer() {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07003161 // Iterate in reverse order. Shortcuts container is added later to the dragLayer,
3162 // and will be one of the last views.
3163 for (int i = mDragLayer.getChildCount() - 1; i >= 0; i--) {
3164 View child = mDragLayer.getChildAt(i);
Sunny Goyala2454ad2016-07-22 10:50:11 -07003165 if (child instanceof DeepShortcutsContainer
3166 && ((DeepShortcutsContainer) child).isOpen()) {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07003167 return (DeepShortcutsContainer) child;
3168 }
3169 }
3170 return null;
Tony Wickham49c8d292016-07-01 11:44:34 -07003171 }
3172
Tony Wickhamdadb3042016-02-24 11:07:00 -08003173 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003174 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003175 if (!isDraggingEnabled()) return false;
3176 if (isWorkspaceLocked()) return false;
3177 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003178
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07003179 if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
3180 (v == mAllAppsButton && mAllAppsButton != null)) {
Winson Chung76648c52015-07-10 14:33:23 -07003181 onLongClickAllAppsButton(v);
3182 return true;
3183 }
3184
Adam Cohen1697b792013-09-17 19:08:21 -07003185 if (v instanceof Workspace) {
3186 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003187 if (!mWorkspace.isTouchActive()) {
3188 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003189 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3190 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3191 return true;
3192 } else {
3193 return false;
3194 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003195 } else {
3196 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003197 }
Adam Cohen1697b792013-09-17 19:08:21 -07003198 }
3199
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003200 CellLayout.CellInfo longClickCellInfo = null;
3201 View itemUnderLongClick = null;
3202 if (v.getTag() instanceof ItemInfo) {
3203 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003204 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003205 itemUnderLongClick = longClickCellInfo.cell;
3206 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003207 }
3208
Winson Chung3d503fb2011-07-13 17:25:49 -07003209 // The hotseat touch handling does not go through Workspace, and we always allow long press
3210 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003211 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003212 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003213 // User long pressed on empty space
3214 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3215 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003216 if (mWorkspace.isInOverviewMode()) {
3217 mWorkspace.startReordering(v);
3218 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003219 showOverviewMode(true);
Hyunyoung Song645764e2016-06-06 14:19:02 -07003220 mHotseat.requestDisallowInterceptTouchEvent(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003221 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003222 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003223 final boolean isAllAppsButton =
3224 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3225 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3226 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003227 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003228 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003229 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003230 }
3231 }
3232 }
3233 return true;
3234 }
3235
Winson Chung3d503fb2011-07-13 17:25:49 -07003236 boolean isHotseatLayout(View layout) {
3237 return mHotseat != null && layout != null &&
3238 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3239 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003240
Winson Chung3d503fb2011-07-13 17:25:49 -07003241 /**
3242 * Returns the CellLayout of the specified container at the specified screen.
3243 */
Sunny Goyal92820592015-03-02 11:31:35 -08003244 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003245 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3246 if (mHotseat != null) {
3247 return mHotseat.getLayout();
3248 } else {
3249 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003250 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003251 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003252 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003253 }
3254 }
3255
Winson Chungb745afb2015-03-02 11:51:23 -08003256 /**
3257 * For overridden classes.
3258 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003259 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003260 return isAppsViewVisible();
3261 }
3262
3263 public boolean isAppsViewVisible() {
3264 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3265 }
3266
3267 public boolean isWidgetsViewVisible() {
3268 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003269 }
3270
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003271 private void setWorkspaceBackground(int background) {
3272 switch (background) {
3273 case WORKSPACE_BACKGROUND_TRANSPARENT:
3274 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3275 break;
3276 case WORKSPACE_BACKGROUND_BLACK:
3277 getWindow().setBackgroundDrawable(null);
3278 break;
3279 default:
3280 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3281 }
Craig Mautner360310b2012-10-26 15:13:08 -07003282 }
3283
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003284 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003285 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3286 int curflags = getWindow().getAttributes().flags
3287 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3288 if (wpflags != curflags) {
3289 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3290 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003291 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003292 }
3293
Michael Jurkae326f182011-11-21 14:05:46 -08003294 @Override
3295 public void onTrimMemory(int level) {
3296 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003297 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3298 // The widget preview db can result in holding onto over
3299 // 3MB of memory for caching which isn't necessary.
3300 SQLiteDatabase.releaseMemory();
3301
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003302 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003303 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003304 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003305 if (mLauncherCallbacks != null) {
3306 mLauncherCallbacks.onTrimMemory(level);
3307 }
Michael Jurkae326f182011-11-21 14:05:46 -08003308 }
3309
Winson5c6bdbb2015-09-03 11:36:19 -07003310 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003311 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003312 }
3313
Winson5c6bdbb2015-09-03 11:36:19 -07003314 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003315 boolean changed = mState != State.WORKSPACE ||
3316 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003317 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003318 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003319 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003320 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003321
Michael Jurkab3e22d92011-10-31 15:58:33 -07003322 // Set focus to the AppsCustomize button
3323 if (mAllAppsButton != null) {
3324 mAllAppsButton.requestFocus();
3325 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003326 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003327
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003328 // Change the state *after* we've called all the transition code
3329 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003330
Winson Chung5fb63472011-02-02 17:03:37 -08003331 // Resume the auto-advance of widgets
3332 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003333 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003334
Winson Chung0f785722015-04-08 10:27:49 -07003335 if (changed) {
3336 // Send an accessibility event to announce the context change
3337 getWindow().getDecorView()
3338 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003339 }
Winson5c6bdbb2015-09-03 11:36:19 -07003340 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003341 }
3342
Winsone9f27272015-10-13 10:47:51 -07003343 /**
3344 * Shows the overview button.
3345 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07003346 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003347 showOverviewMode(animated, false);
3348 }
3349
3350 /**
3351 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3352 * onto one of the overview panel buttons.
3353 */
3354 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3355 Runnable postAnimRunnable = null;
3356 if (requestButtonFocus) {
3357 postAnimRunnable = new Runnable() {
3358 @Override
3359 public void run() {
3360 // Hitting the menu button when in touch mode does not trigger touch mode to
3361 // be disabled, so if requested, force focus on one of the overview panel
3362 // buttons.
3363 mOverviewPanel.requestFocusFromTouch();
3364 }
3365 };
3366 }
Adam Cohened307df2013-10-02 09:37:31 -07003367 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003368 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003369 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003370 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003371 }
3372
Winson Chungb745afb2015-03-02 11:51:23 -08003373 /**
3374 * Shows the apps view.
3375 */
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003376 public void showAppsView(boolean animated, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003377 boolean focusSearchBar) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003378 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07003379 if (updatePredictedApps) {
3380 tryAndUpdatePredictedApps();
3381 }
Winson Chung76648c52015-07-10 14:33:23 -07003382 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003383 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003384
Winson Chungb745afb2015-03-02 11:51:23 -08003385 /**
3386 * Shows the widgets view.
3387 */
3388 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003389 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003390 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003391 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003392 }
Winson Chung76648c52015-07-10 14:33:23 -07003393 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003394
3395 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003396 @Override
3397 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003398 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003399 }
3400 });
Winson Chungb745afb2015-03-02 11:51:23 -08003401 }
3402
3403 /**
3404 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003405 *
3406 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003407 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003408 // TODO: calling method should use the return value so that when {@code false} is returned
3409 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003410 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003411 if (!(mState == State.WORKSPACE ||
3412 mState == State.APPS_SPRING_LOADED ||
3413 mState == State.WIDGETS_SPRING_LOADED ||
3414 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003415 return false;
3416 }
3417 if (toState != State.APPS && toState != State.WIDGETS) {
3418 return false;
3419 }
Winson Chungb745afb2015-03-02 11:51:23 -08003420
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003421 // This is a safe and supported transition to bypass spring_loaded mode.
3422 if (mExitSpringLoadedModeRunnable != null) {
3423 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3424 mExitSpringLoadedModeRunnable = null;
3425 }
3426
Winson Chungb745afb2015-03-02 11:51:23 -08003427 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003428 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3429 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003430 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003431 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003432 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003433
Michael Jurkab3e22d92011-10-31 15:58:33 -07003434 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003435 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003436
3437 // Pause the auto-advance of widgets until we are out of AllApps
3438 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003439 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003440 closeFolder();
Tony Wickham49c8d292016-07-01 11:44:34 -07003441 closeShortcutsContainer();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003442
3443 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003444 getWindow().getDecorView()
3445 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003446 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003447 }
3448
Winson Chungcd99cd32015-04-29 11:03:24 -07003449 /**
3450 * Updates the workspace and interaction state on state change, and return the animation to this
3451 * new state.
3452 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003453 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003454 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003455 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003456 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003457 updateInteraction(fromState, toState);
3458 return anim;
3459 }
3460
Hyunyoung Song3f471442015-04-08 19:01:34 -07003461 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003462 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003463 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003464 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003465 }
Winson Chungb745afb2015-03-02 11:51:23 -08003466
Winson Chung006ee262015-08-03 14:40:11 -07003467 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003468 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003469 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003470
3471 if (isAppsViewVisible()) {
3472 mState = State.APPS_SPRING_LOADED;
3473 } else if (isWidgetsViewVisible()) {
3474 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003475 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003476 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003477 } else {
3478 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003479 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003480 }
Adam Cohen7777d962011-08-18 18:58:38 -07003481
Hyunyoung Song3f471442015-04-08 19:01:34 -07003482 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003483 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003484 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003485
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003486 if (mExitSpringLoadedModeRunnable != null) {
3487 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3488 }
3489 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003490 @Override
3491 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003492 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003493 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3494 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003495 // Before we show workspace, hide all apps again because
3496 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3497 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003498 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003499 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003500 } else {
3501 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003502 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003503 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07003504 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003505 };
3506 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003507 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003508
Tony Wickhame0c33232016-02-08 11:37:04 -08003509 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003510 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3511 || mState == State.WIDGETS_SPRING_LOADED;
3512 }
3513
Michael Jurkad3ef3062010-11-23 16:23:58 -08003514 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003515 if (mState == State.APPS_SPRING_LOADED) {
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003516 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07003517 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003518 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003519 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003520 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3521 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003522 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003523 }
3524
Winson Chung4ac30062015-05-08 17:34:17 -07003525 /**
3526 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3527 * resumed.
3528 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003529 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003530 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003531 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003532 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003533 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003534 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003535 }
3536 }
3537 }
3538
Michael Jurkab3e22d92011-10-31 15:58:33 -07003539 void lockAllApps() {
3540 // TODO
3541 }
3542
3543 void unlockAllApps() {
3544 // TODO
3545 }
3546
Winsonf768d932015-09-25 16:12:35 -07003547 public boolean launcherCallbacksProvidesSearch() {
3548 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3549 }
3550
Michael Jurkad7c28052012-04-27 15:43:36 -07003551 @Override
3552 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003553 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003554 final List<CharSequence> text = event.getText();
3555 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003556 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003557 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003558 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003559 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003560 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003561 } else if (mWorkspace != null) {
3562 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003563 } else {
3564 text.add(getString(R.string.all_apps_home_button_label));
3565 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003566 return result;
3567 }
3568
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003569 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003570 * If the activity is currently paused, signal that we need to run the passed Runnable
3571 * in onResume.
3572 *
3573 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003574 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3575 * wrong when we're not running, and if the activity comes back to what the configuration was
3576 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003577 *
3578 * Implementation of the method from LauncherModel.Callbacks.
3579 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003580 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003581 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003582 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003583 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003584 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003585 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003586 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003587 }
3588 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003589 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003590 return true;
3591 } else {
3592 return false;
3593 }
3594 }
3595
Michael Jurkac402cd92013-05-20 15:49:32 +02003596 private boolean waitUntilResume(Runnable run) {
3597 return waitUntilResume(run, false);
3598 }
3599
Michael Jurka1e2f4652013-07-08 18:03:46 -07003600 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003601 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003602 }
3603
Michael Jurka7607c2f2013-04-03 14:33:19 -07003604 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003605 * If the activity is currently paused, signal that we need to re-run the loader
3606 * in onResume.
3607 *
3608 * This needs to be called from incoming places where resources might have been loaded
3609 * while we are paused. That is becaues the Configuration might be wrong
3610 * when we're not running, and if it comes back to what it was when we
3611 * were paused, we are not restarted.
3612 *
3613 * Implementation of the method from LauncherModel.Callbacks.
3614 *
3615 * @return true if we are currently paused. The caller might be able to
3616 * skip some work in that case since we will come back again.
3617 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003618 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003619 public boolean setLoadOnResume() {
3620 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003621 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003622 mOnResumeNeedsLoad = true;
3623 return true;
3624 } else {
3625 return false;
3626 }
3627 }
3628
3629 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003630 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003631 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003632 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003633 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003634 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003635 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003636 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003637 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003638 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003639 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003640
Joe Onorato9c1289c2009-08-17 11:03:03 -04003641 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003642 * Clear any pending bind callbacks. This is called when is loader is planning to
3643 * perform a full rebind from scratch.
3644 */
3645 @Override
3646 public void clearPendingBinds() {
3647 mBindOnResumeCallbacks.clear();
3648 if (mPendingExecutor != null) {
3649 mPendingExecutor.markCompleted();
3650 mPendingExecutor = null;
3651 }
3652 }
3653
3654 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003655 * Refreshes the shortcuts shown on the workspace.
3656 *
3657 * Implementation of the method from LauncherModel.Callbacks.
3658 */
3659 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003660 if (LauncherAppState.PROFILE_STARTUP) {
3661 Trace.beginSection("Starting page bind");
3662 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003663 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003664
Winson Chungd64d1762013-08-20 14:37:16 -07003665 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003666 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003667 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003668
Michael Jurka05bf644e2011-11-30 20:28:53 -08003669 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003670 if (mHotseat != null) {
3671 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003672 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003673 if (LauncherAppState.PROFILE_STARTUP) {
3674 Trace.endSection();
3675 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003676 }
3677
Adam Cohendcd297f2013-06-18 13:13:40 -07003678 @Override
3679 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003680 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003681 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3682 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003683 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3684 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3685 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003686 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3687 // If there are no screens, we need to have an empty screen
3688 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003689 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003690 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003691
3692 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003693 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003694 if (hasCustomContentToLeft()) {
3695 mWorkspace.createCustomContentContainer();
3696 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003697 }
Winson Chung64359a52013-07-08 17:17:08 -07003698 }
3699
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003700 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003701 int count = orderedScreenIds.size();
3702 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003703 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003704 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003705 // No need to bind the first screen, as its always bound.
3706 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3707 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003708 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003709 }
3710
Winson Chungd64d1762013-08-20 14:37:16 -07003711 public void bindAppsAdded(final ArrayList<Long> newScreens,
3712 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003713 final ArrayList<ItemInfo> addAnimated,
3714 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003715 Runnable r = new Runnable() {
3716 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003717 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003718 }
3719 };
3720 if (waitUntilResume(r)) {
3721 return;
3722 }
3723
Winson Chungd64d1762013-08-20 14:37:16 -07003724 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003725 if (newScreens != null) {
3726 bindAddScreens(newScreens);
3727 }
Winson Chungd64d1762013-08-20 14:37:16 -07003728
3729 // We add the items without animation on non-visible pages, and with
3730 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003731 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003732 bindItems(addNotAnimated, 0,
3733 addNotAnimated.size(), false);
3734 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003735 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003736 bindItems(addAnimated, 0,
3737 addAnimated.size(), true);
3738 }
Winson Chungc58497e2013-09-03 17:48:37 -07003739
Winson Chung87412982013-10-03 18:34:14 -07003740 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003741 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003742
Winson Chungb745afb2015-03-02 11:51:23 -08003743 if (addedApps != null && mAppsView != null) {
3744 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003745 }
Winson Chungd64d1762013-08-20 14:37:16 -07003746 }
3747
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003748 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003749 * Bind the items start-end from the list.
3750 *
3751 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003752 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003753 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003754 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3755 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003756 Runnable r = new Runnable() {
3757 public void run() {
3758 bindItems(shortcuts, start, end, forceAnimateIcons);
3759 }
3760 };
3761 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003762 return;
3763 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003764
Winson Chungf0c6ae02012-03-21 16:10:31 -07003765 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003766 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3767 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003768 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003769 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003770 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003771 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003772 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003773
3774 // Short circuit if we are loading dock items for a configuration which has no dock
3775 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3776 mHotseat == null) {
3777 continue;
3778 }
3779
Sunny Goyal639e9062015-08-19 19:17:06 -07003780 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003781 switch (item.itemType) {
3782 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3783 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003784 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003785 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003786 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003787 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003788 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003789 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003790 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003791 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003792 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003793 default:
3794 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003795 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003796
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003797 /*
3798 * Remove colliding items.
3799 */
3800 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3801 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3802 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3803 View v = cl.getChildAt(item.cellX, item.cellY);
3804 Object tag = v.getTag();
3805 String desc = "Collision while binding workspace item: " + item
3806 + ". Collides with " + tag;
3807 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3808 throw (new RuntimeException(desc));
3809 } else {
3810 Log.d(TAG, desc);
3811 LauncherModel.deleteItemFromDatabase(this, item);
3812 continue;
3813 }
3814 }
3815 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003816 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3817 item.cellY, 1, 1);
3818 if (animateIcons) {
3819 // Animate all the applications up now
3820 view.setAlpha(0f);
3821 view.setScaleX(0f);
3822 view.setScaleY(0f);
3823 bounceAnims.add(createNewAppBounceAnimation(view, i));
3824 newShortcutsScreenId = item.screenId;
3825 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003826 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003827
Winson Chung997a9232013-07-24 15:33:46 -07003828 if (animateIcons) {
3829 // Animate to the correct page
3830 if (newShortcutsScreenId > -1) {
3831 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003832 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003833 final Runnable startBounceAnimRunnable = new Runnable() {
3834 public void run() {
3835 anim.playTogether(bounceAnims);
3836 anim.start();
3837 }
3838 };
Winson Chung997a9232013-07-24 15:33:46 -07003839 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003840 // We post the animation slightly delayed to prevent slowdowns
3841 // when we are loading right after we return to launcher.
3842 mWorkspace.postDelayed(new Runnable() {
3843 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003844 if (mWorkspace != null) {
3845 mWorkspace.snapToPage(newScreenIndex);
3846 mWorkspace.postDelayed(startBounceAnimRunnable,
3847 NEW_APPS_ANIMATION_DELAY);
3848 }
Winson Chung94d67682013-09-25 16:29:40 -07003849 }
3850 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003851 } else {
3852 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003853 }
3854 }
Winson Chung64359a52013-07-08 17:17:08 -07003855 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003856 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003857 }
3858
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003859 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3860 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3861 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003862 view.updateAppWidget(null);
3863 view.setOnClickListener(this);
3864 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003865 mWorkspace.requestLayout();
3866 }
3867
Joe Onorato9c1289c2009-08-17 11:03:03 -04003868 /**
3869 * Add the views for a widget to the workspace.
3870 *
3871 * Implementation of the method from LauncherModel.Callbacks.
3872 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003873 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003874 Runnable r = new Runnable() {
3875 public void run() {
3876 bindAppWidget(item);
3877 }
3878 };
3879 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003880 return;
3881 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003882
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003883 if (mIsSafeModeEnabled) {
3884 bindSafeModeWidget(item);
3885 return;
3886 }
3887
Daniel Sandler843e8602010-06-07 14:59:01 -04003888 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3889 if (DEBUG_WIDGETS) {
3890 Log.d(TAG, "bindAppWidget: " + item);
3891 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003892
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003893 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003894
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003895 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3896 // If the provider is not ready, bind as a pending widget.
3897 appWidgetInfo = null;
3898 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3899 // The widget id is not valid. Try to find the widget based on the provider info.
3900 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3901 } else {
3902 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3903 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003904
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003905 // If the provider is ready, but the width is not yet restored, try to restore it.
3906 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3907 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003908 if (appWidgetInfo == null) {
3909 if (DEBUG_WIDGETS) {
3910 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3911 + " belongs to component " + item.providerName
3912 + ", as the povider is null");
3913 }
3914 LauncherModel.deleteItemFromDatabase(this, item);
3915 return;
3916 }
Sunny Goyalff572272014-07-23 13:58:07 -07003917
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003918 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003919 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003920 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3921 // Id has not been allocated yet. Allocate a new id.
3922 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3923 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003924
Sunny Goyald478c832016-04-01 12:04:16 -07003925 // Also try to bind the widget. If the bind fails, the user will be shown
3926 // a click to setup UI, which will ask for the bind permission.
3927 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3928 pendingInfo.spanX = item.spanX;
3929 pendingInfo.spanY = item.spanY;
3930 pendingInfo.minSpanX = item.minSpanX;
3931 pendingInfo.minSpanY = item.minSpanY;
3932 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
3933 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3934 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003935
Sunny Goyald478c832016-04-01 12:04:16 -07003936 // Bind succeeded
3937 if (success) {
3938 // If the widget has a configure activity, it is still needs to set it up,
3939 // otherwise the widget is ready to go.
3940 item.restoreStatus = (appWidgetInfo.configure == null)
3941 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3942 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003943 }
Sunny Goyald478c832016-04-01 12:04:16 -07003944
3945 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003946 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003947 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003948 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003949 // The widget was marked as UI not ready, but there is no configure activity to
3950 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003951 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3952 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003953 }
Sunny Goyalff572272014-07-23 13:58:07 -07003954 }
3955
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003956 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003957 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003958 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3959 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003960 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003961
Sunny Goyal56c73602015-09-25 12:55:01 -07003962 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003963 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003964 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003965 deleteWidgetInfo(item);
3966 return;
3967 }
3968
Sunny Goyal233ee962015-08-03 13:05:01 -07003969 item.minSpanX = appWidgetInfo.minSpanX;
3970 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003971 addAppWidgetToWorkspace(
3972 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3973 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003974 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003975 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003976 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003977 view.updateAppWidget(null);
3978 view.setOnClickListener(this);
3979 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003980 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003981 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003982
Daniel Sandler843e8602010-06-07 14:59:01 -04003983 if (DEBUG_WIDGETS) {
3984 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3985 + (SystemClock.uptimeMillis()-start) + "ms");
3986 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003987 }
3988
Sunny Goyalff572272014-07-23 13:58:07 -07003989 /**
3990 * Restores a pending widget.
3991 *
3992 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003993 */
Sunny Goyald478c832016-04-01 12:04:16 -07003994 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003995 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3996 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3997 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003998 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003999 }
4000
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004001 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07004002 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07004003
4004 mWorkspace.reinflateWidgetsIfNecessary();
4005 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07004006 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07004007 }
4008
Adam Cohen1462de32012-07-24 22:34:36 -07004009 public void onPageBoundSynchronously(int page) {
4010 mSynchronouslyBoundPages.add(page);
4011 }
4012
Sunny Goyal527c7d32015-08-28 15:19:36 -07004013 @Override
4014 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
4015 if (mPendingExecutor != null) {
4016 mPendingExecutor.markCompleted();
4017 }
4018 mPendingExecutor = executor;
4019 executor.attachTo(this);
4020 }
4021
4022 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4023 if (mPendingExecutor == executor) {
4024 mPendingExecutor = null;
4025 }
4026 }
4027
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07004028 @Override
4029 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
4030 Runnable r = new Runnable() {
4031 public void run() {
4032 finishFirstPageBind(executor);
4033 }
4034 };
4035 if (waitUntilResume(r)) {
4036 return;
4037 }
4038
4039 Runnable onComplete = new Runnable() {
4040 @Override
4041 public void run() {
4042 if (executor != null) {
4043 executor.onLoadAnimationCompleted();
4044 }
4045 }
4046 };
4047 if (mDragLayer.getAlpha() < 1) {
4048 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
4049 } else {
4050 onComplete.run();
4051 }
4052 }
4053
Joe Onorato9c1289c2009-08-17 11:03:03 -04004054 /**
4055 * Callback saying that there aren't any more items to bind.
4056 *
4057 * Implementation of the method from LauncherModel.Callbacks.
4058 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004059 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004060 Runnable r = new Runnable() {
4061 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004062 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004063 }
4064 };
4065 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004066 return;
4067 }
Sunny Goyale26d1002016-06-20 14:52:14 -07004068 if (LauncherAppState.PROFILE_STARTUP) {
4069 Trace.beginSection("Page bind completed");
4070 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004071 if (mSavedState != null) {
4072 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004073 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004074 }
Sunny Goyal08442b82015-10-21 17:15:08 -07004075
Joe Onorato9c1289c2009-08-17 11:03:03 -04004076 mSavedState = null;
4077 }
4078
Adam Cohen1462de32012-07-24 22:34:36 -07004079 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004080
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004081 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07004082
4083 // If we received the result of any pending adds while the loader was running (e.g. the
4084 // widget configuration forced an orientation change), process them now.
4085 if (sPendingAddItem != null) {
4086 final long screenId = completeAdd(sPendingAddItem);
4087
4088 // TODO: this moves the user to the page where the pending item was added. Ideally,
4089 // the screen would be guaranteed to exist after bind, and the page would be set through
4090 // the workspace restore process.
4091 mWorkspace.post(new Runnable() {
4092 @Override
4093 public void run() {
4094 mWorkspace.snapToScreenId(screenId);
4095 }
4096 });
4097 sPendingAddItem = null;
4098 }
4099
Sunny Goyal756adbc2015-04-16 15:20:51 -07004100 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004101
4102 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004103 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004104 }
Sunny Goyale26d1002016-06-20 14:52:14 -07004105 if (LauncherAppState.PROFILE_STARTUP) {
4106 Trace.endSection();
4107 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004108 }
4109
Winson Chunga2413752012-04-03 14:22:34 -07004110 private boolean canRunNewAppsAnimation() {
4111 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07004112 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07004113 }
4114
Winson Chungc9168342013-06-26 14:54:55 -07004115 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004116 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004117 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4118 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004119 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004120 return bounceAnim;
4121 }
4122
Adam Cohen27772732013-11-11 14:00:56 +00004123 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004124 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004125 }
4126
Tony Wickham55616cd2015-09-23 14:55:17 -07004127 public int getSearchBarHeight() {
4128 if (mLauncherCallbacks != null) {
4129 return mLauncherCallbacks.getSearchBarHeight();
4130 }
4131 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4132 }
4133
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004134 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004135 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4136 * multiple calls to bind the same list.)
4137 */
4138 @Thunk ArrayList<AppInfo> mTmpAppsList;
4139 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4140 public void run() {
4141 bindAllApplications(mTmpAppsList);
4142 mTmpAppsList = null;
4143 }
4144 };
4145
4146 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004147 * Add the icons for all apps.
4148 *
4149 * Implementation of the method from LauncherModel.Callbacks.
4150 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004151 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004152 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4153 mTmpAppsList = apps;
4154 return;
4155 }
4156
Winson Chungb745afb2015-03-02 11:51:23 -08004157 if (mAppsView != null) {
4158 mAppsView.setApps(apps);
4159 }
Adam Cohen9211d422014-10-07 18:14:53 -07004160 if (mLauncherCallbacks != null) {
4161 mLauncherCallbacks.bindAllApplications(apps);
4162 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004163 }
4164
4165 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004166 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
4167 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
4168 */
4169 @Override
4170 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
4171 mDeepShortcutMap = deepShortcutMapCopy;
4172 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
4173 }
4174
Tony Wickham1bce7fd2016-04-28 17:39:03 -07004175 public List<String> getShortcutIdsForItem(ItemInfo info) {
4176 if (!DeepShortcutManager.supportsShortcuts(info)) {
4177 return Collections.EMPTY_LIST;
4178 }
4179 ComponentName component = info.getTargetComponent();
4180 List<String> ids = mDeepShortcutMap.get(new ComponentKey(component, info.user));
4181 return ids == null ? Collections.EMPTY_LIST : ids;
4182 }
4183
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004184 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004185 * A package was updated.
4186 *
4187 * Implementation of the method from LauncherModel.Callbacks.
4188 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004189 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004190 Runnable r = new Runnable() {
4191 public void run() {
4192 bindAppsUpdated(apps);
4193 }
4194 };
4195 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004196 return;
4197 }
4198
Winson Chungb745afb2015-03-02 11:51:23 -08004199 if (mAppsView != null) {
4200 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004201 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004202 }
4203
Sunny Goyal4390ace2014-10-13 11:33:11 -07004204 @Override
4205 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4206 Runnable r = new Runnable() {
4207 public void run() {
4208 bindWidgetsRestored(widgets);
4209 }
4210 };
4211 if (waitUntilResume(r)) {
4212 return;
4213 }
4214 mWorkspace.widgetsRestored(widgets);
4215 }
4216
Joe Onorato9c1289c2009-08-17 11:03:03 -04004217 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004218 * Some shortcuts were updated in the background.
4219 *
4220 * Implementation of the method from LauncherModel.Callbacks.
4221 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004222 @Override
4223 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4224 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004225 Runnable r = new Runnable() {
4226 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004227 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004228 }
4229 };
4230 if (waitUntilResume(r)) {
4231 return;
4232 }
4233
Sunny Goyal4390ace2014-10-13 11:33:11 -07004234 if (!updated.isEmpty()) {
4235 mWorkspace.updateShortcuts(updated);
4236 }
4237
4238 if (!removed.isEmpty()) {
4239 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4240 for (ShortcutInfo si : removed) {
4241 removedComponents.add(si.getTargetComponent());
4242 }
4243 mWorkspace.removeItemsByComponentName(removedComponents, user);
4244 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004245 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004246 }
4247 }
4248
4249 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004250 * Update the state of a package, typically related to install state.
4251 *
4252 * Implementation of the method from LauncherModel.Callbacks.
4253 */
Sunny Goyale755d462014-07-22 13:48:29 -07004254 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004255 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4256 Runnable r = new Runnable() {
4257 public void run() {
4258 bindRestoreItemsChange(updates);
4259 }
4260 };
4261 if (waitUntilResume(r)) {
4262 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004263 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004264
Sunny Goyal756adbc2015-04-16 15:20:51 -07004265 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004266 }
4267
4268 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004269 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004270 * in addition to specific applications to remove, the reason being that
4271 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004272 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004273 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004274 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004275 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004276 public void bindWorkspaceComponentsRemoved(
4277 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4278 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004279 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004280 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004281 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004282 }
Winson Chungd64d1762013-08-20 14:37:16 -07004283 };
4284 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004285 return;
4286 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004287 if (!packageNames.isEmpty()) {
4288 mWorkspace.removeItemsByPackageName(packageNames, user);
4289 }
4290 if (!components.isEmpty()) {
4291 mWorkspace.removeItemsByComponentName(components, user);
4292 }
4293 // Notify the drag controller
4294 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004295
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004296 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004297
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004298 @Override
4299 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4300 Runnable r = new Runnable() {
4301 public void run() {
4302 bindAppInfosRemoved(appInfos);
4303 }
4304 };
4305 if (waitUntilResume(r)) {
4306 return;
Joe Onorato36115782010-06-17 13:28:48 -04004307 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004308
Winson Chungdf95eb12013-10-16 14:57:07 -07004309 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004310 if (mAppsView != null) {
4311 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004312 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004313 }
Joe Onoratobe386092009-11-17 17:32:16 -08004314
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004315 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004316 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004317 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004318 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004319 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004320
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004321 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004322 public void bindWidgetsModel(WidgetsModel model) {
4323 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004324 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004325 return;
4326 }
4327
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004328 if (mWidgetsView != null && model != null) {
4329 mWidgetsView.addWidgets(model);
4330 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004331 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004332 }
4333
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004334 @Override
4335 public void notifyWidgetProvidersChanged() {
4336 if (mWorkspace.getState().shouldUpdateWidget) {
4337 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4338 }
4339 }
4340
Winson Chung400438b2011-01-16 17:53:48 -08004341 private int mapConfigurationOriActivityInfoOri(int configOri) {
4342 final Display d = getWindowManager().getDefaultDisplay();
4343 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4344 switch (d.getRotation()) {
4345 case Surface.ROTATION_0:
4346 case Surface.ROTATION_180:
4347 // We are currently in the same basic orientation as the natural orientation
4348 naturalOri = configOri;
4349 break;
4350 case Surface.ROTATION_90:
4351 case Surface.ROTATION_270:
4352 // We are currently in the other basic orientation to the natural orientation
4353 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4354 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4355 break;
4356 }
4357
4358 int[] oriMap = {
4359 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4360 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4361 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4362 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4363 };
4364 // Since the map starts at portrait, we need to offset if this device's natural orientation
4365 // is landscape.
4366 int indexOffset = 0;
4367 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4368 indexOffset = 1;
4369 }
4370 return oriMap[(d.getRotation() + indexOffset) % 4];
4371 }
Adam Cohen446e9402011-09-15 18:21:21 -07004372
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004373 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004374 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004375 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004376 if (Utilities.ATLEAST_JB_MR2) {
4377 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4378 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004379 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4380 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004381 }
Winson Chung4b919f82012-05-01 10:44:08 -07004382 }
4383 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004384
Winson Chung4b919f82012-05-01 10:44:08 -07004385 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004386 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004387 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004388 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004389 } else {
4390 mHandler.postDelayed(new Runnable() {
4391 public void run() {
4392 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4393 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004394 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004395 }
Winson Chung4b919f82012-05-01 10:44:08 -07004396 }
Winson Chung400438b2011-01-16 17:53:48 -08004397 }
4398
Adam Cohenea90f832014-05-21 15:03:34 -07004399 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004400 * To be overridden by subclasses to indicate that there is an activity to launch
4401 * before showing the standard launcher experience.
4402 */
4403 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004404 if (mLauncherCallbacks != null) {
4405 return mLauncherCallbacks.hasFirstRunActivity();
4406 }
Adam Cohen432609a2014-03-13 17:03:22 -07004407 return false;
4408 }
4409
4410 /**
4411 * To be overridden by subclasses to launch any first run activity
4412 */
4413 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004414 if (mLauncherCallbacks != null) {
4415 return mLauncherCallbacks.getFirstRunActivity();
4416 }
Adam Cohen432609a2014-03-13 17:03:22 -07004417 return null;
4418 }
4419
Winson Chunga6945242014-01-08 14:04:34 -08004420 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004421 return !ActivityManager.isRunningInTestHarness() &&
4422 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004423 }
4424
Adam Cohen432609a2014-03-13 17:03:22 -07004425 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004426 if (shouldRunFirstRunActivity() &&
4427 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004428 Intent firstRunIntent = getFirstRunActivity();
4429 if (firstRunIntent != null) {
4430 startActivity(firstRunIntent);
4431 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004432 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004433 }
4434 }
Adam Cohen432609a2014-03-13 17:03:22 -07004435 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004436 }
4437
4438 private void markFirstRunActivityShown() {
4439 SharedPreferences.Editor editor = mSharedPrefs.edit();
4440 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4441 editor.apply();
4442 }
4443
Hyunyoung Songc001cf52016-07-21 17:32:43 -07004444 private void markAppsViewShown() {
4445 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4446 return;
4447 }
4448 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
4449 }
4450
4451 private boolean shouldShowDiscoveryBounce() {
4452 if (mState != mState.WORKSPACE) {
4453 return false;
4454 }
4455 if (mLauncherCallbacks != null && mLauncherCallbacks.shouldShowDiscoveryBounce()) {
4456 return true;
4457 }
4458 if (!mIsResumeFromActionScreenOff) {
4459 return false;
4460 }
4461 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4462 return false;
4463 }
4464 return true;
4465 }
4466
Adam Cohen432609a2014-03-13 17:03:22 -07004467 /**
4468 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4469 * screen that must be displayed and dismissed.
4470 */
4471 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004472 if (mLauncherCallbacks != null) {
4473 return mLauncherCallbacks.hasDismissableIntroScreen();
4474 }
Adam Cohen432609a2014-03-13 17:03:22 -07004475 return false;
4476 }
4477
4478 /**
4479 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4480 */
4481 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004482 if (mLauncherCallbacks != null) {
4483 return mLauncherCallbacks.getIntroScreen();
4484 }
Adam Cohen432609a2014-03-13 17:03:22 -07004485 return null;
4486 }
4487
4488 /**
4489 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4490 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4491 */
4492 private boolean shouldShowIntroScreen() {
4493 return hasDismissableIntroScreen() &&
4494 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4495 }
4496
4497 protected void showIntroScreen() {
4498 View introScreen = getIntroScreen();
4499 changeWallpaperVisiblity(false);
4500 if (introScreen != null) {
4501 mDragLayer.showOverlayView(introScreen);
4502 }
4503 }
4504
Winson Chung5ffd51d2015-06-25 11:36:50 -07004505 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004506 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004507 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004508 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004509 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004510 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004511 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4512 if (userHandle != null) {
4513 user = UserHandleCompat.fromUser(userHandle);
4514 }
4515 }
4516 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004517 }
4518
4519 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004520 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004521 if (user == null) {
4522 user = UserHandleCompat.myUserHandle();
4523 }
4524
4525 // Called from search suggestion, add the profile extra to the intent to ensure that we
4526 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004527 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004528 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004529 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004530 return null;
4531 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004532 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004533 }
4534
Winson Chung5ffd51d2015-06-25 11:36:50 -07004535 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004536 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4537 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004538 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004539 UserHandleCompat.myUserHandle());
4540 }
4541
Winson Chung5ffd51d2015-06-25 11:36:50 -07004542 protected void moveWorkspaceToDefaultScreen() {
4543 mWorkspace.moveToDefaultScreen(false);
4544 }
4545
Winson Chung80baf5a2010-08-09 16:03:15 -07004546 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004547 * Returns a FastBitmapDrawable with the icon, accurately sized.
4548 */
4549 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4550 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4551 d.setFilterBitmap(true);
4552 resizeIconDrawable(d);
4553 return d;
4554 }
4555
4556 /**
4557 * Resizes an icon drawable to the correct icon size.
4558 */
Winson5fbe0742015-09-30 15:33:00 -07004559 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004560 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004561 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004562 }
4563
4564 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004565 * Prints out out state for debugging.
4566 */
4567 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004568 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004569 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004570 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4571 Log.d(TAG, "mRestoring=" + mRestoring);
4572 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004573 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004574 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004575
Daniel Sandler325dc232013-06-05 22:57:57 -04004576 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004577 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004578
4579 @Override
4580 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4581 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004582 // Dump workspace
4583 writer.println(prefix + "Workspace Items");
4584 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4585 writer.println(prefix + " Homescreen " + i);
4586
4587 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4588 for (int j = 0; j < layout.getChildCount(); j++) {
4589 Object tag = layout.getChildAt(j).getTag();
4590 if (tag != null) {
4591 writer.println(prefix + " " + tag.toString());
4592 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004593 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004594 }
Sunny Goyala1365452015-10-01 15:46:24 -07004595
4596 writer.println(prefix + " Hotseat");
4597 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4598 for (int j = 0; j < layout.getChildCount(); j++) {
4599 Object tag = layout.getChildAt(j).getTag();
4600 if (tag != null) {
4601 writer.println(prefix + " " + tag.toString());
4602 }
4603 }
4604
Sunny Goyal713edfc2016-05-06 09:58:34 -07004605 try {
4606 FileLog.flushAll(writer);
4607 } catch (Exception e) {
4608 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004609 }
4610
Adam Cohen9211d422014-10-07 18:14:53 -07004611 if (mLauncherCallbacks != null) {
4612 mLauncherCallbacks.dump(prefix, fd, writer, args);
4613 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004614 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004615
Adam Cohen59400422014-03-05 18:07:04 -08004616 public static CustomAppWidget getCustomAppWidget(String name) {
4617 return sCustomAppWidgets.get(name);
4618 }
4619
4620 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4621 return sCustomAppWidgets;
4622 }
4623
Sunny Goyal29538332016-02-18 09:10:19 -08004624 public static List<View> getFolderContents(View icon) {
4625 if (icon instanceof FolderIcon) {
4626 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4627 } else {
4628 return Collections.EMPTY_LIST;
4629 }
4630 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004631
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004632 public static Launcher getLauncher(Context context) {
4633 if (context instanceof Launcher) {
4634 return (Launcher) context;
4635 }
4636 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4637 }
4638
Sunny Goyal745bad92016-05-02 10:54:12 -07004639 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4640
4641 @Override
4642 public void onSharedPreferenceChanged(
4643 SharedPreferences sharedPreferences, String key) {
4644 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4645 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4646 if (!waitUntilResume(this, true)) {
4647 run();
4648 }
4649 }
4650 }
4651
4652 @Override
4653 public void run() {
4654 setOrientation();
4655 }
4656 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004657}