blob: 78e0aa0c161cf9bad5102bce591807a433cc8a3a [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);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001379 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001380 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001381
Tony Wickham34d2c912015-09-11 09:27:58 -07001382 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001383 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001384
Winson Chungef7f8742015-06-04 17:18:17 -07001385 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001386 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001387 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001388 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1389 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1390 } else {
1391 mAppsView.setSearchBarController(new DefaultAppSearchController());
1392 }
Craig Mautner360310b2012-10-26 15:13:08 -07001393
Winson Chung3d503fb2011-07-13 17:25:49 -07001394 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001395 mDragController.setDragScoller(mWorkspace);
1396 mDragController.setScrollView(mDragLayer);
1397 mDragController.setMoveTarget(mWorkspace);
1398 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001399 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001400
Peter Schiller310a9882016-06-30 13:52:36 -07001401 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1402 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1403 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001404
Sunny Goyal322d5562015-06-25 19:35:49 -07001405 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1406 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001407 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001408 }
1409
Winsone9f27272015-10-13 10:47:51 -07001410 private void setupOverviewPanel() {
1411 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1412
1413 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1414 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1415 @Override
1416 public boolean onLongClick(View v) {
1417 return v.performClick();
1418 }
1419 };
1420
1421 // Bind wallpaper button actions
1422 View wallpaperButton = findViewById(R.id.wallpaper_button);
1423 wallpaperButton.setOnClickListener(new OnClickListener() {
1424 @Override
1425 public void onClick(View view) {
1426 if (!mWorkspace.isSwitchingState()) {
1427 onClickWallpaperPicker(view);
1428 }
1429 }
1430 });
1431 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1432 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1433
1434 // Bind widget button actions
1435 mWidgetsButton = findViewById(R.id.widget_button);
1436 mWidgetsButton.setOnClickListener(new OnClickListener() {
1437 @Override
1438 public void onClick(View view) {
1439 if (!mWorkspace.isSwitchingState()) {
1440 onClickAddWidgetButton(view);
1441 }
1442 }
1443 });
1444 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1445 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1446
1447 // Bind settings actions
1448 View settingsButton = findViewById(R.id.settings_button);
1449 boolean hasSettings = hasSettings();
1450 if (hasSettings) {
1451 settingsButton.setOnClickListener(new OnClickListener() {
1452 @Override
1453 public void onClick(View view) {
1454 if (!mWorkspace.isSwitchingState()) {
1455 onClickSettingsButton(view);
1456 }
1457 }
1458 });
1459 settingsButton.setOnLongClickListener(performClickOnLongClick);
1460 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1461 } else {
1462 settingsButton.setVisibility(View.GONE);
1463 }
1464
1465 mOverviewPanel.setAlpha(0f);
1466 }
1467
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001469 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001470 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001471 */
1472 public void setAllAppsButton(View allAppsButton) {
1473 mAllAppsButton = allAppsButton;
1474 }
1475
Sunny Goyalbb011da2016-06-15 15:42:29 -07001476 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001477 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001478 }
1479
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001480 public View getWidgetsButton() {
1481 return mWidgetsButton;
1482 }
1483
Anjali Koppal5ad44842014-03-10 20:34:39 -07001484 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 * Creates a view representing a shortcut.
1486 *
1487 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001489 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001490 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 }
1492
1493 /**
1494 * Creates a view representing a shortcut inflated from the specified resource.
1495 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 * @param parent The group the shortcut belongs to.
1497 * @param info The data structure describing the shortcut.
1498 *
1499 * @return A View inflated from layoutResId.
1500 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001501 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001502 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001503 parent, false);
1504 favorite.applyFromShortcutInfo(info, mIconCache);
1505 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001507 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 return favorite;
1509 }
1510
1511 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 * Add a shortcut to the workspace.
1513 *
1514 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001516 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001517 int cellY) {
1518 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001519 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001520
Sunny Goyal5c97f512015-05-19 16:03:28 -07001521 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001522 if (info == null) {
1523 return;
1524 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001525 final View view = createShortcut(info);
1526
Sunny Goyal5c97f512015-05-19 16:03:28 -07001527 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001528 // First we check if we already know the exact location where we want to add this item.
1529 if (cellX >= 0 && cellY >= 0) {
1530 cellXY[0] = cellX;
1531 cellXY[1] = cellY;
1532 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001533
1534 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001535 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001536 true, null,null)) {
1537 return;
1538 }
1539 DragObject dragObject = new DragObject();
1540 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001541 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1542 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001543 return;
1544 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001545 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001546 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001547 }
1548
1549 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001550 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001551 return;
1552 }
1553
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001554 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555
1556 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001557 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001558 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 }
1560 }
1561
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001563 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001565 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566 */
Adam Cohen091440a2015-03-18 14:16:05 -07001567 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001568 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1569
1570 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001571 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001572 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001573 }
Romain Guycbb89e42009-06-08 15:52:54 -07001574
Adam Cohen59400422014-03-05 18:07:04 -08001575 if (appWidgetInfo.isCustomWidget) {
1576 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001577 }
1578
Adam Cohen59400422014-03-05 18:07:04 -08001579 LauncherAppWidgetInfo launcherInfo;
1580 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1581 launcherInfo.spanX = info.spanX;
1582 launcherInfo.spanY = info.spanY;
1583 launcherInfo.minSpanX = info.minSpanX;
1584 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001585 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001586
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001588 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589
1590 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001591 if (hostView == null) {
1592 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001593 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001594 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001595 hostView.setVisibility(View.VISIBLE);
1596 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001598 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 }
Romain Guycbb89e42009-06-08 15:52:54 -07001600
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001601 private void addAppWidgetToWorkspace(
1602 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001603 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001604 hostView.setTag(item);
1605 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001606
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001607 hostView.setFocusable(true);
1608 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001609
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001610 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001611 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1612
1613 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001614 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001615 }
1616 }
1617
Adam Cohended9f8d2010-11-03 13:25:16 -07001618 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1619 @Override
1620 public void onReceive(Context context, Intent intent) {
1621 final String action = intent.getAction();
1622 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1623 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001624 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001625 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001626
Winson Chungc100e8e2011-08-09 16:02:43 -07001627 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001628 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001629 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001630 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001631 if (!showWorkspace(false)) {
1632 // If we are already on the workspace, then manually reset all apps
1633 mAppsView.reset();
1634 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001635 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001636 mIsResumeFromActionScreenOff = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001637 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1638 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001639 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001640 }
1641 }
1642 };
1643
1644 @Override
1645 public void onAttachedToWindow() {
1646 super.onAttachedToWindow();
1647
1648 // Listen for broadcasts related to user-presence
1649 final IntentFilter filter = new IntentFilter();
1650 filter.addAction(Intent.ACTION_SCREEN_OFF);
1651 filter.addAction(Intent.ACTION_USER_PRESENT);
1652 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001653 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001654 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001655 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001656
1657 if (mLauncherCallbacks != null) {
1658 mLauncherCallbacks.onAttachedToWindow();
1659 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001660 }
1661
1662 @Override
1663 public void onDetachedFromWindow() {
1664 super.onDetachedFromWindow();
1665 mVisible = false;
1666
Adam Cohend113e0c2010-11-11 10:48:05 -08001667 if (mAttached) {
1668 unregisterReceiver(mReceiver);
1669 mAttached = false;
1670 }
Winson Chungb745afb2015-03-02 11:51:23 -08001671 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001672
1673 if (mLauncherCallbacks != null) {
1674 mLauncherCallbacks.onDetachedFromWindow();
1675 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001676 }
1677
1678 public void onWindowVisibilityChanged(int visibility) {
1679 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001680 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001681 // The following code used to be in onResume, but it turns out onResume is called when
1682 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1683 // is a more appropriate event to handle
1684 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001685 if (!mWorkspaceLoading) {
1686 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001687 // We want to let Launcher draw itself at least once before we force it to build
1688 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001689 // apps is nice and speedy.
1690 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001691 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001692 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001693 if (mStarted) return;
1694 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001695 // We delay the layer building a bit in order to give
1696 // other message processing a time to run. In particular
1697 // this avoids a delay in hiding the IME if it was
1698 // currently shown, because doing that may involve
1699 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001700 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001701 final ViewTreeObserver.OnDrawListener listener = this;
1702 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001703 public void run() {
1704 if (mWorkspace != null &&
1705 mWorkspace.getViewTreeObserver() != null) {
1706 mWorkspace.getViewTreeObserver().
1707 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001708 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001709 }
1710 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001711 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001712 }
1713 });
1714 }
1715 clearTypedText();
1716 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001717 }
1718
Adam Cohen091440a2015-03-18 14:16:05 -07001719 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001720 mHandler.removeMessages(ADVANCE_MSG);
1721 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1722 mHandler.sendMessageDelayed(msg, delay);
1723 mAutoAdvanceSentTime = System.currentTimeMillis();
1724 }
1725
Adam Cohen091440a2015-03-18 14:16:05 -07001726 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001727 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1728 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1729 mAutoAdvanceRunning = autoAdvanceRunning;
1730 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001731 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001732 sendAdvanceMessage(delay);
1733 } else {
1734 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001735 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001736 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1737 }
1738 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001739 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001740 }
1741 }
1742 }
1743
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001744 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1745
Adam Cohended9f8d2010-11-03 13:25:16 -07001746 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001747 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001748 if (msg.what == ADVANCE_MSG) {
1749 int i = 0;
1750 for (View key: mWidgetsToAdvance.keySet()) {
1751 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001752 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001753 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001754 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001755 public void run() {
1756 ((Advanceable) v).advance();
1757 }
1758 }, delay);
1759 }
1760 i++;
1761 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001762 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001763 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001764 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001765 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001766 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001767
Winsonc0b52fe2015-09-09 16:38:15 -07001768 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001769 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001770 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1771 if (v instanceof Advanceable) {
1772 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001773 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001774 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001775 }
1776 }
1777
Winsonc0b52fe2015-09-09 16:38:15 -07001778 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001779 if (mWidgetsToAdvance.containsKey(hostView)) {
1780 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001781 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001782 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001783 }
1784
Hyunyoung Song3f471442015-04-08 19:01:34 -07001785 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001786 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1787 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001788 }
1789
Winson Chunga6945242014-01-08 14:04:34 -08001790 public DragLayer getDragLayer() {
1791 return mDragLayer;
1792 }
1793
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001794 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001795 return mAppsView;
1796 }
1797
Hyunyoung Song3f471442015-04-08 19:01:34 -07001798 public WidgetsContainerView getWidgetsView() {
1799 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001800 }
1801
Winson Chunga6945242014-01-08 14:04:34 -08001802 public Workspace getWorkspace() {
1803 return mWorkspace;
1804 }
1805
Sunny Goyal6178f132016-07-11 17:30:03 -07001806 public View getQsbContainer() {
1807 return mQsbContainer;
1808 }
1809
Winson Chunga6945242014-01-08 14:04:34 -08001810 public Hotseat getHotseat() {
1811 return mHotseat;
1812 }
1813
Jorim Jaggid017f882014-01-14 17:08:48 -08001814 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001815 return mOverviewPanel;
1816 }
1817
Sunny Goyal47328fd2016-05-25 18:56:41 -07001818 public DropTargetBar getDropTargetBar() {
1819 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001820 }
1821
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001822 public LauncherAppWidgetHost getAppWidgetHost() {
1823 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824 }
Romain Guycbb89e42009-06-08 15:52:54 -07001825
Winson Chunga9abd0e2010-10-27 17:18:37 -07001826 public LauncherModel getModel() {
1827 return mModel;
1828 }
1829
Adam Cohen79d90c52016-04-22 13:29:20 -07001830 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001831 return mSharedPrefs;
1832 }
1833
Adam Cohen2e6da152015-05-06 11:42:25 -07001834 public DeviceProfile getDeviceProfile() {
1835 return mDeviceProfile;
1836 }
1837
Bjorn Bringertc459e522013-06-07 19:36:01 +01001838 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001839 getWindow().closeAllPanels();
1840
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001841 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001842 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001843 }
1844
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 @Override
1846 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001847 long startTime = 0;
1848 if (DEBUG_RESUME_TIME) {
1849 startTime = System.currentTimeMillis();
1850 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 super.onNewIntent(intent);
1852
1853 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001854 Folder openFolder = mWorkspace.getOpenFolder();
1855 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1856 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1857 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1858 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1859 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001860 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001861 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001862
Adam Cohen6fecd412013-10-02 17:41:50 -07001863 if (mWorkspace == null) {
1864 // Can be cases where mWorkspace is null, this prevents a NPE
1865 return;
1866 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001867 // In all these cases, only animate if we're already on home
1868 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001869
Sunny Goyal935fca12015-10-13 10:19:01 -07001870 closeFolder(alreadyOnHome);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001871 closeShortcutsContainer(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001872 exitSpringLoadedDragMode();
1873
1874 // If we are already on home, then just animate back to the workspace,
1875 // otherwise, just wait until onResume to set the state back to Workspace
1876 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001877 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001878 } else {
1879 mOnResumeState = State.WORKSPACE;
1880 }
1881
1882 final View v = getWindow().peekDecorView();
1883 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001884 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001885 INPUT_METHOD_SERVICE);
1886 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1887 }
1888
Winson Chungb745afb2015-03-02 11:51:23 -08001889 // Reset the apps view
1890 if (!alreadyOnHome && mAppsView != null) {
1891 mAppsView.scrollToTop();
1892 }
1893
Hyunyoung Song3f471442015-04-08 19:01:34 -07001894 // Reset the widgets view
1895 if (!alreadyOnHome && mWidgetsView != null) {
1896 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001897 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001898
Adam Cohen9211d422014-10-07 18:14:53 -07001899 if (mLauncherCallbacks != null) {
1900 mLauncherCallbacks.onHomeIntent();
1901 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 }
Adam Cohened307df2013-10-02 09:37:31 -07001903
Adam Cohen9211d422014-10-07 18:14:53 -07001904 if (mLauncherCallbacks != null) {
1905 mLauncherCallbacks.onNewIntent(intent);
1906 }
Winson15f8b172015-08-19 11:02:39 -07001907
1908 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1909 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1910 // animation.
1911 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001912 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1913 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001914 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1915 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001916
1917 // We use this flag to suppress noisy callbacks above custom content state
1918 // from onResume.
1919 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001920 mWorkspace.post(new Runnable() {
1921 @Override
1922 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001923 if (mWorkspace != null) {
1924 mWorkspace.moveToDefaultScreen(true);
1925 }
Winson15f8b172015-08-19 11:02:39 -07001926 }
1927 });
1928 }
1929 }
1930
1931 if (DEBUG_RESUME_TIME) {
1932 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1933 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001934 }
1935
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001937 public void onRestoreInstanceState(Bundle state) {
1938 super.onRestoreInstanceState(state);
1939 for (int page: mSynchronouslyBoundPages) {
1940 mWorkspace.restoreInstanceStateForChild(page);
1941 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 }
1943
1944 @Override
1945 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001946 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001947 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1948 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001949
Adam Cohen21cd0022013-10-09 18:57:02 -07001950 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001951 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952
Michael Jurka883f55b2010-10-21 15:47:14 -07001953 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001954 // We close any open folder since it will not be re-opened, and we need to make sure
1955 // this state is reflected.
1956 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1957 closeFolder(false);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001958 closeShortcutsContainer(false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001959
Adam Cohendcd297f2013-06-18 13:13:40 -07001960 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001961 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001962 ContentValues itemValues = new ContentValues();
1963 mPendingAddInfo.writeToValues(itemValues);
1964 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001965 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001966 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 }
1968
Adam Cohen9211d422014-10-07 18:14:53 -07001969 if (mLauncherCallbacks != null) {
1970 mLauncherCallbacks.onSaveInstanceState(outState);
1971 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 }
1973
1974 @Override
1975 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001977
Winson Chunge7a03942011-08-05 15:05:12 -07001978 // Remove all pending runnables
1979 mHandler.removeMessages(ADVANCE_MSG);
1980 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001981 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001982 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001983
Winson Chungcd2b0142011-06-08 16:02:26 -07001984 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001985 // It's possible to receive onDestroy after a new Launcher activity has
1986 // been created. In this case, don't interfere with the new Launcher.
1987 if (mModel.isCurrentCallbacks(this)) {
1988 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001989 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001990 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001991
Sunny Goyal745bad92016-05-02 10:54:12 -07001992 if (mRotationPrefChangeHandler != null) {
1993 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1994 }
1995
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001997 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001999 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002001 mAppWidgetHost = null;
2002
2003 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002004
2005 TextKeyListener.getInstance().release();
2006
Tony Wickhame2217252016-03-22 16:34:23 -07002007 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
2008 .removeAccessibilityStateChangeListener(this);
2009
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002010 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002011
Michael Jurka2ecf9952012-06-18 12:52:28 -07002012 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002013
2014 if (mLauncherCallbacks != null) {
2015 mLauncherCallbacks.onDestroy();
2016 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 }
2018
Sunny Goyalae502842016-06-17 08:43:56 -07002019 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
2020 return mAccessibilityDelegate;
2021 }
2022
Adam Cohena9cf38f2011-05-02 15:36:58 -07002023 public DragController getDragController() {
2024 return mDragController;
2025 }
2026
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002028 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07002029 onStartForResult(requestCode);
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002030 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07002031 }
2032
2033 @Override
2034 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2035 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2036 onStartForResult(requestCode);
2037 try {
2038 super.startIntentSenderForResult(intent, requestCode,
2039 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2040 } catch (IntentSender.SendIntentException e) {
2041 throw new ActivityNotFoundException();
2042 }
2043 }
2044
2045 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002046 if (requestCode >= 0) {
2047 setWaitingForResult(true);
2048 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049 }
2050
Winson Chung70d72102011-08-12 11:24:35 -07002051 /**
2052 * Indicates that we want global search for this activity by setting the globalSearch
2053 * argument for {@link #startSearch} to true.
2054 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002056 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002058
Karl Rosaen138a0412009-04-23 19:00:21 -07002059 if (initialQuery == null) {
2060 // Use any text typed in the launcher as the initial query
2061 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002062 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063 if (appSearchData == null) {
2064 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002065 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002066 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002067
2068 // TODO send proper bounds.
2069 Rect sourceBounds = null;
Romain Guycbb89e42009-06-08 15:52:54 -07002070
Ian Parkinson047036e2014-09-01 15:40:53 +01002071 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002072 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002073 if (clearTextImmediately) {
2074 clearTypedText();
2075 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002076
2077 // We need to show the workspace after starting the search
2078 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002079 }
2080
Ian Parkinson047036e2014-09-01 15:40:53 +01002081 /**
2082 * Start a text search.
2083 *
2084 * @return {@code true} if the search will start immediately, so any further keypresses
2085 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2086 * to buffer keypresses.
2087 */
2088 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002089 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002090 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2091 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2092 sourceBounds);
2093 }
2094
Michael Jurkaa33411c2012-06-14 16:18:21 -07002095 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002096 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002097 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002098 }
2099
2100 /**
2101 * Starts the global search activity. This code is a copied from SearchManager
2102 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002103 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002104 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002105 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002106 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2107 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2108 if (globalSearchActivity == null) {
2109 Log.w(TAG, "No global search activity found.");
2110 return;
2111 }
2112 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2113 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2114 intent.setComponent(globalSearchActivity);
2115 // Make sure that we have a Bundle to put source in
2116 if (appSearchData == null) {
2117 appSearchData = new Bundle();
2118 } else {
2119 appSearchData = new Bundle(appSearchData);
2120 }
Adam Cohen9211d422014-10-07 18:14:53 -07002121 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002122 if (!appSearchData.containsKey("source")) {
2123 appSearchData.putString("source", getPackageName());
2124 }
2125 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2126 if (!TextUtils.isEmpty(initialQuery)) {
2127 intent.putExtra(SearchManager.QUERY, initialQuery);
2128 }
2129 if (selectInitialQuery) {
2130 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2131 }
2132 intent.setSourceBounds(sourceBounds);
2133 try {
2134 startActivity(intent);
2135 } catch (ActivityNotFoundException ex) {
2136 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2137 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002138 }
2139
Yura4f93ec62014-02-04 14:15:21 +00002140 public boolean isOnCustomContent() {
2141 return mWorkspace.isOnOrMovingToCustomContent();
2142 }
2143
Winson Chung70d72102011-08-12 11:24:35 -07002144 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002145 public boolean onPrepareOptionsMenu(Menu menu) {
2146 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002147 if (mLauncherCallbacks != null) {
2148 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2149 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002150 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002151 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002152
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002153 @Override
2154 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002155 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002156 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002157 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002158 }
2159
Joe Onorato9c1289c2009-08-17 11:03:03 -04002160 public boolean isWorkspaceLocked() {
2161 return mWorkspaceLoading || mWaitingForResult;
2162 }
2163
Adam Cohen517a7f52014-03-01 12:12:59 -08002164 public boolean isWorkspaceLoading() {
2165 return mWorkspaceLoading;
2166 }
2167
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002168 private void setWorkspaceLoading(boolean value) {
2169 boolean isLocked = isWorkspaceLocked();
2170 mWorkspaceLoading = value;
2171 if (isLocked != isWorkspaceLocked()) {
2172 onWorkspaceLockedChanged();
2173 }
2174 }
2175
2176 private void setWaitingForResult(boolean value) {
2177 boolean isLocked = isWorkspaceLocked();
2178 mWaitingForResult = value;
2179 if (isLocked != isWorkspaceLocked()) {
2180 onWorkspaceLockedChanged();
2181 }
2182 }
2183
Adam Cohen9211d422014-10-07 18:14:53 -07002184 protected void onWorkspaceLockedChanged() {
2185 if (mLauncherCallbacks != null) {
2186 mLauncherCallbacks.onWorkspaceLockedChanged();
2187 }
2188 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002189
Michael Jurka0280c3b2010-09-17 15:00:07 -07002190 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002191 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002192 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002193 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2194 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002195 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002196 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002197
Tony Wickhama0628cc2015-10-14 15:23:04 -07002198 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002199 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002200 if (LOGD) {
2201 Log.d(TAG, "Adding widget from drop");
2202 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002203 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2204 }
2205
Sunny Goyale6b63a32015-01-16 16:14:29 -08002206 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002207 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2208 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002209 if (appWidgetInfo.configure != null) {
2210 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002211 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002212
Bjorn Bringert7984c942009-12-09 15:38:25 +00002213 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002214 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2215 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2216
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002217 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002218 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002219 Runnable onComplete = new Runnable() {
2220 @Override
2221 public void run() {
2222 // Exit spring loaded mode if necessary after adding the widget
2223 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2224 null);
2225 }
2226 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002227 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002228 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002229 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002230 }
2231 }
Romain Guycbb89e42009-06-08 15:52:54 -07002232
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002233 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002234 // Close any folders that may be open.
2235 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002236 mWorkspace.moveToCustomContentScreen(animate);
2237 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002238
2239 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2240 int[] cell, int spanX, int spanY) {
2241 switch (info.itemType) {
2242 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2243 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2244 int span[] = new int[2];
2245 span[0] = spanX;
2246 span[1] = spanY;
2247 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2248 container, screenId, cell, span);
2249 break;
2250 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2251 processShortcutFromDrop(info.componentName, container, screenId, cell);
2252 break;
2253 default:
2254 throw new IllegalStateException("Unknown item type: " + info.itemType);
2255 }
2256 }
2257
Adam Cohenfbba09b2011-07-18 21:43:05 -07002258 /**
2259 * Process a shortcut drop.
2260 *
2261 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002262 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002263 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002264 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002265 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2266 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002267 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002268 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002269 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002270
2271 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002272 mPendingAddInfo.cellX = cell[0];
2273 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002274 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002275
2276 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2277 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002278 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002279 }
2280
Adam Cohenfbba09b2011-07-18 21:43:05 -07002281 /**
2282 * Process a widget drop.
2283 *
2284 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002285 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002286 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002287 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002288 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2289 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002290 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002291 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002292 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002293 mPendingAddInfo.minSpanX = info.minSpanX;
2294 mPendingAddInfo.minSpanY = info.minSpanY;
2295
Adam Cohenfbba09b2011-07-18 21:43:05 -07002296 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002297 mPendingAddInfo.cellX = cell[0];
2298 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002299 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002300 if (span != null) {
2301 mPendingAddInfo.spanX = span[0];
2302 mPendingAddInfo.spanY = span[1];
2303 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002304
Adam Cohened66b2b2012-01-23 17:28:51 -08002305 AppWidgetHostView hostView = info.boundWidget;
2306 int appWidgetId;
2307 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002308 // In the case where we've prebound the widget, we remove it from the DragLayer
2309 if (LOGD) {
2310 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2311 }
2312 getDragLayer().removeView(hostView);
2313
Adam Cohened66b2b2012-01-23 17:28:51 -08002314 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002315 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002316
2317 // Clear the boundWidget so that it doesn't get destroyed.
2318 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002319 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002320 // In this case, we either need to start an activity to get permission to bind
2321 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002322 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002323 Bundle options = info.bindOptions;
2324
Sunny Goyalffe83f12014-08-14 17:39:34 -07002325 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2326 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002327 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002328 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002329 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002330 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002331 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2332 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2333 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002334 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2335 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002336 // TODO: we need to make sure that this accounts for the options bundle.
2337 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002338 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2339 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002340 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002341 }
2342
Adam Cohendcd297f2013-06-18 13:13:40 -07002343 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002344 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002345 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002346 folderInfo.title = getText(R.string.folder_name);
2347
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002348 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002349 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2350 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002351
2352 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002353 FolderIcon newFolder =
2354 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002355 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002356 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002357 // Force measure the new folder icon
2358 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2359 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002360 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002361 }
Romain Guycbb89e42009-06-08 15:52:54 -07002362
Winsonc0b52fe2015-09-09 16:38:15 -07002363 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002364 * Unbinds the view for the specified item, and removes the item and all its children.
2365 *
2366 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002367 * @param itemInfo the {@link ItemInfo} for this view.
2368 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002369 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002370 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002371 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002372 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002373 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2374 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002375 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002376 } else {
2377 mWorkspace.removeWorkspaceItem(v);
2378 }
Winsonc0b52fe2015-09-09 16:38:15 -07002379 if (deleteFromDb) {
2380 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2381 }
2382 } else if (itemInfo instanceof FolderInfo) {
2383 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002384 if (v instanceof FolderIcon) {
2385 ((FolderIcon) v).removeListeners();
2386 }
Winsonc0b52fe2015-09-09 16:38:15 -07002387 mWorkspace.removeWorkspaceItem(v);
2388 if (deleteFromDb) {
2389 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2390 }
2391 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2392 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002393 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002394 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002395 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002396 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002397 }
2398 } else {
2399 return false;
2400 }
2401 return true;
2402 }
2403
2404 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002405 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002406 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002407 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002408 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002409 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002410 // Deleting an app widget ID is a void call but writes to disk before returning
2411 // to the caller...
2412 new AsyncTask<Void, Void, Void>() {
2413 public Void doInBackground(Void ... args) {
2414 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2415 return null;
2416 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002417 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002418 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002419 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002420 }
2421
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002422 @Override
2423 public boolean dispatchKeyEvent(KeyEvent event) {
2424 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2425 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002426 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002427 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002428 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002429 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002430 dumpState();
2431 return true;
2432 }
2433 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002434 }
2435 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2436 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002437 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438 return true;
2439 }
2440 }
2441
2442 return super.dispatchKeyEvent(event);
2443 }
2444
Joe Onorato88ec0992009-11-19 13:16:06 -08002445 @Override
2446 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002447 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2448 return;
2449 }
2450
Sunny Goyal45478022015-06-08 16:52:41 -07002451 if (mDragController.isDragging()) {
2452 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002453 return;
2454 }
2455
Tony Wickham49c8d292016-07-01 11:44:34 -07002456 if (getOpenShortcutsContainer() != null) {
2457 closeShortcutsContainer();
2458 } else if (isAppsViewVisible()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002459 showWorkspace(true);
2460 } else if (isWidgetsViewVisible()) {
2461 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002462 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002463 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002464 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002465 Folder openFolder = mWorkspace.getOpenFolder();
2466 if (openFolder.isEditingName()) {
2467 openFolder.dismissEditingName();
2468 } else {
2469 closeFolder();
2470 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002471 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002472 mWorkspace.exitWidgetResizeMode();
2473
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002474 // Back button is a no-op here, but give at least some feedback for the button press
2475 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002476 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002477 }
2478
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002479 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002480 * Launches the intent referred by the clicked shortcut.
2481 *
2482 * @param v The view representing the clicked shortcut.
2483 */
2484 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002485 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2486 // view has detached (it's possible for this to happen if the view is removed mid touch).
2487 if (v.getWindowToken() == null) {
2488 return;
2489 }
2490
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002491 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002492 return;
2493 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002494
Adam Cohen1697b792013-09-17 19:08:21 -07002495 if (v instanceof Workspace) {
2496 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002497 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002498 }
2499 return;
2500 }
2501
2502 if (v instanceof CellLayout) {
2503 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002504 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2505 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002506 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002507 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002508 }
2509
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002510 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002511 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002512 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002513 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002514 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002515 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002516 }
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002517 } else if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2518 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002519 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002520 } else if (tag instanceof AppInfo) {
2521 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002522 } else if (tag instanceof LauncherAppWidgetInfo) {
2523 if (v instanceof PendingAppWidgetHostView) {
2524 onClickPendingWidget((PendingAppWidgetHostView) v);
2525 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002526 }
2527 }
2528
Sunny Goyal70660032015-05-14 00:07:08 -07002529 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002530 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002531 return false;
2532 }
2533
Michael Jurkaaf442092010-06-10 17:01:57 -07002534 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002535 * Event handler for the app widget view which has not fully restored.
2536 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002537 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002538 if (mIsSafeModeEnabled) {
2539 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2540 return;
2541 }
2542
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002543 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002544 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002545 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2546 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2547 // This should not happen, as we make sure that an Id is allocated during bind.
2548 return;
2549 }
2550 LauncherAppWidgetProviderInfo appWidgetInfo =
2551 mAppWidgetManager.findProvider(info.providerName, info.user);
2552 if (appWidgetInfo != null) {
2553 mPendingAddWidgetId = info.appWidgetId;
2554 mPendingAddInfo.copyFrom(info);
2555 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002556
Sunny Goyald478c832016-04-01 12:04:16 -07002557 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2558 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingAddWidgetId);
2559 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
2560 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2561 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2562 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2563 }
2564 } else {
2565 LauncherAppWidgetProviderInfo appWidgetInfo =
2566 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2567 if (appWidgetInfo != null) {
2568 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2569 }
Sunny Goyalff572272014-07-23 13:58:07 -07002570 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002571 } else if (info.installProgress < 0) {
2572 // The install has not been queued
2573 final String packageName = info.providerName.getPackageName();
2574 showBrokenAppInstallDialog(packageName,
2575 new DialogInterface.OnClickListener() {
2576 public void onClick(DialogInterface dialog, int id) {
2577 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2578 }
2579 });
2580 } else {
2581 // Download has started.
2582 final String packageName = info.providerName.getPackageName();
2583 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002584 }
2585 }
2586
Sunny Goyald478c832016-04-01 12:04:16 -07002587 private void startRestoredWidgetReconfigActivity(
2588 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
2589 mPendingAddWidgetInfo = provider;
2590 mPendingAddInfo.copyFrom(info);
2591 mPendingAddWidgetId = info.appWidgetId;
2592 mAppWidgetManager.startConfigActivity(provider,
2593 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2594 }
2595
Sunny Goyalff572272014-07-23 13:58:07 -07002596 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002597 * Event handler for the "grid" button that appears on the home screen, which
2598 * enters all apps mode.
2599 *
2600 * @param v The view that was clicked.
2601 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002602 protected void onClickAllAppsButton(View v) {
2603 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002604 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002605 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.TAP,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002606 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002607 showAppsView(true /* animated */, true /* updatePredictedApps */,
2608 false /* focusSearchBar */);
Winson Chung76648c52015-07-10 14:33:23 -07002609 }
2610 }
2611
2612 protected void onLongClickAllAppsButton(View v) {
2613 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2614 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002615 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.LONGPRESS,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002616 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002617 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07002618 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002619 }
2620 }
2621
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002622 private void showBrokenAppInstallDialog(final String packageName,
2623 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002624 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002625 .setTitle(R.string.abandoned_promises_title)
2626 .setMessage(R.string.abandoned_promise_explanation)
2627 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2628 .setNeutralButton(R.string.abandoned_clean_this,
2629 new DialogInterface.OnClickListener() {
2630 public void onClick(DialogInterface dialog, int id) {
2631 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2632 mWorkspace.removeAbandonedPromise(packageName, user);
2633 }
2634 })
2635 .create().show();
2636 return;
2637 }
2638
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002639 /**
2640 * Event handler for an app shortcut click.
2641 *
2642 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2643 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002644 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002645 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2646 Object tag = v.getTag();
2647 if (!(tag instanceof ShortcutInfo)) {
2648 throw new IllegalArgumentException("Input must be a Shortcut");
2649 }
2650
2651 // Open shortcut
2652 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002653
2654 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002655 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2656 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002657 // Launch activity anyway, framework will tell the user why the app is suspended.
2658 } else {
2659 int error = R.string.activity_not_available;
2660 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2661 error = R.string.safemode_shortcut_error;
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002662 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0) {
2663 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002664 }
2665 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2666 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002667 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002668 }
2669
Chris Wren40c5ed32014-06-24 18:24:23 -04002670 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002671 if ((v instanceof BubbleTextView)
2672 && shortcut.isPromise()
2673 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002674 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002675 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002676 new DialogInterface.OnClickListener() {
2677 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002678 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002679 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002680 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002681 return;
2682 }
2683
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002684 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002685 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002686 }
2687
Sunny Goyala7ce1662016-05-31 15:01:35 -07002688 private void startAppShortcutOrInfoActivity(View v) {
2689 ItemInfo item = (ItemInfo) v.getTag();
2690 Intent intent = item.getIntent();
2691 if (intent == null) {
2692 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002693 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002694 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002695 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002696
2697 if (success && v instanceof BubbleTextView) {
2698 mWaitingForResume = (BubbleTextView) v;
2699 mWaitingForResume.setStayPressed(true);
2700 }
2701 }
2702
2703 /**
2704 * Event handler for a folder icon click.
2705 *
2706 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2707 */
2708 protected void onClickFolderIcon(View v) {
2709 if (LOGD) Log.d(TAG, "onClickFolder");
2710 if (!(v instanceof FolderIcon)){
2711 throw new IllegalArgumentException("Input must be a FolderIcon");
2712 }
2713
2714 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002715 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002716 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002717 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002718 }
Michael Jurka5130e402011-10-13 04:55:35 -07002719 }
2720
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002721 /**
2722 * Event handler for the (Add) Widgets button that appears after a long press
2723 * on the home screen.
2724 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002725 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002726 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002727 if (mIsSafeModeEnabled) {
2728 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2729 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002730 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002731 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002732 }
2733
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002734 /**
2735 * Event handler for the wallpaper picker button that appears after a long press
2736 * on the home screen.
2737 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002738 public void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002739 if (!Utilities.isWallapaperAllowed(this)) {
2740 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2741 return;
2742 }
2743
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002744 String pickerPackage = getString(R.string.wallpaper_picker_package);
2745 if (TextUtils.isEmpty(pickerPackage)) {
2746 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2747 }
2748
Tony Wickham785f7a52015-08-31 17:28:32 -07002749 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2750 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002751
2752 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002753 .setPackage(pickerPackage)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002754 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
2755 intent.setSourceBounds(getViewBounds(v));
2756 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002757 }
2758
2759 /**
2760 * Event handler for a click on the settings button that appears after a long press
2761 * on the home screen.
2762 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002763 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002764 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002765 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2766 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002767 }
2768
Adam Cohen61f560d2013-09-30 15:58:20 -07002769 public View.OnTouchListener getHapticFeedbackTouchListener() {
2770 if (mHapticFeedbackTouchListener == null) {
2771 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002772 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002773 @Override
2774 public boolean onTouch(View v, MotionEvent event) {
2775 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2776 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2777 }
2778 return false;
2779 }
2780 };
2781 }
2782 return mHapticFeedbackTouchListener;
2783 }
2784
Tony Wickhame2217252016-03-22 16:34:23 -07002785 @Override
2786 public void onAccessibilityStateChanged(boolean enabled) {
2787 mDragLayer.onAccessibilityStateChanged(enabled);
2788 }
2789
Adam Cohen9211d422014-10-07 18:14:53 -07002790 public void onDragStarted(View view) {
2791 if (isOnCustomContent()) {
2792 // Custom content screen doesn't participate in drag and drop. If on custom
2793 // content screen, move to default.
2794 moveWorkspaceToDefaultScreen();
2795 }
Adam Cohen9211d422014-10-07 18:14:53 -07002796 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002797
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002798 /**
2799 * Called when the user stops interacting with the launcher.
2800 * This implies that the user is now on the homescreen and is not doing housekeeping.
2801 */
Adam Cohen9211d422014-10-07 18:14:53 -07002802 protected void onInteractionEnd() {
2803 if (mLauncherCallbacks != null) {
2804 mLauncherCallbacks.onInteractionEnd();
2805 }
2806 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002807
2808 /**
2809 * Called when the user starts interacting with the launcher.
2810 * The possible interactions are:
2811 * - open all apps
2812 * - reorder an app shortcut, or a widget
2813 * - open the overview mode.
2814 * This is a good time to stop doing things that only make sense
2815 * when the user is on the homescreen and not doing housekeeping.
2816 */
Adam Cohen9211d422014-10-07 18:14:53 -07002817 protected void onInteractionBegin() {
2818 if (mLauncherCallbacks != null) {
2819 mLauncherCallbacks.onInteractionBegin();
2820 }
2821 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002822
Winson Chungcd99cd32015-04-29 11:03:24 -07002823 /** Updates the interaction state. */
2824 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002825 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002826 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002827 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2828 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002829 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002830 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002831 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002832 onInteractionEnd();
2833 }
2834 }
2835
Sunny Goyala7ce1662016-05-31 15:01:35 -07002836 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002837 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002838 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2839 try {
2840 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2841 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2842 // is enabled by default on NYC.
2843 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2844 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002845
2846 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2847 String id = ((ShortcutInfo) info).getDeepShortcutId();
2848 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002849 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2850 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002851 } else {
2852 // Could be launching some bookkeeping activity
2853 startActivity(intent, optsBundle);
2854 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002855 } finally {
2856 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002857 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002858 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002859 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2860 // corresponding permission. Show the appropriate permission prompt if that
2861 // is the case.
2862 if (intent.getComponent() == null
2863 && Intent.ACTION_CALL.equals(intent.getAction())
2864 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2865 PackageManager.PERMISSION_GRANTED) {
2866 // TODO: Rename sPendingAddItem to a generic name.
2867 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2868 0, info);
2869 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2870 REQUEST_PERMISSION_CALL_PHONE);
2871 } else {
2872 // No idea why this was thrown.
2873 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002874 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002875 }
2876 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002877
Sunny Goyala7ce1662016-05-31 15:01:35 -07002878 private Bundle getActivityLaunchOptions(View v) {
2879 if (Utilities.ATLEAST_MARSHMALLOW) {
2880 int left = 0, top = 0;
2881 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2882 if (v instanceof TextView) {
2883 // Launch from center of icon, not entire view
2884 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2885 if (icon != null) {
2886 Rect bounds = icon.getBounds();
2887 left = (width - bounds.width()) / 2;
2888 top = v.getPaddingTop();
2889 width = bounds.width();
2890 height = bounds.height();
2891 }
2892 }
2893 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2894 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2895 // On L devices, we use the device default slide-up transition.
2896 // On L MR1 devices, we use a custom version of the slide-up transition which
2897 // doesn't have the delay present in the device default.
2898 return ActivityOptions.makeCustomAnimation(
2899 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2900 }
2901 return null;
2902 }
2903
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002904 private Rect getViewBounds(View v) {
2905 int[] pos = new int[2];
2906 v.getLocationOnScreen(pos);
2907 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2908 }
2909
Sunny Goyala7ce1662016-05-31 15:01:35 -07002910 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002911 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2912 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2913 return false;
2914 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002915 // Only launch using the new animation if the shortcut has not opted out (this is a
2916 // private contract between launcher and may be ignored in the future).
2917 boolean useLaunchAnimation = (v != null) &&
2918 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2919 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2920
2921 UserHandleCompat user = null;
2922 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2923 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2924 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002925 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002926
2927 // Prepare intent
2928 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2929 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002930 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002931 }
2932 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002933 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2934 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
2935 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002936 // Shortcuts need some special checks due to legacy reasons.
2937 startShortcutIntentSafely(intent, optsBundle, item);
2938 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2939 // Could be launching some bookkeeping activity
2940 startActivity(intent, optsBundle);
2941 } else {
2942 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2943 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2944 }
2945 return true;
2946 } catch (ActivityNotFoundException|SecurityException e) {
2947 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2948 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2949 }
2950 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002951 }
2952
Adam Cohen268c4752012-06-06 17:47:33 -07002953 /**
2954 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2955 * in the DragLayer in the exact absolute location of the original FolderIcon.
2956 */
2957 private void copyFolderIconToImage(FolderIcon fi) {
2958 final int width = fi.getMeasuredWidth();
2959 final int height = fi.getMeasuredHeight();
2960
2961 // Lazy load ImageView, Bitmap and Canvas
2962 if (mFolderIconImageView == null) {
2963 mFolderIconImageView = new ImageView(this);
2964 }
2965 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2966 mFolderIconBitmap.getHeight() != height) {
2967 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2968 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2969 }
2970
2971 DragLayer.LayoutParams lp;
2972 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2973 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2974 } else {
2975 lp = new DragLayer.LayoutParams(width, height);
2976 }
2977
Adam Cohen307fe232012-08-16 17:55:58 -07002978 // The layout from which the folder is being opened may be scaled, adjust the starting
2979 // view size by this scale factor.
2980 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002981 lp.customPosition = true;
2982 lp.x = mRectForFolderAnimation.left;
2983 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002984 lp.width = (int) (scale * width);
2985 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002986
2987 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2988 fi.draw(mFolderIconCanvas);
2989 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002990 if (fi.getFolder() != null) {
2991 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2992 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002993 }
Adam Cohen268c4752012-06-06 17:47:33 -07002994 // Just in case this image view is still in the drag layer from a previous animation,
2995 // we remove it and re-add it.
2996 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2997 mDragLayer.removeView(mFolderIconImageView);
2998 }
2999 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003000 if (fi.getFolder() != null) {
3001 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003002 }
Adam Cohen268c4752012-06-06 17:47:33 -07003003 }
3004
Adam Cohen37b2a492016-04-06 18:36:06 -07003005 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003006 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07003007 FolderInfo info = (FolderInfo) fi.getTag();
3008 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3009 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003010 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3011 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003012 }
3013
Adam Cohen268c4752012-06-06 17:47:33 -07003014 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3015 copyFolderIconToImage(fi);
3016 fi.setVisibility(View.INVISIBLE);
3017
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003018 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
3019 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003020 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003021 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3022 }
3023 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003024 oa.start();
3025 }
3026
Sunny Goyal935fca12015-10-13 10:19:01 -07003027 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003028 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003029 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003030
Adam Cohen268c4752012-06-06 17:47:33 -07003031 // We remove and re-draw the FolderIcon in-case it has changed
3032 mDragLayer.removeView(mFolderIconImageView);
3033 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08003034
3035 if (cl != null) {
3036 cl.clearFolderLeaveBehind();
3037 }
3038
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003039 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003040 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003041 oa.addListener(new AnimatorListenerAdapter() {
3042 @Override
3043 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003044 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003045 // Remove the ImageView copy of the FolderIcon and make the original visible.
3046 mDragLayer.removeView(mFolderIconImageView);
3047 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003048 }
3049 }
3050 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003051 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003052 if (!animate) {
3053 oa.end();
3054 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003055 }
3056
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003057 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003058 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003059 * is animated relative to the specified View. If the View is null, no animation
3060 * is played.
3061 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003062 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003063 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003064 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003065
Adam Cohenfb91f302012-06-11 15:45:18 -07003066 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003067 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3068 if (openFolder != null && openFolder != folder) {
3069 // Close any open folder before opening a folder.
3070 closeFolder();
3071 }
3072
Adam Cohena9cf38f2011-05-02 15:36:58 -07003073 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003074
Adam Cohena9cf38f2011-05-02 15:36:58 -07003075 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003076
Sunny Goyalf4066152015-04-15 09:42:19 -07003077 // While the folder is open, the position of the icon cannot change.
3078 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3079
Adam Cohen4554ee12011-08-03 16:13:21 -07003080 // Just verify that the folder hasn't already been added to the DragLayer.
3081 // There was a one-off crash where the folder had a parent already.
3082 if (folder.getParent() == null) {
3083 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003084 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003085 } else {
3086 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3087 folder.getParent() + ").");
3088 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003089 folder.animateOpen();
3090
3091 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003092
3093 // Notify the accessibility manager that this folder "window" has appeared and occluded
3094 // the workspace items
3095 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3096 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003097 }
3098
3099 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003100 closeFolder(true);
3101 }
3102
3103 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003104 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003105 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003106 if (folder.isEditingName()) {
3107 folder.dismissEditingName();
3108 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003109 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003110 }
3111 }
3112
Sunny Goyal935fca12015-10-13 10:19:01 -07003113 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003114 animate &= !Utilities.isPowerSaverOn(this);
3115
Adam Cohen4554ee12011-08-03 16:13:21 -07003116 folder.getInfo().opened = false;
3117
3118 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3119 if (parent != null) {
3120 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003121 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003122 if (fi != null) {
3123 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3124 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003125 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003126 if (animate) {
3127 folder.animateClosed();
3128 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003129 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003130 }
Winson Chung83ca4802013-04-12 15:10:52 -07003131
Sunny Goyal935fca12015-10-13 10:19:01 -07003132 // Notify the accessibility manager that this folder "window" has disappeared and no
3133 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003134 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003135 }
3136
Tony Wickham49c8d292016-07-01 11:44:34 -07003137 public void closeShortcutsContainer() {
Sunny Goyala2454ad2016-07-22 10:50:11 -07003138 closeShortcutsContainer(true);
3139 }
3140
3141 public void closeShortcutsContainer(boolean animate) {
Tony Wickham49c8d292016-07-01 11:44:34 -07003142 DeepShortcutsContainer deepShortcutsContainer = getOpenShortcutsContainer();
3143 if (deepShortcutsContainer != null) {
Sunny Goyala2454ad2016-07-22 10:50:11 -07003144 if (animate) {
3145 deepShortcutsContainer.animateClose();
3146 } else {
3147 deepShortcutsContainer.close();
3148 }
Tony Wickham49c8d292016-07-01 11:44:34 -07003149 }
3150 }
3151
3152 /**
3153 * @return The open shortcuts container, or null if there is none
3154 */
3155 public DeepShortcutsContainer getOpenShortcutsContainer() {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07003156 // Iterate in reverse order. Shortcuts container is added later to the dragLayer,
3157 // and will be one of the last views.
3158 for (int i = mDragLayer.getChildCount() - 1; i >= 0; i--) {
3159 View child = mDragLayer.getChildAt(i);
Sunny Goyala2454ad2016-07-22 10:50:11 -07003160 if (child instanceof DeepShortcutsContainer
3161 && ((DeepShortcutsContainer) child).isOpen()) {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07003162 return (DeepShortcutsContainer) child;
3163 }
3164 }
3165 return null;
Tony Wickham49c8d292016-07-01 11:44:34 -07003166 }
3167
Tony Wickhamdadb3042016-02-24 11:07:00 -08003168 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003169 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003170 if (!isDraggingEnabled()) return false;
3171 if (isWorkspaceLocked()) return false;
3172 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003173
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07003174 if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
3175 (v == mAllAppsButton && mAllAppsButton != null)) {
Winson Chung76648c52015-07-10 14:33:23 -07003176 onLongClickAllAppsButton(v);
3177 return true;
3178 }
3179
Adam Cohen1697b792013-09-17 19:08:21 -07003180 if (v instanceof Workspace) {
3181 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003182 if (!mWorkspace.isTouchActive()) {
3183 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003184 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3185 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3186 return true;
3187 } else {
3188 return false;
3189 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003190 } else {
3191 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003192 }
Adam Cohen1697b792013-09-17 19:08:21 -07003193 }
3194
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003195 CellLayout.CellInfo longClickCellInfo = null;
3196 View itemUnderLongClick = null;
3197 if (v.getTag() instanceof ItemInfo) {
3198 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003199 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003200 itemUnderLongClick = longClickCellInfo.cell;
3201 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003202 }
3203
Winson Chung3d503fb2011-07-13 17:25:49 -07003204 // The hotseat touch handling does not go through Workspace, and we always allow long press
3205 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003206 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003207 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003208 // User long pressed on empty space
3209 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3210 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003211 if (mWorkspace.isInOverviewMode()) {
3212 mWorkspace.startReordering(v);
3213 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003214 showOverviewMode(true);
Hyunyoung Song645764e2016-06-06 14:19:02 -07003215 mHotseat.requestDisallowInterceptTouchEvent(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003216 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003217 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003218 final boolean isAllAppsButton =
3219 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3220 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3221 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003222 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003223 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003224 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003225 }
3226 }
3227 }
3228 return true;
3229 }
3230
Winson Chung3d503fb2011-07-13 17:25:49 -07003231 boolean isHotseatLayout(View layout) {
3232 return mHotseat != null && layout != null &&
3233 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3234 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003235
Winson Chung3d503fb2011-07-13 17:25:49 -07003236 /**
3237 * Returns the CellLayout of the specified container at the specified screen.
3238 */
Sunny Goyal92820592015-03-02 11:31:35 -08003239 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003240 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3241 if (mHotseat != null) {
3242 return mHotseat.getLayout();
3243 } else {
3244 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003245 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003246 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003247 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003248 }
3249 }
3250
Winson Chungb745afb2015-03-02 11:51:23 -08003251 /**
3252 * For overridden classes.
3253 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003254 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003255 return isAppsViewVisible();
3256 }
3257
3258 public boolean isAppsViewVisible() {
3259 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3260 }
3261
3262 public boolean isWidgetsViewVisible() {
3263 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003264 }
3265
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003266 private void setWorkspaceBackground(int background) {
3267 switch (background) {
3268 case WORKSPACE_BACKGROUND_TRANSPARENT:
3269 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3270 break;
3271 case WORKSPACE_BACKGROUND_BLACK:
3272 getWindow().setBackgroundDrawable(null);
3273 break;
3274 default:
3275 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3276 }
Craig Mautner360310b2012-10-26 15:13:08 -07003277 }
3278
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003279 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003280 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3281 int curflags = getWindow().getAttributes().flags
3282 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3283 if (wpflags != curflags) {
3284 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3285 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003286 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003287 }
3288
Michael Jurkae326f182011-11-21 14:05:46 -08003289 @Override
3290 public void onTrimMemory(int level) {
3291 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003292 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3293 // The widget preview db can result in holding onto over
3294 // 3MB of memory for caching which isn't necessary.
3295 SQLiteDatabase.releaseMemory();
3296
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003297 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003298 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003299 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003300 if (mLauncherCallbacks != null) {
3301 mLauncherCallbacks.onTrimMemory(level);
3302 }
Michael Jurkae326f182011-11-21 14:05:46 -08003303 }
3304
Winson5c6bdbb2015-09-03 11:36:19 -07003305 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003306 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003307 }
3308
Winson5c6bdbb2015-09-03 11:36:19 -07003309 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003310 boolean changed = mState != State.WORKSPACE ||
3311 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003312 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003313 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003314 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003315 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003316
Michael Jurkab3e22d92011-10-31 15:58:33 -07003317 // Set focus to the AppsCustomize button
3318 if (mAllAppsButton != null) {
3319 mAllAppsButton.requestFocus();
3320 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003321 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003322
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003323 // Change the state *after* we've called all the transition code
3324 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003325
Winson Chung5fb63472011-02-02 17:03:37 -08003326 // Resume the auto-advance of widgets
3327 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003328 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003329
Winson Chung0f785722015-04-08 10:27:49 -07003330 if (changed) {
3331 // Send an accessibility event to announce the context change
3332 getWindow().getDecorView()
3333 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003334 }
Winson5c6bdbb2015-09-03 11:36:19 -07003335 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003336 }
3337
Winsone9f27272015-10-13 10:47:51 -07003338 /**
3339 * Shows the overview button.
3340 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07003341 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003342 showOverviewMode(animated, false);
3343 }
3344
3345 /**
3346 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3347 * onto one of the overview panel buttons.
3348 */
3349 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3350 Runnable postAnimRunnable = null;
3351 if (requestButtonFocus) {
3352 postAnimRunnable = new Runnable() {
3353 @Override
3354 public void run() {
3355 // Hitting the menu button when in touch mode does not trigger touch mode to
3356 // be disabled, so if requested, force focus on one of the overview panel
3357 // buttons.
3358 mOverviewPanel.requestFocusFromTouch();
3359 }
3360 };
3361 }
Adam Cohened307df2013-10-02 09:37:31 -07003362 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003363 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003364 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003365 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003366 }
3367
Winson Chungb745afb2015-03-02 11:51:23 -08003368 /**
3369 * Shows the apps view.
3370 */
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003371 public void showAppsView(boolean animated, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003372 boolean focusSearchBar) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003373 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07003374 if (updatePredictedApps) {
3375 tryAndUpdatePredictedApps();
3376 }
Winson Chung76648c52015-07-10 14:33:23 -07003377 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003378 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003379
Winson Chungb745afb2015-03-02 11:51:23 -08003380 /**
3381 * Shows the widgets view.
3382 */
3383 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003384 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003385 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003386 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003387 }
Winson Chung76648c52015-07-10 14:33:23 -07003388 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003389
3390 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003391 @Override
3392 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003393 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003394 }
3395 });
Winson Chungb745afb2015-03-02 11:51:23 -08003396 }
3397
3398 /**
3399 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003400 *
3401 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003402 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003403 // TODO: calling method should use the return value so that when {@code false} is returned
3404 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003405 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003406 if (!(mState == State.WORKSPACE ||
3407 mState == State.APPS_SPRING_LOADED ||
3408 mState == State.WIDGETS_SPRING_LOADED ||
3409 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003410 return false;
3411 }
3412 if (toState != State.APPS && toState != State.WIDGETS) {
3413 return false;
3414 }
Winson Chungb745afb2015-03-02 11:51:23 -08003415
3416 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003417 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3418 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003419 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003420 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003421 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003422
Michael Jurkab3e22d92011-10-31 15:58:33 -07003423 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003424 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003425
3426 // Pause the auto-advance of widgets until we are out of AllApps
3427 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003428 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003429 closeFolder();
Tony Wickham49c8d292016-07-01 11:44:34 -07003430 closeShortcutsContainer();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003431
3432 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003433 getWindow().getDecorView()
3434 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003435 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003436 }
3437
Winson Chungcd99cd32015-04-29 11:03:24 -07003438 /**
3439 * Updates the workspace and interaction state on state change, and return the animation to this
3440 * new state.
3441 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003442 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003443 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003444 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003445 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003446 updateInteraction(fromState, toState);
3447 return anim;
3448 }
3449
Hyunyoung Song3f471442015-04-08 19:01:34 -07003450 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003451 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003452 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003453 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003454 }
Winson Chungb745afb2015-03-02 11:51:23 -08003455
Winson Chung006ee262015-08-03 14:40:11 -07003456 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003457 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003458 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003459
3460 if (isAppsViewVisible()) {
3461 mState = State.APPS_SPRING_LOADED;
3462 } else if (isWidgetsViewVisible()) {
3463 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003464 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003465 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003466 } else {
3467 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003468 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003469 }
Adam Cohen7777d962011-08-18 18:58:38 -07003470
Hyunyoung Song3f471442015-04-08 19:01:34 -07003471 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003472 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003473 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003474
Winson Chunge7a03942011-08-05 15:05:12 -07003475 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003476 @Override
3477 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003478 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003479 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3480 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003481 // Before we show workspace, hide all apps again because
3482 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3483 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003484 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003485 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003486 } else {
3487 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003488 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003489 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003490 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003491 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003492
Tony Wickhame0c33232016-02-08 11:37:04 -08003493 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003494 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3495 || mState == State.WIDGETS_SPRING_LOADED;
3496 }
3497
Michael Jurkad3ef3062010-11-23 16:23:58 -08003498 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003499 if (mState == State.APPS_SPRING_LOADED) {
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003500 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07003501 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003502 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003503 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003504 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3505 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003506 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003507 }
3508
Winson Chung4ac30062015-05-08 17:34:17 -07003509 /**
3510 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3511 * resumed.
3512 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003513 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003514 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003515 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003516 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003517 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003518 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003519 }
3520 }
3521 }
3522
Michael Jurkab3e22d92011-10-31 15:58:33 -07003523 void lockAllApps() {
3524 // TODO
3525 }
3526
3527 void unlockAllApps() {
3528 // TODO
3529 }
3530
Winsonf768d932015-09-25 16:12:35 -07003531 public boolean launcherCallbacksProvidesSearch() {
3532 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3533 }
3534
Michael Jurkad7c28052012-04-27 15:43:36 -07003535 @Override
3536 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003537 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003538 final List<CharSequence> text = event.getText();
3539 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003540 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003541 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003542 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003543 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003544 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003545 } else if (mWorkspace != null) {
3546 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003547 } else {
3548 text.add(getString(R.string.all_apps_home_button_label));
3549 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003550 return result;
3551 }
3552
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003553 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003554 * If the activity is currently paused, signal that we need to run the passed Runnable
3555 * in onResume.
3556 *
3557 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003558 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3559 * wrong when we're not running, and if the activity comes back to what the configuration was
3560 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003561 *
3562 * Implementation of the method from LauncherModel.Callbacks.
3563 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003564 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003565 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003566 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003567 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003568 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003569 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003570 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003571 }
3572 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003573 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003574 return true;
3575 } else {
3576 return false;
3577 }
3578 }
3579
Michael Jurkac402cd92013-05-20 15:49:32 +02003580 private boolean waitUntilResume(Runnable run) {
3581 return waitUntilResume(run, false);
3582 }
3583
Michael Jurka1e2f4652013-07-08 18:03:46 -07003584 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003585 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003586 }
3587
Michael Jurka7607c2f2013-04-03 14:33:19 -07003588 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003589 * If the activity is currently paused, signal that we need to re-run the loader
3590 * in onResume.
3591 *
3592 * This needs to be called from incoming places where resources might have been loaded
3593 * while we are paused. That is becaues the Configuration might be wrong
3594 * when we're not running, and if it comes back to what it was when we
3595 * were paused, we are not restarted.
3596 *
3597 * Implementation of the method from LauncherModel.Callbacks.
3598 *
3599 * @return true if we are currently paused. The caller might be able to
3600 * skip some work in that case since we will come back again.
3601 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003602 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003603 public boolean setLoadOnResume() {
3604 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003605 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003606 mOnResumeNeedsLoad = true;
3607 return true;
3608 } else {
3609 return false;
3610 }
3611 }
3612
3613 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003614 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003615 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003616 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003617 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003618 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003619 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003620 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003621 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003622 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003623 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003624
Joe Onorato9c1289c2009-08-17 11:03:03 -04003625 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003626 * Clear any pending bind callbacks. This is called when is loader is planning to
3627 * perform a full rebind from scratch.
3628 */
3629 @Override
3630 public void clearPendingBinds() {
3631 mBindOnResumeCallbacks.clear();
3632 if (mPendingExecutor != null) {
3633 mPendingExecutor.markCompleted();
3634 mPendingExecutor = null;
3635 }
3636 }
3637
3638 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003639 * Refreshes the shortcuts shown on the workspace.
3640 *
3641 * Implementation of the method from LauncherModel.Callbacks.
3642 */
3643 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003644 if (LauncherAppState.PROFILE_STARTUP) {
3645 Trace.beginSection("Starting page bind");
3646 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003647 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003648
Winson Chungd64d1762013-08-20 14:37:16 -07003649 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003650 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003651 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003652
Michael Jurka05bf644e2011-11-30 20:28:53 -08003653 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003654 if (mHotseat != null) {
3655 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003656 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003657 if (LauncherAppState.PROFILE_STARTUP) {
3658 Trace.endSection();
3659 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003660 }
3661
Adam Cohendcd297f2013-06-18 13:13:40 -07003662 @Override
3663 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003664 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003665 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3666 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003667 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3668 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3669 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003670 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3671 // If there are no screens, we need to have an empty screen
3672 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003673 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003674 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003675
3676 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003677 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003678 if (hasCustomContentToLeft()) {
3679 mWorkspace.createCustomContentContainer();
3680 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003681 }
Winson Chung64359a52013-07-08 17:17:08 -07003682 }
3683
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003684 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003685 int count = orderedScreenIds.size();
3686 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003687 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003688 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003689 // No need to bind the first screen, as its always bound.
3690 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3691 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003692 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003693 }
3694
Winson Chungd64d1762013-08-20 14:37:16 -07003695 public void bindAppsAdded(final ArrayList<Long> newScreens,
3696 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003697 final ArrayList<ItemInfo> addAnimated,
3698 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003699 Runnable r = new Runnable() {
3700 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003701 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003702 }
3703 };
3704 if (waitUntilResume(r)) {
3705 return;
3706 }
3707
Winson Chungd64d1762013-08-20 14:37:16 -07003708 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003709 if (newScreens != null) {
3710 bindAddScreens(newScreens);
3711 }
Winson Chungd64d1762013-08-20 14:37:16 -07003712
3713 // We add the items without animation on non-visible pages, and with
3714 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003715 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003716 bindItems(addNotAnimated, 0,
3717 addNotAnimated.size(), false);
3718 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003719 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003720 bindItems(addAnimated, 0,
3721 addAnimated.size(), true);
3722 }
Winson Chungc58497e2013-09-03 17:48:37 -07003723
Winson Chung87412982013-10-03 18:34:14 -07003724 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003725 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003726
Winson Chungb745afb2015-03-02 11:51:23 -08003727 if (addedApps != null && mAppsView != null) {
3728 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003729 }
Winson Chungd64d1762013-08-20 14:37:16 -07003730 }
3731
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003732 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003733 * Bind the items start-end from the list.
3734 *
3735 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003736 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003737 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003738 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3739 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003740 Runnable r = new Runnable() {
3741 public void run() {
3742 bindItems(shortcuts, start, end, forceAnimateIcons);
3743 }
3744 };
3745 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003746 return;
3747 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003748
Winson Chungf0c6ae02012-03-21 16:10:31 -07003749 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003750 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3751 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003752 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003753 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003754 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003755 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003756 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003757
3758 // Short circuit if we are loading dock items for a configuration which has no dock
3759 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3760 mHotseat == null) {
3761 continue;
3762 }
3763
Sunny Goyal639e9062015-08-19 19:17:06 -07003764 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003765 switch (item.itemType) {
3766 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3767 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003768 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003769 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003770 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003771 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003772 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003773 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003774 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003775 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003776 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003777 default:
3778 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003779 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003780
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003781 /*
3782 * Remove colliding items.
3783 */
3784 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3785 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3786 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3787 View v = cl.getChildAt(item.cellX, item.cellY);
3788 Object tag = v.getTag();
3789 String desc = "Collision while binding workspace item: " + item
3790 + ". Collides with " + tag;
3791 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3792 throw (new RuntimeException(desc));
3793 } else {
3794 Log.d(TAG, desc);
3795 LauncherModel.deleteItemFromDatabase(this, item);
3796 continue;
3797 }
3798 }
3799 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003800 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3801 item.cellY, 1, 1);
3802 if (animateIcons) {
3803 // Animate all the applications up now
3804 view.setAlpha(0f);
3805 view.setScaleX(0f);
3806 view.setScaleY(0f);
3807 bounceAnims.add(createNewAppBounceAnimation(view, i));
3808 newShortcutsScreenId = item.screenId;
3809 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003810 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003811
Winson Chung997a9232013-07-24 15:33:46 -07003812 if (animateIcons) {
3813 // Animate to the correct page
3814 if (newShortcutsScreenId > -1) {
3815 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003816 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003817 final Runnable startBounceAnimRunnable = new Runnable() {
3818 public void run() {
3819 anim.playTogether(bounceAnims);
3820 anim.start();
3821 }
3822 };
Winson Chung997a9232013-07-24 15:33:46 -07003823 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003824 // We post the animation slightly delayed to prevent slowdowns
3825 // when we are loading right after we return to launcher.
3826 mWorkspace.postDelayed(new Runnable() {
3827 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003828 if (mWorkspace != null) {
3829 mWorkspace.snapToPage(newScreenIndex);
3830 mWorkspace.postDelayed(startBounceAnimRunnable,
3831 NEW_APPS_ANIMATION_DELAY);
3832 }
Winson Chung94d67682013-09-25 16:29:40 -07003833 }
3834 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003835 } else {
3836 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003837 }
3838 }
Winson Chung64359a52013-07-08 17:17:08 -07003839 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003840 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003841 }
3842
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003843 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3844 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3845 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003846 view.updateAppWidget(null);
3847 view.setOnClickListener(this);
3848 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003849 mWorkspace.requestLayout();
3850 }
3851
Joe Onorato9c1289c2009-08-17 11:03:03 -04003852 /**
3853 * Add the views for a widget to the workspace.
3854 *
3855 * Implementation of the method from LauncherModel.Callbacks.
3856 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003857 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003858 Runnable r = new Runnable() {
3859 public void run() {
3860 bindAppWidget(item);
3861 }
3862 };
3863 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003864 return;
3865 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003866
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003867 if (mIsSafeModeEnabled) {
3868 bindSafeModeWidget(item);
3869 return;
3870 }
3871
Daniel Sandler843e8602010-06-07 14:59:01 -04003872 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3873 if (DEBUG_WIDGETS) {
3874 Log.d(TAG, "bindAppWidget: " + item);
3875 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003876
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003877 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003878
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003879 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3880 // If the provider is not ready, bind as a pending widget.
3881 appWidgetInfo = null;
3882 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3883 // The widget id is not valid. Try to find the widget based on the provider info.
3884 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3885 } else {
3886 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3887 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003888
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003889 // If the provider is ready, but the width is not yet restored, try to restore it.
3890 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3891 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003892 if (appWidgetInfo == null) {
3893 if (DEBUG_WIDGETS) {
3894 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3895 + " belongs to component " + item.providerName
3896 + ", as the povider is null");
3897 }
3898 LauncherModel.deleteItemFromDatabase(this, item);
3899 return;
3900 }
Sunny Goyalff572272014-07-23 13:58:07 -07003901
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003902 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003903 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003904 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3905 // Id has not been allocated yet. Allocate a new id.
3906 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3907 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003908
Sunny Goyald478c832016-04-01 12:04:16 -07003909 // Also try to bind the widget. If the bind fails, the user will be shown
3910 // a click to setup UI, which will ask for the bind permission.
3911 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3912 pendingInfo.spanX = item.spanX;
3913 pendingInfo.spanY = item.spanY;
3914 pendingInfo.minSpanX = item.minSpanX;
3915 pendingInfo.minSpanY = item.minSpanY;
3916 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
3917 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3918 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003919
Sunny Goyald478c832016-04-01 12:04:16 -07003920 // Bind succeeded
3921 if (success) {
3922 // If the widget has a configure activity, it is still needs to set it up,
3923 // otherwise the widget is ready to go.
3924 item.restoreStatus = (appWidgetInfo.configure == null)
3925 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3926 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003927 }
Sunny Goyald478c832016-04-01 12:04:16 -07003928
3929 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003930 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003931 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003932 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003933 // The widget was marked as UI not ready, but there is no configure activity to
3934 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003935 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3936 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003937 }
Sunny Goyalff572272014-07-23 13:58:07 -07003938 }
3939
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003940 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003941 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003942 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3943 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003944 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003945
Sunny Goyal56c73602015-09-25 12:55:01 -07003946 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003947 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003948 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003949 deleteWidgetInfo(item);
3950 return;
3951 }
3952
Sunny Goyal233ee962015-08-03 13:05:01 -07003953 item.minSpanX = appWidgetInfo.minSpanX;
3954 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003955 addAppWidgetToWorkspace(
3956 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3957 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003958 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003959 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003960 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003961 view.updateAppWidget(null);
3962 view.setOnClickListener(this);
3963 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003964 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003965 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003966
Daniel Sandler843e8602010-06-07 14:59:01 -04003967 if (DEBUG_WIDGETS) {
3968 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3969 + (SystemClock.uptimeMillis()-start) + "ms");
3970 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003971 }
3972
Sunny Goyalff572272014-07-23 13:58:07 -07003973 /**
3974 * Restores a pending widget.
3975 *
3976 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003977 */
Sunny Goyald478c832016-04-01 12:04:16 -07003978 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003979 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3980 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3981 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003982 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003983 }
3984
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003985 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003986 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003987
3988 mWorkspace.reinflateWidgetsIfNecessary();
3989 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003990 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003991 }
3992
Adam Cohen1462de32012-07-24 22:34:36 -07003993 public void onPageBoundSynchronously(int page) {
3994 mSynchronouslyBoundPages.add(page);
3995 }
3996
Sunny Goyal527c7d32015-08-28 15:19:36 -07003997 @Override
3998 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3999 if (mPendingExecutor != null) {
4000 mPendingExecutor.markCompleted();
4001 }
4002 mPendingExecutor = executor;
4003 executor.attachTo(this);
4004 }
4005
4006 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4007 if (mPendingExecutor == executor) {
4008 mPendingExecutor = null;
4009 }
4010 }
4011
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07004012 @Override
4013 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
4014 Runnable r = new Runnable() {
4015 public void run() {
4016 finishFirstPageBind(executor);
4017 }
4018 };
4019 if (waitUntilResume(r)) {
4020 return;
4021 }
4022
4023 Runnable onComplete = new Runnable() {
4024 @Override
4025 public void run() {
4026 if (executor != null) {
4027 executor.onLoadAnimationCompleted();
4028 }
4029 }
4030 };
4031 if (mDragLayer.getAlpha() < 1) {
4032 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
4033 } else {
4034 onComplete.run();
4035 }
4036 }
4037
Joe Onorato9c1289c2009-08-17 11:03:03 -04004038 /**
4039 * Callback saying that there aren't any more items to bind.
4040 *
4041 * Implementation of the method from LauncherModel.Callbacks.
4042 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004043 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004044 Runnable r = new Runnable() {
4045 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004046 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004047 }
4048 };
4049 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004050 return;
4051 }
Sunny Goyale26d1002016-06-20 14:52:14 -07004052 if (LauncherAppState.PROFILE_STARTUP) {
4053 Trace.beginSection("Page bind completed");
4054 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004055 if (mSavedState != null) {
4056 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004057 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004058 }
Sunny Goyal08442b82015-10-21 17:15:08 -07004059
Joe Onorato9c1289c2009-08-17 11:03:03 -04004060 mSavedState = null;
4061 }
4062
Adam Cohen1462de32012-07-24 22:34:36 -07004063 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004064
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004065 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07004066
4067 // If we received the result of any pending adds while the loader was running (e.g. the
4068 // widget configuration forced an orientation change), process them now.
4069 if (sPendingAddItem != null) {
4070 final long screenId = completeAdd(sPendingAddItem);
4071
4072 // TODO: this moves the user to the page where the pending item was added. Ideally,
4073 // the screen would be guaranteed to exist after bind, and the page would be set through
4074 // the workspace restore process.
4075 mWorkspace.post(new Runnable() {
4076 @Override
4077 public void run() {
4078 mWorkspace.snapToScreenId(screenId);
4079 }
4080 });
4081 sPendingAddItem = null;
4082 }
4083
Sunny Goyal756adbc2015-04-16 15:20:51 -07004084 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004085
4086 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004087 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004088 }
Sunny Goyale26d1002016-06-20 14:52:14 -07004089 if (LauncherAppState.PROFILE_STARTUP) {
4090 Trace.endSection();
4091 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004092 }
4093
Winson Chunga2413752012-04-03 14:22:34 -07004094 private boolean canRunNewAppsAnimation() {
4095 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07004096 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07004097 }
4098
Winson Chungc9168342013-06-26 14:54:55 -07004099 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004100 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004101 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4102 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004103 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004104 return bounceAnim;
4105 }
4106
Adam Cohen27772732013-11-11 14:00:56 +00004107 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004108 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004109 }
4110
Tony Wickham55616cd2015-09-23 14:55:17 -07004111 public int getSearchBarHeight() {
4112 if (mLauncherCallbacks != null) {
4113 return mLauncherCallbacks.getSearchBarHeight();
4114 }
4115 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4116 }
4117
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004118 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004119 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4120 * multiple calls to bind the same list.)
4121 */
4122 @Thunk ArrayList<AppInfo> mTmpAppsList;
4123 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4124 public void run() {
4125 bindAllApplications(mTmpAppsList);
4126 mTmpAppsList = null;
4127 }
4128 };
4129
4130 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004131 * Add the icons for all apps.
4132 *
4133 * Implementation of the method from LauncherModel.Callbacks.
4134 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004135 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004136 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4137 mTmpAppsList = apps;
4138 return;
4139 }
4140
Winson Chungb745afb2015-03-02 11:51:23 -08004141 if (mAppsView != null) {
4142 mAppsView.setApps(apps);
4143 }
Adam Cohen9211d422014-10-07 18:14:53 -07004144 if (mLauncherCallbacks != null) {
4145 mLauncherCallbacks.bindAllApplications(apps);
4146 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004147 }
4148
4149 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004150 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
4151 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
4152 */
4153 @Override
4154 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
4155 mDeepShortcutMap = deepShortcutMapCopy;
4156 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
4157 }
4158
Tony Wickham1bce7fd2016-04-28 17:39:03 -07004159 public List<String> getShortcutIdsForItem(ItemInfo info) {
4160 if (!DeepShortcutManager.supportsShortcuts(info)) {
4161 return Collections.EMPTY_LIST;
4162 }
4163 ComponentName component = info.getTargetComponent();
4164 List<String> ids = mDeepShortcutMap.get(new ComponentKey(component, info.user));
4165 return ids == null ? Collections.EMPTY_LIST : ids;
4166 }
4167
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004168 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004169 * A package was updated.
4170 *
4171 * Implementation of the method from LauncherModel.Callbacks.
4172 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004173 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004174 Runnable r = new Runnable() {
4175 public void run() {
4176 bindAppsUpdated(apps);
4177 }
4178 };
4179 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004180 return;
4181 }
4182
Winson Chungb745afb2015-03-02 11:51:23 -08004183 if (mAppsView != null) {
4184 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004185 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004186 }
4187
Sunny Goyal4390ace2014-10-13 11:33:11 -07004188 @Override
4189 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4190 Runnable r = new Runnable() {
4191 public void run() {
4192 bindWidgetsRestored(widgets);
4193 }
4194 };
4195 if (waitUntilResume(r)) {
4196 return;
4197 }
4198 mWorkspace.widgetsRestored(widgets);
4199 }
4200
Joe Onorato9c1289c2009-08-17 11:03:03 -04004201 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004202 * Some shortcuts were updated in the background.
4203 *
4204 * Implementation of the method from LauncherModel.Callbacks.
4205 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004206 @Override
4207 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4208 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004209 Runnable r = new Runnable() {
4210 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004211 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004212 }
4213 };
4214 if (waitUntilResume(r)) {
4215 return;
4216 }
4217
Sunny Goyal4390ace2014-10-13 11:33:11 -07004218 if (!updated.isEmpty()) {
4219 mWorkspace.updateShortcuts(updated);
4220 }
4221
4222 if (!removed.isEmpty()) {
4223 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4224 for (ShortcutInfo si : removed) {
4225 removedComponents.add(si.getTargetComponent());
4226 }
4227 mWorkspace.removeItemsByComponentName(removedComponents, user);
4228 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004229 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004230 }
4231 }
4232
4233 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004234 * Update the state of a package, typically related to install state.
4235 *
4236 * Implementation of the method from LauncherModel.Callbacks.
4237 */
Sunny Goyale755d462014-07-22 13:48:29 -07004238 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004239 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4240 Runnable r = new Runnable() {
4241 public void run() {
4242 bindRestoreItemsChange(updates);
4243 }
4244 };
4245 if (waitUntilResume(r)) {
4246 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004247 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004248
Sunny Goyal756adbc2015-04-16 15:20:51 -07004249 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004250 }
4251
4252 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004253 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004254 * in addition to specific applications to remove, the reason being that
4255 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004256 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004257 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004258 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004259 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004260 public void bindWorkspaceComponentsRemoved(
4261 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4262 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004263 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004264 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004265 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004266 }
Winson Chungd64d1762013-08-20 14:37:16 -07004267 };
4268 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004269 return;
4270 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004271 if (!packageNames.isEmpty()) {
4272 mWorkspace.removeItemsByPackageName(packageNames, user);
4273 }
4274 if (!components.isEmpty()) {
4275 mWorkspace.removeItemsByComponentName(components, user);
4276 }
4277 // Notify the drag controller
4278 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004279
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004280 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004281
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004282 @Override
4283 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4284 Runnable r = new Runnable() {
4285 public void run() {
4286 bindAppInfosRemoved(appInfos);
4287 }
4288 };
4289 if (waitUntilResume(r)) {
4290 return;
Joe Onorato36115782010-06-17 13:28:48 -04004291 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004292
Winson Chungdf95eb12013-10-16 14:57:07 -07004293 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004294 if (mAppsView != null) {
4295 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004296 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004297 }
Joe Onoratobe386092009-11-17 17:32:16 -08004298
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004299 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004300 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004301 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004302 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004303 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004304
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004305 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004306 public void bindWidgetsModel(WidgetsModel model) {
4307 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004308 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004309 return;
4310 }
4311
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004312 if (mWidgetsView != null && model != null) {
4313 mWidgetsView.addWidgets(model);
4314 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004315 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004316 }
4317
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004318 @Override
4319 public void notifyWidgetProvidersChanged() {
4320 if (mWorkspace.getState().shouldUpdateWidget) {
4321 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4322 }
4323 }
4324
Winson Chung400438b2011-01-16 17:53:48 -08004325 private int mapConfigurationOriActivityInfoOri(int configOri) {
4326 final Display d = getWindowManager().getDefaultDisplay();
4327 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4328 switch (d.getRotation()) {
4329 case Surface.ROTATION_0:
4330 case Surface.ROTATION_180:
4331 // We are currently in the same basic orientation as the natural orientation
4332 naturalOri = configOri;
4333 break;
4334 case Surface.ROTATION_90:
4335 case Surface.ROTATION_270:
4336 // We are currently in the other basic orientation to the natural orientation
4337 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4338 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4339 break;
4340 }
4341
4342 int[] oriMap = {
4343 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4344 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4345 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4346 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4347 };
4348 // Since the map starts at portrait, we need to offset if this device's natural orientation
4349 // is landscape.
4350 int indexOffset = 0;
4351 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4352 indexOffset = 1;
4353 }
4354 return oriMap[(d.getRotation() + indexOffset) % 4];
4355 }
Adam Cohen446e9402011-09-15 18:21:21 -07004356
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004357 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004358 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004359 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004360 if (Utilities.ATLEAST_JB_MR2) {
4361 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4362 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004363 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4364 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004365 }
Winson Chung4b919f82012-05-01 10:44:08 -07004366 }
4367 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004368
Winson Chung4b919f82012-05-01 10:44:08 -07004369 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004370 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004371 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004372 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004373 } else {
4374 mHandler.postDelayed(new Runnable() {
4375 public void run() {
4376 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4377 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004378 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004379 }
Winson Chung4b919f82012-05-01 10:44:08 -07004380 }
Winson Chung400438b2011-01-16 17:53:48 -08004381 }
4382
Adam Cohenea90f832014-05-21 15:03:34 -07004383 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004384 * To be overridden by subclasses to indicate that there is an activity to launch
4385 * before showing the standard launcher experience.
4386 */
4387 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004388 if (mLauncherCallbacks != null) {
4389 return mLauncherCallbacks.hasFirstRunActivity();
4390 }
Adam Cohen432609a2014-03-13 17:03:22 -07004391 return false;
4392 }
4393
4394 /**
4395 * To be overridden by subclasses to launch any first run activity
4396 */
4397 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004398 if (mLauncherCallbacks != null) {
4399 return mLauncherCallbacks.getFirstRunActivity();
4400 }
Adam Cohen432609a2014-03-13 17:03:22 -07004401 return null;
4402 }
4403
Winson Chunga6945242014-01-08 14:04:34 -08004404 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004405 return !ActivityManager.isRunningInTestHarness() &&
4406 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004407 }
4408
Adam Cohen432609a2014-03-13 17:03:22 -07004409 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004410 if (shouldRunFirstRunActivity() &&
4411 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004412 Intent firstRunIntent = getFirstRunActivity();
4413 if (firstRunIntent != null) {
4414 startActivity(firstRunIntent);
4415 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004416 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004417 }
4418 }
Adam Cohen432609a2014-03-13 17:03:22 -07004419 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004420 }
4421
4422 private void markFirstRunActivityShown() {
4423 SharedPreferences.Editor editor = mSharedPrefs.edit();
4424 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4425 editor.apply();
4426 }
4427
Hyunyoung Songc001cf52016-07-21 17:32:43 -07004428 private void markAppsViewShown() {
4429 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4430 return;
4431 }
4432 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
4433 }
4434
4435 private boolean shouldShowDiscoveryBounce() {
4436 if (mState != mState.WORKSPACE) {
4437 return false;
4438 }
4439 if (mLauncherCallbacks != null && mLauncherCallbacks.shouldShowDiscoveryBounce()) {
4440 return true;
4441 }
4442 if (!mIsResumeFromActionScreenOff) {
4443 return false;
4444 }
4445 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4446 return false;
4447 }
4448 return true;
4449 }
4450
Adam Cohen432609a2014-03-13 17:03:22 -07004451 /**
4452 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4453 * screen that must be displayed and dismissed.
4454 */
4455 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004456 if (mLauncherCallbacks != null) {
4457 return mLauncherCallbacks.hasDismissableIntroScreen();
4458 }
Adam Cohen432609a2014-03-13 17:03:22 -07004459 return false;
4460 }
4461
4462 /**
4463 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4464 */
4465 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004466 if (mLauncherCallbacks != null) {
4467 return mLauncherCallbacks.getIntroScreen();
4468 }
Adam Cohen432609a2014-03-13 17:03:22 -07004469 return null;
4470 }
4471
4472 /**
4473 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4474 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4475 */
4476 private boolean shouldShowIntroScreen() {
4477 return hasDismissableIntroScreen() &&
4478 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4479 }
4480
4481 protected void showIntroScreen() {
4482 View introScreen = getIntroScreen();
4483 changeWallpaperVisiblity(false);
4484 if (introScreen != null) {
4485 mDragLayer.showOverlayView(introScreen);
4486 }
4487 }
4488
Winson Chung5ffd51d2015-06-25 11:36:50 -07004489 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004490 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004491 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004492 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004493 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004494 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004495 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4496 if (userHandle != null) {
4497 user = UserHandleCompat.fromUser(userHandle);
4498 }
4499 }
4500 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004501 }
4502
4503 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004504 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004505 if (user == null) {
4506 user = UserHandleCompat.myUserHandle();
4507 }
4508
4509 // Called from search suggestion, add the profile extra to the intent to ensure that we
4510 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004511 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004512 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004513 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004514 return null;
4515 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004516 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004517 }
4518
Winson Chung5ffd51d2015-06-25 11:36:50 -07004519 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004520 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4521 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004522 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004523 UserHandleCompat.myUserHandle());
4524 }
4525
Winson Chung5ffd51d2015-06-25 11:36:50 -07004526 protected void moveWorkspaceToDefaultScreen() {
4527 mWorkspace.moveToDefaultScreen(false);
4528 }
4529
Winson Chung80baf5a2010-08-09 16:03:15 -07004530 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004531 * Returns a FastBitmapDrawable with the icon, accurately sized.
4532 */
4533 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4534 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4535 d.setFilterBitmap(true);
4536 resizeIconDrawable(d);
4537 return d;
4538 }
4539
4540 /**
4541 * Resizes an icon drawable to the correct icon size.
4542 */
Winson5fbe0742015-09-30 15:33:00 -07004543 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004544 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004545 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004546 }
4547
4548 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004549 * Prints out out state for debugging.
4550 */
4551 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004552 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004553 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004554 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4555 Log.d(TAG, "mRestoring=" + mRestoring);
4556 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004557 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004558 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004559
Daniel Sandler325dc232013-06-05 22:57:57 -04004560 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004561 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004562
4563 @Override
4564 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4565 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004566 // Dump workspace
4567 writer.println(prefix + "Workspace Items");
4568 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4569 writer.println(prefix + " Homescreen " + i);
4570
4571 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4572 for (int j = 0; j < layout.getChildCount(); j++) {
4573 Object tag = layout.getChildAt(j).getTag();
4574 if (tag != null) {
4575 writer.println(prefix + " " + tag.toString());
4576 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004577 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004578 }
Sunny Goyala1365452015-10-01 15:46:24 -07004579
4580 writer.println(prefix + " Hotseat");
4581 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4582 for (int j = 0; j < layout.getChildCount(); j++) {
4583 Object tag = layout.getChildAt(j).getTag();
4584 if (tag != null) {
4585 writer.println(prefix + " " + tag.toString());
4586 }
4587 }
4588
Sunny Goyal713edfc2016-05-06 09:58:34 -07004589 try {
4590 FileLog.flushAll(writer);
4591 } catch (Exception e) {
4592 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004593 }
4594
Adam Cohen9211d422014-10-07 18:14:53 -07004595 if (mLauncherCallbacks != null) {
4596 mLauncherCallbacks.dump(prefix, fd, writer, args);
4597 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004598 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004599
Adam Cohen59400422014-03-05 18:07:04 -08004600 public static CustomAppWidget getCustomAppWidget(String name) {
4601 return sCustomAppWidgets.get(name);
4602 }
4603
4604 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4605 return sCustomAppWidgets;
4606 }
4607
Sunny Goyal29538332016-02-18 09:10:19 -08004608 public static List<View> getFolderContents(View icon) {
4609 if (icon instanceof FolderIcon) {
4610 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4611 } else {
4612 return Collections.EMPTY_LIST;
4613 }
4614 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004615
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004616 public static Launcher getLauncher(Context context) {
4617 if (context instanceof Launcher) {
4618 return (Launcher) context;
4619 }
4620 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4621 }
4622
Sunny Goyal745bad92016-05-02 10:54:12 -07004623 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4624
4625 @Override
4626 public void onSharedPreferenceChanged(
4627 SharedPreferences sharedPreferences, String key) {
4628 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4629 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4630 if (!waitUntilResume(this, true)) {
4631 run();
4632 }
4633 }
4634 }
4635
4636 @Override
4637 public void run() {
4638 setOrientation();
4639 }
4640 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004641}