blob: 5f5ac2e3c0d4b410ee7c4032e51cbc1014ea9510 [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
Adam Cohen091440a2015-03-18 14:16:05 -0700351 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700352 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700353 if (mWorkspace != null) {
354 mWorkspace.buildPageHardwareLayers();
355 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700356 }
357 };
358
Adam Cohendb364c32014-05-20 14:23:40 -0700359 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800360
Adam Cohen091440a2015-03-18 14:16:05 -0700361 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800362 int requestCode;
363 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700364 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700365 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800366 int cellX;
367 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700368 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800369 }
370
Hyunyoung Songaa953652016-04-19 18:30:24 -0700371 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800372
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700373 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700374 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400375
376 @Thunk void setOrientation() {
377 if (mRotationEnabled) {
378 unlockScreenOrientation(true);
379 } else {
380 setRequestedOrientation(
381 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700382 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400383 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700384
Sunny Goyal745bad92016-05-02 10:54:12 -0700385 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700386
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 @Override
388 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700389 if (DEBUG_STRICT_MODE) {
390 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
391 .detectDiskReads()
392 .detectDiskWrites()
393 .detectNetwork() // or .detectAll() for all detectable problems
394 .penaltyLog()
395 .build());
396 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
397 .detectLeakedSqlLiteObjects()
398 .detectLeakedClosableObjects()
399 .penaltyLog()
400 .penaltyDeath()
401 .build());
402 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700403 if (LauncherAppState.PROFILE_STARTUP) {
404 Trace.beginSection("Launcher-onCreate");
405 }
Winson Chunga2413752012-04-03 14:22:34 -0700406
Adam Cohen9211d422014-10-07 18:14:53 -0700407 if (mLauncherCallbacks != null) {
408 mLauncherCallbacks.preOnCreate();
409 }
410
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400412
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400413 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700414
Adam Cohen2e6da152015-05-06 11:42:25 -0700415 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700416 mDeviceProfile = getResources().getConfiguration().orientation
417 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700418 app.getInvariantDeviceProfile().landscapeProfile
419 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700420
Sunny Goyalf7258242015-10-19 16:59:07 -0700421 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700422 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800423 mModel = app.setLauncher(this);
424 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700425 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700426
Joe Onorato41a12d22009-10-31 18:30:00 -0400427 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700428 mAllAppsController = new AllAppsTransitionController(this);
429 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700430
Sunny Goyalffe83f12014-08-14 17:39:34 -0700431 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700432
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700433 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
434 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700435
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700436 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
437 // this also ensures that any synchronous binding below doesn't re-trigger another
438 // LauncherModel load.
439 mPaused = false;
440
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700442
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 setupViews();
Winson1f064272016-07-18 17:18:02 -0700444 mDeviceProfile.layout(this, false /* notifyListeners */);
Tony Wickham827cef22016-03-17 15:39:39 -0700445 mExtractedColors = new ExtractedColors();
446 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447
Tony Wickhame2217252016-03-22 16:34:23 -0700448 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
449 .addAccessibilityStateChangeListener(this);
450
Joe Onorato7c312c12009-08-13 21:36:53 -0700451 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700452
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 mSavedState = savedInstanceState;
454 restoreState(mSavedState);
455
Sunny Goyale26d1002016-06-20 14:52:14 -0700456 if (LauncherAppState.PROFILE_STARTUP) {
457 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458 }
459
460 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700461 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700462 // If the user leaves launcher, then we should just load items asynchronously when
463 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700464 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700465 } else {
466 // We only load the page synchronously if the user rotates (or triggers a
467 // configuration change) while launcher is in the foreground
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700468 if (!mModel.startLoader(mWorkspace.getRestorePage())) {
469 // If we are not binding synchronously, show a fade in animation when
470 // the first page bind completes.
471 mDragLayer.setAlpha(0);
472 }
Winson Chung4a2afa32012-07-19 14:53:05 -0700473 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474 }
475
476 // For handling default keys
477 mDefaultKeySsb = new SpannableStringBuilder();
478 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800479
480 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700481 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
482 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800483
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800484 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700485 // In case we are on a device with locked rotation, we should look at preferences to check
486 // if the user has specifically allowed rotation.
487 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700488 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700489 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
490 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700491 }
492
493 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
494 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400495 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700496
Adam Cohen9211d422014-10-07 18:14:53 -0700497 if (mLauncherCallbacks != null) {
498 mLauncherCallbacks.onCreate(savedInstanceState);
499 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700500
501 if (shouldShowIntroScreen()) {
502 showIntroScreen();
503 } else {
504 showFirstRunActivity();
Adam Cohenc3e12c72014-10-31 16:15:36 -0700505 }
Adam Cohen9211d422014-10-07 18:14:53 -0700506 }
507
Sunny Goyal7779d622015-06-11 16:18:39 -0700508 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700509 public void onExtractedColorsChanged() {
510 loadExtractedColorsAndColorItems();
511 }
512
513 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700514 // TODO: do this in pre-N as well, once the extraction part is complete.
515 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700516 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700517 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700518 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham827cef22016-03-17 15:39:39 -0700519 }
520 }
521
Adam Cohen9211d422014-10-07 18:14:53 -0700522 private LauncherCallbacks mLauncherCallbacks;
523
524 public void onPostCreate(Bundle savedInstanceState) {
525 super.onPostCreate(savedInstanceState);
526 if (mLauncherCallbacks != null) {
527 mLauncherCallbacks.onPostCreate(savedInstanceState);
528 }
529 }
530
Winson1f064272016-07-18 17:18:02 -0700531 public void onInsetsChanged(Rect insets) {
532 mDeviceProfile.updateInsets(insets);
533 mDeviceProfile.layout(this, true /* notifyListeners */);
534 }
535
Sunny Goyal32554d12015-12-03 15:31:25 -0800536 /**
537 * Call this after onCreate to set or clear overlay.
538 */
539 public void setLauncherOverlay(LauncherOverlay overlay) {
540 if (overlay != null) {
541 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
542 }
543 mWorkspace.setLauncherOverlay(overlay);
544 }
545
Adam Cohen9211d422014-10-07 18:14:53 -0700546 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
547 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700548 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700549 private boolean mWorkspaceImportanceStored = false;
550 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700551 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800552 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700553 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
554
555 @Override
556 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700557 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700558 return;
559 }
560 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700561 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700562 if (mWorkspace != null) {
563 mWorkspaceImportanceForAccessibility =
564 mWorkspace.getImportantForAccessibility();
565 mWorkspace.setImportantForAccessibility(
566 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
567 mWorkspaceImportanceStored = true;
568 }
569 if (mHotseat != null) {
570 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
571 mHotseat.setImportantForAccessibility(
572 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
573 mHotseatImportanceStored = true;
574 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700575 }
576
577 @Override
578 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700579 if (mWorkspaceImportanceStored && mWorkspace != null) {
580 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
581 }
582 if (mHotseatImportanceStored && mHotseat != null) {
583 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
584 }
585 mWorkspaceImportanceStored = false;
586 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700587 }
588 });
Adam Cohen9211d422014-10-07 18:14:53 -0700589 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700590 }
591
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700592 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700593 public void onLauncherProviderChange() {
594 if (mLauncherCallbacks != null) {
595 mLauncherCallbacks.onLauncherProviderChange();
596 }
597 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700598
Adam Cohen9211d422014-10-07 18:14:53 -0700599 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700600 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700601 if (mLauncherCallbacks != null) {
602 return mLauncherCallbacks.hasCustomContentToLeft();
603 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700604 return false;
605 }
606
Dave Hawkeya8881582013-09-17 15:55:33 -0600607 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500608 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600609 * {@link #addToCustomContentPage}. This will only be invoked if
610 * {@link #hasCustomContentToLeft()} is {@code true}.
611 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500612 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700613 if (mLauncherCallbacks != null) {
614 mLauncherCallbacks.populateCustomContentContainer();
615 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600616 }
617
618 /**
619 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
620 * ensure the custom content page is added or removed if necessary.
621 */
622 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600623 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600624 // Not bound yet, wait for bindScreens to be called.
625 return;
626 }
627
628 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
629 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500630 mWorkspace.createCustomContentContainer();
631 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600632 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
633 mWorkspace.removeCustomContentPage();
634 }
635 }
636
Hyunyoung Songaa953652016-04-19 18:30:24 -0700637 public UserEventDispatcher getUserEventDispatcher() {
638 if (mLauncherCallbacks != null) {
639 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
640 if (dispatcher != null) {
641 return dispatcher;
642 }
643 }
644
Sunny Goyal64976d52016-06-20 14:56:28 -0700645 // Logger object is a singleton and does not have to be coupled with the foreground
646 // activity. Since most user event logging is done on the UI, the object is retrieved
647 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700648 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700649 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700650 }
651 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800652 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100653
Hyunyoung Song3f471442015-04-08 19:01:34 -0700654 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700655 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
656 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700657 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700658 }
659
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000660 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700661 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
662 // This cast is safe as long as the id < 0x00FFFFFF
663 // Since we jail all the dynamically generated views, there should be no clashes
664 // with any other views.
665 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000666 }
667
668 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700669 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
670 * a configuration step, this allows the proper animations to run after other transitions.
671 */
Adam Cohendb364c32014-05-20 14:23:40 -0700672 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700673 long screenId = args.screenId;
674 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
675 // When the screen id represents an actual screen (as opposed to a rank) we make sure
676 // that the drop page actually exists.
677 screenId = ensurePendingDropLayoutExists(args.screenId);
678 }
Adam Cohendb364c32014-05-20 14:23:40 -0700679
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800680 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800681 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700682 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700683 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700684 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800685 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700686 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700687 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700688 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyald478c832016-04-01 12:04:16 -0700689 completeRestoreAppWidget(args.appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700690 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700691 case REQUEST_BIND_PENDING_APPWIDGET: {
692 int widgetId = args.appWidgetId;
693 LauncherAppWidgetInfo info =
694 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
695 if (info != null) {
696 // Since the view was just bound, also launch the configure activity if needed
697 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
698 .getLauncherAppWidgetInfo(widgetId);
699 if (provider != null && provider.configure != null) {
700 startRestoredWidgetReconfigActivity(provider, info);
701 }
702 }
703 break;
704 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800705 }
Michael Jurka27614d22012-04-02 06:40:22 -0700706 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
707 // if you turned the screen off and then back while in All Apps, Launcher would not
708 // return to the workspace. Clearing mAddInfo.container here fixes this issue
709 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700710 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800711 }
712
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800713 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700714 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700715 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700716 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700717 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800718 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700719
Adam Cohenad4e15c2013-10-17 16:21:35 -0700720 Runnable exitSpringLoaded = new Runnable() {
721 @Override
722 public void run() {
723 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
724 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
725 }
726 };
727
Michael Jurka8b805b12012-04-18 14:23:14 -0700728 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700729 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700730 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700731 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
732 if (resultCode == RESULT_CANCELED) {
733 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700734 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700735 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700736 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800737 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700738 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700739 }
740 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200741 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
742 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700743 // User could have free-scrolled between pages before picking a wallpaper; make sure
744 // we move to the closest one now.
745 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700746 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200747 }
748 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700749 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200750
Adam Cohened66b2b2012-01-23 17:28:51 -0800751 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700752 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700753
Adam Cohendb364c32014-05-20 14:23:40 -0700754 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800755 // We have special handling for widgets
756 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800757 final int appWidgetId;
758 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
759 : -1;
760 if (widgetId < 0) {
761 appWidgetId = pendingAddWidgetId;
762 } else {
763 appWidgetId = widgetId;
764 }
765
Adam Cohenad4e15c2013-10-17 16:21:35 -0700766 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800767 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700768 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
769 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700770 result = RESULT_CANCELED;
771 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700772 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700773 @Override
774 public void run() {
775 exitSpringLoadedDragModeDelayed(false, 0, null);
776 }
777 };
Adam Cohendb364c32014-05-20 14:23:40 -0700778 if (workspaceLocked) {
779 // No need to remove the empty screen if we're mid-binding, as the
780 // the bind will not add the empty screen.
781 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
782 } else {
783 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
784 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
785 }
Winson Chung5aaab772012-04-27 15:24:02 -0700786 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700787 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700788 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
789 // When the screen id represents an actual screen (as opposed to a rank)
790 // we make sure that the drop page actually exists.
791 mPendingAddInfo.screenId =
792 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
793 }
Adam Cohendb364c32014-05-20 14:23:40 -0700794 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
795
796 dropLayout.setDropPending(true);
797 final Runnable onComplete = new Runnable() {
798 @Override
799 public void run() {
800 completeTwoStageWidgetDrop(resultCode, appWidgetId);
801 dropLayout.setDropPending(false);
802 }
803 };
804 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
805 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
806 } else {
807 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
808 mPendingAddInfo);
809 sPendingAddItem = args;
810 }
Winson Chung5aaab772012-04-27 15:24:02 -0700811 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800812 return;
813 }
814
Sunny Goyald478c832016-04-01 12:04:16 -0700815 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
816 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700817 if (resultCode == RESULT_OK) {
818 // Update the widget view.
819 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
820 pendingAddWidgetId, mPendingAddInfo);
821 if (workspaceLocked) {
822 sPendingAddItem = args;
823 } else {
824 completeAdd(args);
825 }
826 }
827 // Leave the widget in the pending state if the user canceled the configure.
828 return;
829 }
830
Sunny Goyalaad90582015-07-09 14:22:50 -0700831 if (requestCode == REQUEST_CREATE_SHORTCUT) {
832 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
833 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
834 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
835 mPendingAddInfo);
836 if (isWorkspaceLocked()) {
837 sPendingAddItem = args;
838 } else {
839 completeAdd(args);
840 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
841 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
842 }
843 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700844 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
845 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800848 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800849
850 }
851
852 @Override
853 protected void onActivityResult(
854 final int requestCode, final int resultCode, final Intent data) {
855 handleActivityResult(requestCode, resultCode, data);
856 if (mLauncherCallbacks != null) {
857 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
858 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800859 }
860
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600861 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700862 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600863 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700864 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
865 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
866 View v = null;
867 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
868 if (layout != null) {
869 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
870 }
871 Intent intent = sPendingAddItem.intent;
872 sPendingAddItem = null;
873 if (grantResults.length > 0
874 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700875 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700876 } else {
877 // TODO: Show a snack bar with link to settings
878 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
879 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
880 }
881 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600882 if (mLauncherCallbacks != null) {
883 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
884 grantResults);
885 }
886 }
887
Adam Cohendb364c32014-05-20 14:23:40 -0700888 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
889 appWidgetId, ItemInfo info) {
890 PendingAddArguments args = new PendingAddArguments();
891 args.requestCode = requestCode;
892 args.intent = data;
893 args.container = info.container;
894 args.screenId = info.screenId;
895 args.cellX = info.cellX;
896 args.cellY = info.cellY;
897 args.appWidgetId = appWidgetId;
898 return args;
899 }
900
901 /**
902 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
903 *
904 * @param screenId the screen id to check
905 * @return the new screen, or screenId if it exists
906 */
907 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800908 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700909 if (dropLayout == null) {
910 // it's possible that the add screen was removed because it was
911 // empty and a re-bind occurred
912 mWorkspace.addExtraEmptyScreen();
913 return mWorkspace.commitExtraEmptyScreen();
914 } else {
915 return screenId;
916 }
917 }
918
Adam Cohen091440a2015-03-18 14:16:05 -0700919 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800920 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800921 Runnable onCompleteRunnable = null;
922 int animationType = 0;
923
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700924 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800925 if (resultCode == RESULT_OK) {
926 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
927 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700928 mPendingAddWidgetInfo);
929 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800930 onCompleteRunnable = new Runnable() {
931 @Override
932 public void run() {
933 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700934 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700935 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
936 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800937 }
938 };
939 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800940 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800941 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800942 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700943 if (mDragLayer.getAnimatedView() != null) {
944 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
945 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
946 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700947 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700948 // The animated view may be null in the case of a rotation during widget configuration
949 onCompleteRunnable.run();
950 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 }
952
953 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700954 protected void onStop() {
955 super.onStop();
956 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700957
958 if (mLauncherCallbacks != null) {
959 mLauncherCallbacks.onStop();
960 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700961
962 if (Utilities.isNycMR1OrAbove()) {
963 mAppWidgetHost.stopListening();
964 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700965 }
966
967 @Override
968 protected void onStart() {
969 super.onStart();
970 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700971
972 if (mLauncherCallbacks != null) {
973 mLauncherCallbacks.onStart();
974 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700975
976 if (Utilities.isNycMR1OrAbove()) {
977 mAppWidgetHost.startListening();
978 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700979 }
980
981 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200983 long startTime = 0;
984 if (DEBUG_RESUME_TIME) {
985 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400986 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200987 }
Adam Cohen9211d422014-10-07 18:14:53 -0700988
989 if (mLauncherCallbacks != null) {
990 mLauncherCallbacks.preOnResume();
991 }
992
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700994 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700995
Winson Chung4a2afa32012-07-19 14:53:05 -0700996 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700997 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700998 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800999 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -07001000 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -07001001 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -07001002 // view after launching an app, as they may be depending on the UI to be static to
1003 // switch to another app, otherwise, if it was
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07001004 showAppsView(false /* animated */, !launchedFromApp /* updatePredictedApps */,
1005 false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08001006 } else if (mOnResumeState == State.WIDGETS) {
1007 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001008 }
1009 mOnResumeState = State.NONE;
1010
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001011 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001012 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1013 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001014
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001015 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001016 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001017 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001018 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001019 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001020 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001021 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001022 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001023 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1024 // execute them here
1025 long startTimeCallbacks = 0;
1026 if (DEBUG_RESUME_TIME) {
1027 startTimeCallbacks = System.currentTimeMillis();
1028 }
1029
Michael Jurka1e2f4652013-07-08 18:03:46 -07001030 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1031 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001032 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001033 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001034 if (DEBUG_RESUME_TIME) {
1035 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1036 (System.currentTimeMillis() - startTimeCallbacks));
1037 }
Michael Jurka447bf842013-05-15 14:52:15 +02001038 }
Michael Jurka54554252013-08-01 12:52:23 +02001039 if (mOnResumeCallbacks.size() > 0) {
1040 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1041 mOnResumeCallbacks.get(i).run();
1042 }
1043 mOnResumeCallbacks.clear();
1044 }
Winson Chunge4e50662012-01-23 14:45:13 -08001045
1046 // Reset the pressed state of icons that were locked in the press state while activities
1047 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001048 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001049 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001050 mWaitingForResume.setStayPressed(false);
1051 }
Winson Chung82963d52013-10-09 11:20:57 -07001052
Adam Cohen06dff352012-06-01 17:17:08 -07001053 // It is possible that widgets can receive updates while launcher is not in the foreground.
1054 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1055 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1056 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001057 if (!isWorkspaceLoading()) {
1058 getWorkspace().reinflateWidgetsIfNecessary();
1059 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001060
Michael Jurka447bf842013-05-15 14:52:15 +02001061 if (DEBUG_RESUME_TIME) {
1062 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1063 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001064
Adam Cohen4b66bf32015-09-18 12:15:19 -07001065 // We want to suppress callbacks about CustomContent being shown if we have just received
1066 // onNewIntent while the user was present within launcher. In that case, we post a call
1067 // to move the user to the main screen (which will occur after onResume). We don't want to
1068 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1069 // suppress here.
1070 if (mWorkspace.getCustomContentCallbacks() != null
1071 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001072 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001073 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001074 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1075 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001076 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001077 }
1078 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001079 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001080 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001081 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001082
Sunny Goyal756adbc2015-04-16 15:20:51 -07001083 if (!isWorkspaceLoading()) {
1084 // Process any items that were added while Launcher was away.
1085 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1086 }
Adam Cohen9211d422014-10-07 18:14:53 -07001087
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001088 if (shouldShowDiscoveryBounce()) {
1089 mAllAppsController.showDiscoveryBounce();
1090 }
1091 mIsResumeFromActionScreenOff = false;
Adam Cohen9211d422014-10-07 18:14:53 -07001092 if (mLauncherCallbacks != null) {
1093 mLauncherCallbacks.onResume();
1094 }
Adam Cohen06dff352012-06-01 17:17:08 -07001095 }
1096
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001098 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001099 // Ensure that items added to Launcher are queued until Launcher returns
1100 InstallShortcutReceiver.enableInstallQueue();
1101
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001102 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001103 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001104 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001105 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001106
1107 // We call onHide() aggressively. The custom content callbacks should be able to
1108 // debounce excess onHide calls.
1109 if (mWorkspace.getCustomContentCallbacks() != null) {
1110 mWorkspace.getCustomContentCallbacks().onHide();
1111 }
Romain Guycbb89e42009-06-08 15:52:54 -07001112
Adam Cohen9211d422014-10-07 18:14:53 -07001113 if (mLauncherCallbacks != null) {
1114 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001115 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001116 }
1117
1118 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001119 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1120 // by a onResume or by scrolling otherwise.
1121 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001122
1123 // Custom content is completely hidden
1124 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001125
1126 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1127 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001128
1129 // Indicates whether the user is allowed to scroll away from the custom content.
1130 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001131 }
1132
Adam Cohenc2d6e892014-10-16 09:49:24 -07001133 public interface LauncherOverlay {
1134
1135 /**
1136 * Touch interaction leading to overscroll has begun
1137 */
1138 public void onScrollInteractionBegin();
1139
1140 /**
1141 * Touch interaction related to overscroll has ended
1142 */
1143 public void onScrollInteractionEnd();
1144
1145 /**
1146 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1147 * screen (or in the case of RTL, the rightmost screen).
1148 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001149 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001150
1151 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001152 * Called when the launcher is ready to use the overlay
1153 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001154 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001155 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001156 }
1157
Jun Mukai8af0cd82015-05-12 12:32:12 -07001158 public interface LauncherSearchCallbacks {
1159 /**
1160 * Called when the search overlay is shown.
1161 */
1162 public void onSearchOverlayOpened();
1163
1164 /**
1165 * Called when the search overlay is dismissed.
1166 */
1167 public void onSearchOverlayClosed();
1168 }
1169
Adam Cohenc2d6e892014-10-16 09:49:24 -07001170 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001171 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001172 }
1173
1174 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1175
Sunny Goyalc86df472016-02-25 09:19:38 -08001176 public void onScrollChanged(float progress) {
1177 if (mWorkspace != null) {
1178 mWorkspace.onOverlayScrollChanged(progress);
1179 }
1180 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001181 }
1182
Jorim Jaggid017f882014-01-14 17:08:48 -08001183 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001184 if (mLauncherCallbacks != null) {
1185 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001186 } else {
1187 // On devices with a locked orientation, we will at least have the allow rotation
1188 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001189 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001190 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001191 }
1192
Adam Cohen9211d422014-10-07 18:14:53 -07001193 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001194 CustomContentCallbacks callbacks, String description) {
1195 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001196 }
1197
Adam Cohenedb40762013-07-18 16:45:45 -07001198 // The custom content needs to offset its content to account for the QSB
1199 public int getTopOffsetForCustomContent() {
1200 return mWorkspace.getPaddingTop();
1201 }
1202
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001203 @Override
1204 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001205 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001206 if (mModel.isCurrentCallbacks(this)) {
1207 mModel.stopLoader();
1208 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001209 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1210
Romain Guy13c2e7b2010-03-10 19:45:00 -08001211 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001212 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001213
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001214 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001215 @Override
1216 public void onWindowFocusChanged(boolean hasFocus) {
1217 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001218 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001219
1220 if (mLauncherCallbacks != null) {
1221 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1222 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001223 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001224
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 private boolean acceptFilter() {
1226 final InputMethodManager inputManager = (InputMethodManager)
1227 getSystemService(Context.INPUT_METHOD_SERVICE);
1228 return !inputManager.isFullscreenMode();
1229 }
1230
1231 @Override
1232 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001233 final int uniChar = event.getUnicodeChar();
1234 final boolean handled = super.onKeyDown(keyCode, event);
1235 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1236 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1238 keyCode, event);
1239 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001240 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001241 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001242 // showSearchDialog()
1243 // If there are multiple keystrokes before the search dialog takes focus,
1244 // onSearchRequested() will be called for every keystroke,
1245 // but it is idempotent, so it's fine.
1246 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 }
1248 }
1249
Joe Onorato8a9625e2010-01-28 15:55:35 -08001250 // Eat the long press event so the keyboard doesn't come up.
1251 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1252 return true;
1253 }
1254
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 return handled;
1256 }
1257
Winson46163472015-09-22 17:31:56 -07001258 @Override
1259 public boolean onKeyUp(int keyCode, KeyEvent event) {
1260 if (keyCode == KeyEvent.KEYCODE_MENU) {
1261 // Ignore the menu key if we are currently dragging or are on the custom content screen
1262 if (!isOnCustomContent() && !mDragController.isDragging()) {
1263 // Close any open folders
1264 closeFolder();
1265
Tony Wickham49c8d292016-07-01 11:44:34 -07001266 // Close any shortcuts containers
1267 closeShortcutsContainer();
1268
Winson46163472015-09-22 17:31:56 -07001269 // Stop resizing any widgets
1270 mWorkspace.exitWidgetResizeMode();
1271
1272 // Show the overview mode if we are on the workspace
1273 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1274 !mWorkspace.isSwitchingState()) {
1275 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001276 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001277 }
1278 }
1279 return true;
1280 }
1281 return super.onKeyUp(keyCode, event);
1282 }
1283
Karl Rosaen138a0412009-04-23 19:00:21 -07001284 private String getTypedText() {
1285 return mDefaultKeySsb.toString();
1286 }
1287
1288 private void clearTypedText() {
1289 mDefaultKeySsb.clear();
1290 mDefaultKeySsb.clearSpans();
1291 Selection.setSelection(mDefaultKeySsb, 0);
1292 }
1293
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001295 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1296 * State
1297 */
1298 private static State intToState(int stateOrdinal) {
1299 State state = State.WORKSPACE;
1300 final State[] stateValues = State.values();
1301 for (int i = 0; i < stateValues.length; i++) {
1302 if (stateValues[i].ordinal() == stateOrdinal) {
1303 state = stateValues[i];
1304 break;
1305 }
1306 }
1307 return state;
1308 }
1309
1310 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 * Restores the previous state, if it exists.
1312 *
1313 * @param savedState The previous state.
1314 */
1315 private void restoreState(Bundle savedState) {
1316 if (savedState == null) {
1317 return;
1318 }
1319
Michael Jurka883f55b2010-10-21 15:47:14 -07001320 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001321 if (state == State.APPS || state == State.WIDGETS) {
1322 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001323 }
1324
Adam Cohen21cd0022013-10-09 18:57:02 -07001325 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1326 PagedView.INVALID_RESTORE_PAGE);
1327 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001328 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 }
1330
Sunny Goyal756cd262015-08-20 12:33:21 -07001331 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1332 if (itemValues != null) {
1333 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001334 AppWidgetProviderInfo info = savedState.getParcelable(
1335 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001336 mPendingAddWidgetInfo = info == null ?
1337 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1338
Adam Cohen4637b5a2013-11-04 18:21:24 -08001339 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001340 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 mRestoring = true;
1342 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 }
1344
1345 /**
1346 * Finds all the views we need and configure them properly.
1347 */
1348 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001349 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001350 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001351 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001352 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyal6178f132016-07-11 17:30:03 -07001353 mQsbContainer = mDragLayer.findViewById(mDeviceProfile.isVerticalBarLayout()
1354 ? R.id.workspace_blocked_row : R.id.qsb_container);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001355 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001356
Sunny Goyal784f9c32016-03-23 16:56:54 -07001357 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1358 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1359 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001360 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361
Winson Chung4c98d922011-05-31 16:50:48 -07001362 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001363
1364 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001365
Winson Chung3d503fb2011-07-13 17:25:49 -07001366 // Setup the hotseat
1367 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1368 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001369 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001370 }
1371
Winsone9f27272015-10-13 10:47:51 -07001372 // Setup the overview panel
1373 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001374
Winson Chung4c98d922011-05-31 16:50:48 -07001375 // Setup the workspace
1376 mWorkspace.setHapticFeedbackEnabled(false);
1377 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001378 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001379 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1380 // default state, otherwise we will update to the wrong offsets in RTL
1381 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001382 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001383 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001384
Tony Wickham34d2c912015-09-11 09:27:58 -07001385 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001386 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001387
Winson Chungef7f8742015-06-04 17:18:17 -07001388 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001389 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001390 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001391 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1392 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1393 } else {
1394 mAppsView.setSearchBarController(new DefaultAppSearchController());
1395 }
Craig Mautner360310b2012-10-26 15:13:08 -07001396
Winson Chung3d503fb2011-07-13 17:25:49 -07001397 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001398 mDragController.setDragScoller(mWorkspace);
1399 mDragController.setScrollView(mDragLayer);
1400 mDragController.setMoveTarget(mWorkspace);
1401 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001402 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001403
Peter Schiller310a9882016-06-30 13:52:36 -07001404 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1405 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1406 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001407
Sunny Goyal322d5562015-06-25 19:35:49 -07001408 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1409 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001410 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411 }
1412
Winsone9f27272015-10-13 10:47:51 -07001413 private void setupOverviewPanel() {
1414 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1415
1416 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1417 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1418 @Override
1419 public boolean onLongClick(View v) {
1420 return v.performClick();
1421 }
1422 };
1423
1424 // Bind wallpaper button actions
1425 View wallpaperButton = findViewById(R.id.wallpaper_button);
1426 wallpaperButton.setOnClickListener(new OnClickListener() {
1427 @Override
1428 public void onClick(View view) {
1429 if (!mWorkspace.isSwitchingState()) {
1430 onClickWallpaperPicker(view);
1431 }
1432 }
1433 });
1434 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1435 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1436
1437 // Bind widget button actions
1438 mWidgetsButton = findViewById(R.id.widget_button);
1439 mWidgetsButton.setOnClickListener(new OnClickListener() {
1440 @Override
1441 public void onClick(View view) {
1442 if (!mWorkspace.isSwitchingState()) {
1443 onClickAddWidgetButton(view);
1444 }
1445 }
1446 });
1447 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1448 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1449
1450 // Bind settings actions
1451 View settingsButton = findViewById(R.id.settings_button);
1452 boolean hasSettings = hasSettings();
1453 if (hasSettings) {
1454 settingsButton.setOnClickListener(new OnClickListener() {
1455 @Override
1456 public void onClick(View view) {
1457 if (!mWorkspace.isSwitchingState()) {
1458 onClickSettingsButton(view);
1459 }
1460 }
1461 });
1462 settingsButton.setOnLongClickListener(performClickOnLongClick);
1463 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1464 } else {
1465 settingsButton.setVisibility(View.GONE);
1466 }
1467
1468 mOverviewPanel.setAlpha(0f);
1469 }
1470
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001472 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001473 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001474 */
1475 public void setAllAppsButton(View allAppsButton) {
1476 mAllAppsButton = allAppsButton;
1477 }
1478
Sunny Goyalbb011da2016-06-15 15:42:29 -07001479 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001480 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001481 }
1482
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001483 public View getWidgetsButton() {
1484 return mWidgetsButton;
1485 }
1486
Anjali Koppal5ad44842014-03-10 20:34:39 -07001487 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 * Creates a view representing a shortcut.
1489 *
1490 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001492 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001493 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494 }
1495
1496 /**
1497 * Creates a view representing a shortcut inflated from the specified resource.
1498 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 * @param parent The group the shortcut belongs to.
1500 * @param info The data structure describing the shortcut.
1501 *
1502 * @return A View inflated from layoutResId.
1503 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001504 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001505 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001506 parent, false);
1507 favorite.applyFromShortcutInfo(info, mIconCache);
1508 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001510 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 return favorite;
1512 }
1513
1514 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 * Add a shortcut to the workspace.
1516 *
1517 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001519 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001520 int cellY) {
1521 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001522 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001523
Sunny Goyal5c97f512015-05-19 16:03:28 -07001524 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001525 if (info == null) {
1526 return;
1527 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001528 final View view = createShortcut(info);
1529
Sunny Goyal5c97f512015-05-19 16:03:28 -07001530 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001531 // First we check if we already know the exact location where we want to add this item.
1532 if (cellX >= 0 && cellY >= 0) {
1533 cellXY[0] = cellX;
1534 cellXY[1] = cellY;
1535 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001536
1537 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001538 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001539 true, null,null)) {
1540 return;
1541 }
1542 DragObject dragObject = new DragObject();
1543 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001544 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1545 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001546 return;
1547 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001548 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001549 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001550 }
1551
1552 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001553 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001554 return;
1555 }
1556
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001557 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558
1559 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001560 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001561 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 }
1563 }
1564
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001566 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001568 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 */
Adam Cohen091440a2015-03-18 14:16:05 -07001570 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001571 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1572
1573 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001574 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001575 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001576 }
Romain Guycbb89e42009-06-08 15:52:54 -07001577
Adam Cohen59400422014-03-05 18:07:04 -08001578 if (appWidgetInfo.isCustomWidget) {
1579 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001580 }
1581
Adam Cohen59400422014-03-05 18:07:04 -08001582 LauncherAppWidgetInfo launcherInfo;
1583 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1584 launcherInfo.spanX = info.spanX;
1585 launcherInfo.spanY = info.spanY;
1586 launcherInfo.minSpanX = info.minSpanX;
1587 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001588 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001589
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001591 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592
1593 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001594 if (hostView == null) {
1595 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001596 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001597 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001598 hostView.setVisibility(View.VISIBLE);
1599 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001601 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602 }
Romain Guycbb89e42009-06-08 15:52:54 -07001603
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001604 private void addAppWidgetToWorkspace(
1605 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001606 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001607 hostView.setTag(item);
1608 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001609
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001610 hostView.setFocusable(true);
1611 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001612
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001613 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001614 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1615
1616 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001617 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001618 }
1619 }
1620
Adam Cohended9f8d2010-11-03 13:25:16 -07001621 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1622 @Override
1623 public void onReceive(Context context, Intent intent) {
1624 final String action = intent.getAction();
1625 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1626 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001627 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001628 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001629
Winson Chungc100e8e2011-08-09 16:02:43 -07001630 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001631 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001632 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001633 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001634 if (!showWorkspace(false)) {
1635 // If we are already on the workspace, then manually reset all apps
1636 mAppsView.reset();
1637 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001638 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001639 mIsResumeFromActionScreenOff = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001640 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1641 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001642 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001643 }
1644 }
1645 };
1646
1647 @Override
1648 public void onAttachedToWindow() {
1649 super.onAttachedToWindow();
1650
1651 // Listen for broadcasts related to user-presence
1652 final IntentFilter filter = new IntentFilter();
1653 filter.addAction(Intent.ACTION_SCREEN_OFF);
1654 filter.addAction(Intent.ACTION_USER_PRESENT);
1655 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001656 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001657 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001658 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001659
1660 if (mLauncherCallbacks != null) {
1661 mLauncherCallbacks.onAttachedToWindow();
1662 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001663 }
1664
1665 @Override
1666 public void onDetachedFromWindow() {
1667 super.onDetachedFromWindow();
1668 mVisible = false;
1669
Adam Cohend113e0c2010-11-11 10:48:05 -08001670 if (mAttached) {
1671 unregisterReceiver(mReceiver);
1672 mAttached = false;
1673 }
Winson Chungb745afb2015-03-02 11:51:23 -08001674 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001675
1676 if (mLauncherCallbacks != null) {
1677 mLauncherCallbacks.onDetachedFromWindow();
1678 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001679 }
1680
1681 public void onWindowVisibilityChanged(int visibility) {
1682 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001683 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001684 // The following code used to be in onResume, but it turns out onResume is called when
1685 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1686 // is a more appropriate event to handle
1687 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001688 if (!mWorkspaceLoading) {
1689 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001690 // We want to let Launcher draw itself at least once before we force it to build
1691 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001692 // apps is nice and speedy.
1693 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001694 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001695 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001696 if (mStarted) return;
1697 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001698 // We delay the layer building a bit in order to give
1699 // other message processing a time to run. In particular
1700 // this avoids a delay in hiding the IME if it was
1701 // currently shown, because doing that may involve
1702 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001703 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001704 final ViewTreeObserver.OnDrawListener listener = this;
1705 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001706 public void run() {
1707 if (mWorkspace != null &&
1708 mWorkspace.getViewTreeObserver() != null) {
1709 mWorkspace.getViewTreeObserver().
1710 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001711 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001712 }
1713 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001714 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001715 }
1716 });
1717 }
1718 clearTypedText();
1719 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001720 }
1721
Adam Cohen091440a2015-03-18 14:16:05 -07001722 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001723 mHandler.removeMessages(ADVANCE_MSG);
1724 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1725 mHandler.sendMessageDelayed(msg, delay);
1726 mAutoAdvanceSentTime = System.currentTimeMillis();
1727 }
1728
Adam Cohen091440a2015-03-18 14:16:05 -07001729 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001730 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1731 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1732 mAutoAdvanceRunning = autoAdvanceRunning;
1733 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001734 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001735 sendAdvanceMessage(delay);
1736 } else {
1737 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001738 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001739 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1740 }
1741 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001742 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001743 }
1744 }
1745 }
1746
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001747 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1748
Adam Cohended9f8d2010-11-03 13:25:16 -07001749 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001750 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001751 if (msg.what == ADVANCE_MSG) {
1752 int i = 0;
1753 for (View key: mWidgetsToAdvance.keySet()) {
1754 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001755 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001756 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001757 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001758 public void run() {
1759 ((Advanceable) v).advance();
1760 }
1761 }, delay);
1762 }
1763 i++;
1764 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001765 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001766 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001767 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001768 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001769 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001770
Winsonc0b52fe2015-09-09 16:38:15 -07001771 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001772 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001773 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1774 if (v instanceof Advanceable) {
1775 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001776 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001777 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001778 }
1779 }
1780
Winsonc0b52fe2015-09-09 16:38:15 -07001781 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001782 if (mWidgetsToAdvance.containsKey(hostView)) {
1783 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001784 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001785 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001786 }
1787
Hyunyoung Song3f471442015-04-08 19:01:34 -07001788 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001789 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1790 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001791 }
1792
Winson Chunga6945242014-01-08 14:04:34 -08001793 public DragLayer getDragLayer() {
1794 return mDragLayer;
1795 }
1796
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001797 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001798 return mAppsView;
1799 }
1800
Hyunyoung Song3f471442015-04-08 19:01:34 -07001801 public WidgetsContainerView getWidgetsView() {
1802 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001803 }
1804
Winson Chunga6945242014-01-08 14:04:34 -08001805 public Workspace getWorkspace() {
1806 return mWorkspace;
1807 }
1808
Sunny Goyal6178f132016-07-11 17:30:03 -07001809 public View getQsbContainer() {
1810 return mQsbContainer;
1811 }
1812
Winson Chunga6945242014-01-08 14:04:34 -08001813 public Hotseat getHotseat() {
1814 return mHotseat;
1815 }
1816
Jorim Jaggid017f882014-01-14 17:08:48 -08001817 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001818 return mOverviewPanel;
1819 }
1820
Sunny Goyal47328fd2016-05-25 18:56:41 -07001821 public DropTargetBar getDropTargetBar() {
1822 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001823 }
1824
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001825 public LauncherAppWidgetHost getAppWidgetHost() {
1826 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 }
Romain Guycbb89e42009-06-08 15:52:54 -07001828
Winson Chunga9abd0e2010-10-27 17:18:37 -07001829 public LauncherModel getModel() {
1830 return mModel;
1831 }
1832
Adam Cohen79d90c52016-04-22 13:29:20 -07001833 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001834 return mSharedPrefs;
1835 }
1836
Adam Cohen2e6da152015-05-06 11:42:25 -07001837 public DeviceProfile getDeviceProfile() {
1838 return mDeviceProfile;
1839 }
1840
Bjorn Bringertc459e522013-06-07 19:36:01 +01001841 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001842 getWindow().closeAllPanels();
1843
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001844 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001845 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001846 }
1847
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 @Override
1849 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001850 long startTime = 0;
1851 if (DEBUG_RESUME_TIME) {
1852 startTime = System.currentTimeMillis();
1853 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001854 super.onNewIntent(intent);
1855
1856 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001857 Folder openFolder = mWorkspace.getOpenFolder();
1858 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1859 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1860 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1861 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1862 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001863 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001864 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001865
Adam Cohen6fecd412013-10-02 17:41:50 -07001866 if (mWorkspace == null) {
1867 // Can be cases where mWorkspace is null, this prevents a NPE
1868 return;
1869 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001870 // In all these cases, only animate if we're already on home
1871 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001872
Sunny Goyal935fca12015-10-13 10:19:01 -07001873 closeFolder(alreadyOnHome);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001874 closeShortcutsContainer(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001875 exitSpringLoadedDragMode();
1876
1877 // If we are already on home, then just animate back to the workspace,
1878 // otherwise, just wait until onResume to set the state back to Workspace
1879 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001880 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001881 } else {
1882 mOnResumeState = State.WORKSPACE;
1883 }
1884
1885 final View v = getWindow().peekDecorView();
1886 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001887 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001888 INPUT_METHOD_SERVICE);
1889 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1890 }
1891
Winson Chungb745afb2015-03-02 11:51:23 -08001892 // Reset the apps view
1893 if (!alreadyOnHome && mAppsView != null) {
1894 mAppsView.scrollToTop();
1895 }
1896
Hyunyoung Song3f471442015-04-08 19:01:34 -07001897 // Reset the widgets view
1898 if (!alreadyOnHome && mWidgetsView != null) {
1899 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001900 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001901
Adam Cohen9211d422014-10-07 18:14:53 -07001902 if (mLauncherCallbacks != null) {
1903 mLauncherCallbacks.onHomeIntent();
1904 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905 }
Adam Cohened307df2013-10-02 09:37:31 -07001906
Adam Cohen9211d422014-10-07 18:14:53 -07001907 if (mLauncherCallbacks != null) {
1908 mLauncherCallbacks.onNewIntent(intent);
1909 }
Winson15f8b172015-08-19 11:02:39 -07001910
1911 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1912 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1913 // animation.
1914 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001915 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1916 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001917 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1918 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001919
1920 // We use this flag to suppress noisy callbacks above custom content state
1921 // from onResume.
1922 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001923 mWorkspace.post(new Runnable() {
1924 @Override
1925 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001926 if (mWorkspace != null) {
1927 mWorkspace.moveToDefaultScreen(true);
1928 }
Winson15f8b172015-08-19 11:02:39 -07001929 }
1930 });
1931 }
1932 }
1933
1934 if (DEBUG_RESUME_TIME) {
1935 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1936 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001937 }
1938
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001939 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001940 public void onRestoreInstanceState(Bundle state) {
1941 super.onRestoreInstanceState(state);
1942 for (int page: mSynchronouslyBoundPages) {
1943 mWorkspace.restoreInstanceStateForChild(page);
1944 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 }
1946
1947 @Override
1948 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001949 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001950 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1951 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001952
Adam Cohen21cd0022013-10-09 18:57:02 -07001953 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001954 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955
Michael Jurka883f55b2010-10-21 15:47:14 -07001956 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001957 // We close any open folder since it will not be re-opened, and we need to make sure
1958 // this state is reflected.
1959 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1960 closeFolder(false);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001961 closeShortcutsContainer(false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001962
Adam Cohendcd297f2013-06-18 13:13:40 -07001963 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001964 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001965 ContentValues itemValues = new ContentValues();
1966 mPendingAddInfo.writeToValues(itemValues);
1967 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001968 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001969 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001970 }
1971
Adam Cohen9211d422014-10-07 18:14:53 -07001972 if (mLauncherCallbacks != null) {
1973 mLauncherCallbacks.onSaveInstanceState(outState);
1974 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 }
1976
1977 @Override
1978 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001980
Winson Chunge7a03942011-08-05 15:05:12 -07001981 // Remove all pending runnables
1982 mHandler.removeMessages(ADVANCE_MSG);
1983 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001984 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001985 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001986
Winson Chungcd2b0142011-06-08 16:02:26 -07001987 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001988 // It's possible to receive onDestroy after a new Launcher activity has
1989 // been created. In this case, don't interfere with the new Launcher.
1990 if (mModel.isCurrentCallbacks(this)) {
1991 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001992 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001993 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001994
Sunny Goyal745bad92016-05-02 10:54:12 -07001995 if (mRotationPrefChangeHandler != null) {
1996 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1997 }
1998
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002000 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002002 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002004 mAppWidgetHost = null;
2005
2006 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007
2008 TextKeyListener.getInstance().release();
2009
Tony Wickhame2217252016-03-22 16:34:23 -07002010 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
2011 .removeAccessibilityStateChangeListener(this);
2012
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002013 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002014
Michael Jurka2ecf9952012-06-18 12:52:28 -07002015 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002016
2017 if (mLauncherCallbacks != null) {
2018 mLauncherCallbacks.onDestroy();
2019 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 }
2021
Sunny Goyalae502842016-06-17 08:43:56 -07002022 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
2023 return mAccessibilityDelegate;
2024 }
2025
Adam Cohena9cf38f2011-05-02 15:36:58 -07002026 public DragController getDragController() {
2027 return mDragController;
2028 }
2029
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002030 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002031 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07002032 onStartForResult(requestCode);
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002033 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07002034 }
2035
2036 @Override
2037 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2038 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2039 onStartForResult(requestCode);
2040 try {
2041 super.startIntentSenderForResult(intent, requestCode,
2042 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2043 } catch (IntentSender.SendIntentException e) {
2044 throw new ActivityNotFoundException();
2045 }
2046 }
2047
2048 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002049 if (requestCode >= 0) {
2050 setWaitingForResult(true);
2051 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002052 }
2053
Winson Chung70d72102011-08-12 11:24:35 -07002054 /**
2055 * Indicates that we want global search for this activity by setting the globalSearch
2056 * argument for {@link #startSearch} to true.
2057 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002059 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002061
Karl Rosaen138a0412009-04-23 19:00:21 -07002062 if (initialQuery == null) {
2063 // Use any text typed in the launcher as the initial query
2064 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002065 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002066 if (appSearchData == null) {
2067 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002068 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002070
2071 // TODO send proper bounds.
2072 Rect sourceBounds = null;
Romain Guycbb89e42009-06-08 15:52:54 -07002073
Ian Parkinson047036e2014-09-01 15:40:53 +01002074 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002075 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002076 if (clearTextImmediately) {
2077 clearTypedText();
2078 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002079
2080 // We need to show the workspace after starting the search
2081 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002082 }
2083
Ian Parkinson047036e2014-09-01 15:40:53 +01002084 /**
2085 * Start a text search.
2086 *
2087 * @return {@code true} if the search will start immediately, so any further keypresses
2088 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2089 * to buffer keypresses.
2090 */
2091 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002092 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002093 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2094 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2095 sourceBounds);
2096 }
2097
Michael Jurkaa33411c2012-06-14 16:18:21 -07002098 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002099 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002100 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002101 }
2102
2103 /**
2104 * Starts the global search activity. This code is a copied from SearchManager
2105 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002106 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002107 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002108 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002109 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2110 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2111 if (globalSearchActivity == null) {
2112 Log.w(TAG, "No global search activity found.");
2113 return;
2114 }
2115 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2116 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2117 intent.setComponent(globalSearchActivity);
2118 // Make sure that we have a Bundle to put source in
2119 if (appSearchData == null) {
2120 appSearchData = new Bundle();
2121 } else {
2122 appSearchData = new Bundle(appSearchData);
2123 }
Adam Cohen9211d422014-10-07 18:14:53 -07002124 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002125 if (!appSearchData.containsKey("source")) {
2126 appSearchData.putString("source", getPackageName());
2127 }
2128 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2129 if (!TextUtils.isEmpty(initialQuery)) {
2130 intent.putExtra(SearchManager.QUERY, initialQuery);
2131 }
2132 if (selectInitialQuery) {
2133 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2134 }
2135 intent.setSourceBounds(sourceBounds);
2136 try {
2137 startActivity(intent);
2138 } catch (ActivityNotFoundException ex) {
2139 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2140 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002141 }
2142
Yura4f93ec62014-02-04 14:15:21 +00002143 public boolean isOnCustomContent() {
2144 return mWorkspace.isOnOrMovingToCustomContent();
2145 }
2146
Winson Chung70d72102011-08-12 11:24:35 -07002147 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002148 public boolean onPrepareOptionsMenu(Menu menu) {
2149 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002150 if (mLauncherCallbacks != null) {
2151 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2152 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002153 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002154 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002155
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002156 @Override
2157 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002158 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002159 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002160 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002161 }
2162
Joe Onorato9c1289c2009-08-17 11:03:03 -04002163 public boolean isWorkspaceLocked() {
2164 return mWorkspaceLoading || mWaitingForResult;
2165 }
2166
Adam Cohen517a7f52014-03-01 12:12:59 -08002167 public boolean isWorkspaceLoading() {
2168 return mWorkspaceLoading;
2169 }
2170
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002171 private void setWorkspaceLoading(boolean value) {
2172 boolean isLocked = isWorkspaceLocked();
2173 mWorkspaceLoading = value;
2174 if (isLocked != isWorkspaceLocked()) {
2175 onWorkspaceLockedChanged();
2176 }
2177 }
2178
2179 private void setWaitingForResult(boolean value) {
2180 boolean isLocked = isWorkspaceLocked();
2181 mWaitingForResult = value;
2182 if (isLocked != isWorkspaceLocked()) {
2183 onWorkspaceLockedChanged();
2184 }
2185 }
2186
Adam Cohen9211d422014-10-07 18:14:53 -07002187 protected void onWorkspaceLockedChanged() {
2188 if (mLauncherCallbacks != null) {
2189 mLauncherCallbacks.onWorkspaceLockedChanged();
2190 }
2191 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002192
Michael Jurka0280c3b2010-09-17 15:00:07 -07002193 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002194 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002195 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002196 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2197 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002198 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002199 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002200
Tony Wickhama0628cc2015-10-14 15:23:04 -07002201 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002202 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002203 if (LOGD) {
2204 Log.d(TAG, "Adding widget from drop");
2205 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002206 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2207 }
2208
Sunny Goyale6b63a32015-01-16 16:14:29 -08002209 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002210 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2211 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002212 if (appWidgetInfo.configure != null) {
2213 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002214 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002215
Bjorn Bringert7984c942009-12-09 15:38:25 +00002216 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002217 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2218 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2219
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002220 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002221 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002222 Runnable onComplete = new Runnable() {
2223 @Override
2224 public void run() {
2225 // Exit spring loaded mode if necessary after adding the widget
2226 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2227 null);
2228 }
2229 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002230 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002231 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002232 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002233 }
2234 }
Romain Guycbb89e42009-06-08 15:52:54 -07002235
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002236 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002237 // Close any folders that may be open.
2238 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002239 mWorkspace.moveToCustomContentScreen(animate);
2240 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002241
2242 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2243 int[] cell, int spanX, int spanY) {
2244 switch (info.itemType) {
2245 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2246 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2247 int span[] = new int[2];
2248 span[0] = spanX;
2249 span[1] = spanY;
2250 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2251 container, screenId, cell, span);
2252 break;
2253 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2254 processShortcutFromDrop(info.componentName, container, screenId, cell);
2255 break;
2256 default:
2257 throw new IllegalStateException("Unknown item type: " + info.itemType);
2258 }
2259 }
2260
Adam Cohenfbba09b2011-07-18 21:43:05 -07002261 /**
2262 * Process a shortcut drop.
2263 *
2264 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002265 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002266 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002267 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002268 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2269 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002270 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002271 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002272 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002273
2274 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002275 mPendingAddInfo.cellX = cell[0];
2276 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002277 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002278
2279 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2280 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002281 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002282 }
2283
Adam Cohenfbba09b2011-07-18 21:43:05 -07002284 /**
2285 * Process a widget drop.
2286 *
2287 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002288 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002289 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002290 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002291 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2292 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002293 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002294 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002295 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002296 mPendingAddInfo.minSpanX = info.minSpanX;
2297 mPendingAddInfo.minSpanY = info.minSpanY;
2298
Adam Cohenfbba09b2011-07-18 21:43:05 -07002299 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002300 mPendingAddInfo.cellX = cell[0];
2301 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002302 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002303 if (span != null) {
2304 mPendingAddInfo.spanX = span[0];
2305 mPendingAddInfo.spanY = span[1];
2306 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002307
Adam Cohened66b2b2012-01-23 17:28:51 -08002308 AppWidgetHostView hostView = info.boundWidget;
2309 int appWidgetId;
2310 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002311 // In the case where we've prebound the widget, we remove it from the DragLayer
2312 if (LOGD) {
2313 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2314 }
2315 getDragLayer().removeView(hostView);
2316
Adam Cohened66b2b2012-01-23 17:28:51 -08002317 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002318 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002319
2320 // Clear the boundWidget so that it doesn't get destroyed.
2321 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002322 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002323 // In this case, we either need to start an activity to get permission to bind
2324 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002325 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002326 Bundle options = info.bindOptions;
2327
Sunny Goyalffe83f12014-08-14 17:39:34 -07002328 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2329 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002330 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002331 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002332 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002333 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002334 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2335 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2336 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002337 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2338 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002339 // TODO: we need to make sure that this accounts for the options bundle.
2340 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002341 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2342 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002343 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002344 }
2345
Adam Cohendcd297f2013-06-18 13:13:40 -07002346 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002347 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002348 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002349 folderInfo.title = getText(R.string.folder_name);
2350
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002351 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002352 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2353 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002354
2355 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002356 FolderIcon newFolder =
2357 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002358 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002359 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002360 // Force measure the new folder icon
2361 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2362 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002363 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002364 }
Romain Guycbb89e42009-06-08 15:52:54 -07002365
Winsonc0b52fe2015-09-09 16:38:15 -07002366 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002367 * Unbinds the view for the specified item, and removes the item and all its children.
2368 *
2369 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002370 * @param itemInfo the {@link ItemInfo} for this view.
2371 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002372 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002373 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002374 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002375 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002376 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2377 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002378 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002379 } else {
2380 mWorkspace.removeWorkspaceItem(v);
2381 }
Winsonc0b52fe2015-09-09 16:38:15 -07002382 if (deleteFromDb) {
2383 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2384 }
2385 } else if (itemInfo instanceof FolderInfo) {
2386 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002387 if (v instanceof FolderIcon) {
2388 ((FolderIcon) v).removeListeners();
2389 }
Winsonc0b52fe2015-09-09 16:38:15 -07002390 mWorkspace.removeWorkspaceItem(v);
2391 if (deleteFromDb) {
2392 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2393 }
2394 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2395 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002396 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002397 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002398 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002399 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002400 }
2401 } else {
2402 return false;
2403 }
2404 return true;
2405 }
2406
2407 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002408 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002409 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002410 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002411 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002412 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002413 // Deleting an app widget ID is a void call but writes to disk before returning
2414 // to the caller...
2415 new AsyncTask<Void, Void, Void>() {
2416 public Void doInBackground(Void ... args) {
2417 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2418 return null;
2419 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002420 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002421 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002422 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002423 }
2424
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002425 @Override
2426 public boolean dispatchKeyEvent(KeyEvent event) {
2427 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2428 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002429 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002430 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002431 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002432 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002433 dumpState();
2434 return true;
2435 }
2436 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002437 }
2438 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2439 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002440 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002441 return true;
2442 }
2443 }
2444
2445 return super.dispatchKeyEvent(event);
2446 }
2447
Joe Onorato88ec0992009-11-19 13:16:06 -08002448 @Override
2449 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002450 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2451 return;
2452 }
2453
Sunny Goyal45478022015-06-08 16:52:41 -07002454 if (mDragController.isDragging()) {
2455 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002456 return;
2457 }
2458
Tony Wickham49c8d292016-07-01 11:44:34 -07002459 if (getOpenShortcutsContainer() != null) {
2460 closeShortcutsContainer();
2461 } else if (isAppsViewVisible()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002462 showWorkspace(true);
2463 } else if (isWidgetsViewVisible()) {
2464 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002465 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002466 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002467 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002468 Folder openFolder = mWorkspace.getOpenFolder();
2469 if (openFolder.isEditingName()) {
2470 openFolder.dismissEditingName();
2471 } else {
2472 closeFolder();
2473 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002474 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002475 mWorkspace.exitWidgetResizeMode();
2476
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002477 // Back button is a no-op here, but give at least some feedback for the button press
2478 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002479 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002480 }
2481
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002482 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002483 * Launches the intent referred by the clicked shortcut.
2484 *
2485 * @param v The view representing the clicked shortcut.
2486 */
2487 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002488 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2489 // view has detached (it's possible for this to happen if the view is removed mid touch).
2490 if (v.getWindowToken() == null) {
2491 return;
2492 }
2493
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002494 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002495 return;
2496 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002497
Adam Cohen1697b792013-09-17 19:08:21 -07002498 if (v instanceof Workspace) {
2499 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002500 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002501 }
2502 return;
2503 }
2504
2505 if (v instanceof CellLayout) {
2506 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002507 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2508 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002509 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002510 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002511 }
2512
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002513 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002514 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002515 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002516 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002517 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002518 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002519 }
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002520 } else if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2521 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002522 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002523 } else if (tag instanceof AppInfo) {
2524 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002525 } else if (tag instanceof LauncherAppWidgetInfo) {
2526 if (v instanceof PendingAppWidgetHostView) {
2527 onClickPendingWidget((PendingAppWidgetHostView) v);
2528 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002529 }
2530 }
2531
Sunny Goyal70660032015-05-14 00:07:08 -07002532 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002533 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002534 return false;
2535 }
2536
Michael Jurkaaf442092010-06-10 17:01:57 -07002537 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002538 * Event handler for the app widget view which has not fully restored.
2539 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002540 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002541 if (mIsSafeModeEnabled) {
2542 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2543 return;
2544 }
2545
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002546 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002547 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002548 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2549 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2550 // This should not happen, as we make sure that an Id is allocated during bind.
2551 return;
2552 }
2553 LauncherAppWidgetProviderInfo appWidgetInfo =
2554 mAppWidgetManager.findProvider(info.providerName, info.user);
2555 if (appWidgetInfo != null) {
2556 mPendingAddWidgetId = info.appWidgetId;
2557 mPendingAddInfo.copyFrom(info);
2558 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002559
Sunny Goyald478c832016-04-01 12:04:16 -07002560 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2561 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingAddWidgetId);
2562 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
2563 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2564 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2565 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2566 }
2567 } else {
2568 LauncherAppWidgetProviderInfo appWidgetInfo =
2569 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2570 if (appWidgetInfo != null) {
2571 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2572 }
Sunny Goyalff572272014-07-23 13:58:07 -07002573 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002574 } else if (info.installProgress < 0) {
2575 // The install has not been queued
2576 final String packageName = info.providerName.getPackageName();
2577 showBrokenAppInstallDialog(packageName,
2578 new DialogInterface.OnClickListener() {
2579 public void onClick(DialogInterface dialog, int id) {
2580 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2581 }
2582 });
2583 } else {
2584 // Download has started.
2585 final String packageName = info.providerName.getPackageName();
2586 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002587 }
2588 }
2589
Sunny Goyald478c832016-04-01 12:04:16 -07002590 private void startRestoredWidgetReconfigActivity(
2591 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
2592 mPendingAddWidgetInfo = provider;
2593 mPendingAddInfo.copyFrom(info);
2594 mPendingAddWidgetId = info.appWidgetId;
2595 mAppWidgetManager.startConfigActivity(provider,
2596 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2597 }
2598
Sunny Goyalff572272014-07-23 13:58:07 -07002599 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002600 * Event handler for the "grid" button that appears on the home screen, which
2601 * enters all apps mode.
2602 *
2603 * @param v The view that was clicked.
2604 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002605 protected void onClickAllAppsButton(View v) {
2606 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002607 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002608 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.TAP,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002609 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002610 showAppsView(true /* animated */, true /* updatePredictedApps */,
2611 false /* focusSearchBar */);
Winson Chung76648c52015-07-10 14:33:23 -07002612 }
2613 }
2614
2615 protected void onLongClickAllAppsButton(View v) {
2616 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2617 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002618 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.LONGPRESS,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002619 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002620 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07002621 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002622 }
2623 }
2624
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002625 private void showBrokenAppInstallDialog(final String packageName,
2626 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002627 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002628 .setTitle(R.string.abandoned_promises_title)
2629 .setMessage(R.string.abandoned_promise_explanation)
2630 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2631 .setNeutralButton(R.string.abandoned_clean_this,
2632 new DialogInterface.OnClickListener() {
2633 public void onClick(DialogInterface dialog, int id) {
2634 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2635 mWorkspace.removeAbandonedPromise(packageName, user);
2636 }
2637 })
2638 .create().show();
2639 return;
2640 }
2641
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002642 /**
2643 * Event handler for an app shortcut click.
2644 *
2645 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2646 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002647 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002648 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2649 Object tag = v.getTag();
2650 if (!(tag instanceof ShortcutInfo)) {
2651 throw new IllegalArgumentException("Input must be a Shortcut");
2652 }
2653
2654 // Open shortcut
2655 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002656
2657 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002658 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2659 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002660 // Launch activity anyway, framework will tell the user why the app is suspended.
2661 } else {
2662 int error = R.string.activity_not_available;
2663 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2664 error = R.string.safemode_shortcut_error;
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002665 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0) {
2666 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002667 }
2668 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2669 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002670 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002671 }
2672
Chris Wren40c5ed32014-06-24 18:24:23 -04002673 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002674 if ((v instanceof BubbleTextView)
2675 && shortcut.isPromise()
2676 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002677 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002678 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002679 new DialogInterface.OnClickListener() {
2680 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002681 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002682 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002683 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002684 return;
2685 }
2686
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002687 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002688 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002689 }
2690
Sunny Goyala7ce1662016-05-31 15:01:35 -07002691 private void startAppShortcutOrInfoActivity(View v) {
2692 ItemInfo item = (ItemInfo) v.getTag();
2693 Intent intent = item.getIntent();
2694 if (intent == null) {
2695 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002696 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002697 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002698 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002699
2700 if (success && v instanceof BubbleTextView) {
2701 mWaitingForResume = (BubbleTextView) v;
2702 mWaitingForResume.setStayPressed(true);
2703 }
2704 }
2705
2706 /**
2707 * Event handler for a folder icon click.
2708 *
2709 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2710 */
2711 protected void onClickFolderIcon(View v) {
2712 if (LOGD) Log.d(TAG, "onClickFolder");
2713 if (!(v instanceof FolderIcon)){
2714 throw new IllegalArgumentException("Input must be a FolderIcon");
2715 }
2716
2717 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002718 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002719 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002720 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002721 }
Michael Jurka5130e402011-10-13 04:55:35 -07002722 }
2723
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002724 /**
2725 * Event handler for the (Add) Widgets button that appears after a long press
2726 * on the home screen.
2727 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002728 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002729 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002730 if (mIsSafeModeEnabled) {
2731 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2732 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002733 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002734 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002735 }
2736
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002737 /**
2738 * Event handler for the wallpaper picker button that appears after a long press
2739 * on the home screen.
2740 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002741 public void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002742 if (!Utilities.isWallapaperAllowed(this)) {
2743 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2744 return;
2745 }
2746
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002747 String pickerPackage = getString(R.string.wallpaper_picker_package);
2748 if (TextUtils.isEmpty(pickerPackage)) {
2749 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2750 }
2751
Tony Wickham785f7a52015-08-31 17:28:32 -07002752 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2753 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002754
2755 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002756 .setPackage(pickerPackage)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002757 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
2758 intent.setSourceBounds(getViewBounds(v));
2759 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002760 }
2761
2762 /**
2763 * Event handler for a click on the settings button that appears after a long press
2764 * on the home screen.
2765 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002766 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002767 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002768 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2769 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002770 }
2771
Adam Cohen61f560d2013-09-30 15:58:20 -07002772 public View.OnTouchListener getHapticFeedbackTouchListener() {
2773 if (mHapticFeedbackTouchListener == null) {
2774 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002775 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002776 @Override
2777 public boolean onTouch(View v, MotionEvent event) {
2778 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2779 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2780 }
2781 return false;
2782 }
2783 };
2784 }
2785 return mHapticFeedbackTouchListener;
2786 }
2787
Tony Wickhame2217252016-03-22 16:34:23 -07002788 @Override
2789 public void onAccessibilityStateChanged(boolean enabled) {
2790 mDragLayer.onAccessibilityStateChanged(enabled);
2791 }
2792
Adam Cohen9211d422014-10-07 18:14:53 -07002793 public void onDragStarted(View view) {
2794 if (isOnCustomContent()) {
2795 // Custom content screen doesn't participate in drag and drop. If on custom
2796 // content screen, move to default.
2797 moveWorkspaceToDefaultScreen();
2798 }
Adam Cohen9211d422014-10-07 18:14:53 -07002799 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002800
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002801 /**
2802 * Called when the user stops interacting with the launcher.
2803 * This implies that the user is now on the homescreen and is not doing housekeeping.
2804 */
Adam Cohen9211d422014-10-07 18:14:53 -07002805 protected void onInteractionEnd() {
2806 if (mLauncherCallbacks != null) {
2807 mLauncherCallbacks.onInteractionEnd();
2808 }
2809 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002810
2811 /**
2812 * Called when the user starts interacting with the launcher.
2813 * The possible interactions are:
2814 * - open all apps
2815 * - reorder an app shortcut, or a widget
2816 * - open the overview mode.
2817 * This is a good time to stop doing things that only make sense
2818 * when the user is on the homescreen and not doing housekeeping.
2819 */
Adam Cohen9211d422014-10-07 18:14:53 -07002820 protected void onInteractionBegin() {
2821 if (mLauncherCallbacks != null) {
2822 mLauncherCallbacks.onInteractionBegin();
2823 }
2824 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002825
Winson Chungcd99cd32015-04-29 11:03:24 -07002826 /** Updates the interaction state. */
2827 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002828 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002829 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002830 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2831 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002832 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002833 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002834 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002835 onInteractionEnd();
2836 }
2837 }
2838
Sunny Goyala7ce1662016-05-31 15:01:35 -07002839 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002840 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002841 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2842 try {
2843 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2844 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2845 // is enabled by default on NYC.
2846 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2847 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002848
2849 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2850 String id = ((ShortcutInfo) info).getDeepShortcutId();
2851 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002852 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2853 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002854 } else {
2855 // Could be launching some bookkeeping activity
2856 startActivity(intent, optsBundle);
2857 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002858 } finally {
2859 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002860 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002861 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002862 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2863 // corresponding permission. Show the appropriate permission prompt if that
2864 // is the case.
2865 if (intent.getComponent() == null
2866 && Intent.ACTION_CALL.equals(intent.getAction())
2867 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2868 PackageManager.PERMISSION_GRANTED) {
2869 // TODO: Rename sPendingAddItem to a generic name.
2870 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2871 0, info);
2872 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2873 REQUEST_PERMISSION_CALL_PHONE);
2874 } else {
2875 // No idea why this was thrown.
2876 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002877 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002878 }
2879 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002880
Sunny Goyala7ce1662016-05-31 15:01:35 -07002881 private Bundle getActivityLaunchOptions(View v) {
2882 if (Utilities.ATLEAST_MARSHMALLOW) {
2883 int left = 0, top = 0;
2884 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2885 if (v instanceof TextView) {
2886 // Launch from center of icon, not entire view
2887 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2888 if (icon != null) {
2889 Rect bounds = icon.getBounds();
2890 left = (width - bounds.width()) / 2;
2891 top = v.getPaddingTop();
2892 width = bounds.width();
2893 height = bounds.height();
2894 }
2895 }
2896 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2897 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2898 // On L devices, we use the device default slide-up transition.
2899 // On L MR1 devices, we use a custom version of the slide-up transition which
2900 // doesn't have the delay present in the device default.
2901 return ActivityOptions.makeCustomAnimation(
2902 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2903 }
2904 return null;
2905 }
2906
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002907 private Rect getViewBounds(View v) {
2908 int[] pos = new int[2];
2909 v.getLocationOnScreen(pos);
2910 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2911 }
2912
Sunny Goyala7ce1662016-05-31 15:01:35 -07002913 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002914 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2915 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2916 return false;
2917 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002918 // Only launch using the new animation if the shortcut has not opted out (this is a
2919 // private contract between launcher and may be ignored in the future).
2920 boolean useLaunchAnimation = (v != null) &&
2921 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2922 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2923
2924 UserHandleCompat user = null;
2925 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2926 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2927 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002928 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002929
2930 // Prepare intent
2931 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2932 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002933 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002934 }
2935 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002936 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2937 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
2938 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002939 // Shortcuts need some special checks due to legacy reasons.
2940 startShortcutIntentSafely(intent, optsBundle, item);
2941 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2942 // Could be launching some bookkeeping activity
2943 startActivity(intent, optsBundle);
2944 } else {
2945 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2946 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2947 }
2948 return true;
2949 } catch (ActivityNotFoundException|SecurityException e) {
2950 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2951 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2952 }
2953 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002954 }
2955
Adam Cohen268c4752012-06-06 17:47:33 -07002956 /**
2957 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2958 * in the DragLayer in the exact absolute location of the original FolderIcon.
2959 */
2960 private void copyFolderIconToImage(FolderIcon fi) {
2961 final int width = fi.getMeasuredWidth();
2962 final int height = fi.getMeasuredHeight();
2963
2964 // Lazy load ImageView, Bitmap and Canvas
2965 if (mFolderIconImageView == null) {
2966 mFolderIconImageView = new ImageView(this);
2967 }
2968 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2969 mFolderIconBitmap.getHeight() != height) {
2970 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2971 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2972 }
2973
2974 DragLayer.LayoutParams lp;
2975 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2976 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2977 } else {
2978 lp = new DragLayer.LayoutParams(width, height);
2979 }
2980
Adam Cohen307fe232012-08-16 17:55:58 -07002981 // The layout from which the folder is being opened may be scaled, adjust the starting
2982 // view size by this scale factor.
2983 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002984 lp.customPosition = true;
2985 lp.x = mRectForFolderAnimation.left;
2986 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002987 lp.width = (int) (scale * width);
2988 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002989
2990 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2991 fi.draw(mFolderIconCanvas);
2992 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002993 if (fi.getFolder() != null) {
2994 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2995 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002996 }
Adam Cohen268c4752012-06-06 17:47:33 -07002997 // Just in case this image view is still in the drag layer from a previous animation,
2998 // we remove it and re-add it.
2999 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3000 mDragLayer.removeView(mFolderIconImageView);
3001 }
3002 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003003 if (fi.getFolder() != null) {
3004 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003005 }
Adam Cohen268c4752012-06-06 17:47:33 -07003006 }
3007
Adam Cohen37b2a492016-04-06 18:36:06 -07003008 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003009 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07003010 FolderInfo info = (FolderInfo) fi.getTag();
3011 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3012 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003013 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3014 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003015 }
3016
Adam Cohen268c4752012-06-06 17:47:33 -07003017 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3018 copyFolderIconToImage(fi);
3019 fi.setVisibility(View.INVISIBLE);
3020
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003021 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
3022 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003023 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003024 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3025 }
3026 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003027 oa.start();
3028 }
3029
Sunny Goyal935fca12015-10-13 10:19:01 -07003030 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003031 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003032 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003033
Adam Cohen268c4752012-06-06 17:47:33 -07003034 // We remove and re-draw the FolderIcon in-case it has changed
3035 mDragLayer.removeView(mFolderIconImageView);
3036 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08003037
3038 if (cl != null) {
3039 cl.clearFolderLeaveBehind();
3040 }
3041
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003042 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003043 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003044 oa.addListener(new AnimatorListenerAdapter() {
3045 @Override
3046 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003047 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003048 // Remove the ImageView copy of the FolderIcon and make the original visible.
3049 mDragLayer.removeView(mFolderIconImageView);
3050 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003051 }
3052 }
3053 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003054 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003055 if (!animate) {
3056 oa.end();
3057 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003058 }
3059
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003060 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003061 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003062 * is animated relative to the specified View. If the View is null, no animation
3063 * is played.
3064 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003065 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003066 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003067 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003068
Adam Cohenfb91f302012-06-11 15:45:18 -07003069 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003070 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3071 if (openFolder != null && openFolder != folder) {
3072 // Close any open folder before opening a folder.
3073 closeFolder();
3074 }
3075
Adam Cohena9cf38f2011-05-02 15:36:58 -07003076 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003077
Adam Cohena9cf38f2011-05-02 15:36:58 -07003078 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003079
Sunny Goyalf4066152015-04-15 09:42:19 -07003080 // While the folder is open, the position of the icon cannot change.
3081 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3082
Adam Cohen4554ee12011-08-03 16:13:21 -07003083 // Just verify that the folder hasn't already been added to the DragLayer.
3084 // There was a one-off crash where the folder had a parent already.
3085 if (folder.getParent() == null) {
3086 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003087 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003088 } else {
3089 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3090 folder.getParent() + ").");
3091 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003092 folder.animateOpen();
3093
3094 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003095
3096 // Notify the accessibility manager that this folder "window" has appeared and occluded
3097 // the workspace items
3098 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3099 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003100 }
3101
3102 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003103 closeFolder(true);
3104 }
3105
3106 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003107 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003108 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003109 if (folder.isEditingName()) {
3110 folder.dismissEditingName();
3111 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003112 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003113 }
3114 }
3115
Sunny Goyal935fca12015-10-13 10:19:01 -07003116 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003117 animate &= !Utilities.isPowerSaverOn(this);
3118
Adam Cohen4554ee12011-08-03 16:13:21 -07003119 folder.getInfo().opened = false;
3120
3121 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3122 if (parent != null) {
3123 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003124 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003125 if (fi != null) {
3126 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3127 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003128 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003129 if (animate) {
3130 folder.animateClosed();
3131 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003132 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003133 }
Winson Chung83ca4802013-04-12 15:10:52 -07003134
Sunny Goyal935fca12015-10-13 10:19:01 -07003135 // Notify the accessibility manager that this folder "window" has disappeared and no
3136 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003137 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003138 }
3139
Tony Wickham49c8d292016-07-01 11:44:34 -07003140 public void closeShortcutsContainer() {
Sunny Goyala2454ad2016-07-22 10:50:11 -07003141 closeShortcutsContainer(true);
3142 }
3143
3144 public void closeShortcutsContainer(boolean animate) {
Tony Wickham49c8d292016-07-01 11:44:34 -07003145 DeepShortcutsContainer deepShortcutsContainer = getOpenShortcutsContainer();
3146 if (deepShortcutsContainer != null) {
Sunny Goyala2454ad2016-07-22 10:50:11 -07003147 if (animate) {
3148 deepShortcutsContainer.animateClose();
3149 } else {
3150 deepShortcutsContainer.close();
3151 }
Tony Wickham49c8d292016-07-01 11:44:34 -07003152 }
3153 }
3154
3155 /**
3156 * @return The open shortcuts container, or null if there is none
3157 */
3158 public DeepShortcutsContainer getOpenShortcutsContainer() {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07003159 // Iterate in reverse order. Shortcuts container is added later to the dragLayer,
3160 // and will be one of the last views.
3161 for (int i = mDragLayer.getChildCount() - 1; i >= 0; i--) {
3162 View child = mDragLayer.getChildAt(i);
Sunny Goyala2454ad2016-07-22 10:50:11 -07003163 if (child instanceof DeepShortcutsContainer
3164 && ((DeepShortcutsContainer) child).isOpen()) {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07003165 return (DeepShortcutsContainer) child;
3166 }
3167 }
3168 return null;
Tony Wickham49c8d292016-07-01 11:44:34 -07003169 }
3170
Tony Wickhamdadb3042016-02-24 11:07:00 -08003171 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003172 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003173 if (!isDraggingEnabled()) return false;
3174 if (isWorkspaceLocked()) return false;
3175 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003176
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07003177 if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
3178 (v == mAllAppsButton && mAllAppsButton != null)) {
Winson Chung76648c52015-07-10 14:33:23 -07003179 onLongClickAllAppsButton(v);
3180 return true;
3181 }
3182
Adam Cohen1697b792013-09-17 19:08:21 -07003183 if (v instanceof Workspace) {
3184 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003185 if (!mWorkspace.isTouchActive()) {
3186 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003187 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3188 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3189 return true;
3190 } else {
3191 return false;
3192 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003193 } else {
3194 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003195 }
Adam Cohen1697b792013-09-17 19:08:21 -07003196 }
3197
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003198 CellLayout.CellInfo longClickCellInfo = null;
3199 View itemUnderLongClick = null;
3200 if (v.getTag() instanceof ItemInfo) {
3201 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003202 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003203 itemUnderLongClick = longClickCellInfo.cell;
3204 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003205 }
3206
Winson Chung3d503fb2011-07-13 17:25:49 -07003207 // The hotseat touch handling does not go through Workspace, and we always allow long press
3208 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003209 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003210 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003211 // User long pressed on empty space
3212 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3213 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003214 if (mWorkspace.isInOverviewMode()) {
3215 mWorkspace.startReordering(v);
3216 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003217 showOverviewMode(true);
Hyunyoung Song645764e2016-06-06 14:19:02 -07003218 mHotseat.requestDisallowInterceptTouchEvent(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003219 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003220 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003221 final boolean isAllAppsButton =
3222 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3223 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3224 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003225 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003226 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003227 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003228 }
3229 }
3230 }
3231 return true;
3232 }
3233
Winson Chung3d503fb2011-07-13 17:25:49 -07003234 boolean isHotseatLayout(View layout) {
3235 return mHotseat != null && layout != null &&
3236 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3237 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003238
Winson Chung3d503fb2011-07-13 17:25:49 -07003239 /**
3240 * Returns the CellLayout of the specified container at the specified screen.
3241 */
Sunny Goyal92820592015-03-02 11:31:35 -08003242 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003243 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3244 if (mHotseat != null) {
3245 return mHotseat.getLayout();
3246 } else {
3247 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003248 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003249 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003250 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003251 }
3252 }
3253
Winson Chungb745afb2015-03-02 11:51:23 -08003254 /**
3255 * For overridden classes.
3256 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003257 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003258 return isAppsViewVisible();
3259 }
3260
3261 public boolean isAppsViewVisible() {
3262 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3263 }
3264
3265 public boolean isWidgetsViewVisible() {
3266 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003267 }
3268
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003269 private void setWorkspaceBackground(int background) {
3270 switch (background) {
3271 case WORKSPACE_BACKGROUND_TRANSPARENT:
3272 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3273 break;
3274 case WORKSPACE_BACKGROUND_BLACK:
3275 getWindow().setBackgroundDrawable(null);
3276 break;
3277 default:
3278 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3279 }
Craig Mautner360310b2012-10-26 15:13:08 -07003280 }
3281
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003282 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003283 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3284 int curflags = getWindow().getAttributes().flags
3285 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3286 if (wpflags != curflags) {
3287 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3288 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003289 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003290 }
3291
Michael Jurkae326f182011-11-21 14:05:46 -08003292 @Override
3293 public void onTrimMemory(int level) {
3294 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003295 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3296 // The widget preview db can result in holding onto over
3297 // 3MB of memory for caching which isn't necessary.
3298 SQLiteDatabase.releaseMemory();
3299
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003300 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003301 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003302 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003303 if (mLauncherCallbacks != null) {
3304 mLauncherCallbacks.onTrimMemory(level);
3305 }
Michael Jurkae326f182011-11-21 14:05:46 -08003306 }
3307
Winson5c6bdbb2015-09-03 11:36:19 -07003308 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003309 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003310 }
3311
Winson5c6bdbb2015-09-03 11:36:19 -07003312 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003313 boolean changed = mState != State.WORKSPACE ||
3314 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003315 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003316 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003317 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003318 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003319
Michael Jurkab3e22d92011-10-31 15:58:33 -07003320 // Set focus to the AppsCustomize button
3321 if (mAllAppsButton != null) {
3322 mAllAppsButton.requestFocus();
3323 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003324 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003325
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003326 // Change the state *after* we've called all the transition code
3327 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003328
Winson Chung5fb63472011-02-02 17:03:37 -08003329 // Resume the auto-advance of widgets
3330 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003331 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003332
Winson Chung0f785722015-04-08 10:27:49 -07003333 if (changed) {
3334 // Send an accessibility event to announce the context change
3335 getWindow().getDecorView()
3336 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003337 }
Winson5c6bdbb2015-09-03 11:36:19 -07003338 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003339 }
3340
Winsone9f27272015-10-13 10:47:51 -07003341 /**
3342 * Shows the overview button.
3343 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07003344 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003345 showOverviewMode(animated, false);
3346 }
3347
3348 /**
3349 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3350 * onto one of the overview panel buttons.
3351 */
3352 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3353 Runnable postAnimRunnable = null;
3354 if (requestButtonFocus) {
3355 postAnimRunnable = new Runnable() {
3356 @Override
3357 public void run() {
3358 // Hitting the menu button when in touch mode does not trigger touch mode to
3359 // be disabled, so if requested, force focus on one of the overview panel
3360 // buttons.
3361 mOverviewPanel.requestFocusFromTouch();
3362 }
3363 };
3364 }
Adam Cohened307df2013-10-02 09:37:31 -07003365 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003366 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003367 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003368 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003369 }
3370
Winson Chungb745afb2015-03-02 11:51:23 -08003371 /**
3372 * Shows the apps view.
3373 */
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003374 public void showAppsView(boolean animated, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003375 boolean focusSearchBar) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003376 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07003377 if (updatePredictedApps) {
3378 tryAndUpdatePredictedApps();
3379 }
Winson Chung76648c52015-07-10 14:33:23 -07003380 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003381 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003382
Winson Chungb745afb2015-03-02 11:51:23 -08003383 /**
3384 * Shows the widgets view.
3385 */
3386 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003387 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003388 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003389 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003390 }
Winson Chung76648c52015-07-10 14:33:23 -07003391 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003392
3393 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003394 @Override
3395 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003396 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003397 }
3398 });
Winson Chungb745afb2015-03-02 11:51:23 -08003399 }
3400
3401 /**
3402 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003403 *
3404 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003405 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003406 // TODO: calling method should use the return value so that when {@code false} is returned
3407 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003408 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003409 if (!(mState == State.WORKSPACE ||
3410 mState == State.APPS_SPRING_LOADED ||
3411 mState == State.WIDGETS_SPRING_LOADED ||
3412 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003413 return false;
3414 }
3415 if (toState != State.APPS && toState != State.WIDGETS) {
3416 return false;
3417 }
Winson Chungb745afb2015-03-02 11:51:23 -08003418
3419 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003420 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3421 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003422 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003423 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003424 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003425
Michael Jurkab3e22d92011-10-31 15:58:33 -07003426 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003427 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003428
3429 // Pause the auto-advance of widgets until we are out of AllApps
3430 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003431 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003432 closeFolder();
Tony Wickham49c8d292016-07-01 11:44:34 -07003433 closeShortcutsContainer();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003434
3435 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003436 getWindow().getDecorView()
3437 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003438 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003439 }
3440
Winson Chungcd99cd32015-04-29 11:03:24 -07003441 /**
3442 * Updates the workspace and interaction state on state change, and return the animation to this
3443 * new state.
3444 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003445 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003446 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003447 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003448 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003449 updateInteraction(fromState, toState);
3450 return anim;
3451 }
3452
Hyunyoung Song3f471442015-04-08 19:01:34 -07003453 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003454 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003455 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003456 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003457 }
Winson Chungb745afb2015-03-02 11:51:23 -08003458
Winson Chung006ee262015-08-03 14:40:11 -07003459 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003460 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003461 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003462
3463 if (isAppsViewVisible()) {
3464 mState = State.APPS_SPRING_LOADED;
3465 } else if (isWidgetsViewVisible()) {
3466 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003467 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003468 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003469 } else {
3470 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003471 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003472 }
Adam Cohen7777d962011-08-18 18:58:38 -07003473
Hyunyoung Song3f471442015-04-08 19:01:34 -07003474 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003475 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003476 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003477
Winson Chunge7a03942011-08-05 15:05:12 -07003478 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003479 @Override
3480 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003481 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003482 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3483 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003484 // Before we show workspace, hide all apps again because
3485 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3486 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003487 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003488 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003489 } else {
3490 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003491 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003492 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003493 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003494 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003495
Tony Wickhame0c33232016-02-08 11:37:04 -08003496 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003497 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3498 || mState == State.WIDGETS_SPRING_LOADED;
3499 }
3500
Michael Jurkad3ef3062010-11-23 16:23:58 -08003501 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003502 if (mState == State.APPS_SPRING_LOADED) {
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003503 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07003504 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003505 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003506 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003507 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3508 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003509 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003510 }
3511
Winson Chung4ac30062015-05-08 17:34:17 -07003512 /**
3513 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3514 * resumed.
3515 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003516 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003517 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003518 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003519 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003520 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003521 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003522 }
3523 }
3524 }
3525
Michael Jurkab3e22d92011-10-31 15:58:33 -07003526 void lockAllApps() {
3527 // TODO
3528 }
3529
3530 void unlockAllApps() {
3531 // TODO
3532 }
3533
Winsonf768d932015-09-25 16:12:35 -07003534 public boolean launcherCallbacksProvidesSearch() {
3535 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3536 }
3537
Michael Jurkad7c28052012-04-27 15:43:36 -07003538 @Override
3539 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003540 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003541 final List<CharSequence> text = event.getText();
3542 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003543 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003544 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003545 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003546 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003547 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003548 } else if (mWorkspace != null) {
3549 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003550 } else {
3551 text.add(getString(R.string.all_apps_home_button_label));
3552 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003553 return result;
3554 }
3555
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003556 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003557 * If the activity is currently paused, signal that we need to run the passed Runnable
3558 * in onResume.
3559 *
3560 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003561 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3562 * wrong when we're not running, and if the activity comes back to what the configuration was
3563 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003564 *
3565 * Implementation of the method from LauncherModel.Callbacks.
3566 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003567 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003568 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003569 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003570 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003571 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003572 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003573 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003574 }
3575 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003576 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003577 return true;
3578 } else {
3579 return false;
3580 }
3581 }
3582
Michael Jurkac402cd92013-05-20 15:49:32 +02003583 private boolean waitUntilResume(Runnable run) {
3584 return waitUntilResume(run, false);
3585 }
3586
Michael Jurka1e2f4652013-07-08 18:03:46 -07003587 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003588 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003589 }
3590
Michael Jurka7607c2f2013-04-03 14:33:19 -07003591 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003592 * If the activity is currently paused, signal that we need to re-run the loader
3593 * in onResume.
3594 *
3595 * This needs to be called from incoming places where resources might have been loaded
3596 * while we are paused. That is becaues the Configuration might be wrong
3597 * when we're not running, and if it comes back to what it was when we
3598 * were paused, we are not restarted.
3599 *
3600 * Implementation of the method from LauncherModel.Callbacks.
3601 *
3602 * @return true if we are currently paused. The caller might be able to
3603 * skip some work in that case since we will come back again.
3604 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003605 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003606 public boolean setLoadOnResume() {
3607 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003608 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003609 mOnResumeNeedsLoad = true;
3610 return true;
3611 } else {
3612 return false;
3613 }
3614 }
3615
3616 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003617 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003618 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003619 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003620 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003621 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003622 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003623 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003624 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003625 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003626 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003627
Joe Onorato9c1289c2009-08-17 11:03:03 -04003628 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003629 * Clear any pending bind callbacks. This is called when is loader is planning to
3630 * perform a full rebind from scratch.
3631 */
3632 @Override
3633 public void clearPendingBinds() {
3634 mBindOnResumeCallbacks.clear();
3635 if (mPendingExecutor != null) {
3636 mPendingExecutor.markCompleted();
3637 mPendingExecutor = null;
3638 }
3639 }
3640
3641 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003642 * Refreshes the shortcuts shown on the workspace.
3643 *
3644 * Implementation of the method from LauncherModel.Callbacks.
3645 */
3646 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003647 if (LauncherAppState.PROFILE_STARTUP) {
3648 Trace.beginSection("Starting page bind");
3649 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003650 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003651
Winson Chungd64d1762013-08-20 14:37:16 -07003652 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003653 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003654 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003655
Michael Jurka05bf644e2011-11-30 20:28:53 -08003656 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003657 if (mHotseat != null) {
3658 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003659 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003660 if (LauncherAppState.PROFILE_STARTUP) {
3661 Trace.endSection();
3662 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003663 }
3664
Adam Cohendcd297f2013-06-18 13:13:40 -07003665 @Override
3666 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003667 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003668 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3669 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003670 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3671 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3672 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003673 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3674 // If there are no screens, we need to have an empty screen
3675 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003676 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003677 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003678
3679 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003680 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003681 if (hasCustomContentToLeft()) {
3682 mWorkspace.createCustomContentContainer();
3683 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003684 }
Winsonc7d2e832016-07-28 12:24:55 -07003685
3686 // After we have added all the screens, if the wallpaper was locked to the default state,
3687 // then notify to indicate that it can be released and a proper wallpaper offset can be
3688 // computed before the next layout
3689 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07003690 }
3691
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003692 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003693 int count = orderedScreenIds.size();
3694 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003695 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003696 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003697 // No need to bind the first screen, as its always bound.
3698 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3699 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003700 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003701 }
3702
Winson Chungd64d1762013-08-20 14:37:16 -07003703 public void bindAppsAdded(final ArrayList<Long> newScreens,
3704 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003705 final ArrayList<ItemInfo> addAnimated,
3706 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003707 Runnable r = new Runnable() {
3708 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003709 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003710 }
3711 };
3712 if (waitUntilResume(r)) {
3713 return;
3714 }
3715
Winson Chungd64d1762013-08-20 14:37:16 -07003716 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003717 if (newScreens != null) {
3718 bindAddScreens(newScreens);
3719 }
Winson Chungd64d1762013-08-20 14:37:16 -07003720
3721 // We add the items without animation on non-visible pages, and with
3722 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003723 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003724 bindItems(addNotAnimated, 0,
3725 addNotAnimated.size(), false);
3726 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003727 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003728 bindItems(addAnimated, 0,
3729 addAnimated.size(), true);
3730 }
Winson Chungc58497e2013-09-03 17:48:37 -07003731
Winson Chung87412982013-10-03 18:34:14 -07003732 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003733 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003734
Winson Chungb745afb2015-03-02 11:51:23 -08003735 if (addedApps != null && mAppsView != null) {
3736 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003737 }
Winson Chungd64d1762013-08-20 14:37:16 -07003738 }
3739
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003740 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003741 * Bind the items start-end from the list.
3742 *
3743 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003744 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003745 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003746 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3747 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003748 Runnable r = new Runnable() {
3749 public void run() {
3750 bindItems(shortcuts, start, end, forceAnimateIcons);
3751 }
3752 };
3753 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003754 return;
3755 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003756
Winson Chungf0c6ae02012-03-21 16:10:31 -07003757 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003758 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3759 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003760 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003761 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003762 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003763 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003764 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003765
3766 // Short circuit if we are loading dock items for a configuration which has no dock
3767 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3768 mHotseat == null) {
3769 continue;
3770 }
3771
Sunny Goyal639e9062015-08-19 19:17:06 -07003772 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003773 switch (item.itemType) {
3774 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3775 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003776 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003777 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003778 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003779 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003780 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003781 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003782 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003783 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003784 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003785 default:
3786 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003787 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003788
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003789 /*
3790 * Remove colliding items.
3791 */
3792 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3793 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3794 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3795 View v = cl.getChildAt(item.cellX, item.cellY);
3796 Object tag = v.getTag();
3797 String desc = "Collision while binding workspace item: " + item
3798 + ". Collides with " + tag;
3799 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3800 throw (new RuntimeException(desc));
3801 } else {
3802 Log.d(TAG, desc);
3803 LauncherModel.deleteItemFromDatabase(this, item);
3804 continue;
3805 }
3806 }
3807 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003808 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3809 item.cellY, 1, 1);
3810 if (animateIcons) {
3811 // Animate all the applications up now
3812 view.setAlpha(0f);
3813 view.setScaleX(0f);
3814 view.setScaleY(0f);
3815 bounceAnims.add(createNewAppBounceAnimation(view, i));
3816 newShortcutsScreenId = item.screenId;
3817 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003818 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003819
Winson Chung997a9232013-07-24 15:33:46 -07003820 if (animateIcons) {
3821 // Animate to the correct page
3822 if (newShortcutsScreenId > -1) {
3823 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003824 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003825 final Runnable startBounceAnimRunnable = new Runnable() {
3826 public void run() {
3827 anim.playTogether(bounceAnims);
3828 anim.start();
3829 }
3830 };
Winson Chung997a9232013-07-24 15:33:46 -07003831 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003832 // We post the animation slightly delayed to prevent slowdowns
3833 // when we are loading right after we return to launcher.
3834 mWorkspace.postDelayed(new Runnable() {
3835 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003836 if (mWorkspace != null) {
3837 mWorkspace.snapToPage(newScreenIndex);
3838 mWorkspace.postDelayed(startBounceAnimRunnable,
3839 NEW_APPS_ANIMATION_DELAY);
3840 }
Winson Chung94d67682013-09-25 16:29:40 -07003841 }
3842 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003843 } else {
3844 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003845 }
3846 }
Winson Chung64359a52013-07-08 17:17:08 -07003847 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003848 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003849 }
3850
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003851 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3852 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3853 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003854 view.updateAppWidget(null);
3855 view.setOnClickListener(this);
3856 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003857 mWorkspace.requestLayout();
3858 }
3859
Joe Onorato9c1289c2009-08-17 11:03:03 -04003860 /**
3861 * Add the views for a widget to the workspace.
3862 *
3863 * Implementation of the method from LauncherModel.Callbacks.
3864 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003865 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003866 Runnable r = new Runnable() {
3867 public void run() {
3868 bindAppWidget(item);
3869 }
3870 };
3871 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003872 return;
3873 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003874
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003875 if (mIsSafeModeEnabled) {
3876 bindSafeModeWidget(item);
3877 return;
3878 }
3879
Daniel Sandler843e8602010-06-07 14:59:01 -04003880 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3881 if (DEBUG_WIDGETS) {
3882 Log.d(TAG, "bindAppWidget: " + item);
3883 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003884
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003885 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003886
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003887 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3888 // If the provider is not ready, bind as a pending widget.
3889 appWidgetInfo = null;
3890 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3891 // The widget id is not valid. Try to find the widget based on the provider info.
3892 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3893 } else {
3894 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3895 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003896
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003897 // If the provider is ready, but the width is not yet restored, try to restore it.
3898 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3899 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003900 if (appWidgetInfo == null) {
3901 if (DEBUG_WIDGETS) {
3902 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3903 + " belongs to component " + item.providerName
3904 + ", as the povider is null");
3905 }
3906 LauncherModel.deleteItemFromDatabase(this, item);
3907 return;
3908 }
Sunny Goyalff572272014-07-23 13:58:07 -07003909
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003910 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003911 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003912 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3913 // Id has not been allocated yet. Allocate a new id.
3914 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3915 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003916
Sunny Goyald478c832016-04-01 12:04:16 -07003917 // Also try to bind the widget. If the bind fails, the user will be shown
3918 // a click to setup UI, which will ask for the bind permission.
3919 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3920 pendingInfo.spanX = item.spanX;
3921 pendingInfo.spanY = item.spanY;
3922 pendingInfo.minSpanX = item.minSpanX;
3923 pendingInfo.minSpanY = item.minSpanY;
3924 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
3925 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3926 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003927
Sunny Goyald478c832016-04-01 12:04:16 -07003928 // Bind succeeded
3929 if (success) {
3930 // If the widget has a configure activity, it is still needs to set it up,
3931 // otherwise the widget is ready to go.
3932 item.restoreStatus = (appWidgetInfo.configure == null)
3933 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3934 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003935 }
Sunny Goyald478c832016-04-01 12:04:16 -07003936
3937 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003938 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003939 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003940 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003941 // The widget was marked as UI not ready, but there is no configure activity to
3942 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003943 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3944 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003945 }
Sunny Goyalff572272014-07-23 13:58:07 -07003946 }
3947
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003948 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003949 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003950 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3951 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003952 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003953
Sunny Goyal56c73602015-09-25 12:55:01 -07003954 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003955 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003956 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003957 deleteWidgetInfo(item);
3958 return;
3959 }
3960
Sunny Goyal233ee962015-08-03 13:05:01 -07003961 item.minSpanX = appWidgetInfo.minSpanX;
3962 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003963 addAppWidgetToWorkspace(
3964 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3965 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003966 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003967 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003968 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003969 view.updateAppWidget(null);
3970 view.setOnClickListener(this);
3971 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003972 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003973 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003974
Daniel Sandler843e8602010-06-07 14:59:01 -04003975 if (DEBUG_WIDGETS) {
3976 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3977 + (SystemClock.uptimeMillis()-start) + "ms");
3978 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003979 }
3980
Sunny Goyalff572272014-07-23 13:58:07 -07003981 /**
3982 * Restores a pending widget.
3983 *
3984 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003985 */
Sunny Goyald478c832016-04-01 12:04:16 -07003986 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003987 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3988 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3989 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003990 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003991 }
3992
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003993 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003994 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003995
3996 mWorkspace.reinflateWidgetsIfNecessary();
3997 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003998 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003999 }
4000
Adam Cohen1462de32012-07-24 22:34:36 -07004001 public void onPageBoundSynchronously(int page) {
4002 mSynchronouslyBoundPages.add(page);
4003 }
4004
Sunny Goyal527c7d32015-08-28 15:19:36 -07004005 @Override
4006 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
4007 if (mPendingExecutor != null) {
4008 mPendingExecutor.markCompleted();
4009 }
4010 mPendingExecutor = executor;
4011 executor.attachTo(this);
4012 }
4013
4014 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4015 if (mPendingExecutor == executor) {
4016 mPendingExecutor = null;
4017 }
4018 }
4019
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07004020 @Override
4021 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
4022 Runnable r = new Runnable() {
4023 public void run() {
4024 finishFirstPageBind(executor);
4025 }
4026 };
4027 if (waitUntilResume(r)) {
4028 return;
4029 }
4030
4031 Runnable onComplete = new Runnable() {
4032 @Override
4033 public void run() {
4034 if (executor != null) {
4035 executor.onLoadAnimationCompleted();
4036 }
4037 }
4038 };
4039 if (mDragLayer.getAlpha() < 1) {
4040 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
4041 } else {
4042 onComplete.run();
4043 }
4044 }
4045
Joe Onorato9c1289c2009-08-17 11:03:03 -04004046 /**
4047 * Callback saying that there aren't any more items to bind.
4048 *
4049 * Implementation of the method from LauncherModel.Callbacks.
4050 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004051 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004052 Runnable r = new Runnable() {
4053 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004054 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004055 }
4056 };
4057 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004058 return;
4059 }
Sunny Goyale26d1002016-06-20 14:52:14 -07004060 if (LauncherAppState.PROFILE_STARTUP) {
4061 Trace.beginSection("Page bind completed");
4062 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004063 if (mSavedState != null) {
4064 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004065 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004066 }
Sunny Goyal08442b82015-10-21 17:15:08 -07004067
Joe Onorato9c1289c2009-08-17 11:03:03 -04004068 mSavedState = null;
4069 }
4070
Adam Cohen1462de32012-07-24 22:34:36 -07004071 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004072
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004073 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07004074
4075 // If we received the result of any pending adds while the loader was running (e.g. the
4076 // widget configuration forced an orientation change), process them now.
4077 if (sPendingAddItem != null) {
4078 final long screenId = completeAdd(sPendingAddItem);
4079
4080 // TODO: this moves the user to the page where the pending item was added. Ideally,
4081 // the screen would be guaranteed to exist after bind, and the page would be set through
4082 // the workspace restore process.
4083 mWorkspace.post(new Runnable() {
4084 @Override
4085 public void run() {
4086 mWorkspace.snapToScreenId(screenId);
4087 }
4088 });
4089 sPendingAddItem = null;
4090 }
4091
Sunny Goyal756adbc2015-04-16 15:20:51 -07004092 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004093
4094 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004095 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004096 }
Sunny Goyale26d1002016-06-20 14:52:14 -07004097 if (LauncherAppState.PROFILE_STARTUP) {
4098 Trace.endSection();
4099 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004100 }
4101
Winson Chunga2413752012-04-03 14:22:34 -07004102 private boolean canRunNewAppsAnimation() {
4103 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07004104 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07004105 }
4106
Winson Chungc9168342013-06-26 14:54:55 -07004107 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004108 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004109 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4110 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004111 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004112 return bounceAnim;
4113 }
4114
Adam Cohen27772732013-11-11 14:00:56 +00004115 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004116 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004117 }
4118
Tony Wickham55616cd2015-09-23 14:55:17 -07004119 public int getSearchBarHeight() {
4120 if (mLauncherCallbacks != null) {
4121 return mLauncherCallbacks.getSearchBarHeight();
4122 }
4123 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4124 }
4125
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004126 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004127 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4128 * multiple calls to bind the same list.)
4129 */
4130 @Thunk ArrayList<AppInfo> mTmpAppsList;
4131 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4132 public void run() {
4133 bindAllApplications(mTmpAppsList);
4134 mTmpAppsList = null;
4135 }
4136 };
4137
4138 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004139 * Add the icons for all apps.
4140 *
4141 * Implementation of the method from LauncherModel.Callbacks.
4142 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004143 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004144 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4145 mTmpAppsList = apps;
4146 return;
4147 }
4148
Winson Chungb745afb2015-03-02 11:51:23 -08004149 if (mAppsView != null) {
4150 mAppsView.setApps(apps);
4151 }
Adam Cohen9211d422014-10-07 18:14:53 -07004152 if (mLauncherCallbacks != null) {
4153 mLauncherCallbacks.bindAllApplications(apps);
4154 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004155 }
4156
4157 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004158 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
4159 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
4160 */
4161 @Override
4162 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
4163 mDeepShortcutMap = deepShortcutMapCopy;
4164 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
4165 }
4166
Tony Wickham1bce7fd2016-04-28 17:39:03 -07004167 public List<String> getShortcutIdsForItem(ItemInfo info) {
4168 if (!DeepShortcutManager.supportsShortcuts(info)) {
4169 return Collections.EMPTY_LIST;
4170 }
4171 ComponentName component = info.getTargetComponent();
4172 List<String> ids = mDeepShortcutMap.get(new ComponentKey(component, info.user));
4173 return ids == null ? Collections.EMPTY_LIST : ids;
4174 }
4175
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004176 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004177 * A package was updated.
4178 *
4179 * Implementation of the method from LauncherModel.Callbacks.
4180 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004181 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004182 Runnable r = new Runnable() {
4183 public void run() {
4184 bindAppsUpdated(apps);
4185 }
4186 };
4187 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004188 return;
4189 }
4190
Winson Chungb745afb2015-03-02 11:51:23 -08004191 if (mAppsView != null) {
4192 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004193 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004194 }
4195
Sunny Goyal4390ace2014-10-13 11:33:11 -07004196 @Override
4197 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4198 Runnable r = new Runnable() {
4199 public void run() {
4200 bindWidgetsRestored(widgets);
4201 }
4202 };
4203 if (waitUntilResume(r)) {
4204 return;
4205 }
4206 mWorkspace.widgetsRestored(widgets);
4207 }
4208
Joe Onorato9c1289c2009-08-17 11:03:03 -04004209 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004210 * Some shortcuts were updated in the background.
4211 *
4212 * Implementation of the method from LauncherModel.Callbacks.
4213 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004214 @Override
4215 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4216 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004217 Runnable r = new Runnable() {
4218 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004219 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004220 }
4221 };
4222 if (waitUntilResume(r)) {
4223 return;
4224 }
4225
Sunny Goyal4390ace2014-10-13 11:33:11 -07004226 if (!updated.isEmpty()) {
4227 mWorkspace.updateShortcuts(updated);
4228 }
4229
4230 if (!removed.isEmpty()) {
4231 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4232 for (ShortcutInfo si : removed) {
4233 removedComponents.add(si.getTargetComponent());
4234 }
4235 mWorkspace.removeItemsByComponentName(removedComponents, user);
4236 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004237 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004238 }
4239 }
4240
4241 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004242 * Update the state of a package, typically related to install state.
4243 *
4244 * Implementation of the method from LauncherModel.Callbacks.
4245 */
Sunny Goyale755d462014-07-22 13:48:29 -07004246 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004247 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4248 Runnable r = new Runnable() {
4249 public void run() {
4250 bindRestoreItemsChange(updates);
4251 }
4252 };
4253 if (waitUntilResume(r)) {
4254 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004255 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004256
Sunny Goyal756adbc2015-04-16 15:20:51 -07004257 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004258 }
4259
4260 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004261 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004262 * in addition to specific applications to remove, the reason being that
4263 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004264 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004265 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004266 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004267 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004268 public void bindWorkspaceComponentsRemoved(
4269 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4270 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004271 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004272 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004273 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004274 }
Winson Chungd64d1762013-08-20 14:37:16 -07004275 };
4276 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004277 return;
4278 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004279 if (!packageNames.isEmpty()) {
4280 mWorkspace.removeItemsByPackageName(packageNames, user);
4281 }
4282 if (!components.isEmpty()) {
4283 mWorkspace.removeItemsByComponentName(components, user);
4284 }
4285 // Notify the drag controller
4286 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004287
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004288 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004289
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004290 @Override
4291 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4292 Runnable r = new Runnable() {
4293 public void run() {
4294 bindAppInfosRemoved(appInfos);
4295 }
4296 };
4297 if (waitUntilResume(r)) {
4298 return;
Joe Onorato36115782010-06-17 13:28:48 -04004299 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004300
Winson Chungdf95eb12013-10-16 14:57:07 -07004301 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004302 if (mAppsView != null) {
4303 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004304 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004305 }
Joe Onoratobe386092009-11-17 17:32:16 -08004306
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004307 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004308 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004309 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004310 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004311 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004312
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004313 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004314 public void bindWidgetsModel(WidgetsModel model) {
4315 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004316 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004317 return;
4318 }
4319
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004320 if (mWidgetsView != null && model != null) {
4321 mWidgetsView.addWidgets(model);
4322 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004323 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004324 }
4325
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004326 @Override
4327 public void notifyWidgetProvidersChanged() {
4328 if (mWorkspace.getState().shouldUpdateWidget) {
4329 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4330 }
4331 }
4332
Winson Chung400438b2011-01-16 17:53:48 -08004333 private int mapConfigurationOriActivityInfoOri(int configOri) {
4334 final Display d = getWindowManager().getDefaultDisplay();
4335 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4336 switch (d.getRotation()) {
4337 case Surface.ROTATION_0:
4338 case Surface.ROTATION_180:
4339 // We are currently in the same basic orientation as the natural orientation
4340 naturalOri = configOri;
4341 break;
4342 case Surface.ROTATION_90:
4343 case Surface.ROTATION_270:
4344 // We are currently in the other basic orientation to the natural orientation
4345 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4346 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4347 break;
4348 }
4349
4350 int[] oriMap = {
4351 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4352 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4353 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4354 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4355 };
4356 // Since the map starts at portrait, we need to offset if this device's natural orientation
4357 // is landscape.
4358 int indexOffset = 0;
4359 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4360 indexOffset = 1;
4361 }
4362 return oriMap[(d.getRotation() + indexOffset) % 4];
4363 }
Adam Cohen446e9402011-09-15 18:21:21 -07004364
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004365 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004366 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004367 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004368 if (Utilities.ATLEAST_JB_MR2) {
4369 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4370 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004371 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4372 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004373 }
Winson Chung4b919f82012-05-01 10:44:08 -07004374 }
4375 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004376
Winson Chung4b919f82012-05-01 10:44:08 -07004377 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004378 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004379 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004380 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004381 } else {
4382 mHandler.postDelayed(new Runnable() {
4383 public void run() {
4384 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4385 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004386 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004387 }
Winson Chung4b919f82012-05-01 10:44:08 -07004388 }
Winson Chung400438b2011-01-16 17:53:48 -08004389 }
4390
Adam Cohenea90f832014-05-21 15:03:34 -07004391 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004392 * To be overridden by subclasses to indicate that there is an activity to launch
4393 * before showing the standard launcher experience.
4394 */
4395 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004396 if (mLauncherCallbacks != null) {
4397 return mLauncherCallbacks.hasFirstRunActivity();
4398 }
Adam Cohen432609a2014-03-13 17:03:22 -07004399 return false;
4400 }
4401
4402 /**
4403 * To be overridden by subclasses to launch any first run activity
4404 */
4405 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004406 if (mLauncherCallbacks != null) {
4407 return mLauncherCallbacks.getFirstRunActivity();
4408 }
Adam Cohen432609a2014-03-13 17:03:22 -07004409 return null;
4410 }
4411
Winson Chunga6945242014-01-08 14:04:34 -08004412 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004413 return !ActivityManager.isRunningInTestHarness() &&
4414 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004415 }
4416
Adam Cohen432609a2014-03-13 17:03:22 -07004417 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004418 if (shouldRunFirstRunActivity() &&
4419 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004420 Intent firstRunIntent = getFirstRunActivity();
4421 if (firstRunIntent != null) {
4422 startActivity(firstRunIntent);
4423 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004424 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004425 }
4426 }
Adam Cohen432609a2014-03-13 17:03:22 -07004427 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004428 }
4429
4430 private void markFirstRunActivityShown() {
4431 SharedPreferences.Editor editor = mSharedPrefs.edit();
4432 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4433 editor.apply();
4434 }
4435
Hyunyoung Songc001cf52016-07-21 17:32:43 -07004436 private void markAppsViewShown() {
4437 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4438 return;
4439 }
4440 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
4441 }
4442
4443 private boolean shouldShowDiscoveryBounce() {
4444 if (mState != mState.WORKSPACE) {
4445 return false;
4446 }
4447 if (mLauncherCallbacks != null && mLauncherCallbacks.shouldShowDiscoveryBounce()) {
4448 return true;
4449 }
4450 if (!mIsResumeFromActionScreenOff) {
4451 return false;
4452 }
4453 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4454 return false;
4455 }
4456 return true;
4457 }
4458
Adam Cohen432609a2014-03-13 17:03:22 -07004459 /**
4460 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4461 * screen that must be displayed and dismissed.
4462 */
4463 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004464 if (mLauncherCallbacks != null) {
4465 return mLauncherCallbacks.hasDismissableIntroScreen();
4466 }
Adam Cohen432609a2014-03-13 17:03:22 -07004467 return false;
4468 }
4469
4470 /**
4471 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4472 */
4473 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004474 if (mLauncherCallbacks != null) {
4475 return mLauncherCallbacks.getIntroScreen();
4476 }
Adam Cohen432609a2014-03-13 17:03:22 -07004477 return null;
4478 }
4479
4480 /**
4481 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4482 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4483 */
4484 private boolean shouldShowIntroScreen() {
4485 return hasDismissableIntroScreen() &&
4486 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4487 }
4488
4489 protected void showIntroScreen() {
4490 View introScreen = getIntroScreen();
4491 changeWallpaperVisiblity(false);
4492 if (introScreen != null) {
4493 mDragLayer.showOverlayView(introScreen);
4494 }
4495 }
4496
Winson Chung5ffd51d2015-06-25 11:36:50 -07004497 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004498 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004499 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004500 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004501 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004502 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004503 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4504 if (userHandle != null) {
4505 user = UserHandleCompat.fromUser(userHandle);
4506 }
4507 }
4508 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004509 }
4510
4511 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004512 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004513 if (user == null) {
4514 user = UserHandleCompat.myUserHandle();
4515 }
4516
4517 // Called from search suggestion, add the profile extra to the intent to ensure that we
4518 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004519 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004520 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004521 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004522 return null;
4523 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004524 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004525 }
4526
Winson Chung5ffd51d2015-06-25 11:36:50 -07004527 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004528 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4529 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004530 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004531 UserHandleCompat.myUserHandle());
4532 }
4533
Winson Chung5ffd51d2015-06-25 11:36:50 -07004534 protected void moveWorkspaceToDefaultScreen() {
4535 mWorkspace.moveToDefaultScreen(false);
4536 }
4537
Winson Chung80baf5a2010-08-09 16:03:15 -07004538 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004539 * Returns a FastBitmapDrawable with the icon, accurately sized.
4540 */
4541 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4542 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4543 d.setFilterBitmap(true);
4544 resizeIconDrawable(d);
4545 return d;
4546 }
4547
4548 /**
4549 * Resizes an icon drawable to the correct icon size.
4550 */
Winson5fbe0742015-09-30 15:33:00 -07004551 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004552 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004553 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004554 }
4555
4556 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004557 * Prints out out state for debugging.
4558 */
4559 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004560 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004561 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004562 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4563 Log.d(TAG, "mRestoring=" + mRestoring);
4564 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004565 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004566 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004567
Daniel Sandler325dc232013-06-05 22:57:57 -04004568 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004569 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004570
4571 @Override
4572 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4573 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004574 // Dump workspace
4575 writer.println(prefix + "Workspace Items");
4576 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4577 writer.println(prefix + " Homescreen " + i);
4578
4579 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4580 for (int j = 0; j < layout.getChildCount(); j++) {
4581 Object tag = layout.getChildAt(j).getTag();
4582 if (tag != null) {
4583 writer.println(prefix + " " + tag.toString());
4584 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004585 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004586 }
Sunny Goyala1365452015-10-01 15:46:24 -07004587
4588 writer.println(prefix + " Hotseat");
4589 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4590 for (int j = 0; j < layout.getChildCount(); j++) {
4591 Object tag = layout.getChildAt(j).getTag();
4592 if (tag != null) {
4593 writer.println(prefix + " " + tag.toString());
4594 }
4595 }
4596
Sunny Goyal713edfc2016-05-06 09:58:34 -07004597 try {
4598 FileLog.flushAll(writer);
4599 } catch (Exception e) {
4600 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004601 }
4602
Adam Cohen9211d422014-10-07 18:14:53 -07004603 if (mLauncherCallbacks != null) {
4604 mLauncherCallbacks.dump(prefix, fd, writer, args);
4605 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004606 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004607
Adam Cohen59400422014-03-05 18:07:04 -08004608 public static CustomAppWidget getCustomAppWidget(String name) {
4609 return sCustomAppWidgets.get(name);
4610 }
4611
4612 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4613 return sCustomAppWidgets;
4614 }
4615
Sunny Goyal29538332016-02-18 09:10:19 -08004616 public static List<View> getFolderContents(View icon) {
4617 if (icon instanceof FolderIcon) {
4618 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4619 } else {
4620 return Collections.EMPTY_LIST;
4621 }
4622 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004623
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004624 public static Launcher getLauncher(Context context) {
4625 if (context instanceof Launcher) {
4626 return (Launcher) context;
4627 }
4628 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4629 }
4630
Sunny Goyal745bad92016-05-02 10:54:12 -07004631 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4632
4633 @Override
4634 public void onSharedPreferenceChanged(
4635 SharedPreferences sharedPreferences, String key) {
4636 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4637 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4638 if (!waitUntilResume(this, true)) {
4639 run();
4640 }
4641 }
4642 }
4643
4644 @Override
4645 public void run() {
4646 setOrientation();
4647 }
4648 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004649}