blob: deac73be8af2010c2fd3ae2d47f99c84a4848f7e [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 Goyal2bba4c32015-05-18 15:42:48 -0700468 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700469 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470 }
471
472 // For handling default keys
473 mDefaultKeySsb = new SpannableStringBuilder();
474 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800475
476 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700477 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
478 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800479
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800480 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700481 // In case we are on a device with locked rotation, we should look at preferences to check
482 // if the user has specifically allowed rotation.
483 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700484 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700485 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
486 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700487 }
488
489 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
490 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400491 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700492
Adam Cohen9211d422014-10-07 18:14:53 -0700493 if (mLauncherCallbacks != null) {
494 mLauncherCallbacks.onCreate(savedInstanceState);
495 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700496
497 if (shouldShowIntroScreen()) {
498 showIntroScreen();
499 } else {
500 showFirstRunActivity();
Adam Cohenc3e12c72014-10-31 16:15:36 -0700501 }
Adam Cohen9211d422014-10-07 18:14:53 -0700502 }
503
Sunny Goyal7779d622015-06-11 16:18:39 -0700504 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700505 public void onExtractedColorsChanged() {
506 loadExtractedColorsAndColorItems();
507 }
508
509 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700510 // TODO: do this in pre-N as well, once the extraction part is complete.
511 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700512 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700513 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700514 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham827cef22016-03-17 15:39:39 -0700515 }
516 }
517
Adam Cohen9211d422014-10-07 18:14:53 -0700518 private LauncherCallbacks mLauncherCallbacks;
519
520 public void onPostCreate(Bundle savedInstanceState) {
521 super.onPostCreate(savedInstanceState);
522 if (mLauncherCallbacks != null) {
523 mLauncherCallbacks.onPostCreate(savedInstanceState);
524 }
525 }
526
Winson1f064272016-07-18 17:18:02 -0700527 public void onInsetsChanged(Rect insets) {
528 mDeviceProfile.updateInsets(insets);
529 mDeviceProfile.layout(this, true /* notifyListeners */);
530 }
531
Sunny Goyal32554d12015-12-03 15:31:25 -0800532 /**
533 * Call this after onCreate to set or clear overlay.
534 */
535 public void setLauncherOverlay(LauncherOverlay overlay) {
536 if (overlay != null) {
537 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
538 }
539 mWorkspace.setLauncherOverlay(overlay);
540 }
541
Adam Cohen9211d422014-10-07 18:14:53 -0700542 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
543 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700544 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700545 private boolean mWorkspaceImportanceStored = false;
546 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700547 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800548 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700549 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
550
551 @Override
552 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700553 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700554 return;
555 }
556 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700557 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700558 if (mWorkspace != null) {
559 mWorkspaceImportanceForAccessibility =
560 mWorkspace.getImportantForAccessibility();
561 mWorkspace.setImportantForAccessibility(
562 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
563 mWorkspaceImportanceStored = true;
564 }
565 if (mHotseat != null) {
566 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
567 mHotseat.setImportantForAccessibility(
568 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
569 mHotseatImportanceStored = true;
570 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700571 }
572
573 @Override
574 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700575 if (mWorkspaceImportanceStored && mWorkspace != null) {
576 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
577 }
578 if (mHotseatImportanceStored && mHotseat != null) {
579 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
580 }
581 mWorkspaceImportanceStored = false;
582 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700583 }
584 });
Adam Cohen9211d422014-10-07 18:14:53 -0700585 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700586 }
587
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700588 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700589 public void onLauncherProviderChange() {
590 if (mLauncherCallbacks != null) {
591 mLauncherCallbacks.onLauncherProviderChange();
592 }
593 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700594
Adam Cohen9211d422014-10-07 18:14:53 -0700595 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700596 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700597 if (mLauncherCallbacks != null) {
598 return mLauncherCallbacks.hasCustomContentToLeft();
599 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700600 return false;
601 }
602
Dave Hawkeya8881582013-09-17 15:55:33 -0600603 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500604 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600605 * {@link #addToCustomContentPage}. This will only be invoked if
606 * {@link #hasCustomContentToLeft()} is {@code true}.
607 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500608 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700609 if (mLauncherCallbacks != null) {
610 mLauncherCallbacks.populateCustomContentContainer();
611 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600612 }
613
614 /**
615 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
616 * ensure the custom content page is added or removed if necessary.
617 */
618 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600619 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600620 // Not bound yet, wait for bindScreens to be called.
621 return;
622 }
623
624 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
625 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500626 mWorkspace.createCustomContentContainer();
627 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600628 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
629 mWorkspace.removeCustomContentPage();
630 }
631 }
632
Hyunyoung Songaa953652016-04-19 18:30:24 -0700633 public UserEventDispatcher getUserEventDispatcher() {
634 if (mLauncherCallbacks != null) {
635 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
636 if (dispatcher != null) {
637 return dispatcher;
638 }
639 }
640
Sunny Goyal64976d52016-06-20 14:56:28 -0700641 // Logger object is a singleton and does not have to be coupled with the foreground
642 // activity. Since most user event logging is done on the UI, the object is retrieved
643 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700644 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700645 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700646 }
647 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800648 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100649
Hyunyoung Song3f471442015-04-08 19:01:34 -0700650 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700651 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
652 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700653 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700654 }
655
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000656 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700657 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
658 // This cast is safe as long as the id < 0x00FFFFFF
659 // Since we jail all the dynamically generated views, there should be no clashes
660 // with any other views.
661 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000662 }
663
664 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700665 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
666 * a configuration step, this allows the proper animations to run after other transitions.
667 */
Adam Cohendb364c32014-05-20 14:23:40 -0700668 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700669 long screenId = args.screenId;
670 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
671 // When the screen id represents an actual screen (as opposed to a rank) we make sure
672 // that the drop page actually exists.
673 screenId = ensurePendingDropLayoutExists(args.screenId);
674 }
Adam Cohendb364c32014-05-20 14:23:40 -0700675
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800676 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800677 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700678 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700679 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700680 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800681 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700682 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700683 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700684 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyald478c832016-04-01 12:04:16 -0700685 completeRestoreAppWidget(args.appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700686 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700687 case REQUEST_BIND_PENDING_APPWIDGET: {
688 int widgetId = args.appWidgetId;
689 LauncherAppWidgetInfo info =
690 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
691 if (info != null) {
692 // Since the view was just bound, also launch the configure activity if needed
693 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
694 .getLauncherAppWidgetInfo(widgetId);
695 if (provider != null && provider.configure != null) {
696 startRestoredWidgetReconfigActivity(provider, info);
697 }
698 }
699 break;
700 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800701 }
Michael Jurka27614d22012-04-02 06:40:22 -0700702 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
703 // if you turned the screen off and then back while in All Apps, Launcher would not
704 // return to the workspace. Clearing mAddInfo.container here fixes this issue
705 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700706 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800707 }
708
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800709 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700710 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700711 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700712 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700713 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800714 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700715
Adam Cohenad4e15c2013-10-17 16:21:35 -0700716 Runnable exitSpringLoaded = new Runnable() {
717 @Override
718 public void run() {
719 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
720 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
721 }
722 };
723
Michael Jurka8b805b12012-04-18 14:23:14 -0700724 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700725 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700726 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700727 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
728 if (resultCode == RESULT_CANCELED) {
729 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700730 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700731 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700732 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800733 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700734 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700735 }
736 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200737 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
738 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700739 // User could have free-scrolled between pages before picking a wallpaper; make sure
740 // we move to the closest one now.
741 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700742 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200743 }
744 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700745 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200746
Adam Cohened66b2b2012-01-23 17:28:51 -0800747 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700748 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700749
Adam Cohendb364c32014-05-20 14:23:40 -0700750 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800751 // We have special handling for widgets
752 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800753 final int appWidgetId;
754 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
755 : -1;
756 if (widgetId < 0) {
757 appWidgetId = pendingAddWidgetId;
758 } else {
759 appWidgetId = widgetId;
760 }
761
Adam Cohenad4e15c2013-10-17 16:21:35 -0700762 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800763 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700764 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
765 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700766 result = RESULT_CANCELED;
767 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700768 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700769 @Override
770 public void run() {
771 exitSpringLoadedDragModeDelayed(false, 0, null);
772 }
773 };
Adam Cohendb364c32014-05-20 14:23:40 -0700774 if (workspaceLocked) {
775 // No need to remove the empty screen if we're mid-binding, as the
776 // the bind will not add the empty screen.
777 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
778 } else {
779 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
780 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
781 }
Winson Chung5aaab772012-04-27 15:24:02 -0700782 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700783 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700784 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
785 // When the screen id represents an actual screen (as opposed to a rank)
786 // we make sure that the drop page actually exists.
787 mPendingAddInfo.screenId =
788 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
789 }
Adam Cohendb364c32014-05-20 14:23:40 -0700790 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
791
792 dropLayout.setDropPending(true);
793 final Runnable onComplete = new Runnable() {
794 @Override
795 public void run() {
796 completeTwoStageWidgetDrop(resultCode, appWidgetId);
797 dropLayout.setDropPending(false);
798 }
799 };
800 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
801 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
802 } else {
803 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
804 mPendingAddInfo);
805 sPendingAddItem = args;
806 }
Winson Chung5aaab772012-04-27 15:24:02 -0700807 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800808 return;
809 }
810
Sunny Goyald478c832016-04-01 12:04:16 -0700811 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
812 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700813 if (resultCode == RESULT_OK) {
814 // Update the widget view.
815 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
816 pendingAddWidgetId, mPendingAddInfo);
817 if (workspaceLocked) {
818 sPendingAddItem = args;
819 } else {
820 completeAdd(args);
821 }
822 }
823 // Leave the widget in the pending state if the user canceled the configure.
824 return;
825 }
826
Sunny Goyalaad90582015-07-09 14:22:50 -0700827 if (requestCode == REQUEST_CREATE_SHORTCUT) {
828 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
829 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
830 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
831 mPendingAddInfo);
832 if (isWorkspaceLocked()) {
833 sPendingAddItem = args;
834 } else {
835 completeAdd(args);
836 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
837 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
838 }
839 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700840 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
841 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800844 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800845
846 }
847
848 @Override
849 protected void onActivityResult(
850 final int requestCode, final int resultCode, final Intent data) {
851 handleActivityResult(requestCode, resultCode, data);
852 if (mLauncherCallbacks != null) {
853 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
854 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800855 }
856
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600857 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700858 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600859 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700860 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
861 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
862 View v = null;
863 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
864 if (layout != null) {
865 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
866 }
867 Intent intent = sPendingAddItem.intent;
868 sPendingAddItem = null;
869 if (grantResults.length > 0
870 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700871 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700872 } else {
873 // TODO: Show a snack bar with link to settings
874 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
875 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
876 }
877 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600878 if (mLauncherCallbacks != null) {
879 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
880 grantResults);
881 }
882 }
883
Adam Cohendb364c32014-05-20 14:23:40 -0700884 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
885 appWidgetId, ItemInfo info) {
886 PendingAddArguments args = new PendingAddArguments();
887 args.requestCode = requestCode;
888 args.intent = data;
889 args.container = info.container;
890 args.screenId = info.screenId;
891 args.cellX = info.cellX;
892 args.cellY = info.cellY;
893 args.appWidgetId = appWidgetId;
894 return args;
895 }
896
897 /**
898 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
899 *
900 * @param screenId the screen id to check
901 * @return the new screen, or screenId if it exists
902 */
903 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800904 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700905 if (dropLayout == null) {
906 // it's possible that the add screen was removed because it was
907 // empty and a re-bind occurred
908 mWorkspace.addExtraEmptyScreen();
909 return mWorkspace.commitExtraEmptyScreen();
910 } else {
911 return screenId;
912 }
913 }
914
Adam Cohen091440a2015-03-18 14:16:05 -0700915 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800916 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800917 Runnable onCompleteRunnable = null;
918 int animationType = 0;
919
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700920 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800921 if (resultCode == RESULT_OK) {
922 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
923 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700924 mPendingAddWidgetInfo);
925 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800926 onCompleteRunnable = new Runnable() {
927 @Override
928 public void run() {
929 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700930 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700931 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
932 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800933 }
934 };
935 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800936 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800937 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800938 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700939 if (mDragLayer.getAnimatedView() != null) {
940 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
941 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
942 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700943 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700944 // The animated view may be null in the case of a rotation during widget configuration
945 onCompleteRunnable.run();
946 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947 }
948
949 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700950 protected void onStop() {
951 super.onStop();
952 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700953
954 if (mLauncherCallbacks != null) {
955 mLauncherCallbacks.onStop();
956 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700957
958 if (Utilities.isNycMR1OrAbove()) {
959 mAppWidgetHost.stopListening();
960 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700961 }
962
963 @Override
964 protected void onStart() {
965 super.onStart();
966 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700967
968 if (mLauncherCallbacks != null) {
969 mLauncherCallbacks.onStart();
970 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700971
972 if (Utilities.isNycMR1OrAbove()) {
973 mAppWidgetHost.startListening();
974 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700975 }
976
977 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200979 long startTime = 0;
980 if (DEBUG_RESUME_TIME) {
981 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400982 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200983 }
Adam Cohen9211d422014-10-07 18:14:53 -0700984
985 if (mLauncherCallbacks != null) {
986 mLauncherCallbacks.preOnResume();
987 }
988
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700990 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700991
Winson Chung4a2afa32012-07-19 14:53:05 -0700992 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700993 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700994 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800995 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700996 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700997 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700998 // view after launching an app, as they may be depending on the UI to be static to
999 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -07001000 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07001001 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08001002 } else if (mOnResumeState == State.WIDGETS) {
1003 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001004 }
1005 mOnResumeState = State.NONE;
1006
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001007 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001008 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1009 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001010
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001011 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001012 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001013 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001014 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001015 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001016 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001018 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001019 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1020 // execute them here
1021 long startTimeCallbacks = 0;
1022 if (DEBUG_RESUME_TIME) {
1023 startTimeCallbacks = System.currentTimeMillis();
1024 }
1025
Michael Jurka1e2f4652013-07-08 18:03:46 -07001026 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1027 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001028 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001029 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001030 if (DEBUG_RESUME_TIME) {
1031 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1032 (System.currentTimeMillis() - startTimeCallbacks));
1033 }
Michael Jurka447bf842013-05-15 14:52:15 +02001034 }
Michael Jurka54554252013-08-01 12:52:23 +02001035 if (mOnResumeCallbacks.size() > 0) {
1036 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1037 mOnResumeCallbacks.get(i).run();
1038 }
1039 mOnResumeCallbacks.clear();
1040 }
Winson Chunge4e50662012-01-23 14:45:13 -08001041
1042 // Reset the pressed state of icons that were locked in the press state while activities
1043 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001044 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001045 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001046 mWaitingForResume.setStayPressed(false);
1047 }
Winson Chung82963d52013-10-09 11:20:57 -07001048
Adam Cohen06dff352012-06-01 17:17:08 -07001049 // It is possible that widgets can receive updates while launcher is not in the foreground.
1050 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1051 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1052 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001053 if (!isWorkspaceLoading()) {
1054 getWorkspace().reinflateWidgetsIfNecessary();
1055 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001056
Michael Jurka447bf842013-05-15 14:52:15 +02001057 if (DEBUG_RESUME_TIME) {
1058 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1059 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001060
Adam Cohen4b66bf32015-09-18 12:15:19 -07001061 // We want to suppress callbacks about CustomContent being shown if we have just received
1062 // onNewIntent while the user was present within launcher. In that case, we post a call
1063 // to move the user to the main screen (which will occur after onResume). We don't want to
1064 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1065 // suppress here.
1066 if (mWorkspace.getCustomContentCallbacks() != null
1067 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001068 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001069 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001070 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1071 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001072 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001073 }
1074 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001075 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001076 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001077 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001078
Sunny Goyal756adbc2015-04-16 15:20:51 -07001079 if (!isWorkspaceLoading()) {
1080 // Process any items that were added while Launcher was away.
1081 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1082 }
Adam Cohen9211d422014-10-07 18:14:53 -07001083
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001084 if (shouldShowDiscoveryBounce()) {
1085 mAllAppsController.showDiscoveryBounce();
1086 }
1087 mIsResumeFromActionScreenOff = false;
Adam Cohen9211d422014-10-07 18:14:53 -07001088 if (mLauncherCallbacks != null) {
1089 mLauncherCallbacks.onResume();
1090 }
Adam Cohen06dff352012-06-01 17:17:08 -07001091 }
1092
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001094 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001095 // Ensure that items added to Launcher are queued until Launcher returns
1096 InstallShortcutReceiver.enableInstallQueue();
1097
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001098 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001099 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001100 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001101 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001102
1103 // We call onHide() aggressively. The custom content callbacks should be able to
1104 // debounce excess onHide calls.
1105 if (mWorkspace.getCustomContentCallbacks() != null) {
1106 mWorkspace.getCustomContentCallbacks().onHide();
1107 }
Romain Guycbb89e42009-06-08 15:52:54 -07001108
Adam Cohen9211d422014-10-07 18:14:53 -07001109 if (mLauncherCallbacks != null) {
1110 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001111 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001112 }
1113
1114 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001115 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1116 // by a onResume or by scrolling otherwise.
1117 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001118
1119 // Custom content is completely hidden
1120 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001121
1122 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1123 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001124
1125 // Indicates whether the user is allowed to scroll away from the custom content.
1126 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001127 }
1128
Adam Cohenc2d6e892014-10-16 09:49:24 -07001129 public interface LauncherOverlay {
1130
1131 /**
1132 * Touch interaction leading to overscroll has begun
1133 */
1134 public void onScrollInteractionBegin();
1135
1136 /**
1137 * Touch interaction related to overscroll has ended
1138 */
1139 public void onScrollInteractionEnd();
1140
1141 /**
1142 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1143 * screen (or in the case of RTL, the rightmost screen).
1144 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001145 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001146
1147 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001148 * Called when the launcher is ready to use the overlay
1149 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001150 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001151 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001152 }
1153
Jun Mukai8af0cd82015-05-12 12:32:12 -07001154 public interface LauncherSearchCallbacks {
1155 /**
1156 * Called when the search overlay is shown.
1157 */
1158 public void onSearchOverlayOpened();
1159
1160 /**
1161 * Called when the search overlay is dismissed.
1162 */
1163 public void onSearchOverlayClosed();
1164 }
1165
Adam Cohenc2d6e892014-10-16 09:49:24 -07001166 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001167 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001168 }
1169
1170 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1171
Sunny Goyalc86df472016-02-25 09:19:38 -08001172 public void onScrollChanged(float progress) {
1173 if (mWorkspace != null) {
1174 mWorkspace.onOverlayScrollChanged(progress);
1175 }
1176 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001177 }
1178
Jorim Jaggid017f882014-01-14 17:08:48 -08001179 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001180 if (mLauncherCallbacks != null) {
1181 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001182 } else {
1183 // On devices with a locked orientation, we will at least have the allow rotation
1184 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001185 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001186 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001187 }
1188
Adam Cohen9211d422014-10-07 18:14:53 -07001189 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001190 CustomContentCallbacks callbacks, String description) {
1191 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001192 }
1193
Adam Cohenedb40762013-07-18 16:45:45 -07001194 // The custom content needs to offset its content to account for the QSB
1195 public int getTopOffsetForCustomContent() {
1196 return mWorkspace.getPaddingTop();
1197 }
1198
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001199 @Override
1200 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001201 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001202 if (mModel.isCurrentCallbacks(this)) {
1203 mModel.stopLoader();
1204 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001205 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1206
Romain Guy13c2e7b2010-03-10 19:45:00 -08001207 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001208 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001209
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001210 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001211 @Override
1212 public void onWindowFocusChanged(boolean hasFocus) {
1213 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001214 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001215
1216 if (mLauncherCallbacks != null) {
1217 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1218 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001219 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001220
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221 private boolean acceptFilter() {
1222 final InputMethodManager inputManager = (InputMethodManager)
1223 getSystemService(Context.INPUT_METHOD_SERVICE);
1224 return !inputManager.isFullscreenMode();
1225 }
1226
1227 @Override
1228 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001229 final int uniChar = event.getUnicodeChar();
1230 final boolean handled = super.onKeyDown(keyCode, event);
1231 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1232 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1234 keyCode, event);
1235 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001236 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001237 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001238 // showSearchDialog()
1239 // If there are multiple keystrokes before the search dialog takes focus,
1240 // onSearchRequested() will be called for every keystroke,
1241 // but it is idempotent, so it's fine.
1242 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 }
1244 }
1245
Joe Onorato8a9625e2010-01-28 15:55:35 -08001246 // Eat the long press event so the keyboard doesn't come up.
1247 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1248 return true;
1249 }
1250
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 return handled;
1252 }
1253
Winson46163472015-09-22 17:31:56 -07001254 @Override
1255 public boolean onKeyUp(int keyCode, KeyEvent event) {
1256 if (keyCode == KeyEvent.KEYCODE_MENU) {
1257 // Ignore the menu key if we are currently dragging or are on the custom content screen
1258 if (!isOnCustomContent() && !mDragController.isDragging()) {
1259 // Close any open folders
1260 closeFolder();
1261
Tony Wickham49c8d292016-07-01 11:44:34 -07001262 // Close any shortcuts containers
1263 closeShortcutsContainer();
1264
Winson46163472015-09-22 17:31:56 -07001265 // Stop resizing any widgets
1266 mWorkspace.exitWidgetResizeMode();
1267
1268 // Show the overview mode if we are on the workspace
1269 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1270 !mWorkspace.isSwitchingState()) {
1271 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001272 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001273 }
1274 }
1275 return true;
1276 }
1277 return super.onKeyUp(keyCode, event);
1278 }
1279
Karl Rosaen138a0412009-04-23 19:00:21 -07001280 private String getTypedText() {
1281 return mDefaultKeySsb.toString();
1282 }
1283
1284 private void clearTypedText() {
1285 mDefaultKeySsb.clear();
1286 mDefaultKeySsb.clearSpans();
1287 Selection.setSelection(mDefaultKeySsb, 0);
1288 }
1289
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001291 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1292 * State
1293 */
1294 private static State intToState(int stateOrdinal) {
1295 State state = State.WORKSPACE;
1296 final State[] stateValues = State.values();
1297 for (int i = 0; i < stateValues.length; i++) {
1298 if (stateValues[i].ordinal() == stateOrdinal) {
1299 state = stateValues[i];
1300 break;
1301 }
1302 }
1303 return state;
1304 }
1305
1306 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 * Restores the previous state, if it exists.
1308 *
1309 * @param savedState The previous state.
1310 */
1311 private void restoreState(Bundle savedState) {
1312 if (savedState == null) {
1313 return;
1314 }
1315
Michael Jurka883f55b2010-10-21 15:47:14 -07001316 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001317 if (state == State.APPS || state == State.WIDGETS) {
1318 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001319 }
1320
Adam Cohen21cd0022013-10-09 18:57:02 -07001321 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1322 PagedView.INVALID_RESTORE_PAGE);
1323 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001324 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 }
1326
Sunny Goyal756cd262015-08-20 12:33:21 -07001327 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1328 if (itemValues != null) {
1329 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001330 AppWidgetProviderInfo info = savedState.getParcelable(
1331 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001332 mPendingAddWidgetInfo = info == null ?
1333 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1334
Adam Cohen4637b5a2013-11-04 18:21:24 -08001335 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001336 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 mRestoring = true;
1338 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339 }
1340
1341 /**
1342 * Finds all the views we need and configure them properly.
1343 */
1344 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001345 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001346 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001347 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001348 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyal6178f132016-07-11 17:30:03 -07001349 mQsbContainer = mDragLayer.findViewById(mDeviceProfile.isVerticalBarLayout()
1350 ? R.id.workspace_blocked_row : R.id.qsb_container);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001351 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001352
Sunny Goyal784f9c32016-03-23 16:56:54 -07001353 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1354 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1355 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001356 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357
Winson Chung4c98d922011-05-31 16:50:48 -07001358 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001359
1360 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001361
Winson Chung3d503fb2011-07-13 17:25:49 -07001362 // Setup the hotseat
1363 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1364 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001365 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001366 }
1367
Winsone9f27272015-10-13 10:47:51 -07001368 // Setup the overview panel
1369 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001370
Winson Chung4c98d922011-05-31 16:50:48 -07001371 // Setup the workspace
1372 mWorkspace.setHapticFeedbackEnabled(false);
1373 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001374 mWorkspace.setup(mDragController);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001375 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001376 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001377
Tony Wickham34d2c912015-09-11 09:27:58 -07001378 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001379 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001380
Winson Chungef7f8742015-06-04 17:18:17 -07001381 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001382 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001383 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001384 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1385 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1386 } else {
1387 mAppsView.setSearchBarController(new DefaultAppSearchController());
1388 }
Craig Mautner360310b2012-10-26 15:13:08 -07001389
Winson Chung3d503fb2011-07-13 17:25:49 -07001390 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001391 mDragController.setDragScoller(mWorkspace);
1392 mDragController.setScrollView(mDragLayer);
1393 mDragController.setMoveTarget(mWorkspace);
1394 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001395 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001396
Peter Schiller310a9882016-06-30 13:52:36 -07001397 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1398 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1399 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001400
Sunny Goyal322d5562015-06-25 19:35:49 -07001401 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1402 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001403 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 }
1405
Winsone9f27272015-10-13 10:47:51 -07001406 private void setupOverviewPanel() {
1407 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1408
1409 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1410 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1411 @Override
1412 public boolean onLongClick(View v) {
1413 return v.performClick();
1414 }
1415 };
1416
1417 // Bind wallpaper button actions
1418 View wallpaperButton = findViewById(R.id.wallpaper_button);
1419 wallpaperButton.setOnClickListener(new OnClickListener() {
1420 @Override
1421 public void onClick(View view) {
1422 if (!mWorkspace.isSwitchingState()) {
1423 onClickWallpaperPicker(view);
1424 }
1425 }
1426 });
1427 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1428 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1429
1430 // Bind widget button actions
1431 mWidgetsButton = findViewById(R.id.widget_button);
1432 mWidgetsButton.setOnClickListener(new OnClickListener() {
1433 @Override
1434 public void onClick(View view) {
1435 if (!mWorkspace.isSwitchingState()) {
1436 onClickAddWidgetButton(view);
1437 }
1438 }
1439 });
1440 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1441 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1442
1443 // Bind settings actions
1444 View settingsButton = findViewById(R.id.settings_button);
1445 boolean hasSettings = hasSettings();
1446 if (hasSettings) {
1447 settingsButton.setOnClickListener(new OnClickListener() {
1448 @Override
1449 public void onClick(View view) {
1450 if (!mWorkspace.isSwitchingState()) {
1451 onClickSettingsButton(view);
1452 }
1453 }
1454 });
1455 settingsButton.setOnLongClickListener(performClickOnLongClick);
1456 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1457 } else {
1458 settingsButton.setVisibility(View.GONE);
1459 }
1460
1461 mOverviewPanel.setAlpha(0f);
1462 }
1463
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001465 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001466 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001467 */
1468 public void setAllAppsButton(View allAppsButton) {
1469 mAllAppsButton = allAppsButton;
1470 }
1471
Sunny Goyalbb011da2016-06-15 15:42:29 -07001472 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001473 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001474 }
1475
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001476 public View getWidgetsButton() {
1477 return mWidgetsButton;
1478 }
1479
Anjali Koppal5ad44842014-03-10 20:34:39 -07001480 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 * Creates a view representing a shortcut.
1482 *
1483 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001485 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001486 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 }
1488
1489 /**
1490 * Creates a view representing a shortcut inflated from the specified resource.
1491 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 * @param parent The group the shortcut belongs to.
1493 * @param info The data structure describing the shortcut.
1494 *
1495 * @return A View inflated from layoutResId.
1496 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001497 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001498 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001499 parent, false);
1500 favorite.applyFromShortcutInfo(info, mIconCache);
1501 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001503 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 return favorite;
1505 }
1506
1507 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 * Add a shortcut to the workspace.
1509 *
1510 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001512 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001513 int cellY) {
1514 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001515 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001516
Sunny Goyal5c97f512015-05-19 16:03:28 -07001517 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001518 if (info == null) {
1519 return;
1520 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001521 final View view = createShortcut(info);
1522
Sunny Goyal5c97f512015-05-19 16:03:28 -07001523 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001524 // First we check if we already know the exact location where we want to add this item.
1525 if (cellX >= 0 && cellY >= 0) {
1526 cellXY[0] = cellX;
1527 cellXY[1] = cellY;
1528 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001529
1530 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001531 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001532 true, null,null)) {
1533 return;
1534 }
1535 DragObject dragObject = new DragObject();
1536 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001537 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1538 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001539 return;
1540 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001541 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001542 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001543 }
1544
1545 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001546 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001547 return;
1548 }
1549
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001550 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551
1552 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001553 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001554 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 }
1556 }
1557
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001559 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001561 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 */
Adam Cohen091440a2015-03-18 14:16:05 -07001563 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001564 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1565
1566 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001567 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001568 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001569 }
Romain Guycbb89e42009-06-08 15:52:54 -07001570
Adam Cohen59400422014-03-05 18:07:04 -08001571 if (appWidgetInfo.isCustomWidget) {
1572 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001573 }
1574
Adam Cohen59400422014-03-05 18:07:04 -08001575 LauncherAppWidgetInfo launcherInfo;
1576 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1577 launcherInfo.spanX = info.spanX;
1578 launcherInfo.spanY = info.spanY;
1579 launcherInfo.minSpanX = info.minSpanX;
1580 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001581 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001582
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001584 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585
1586 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001587 if (hostView == null) {
1588 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001589 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001590 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001591 hostView.setVisibility(View.VISIBLE);
1592 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001594 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 }
Romain Guycbb89e42009-06-08 15:52:54 -07001596
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001597 private void addAppWidgetToWorkspace(
1598 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001599 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001600 hostView.setTag(item);
1601 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001602
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001603 hostView.setFocusable(true);
1604 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001605
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001606 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001607 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1608
1609 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001610 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001611 }
1612 }
1613
Adam Cohended9f8d2010-11-03 13:25:16 -07001614 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1615 @Override
1616 public void onReceive(Context context, Intent intent) {
1617 final String action = intent.getAction();
1618 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1619 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001620 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001621 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001622
Winson Chungc100e8e2011-08-09 16:02:43 -07001623 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001624 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001625 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001626 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001627 if (!showWorkspace(false)) {
1628 // If we are already on the workspace, then manually reset all apps
1629 mAppsView.reset();
1630 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001631 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001632 mIsResumeFromActionScreenOff = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001633 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1634 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001635 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001636 }
1637 }
1638 };
1639
1640 @Override
1641 public void onAttachedToWindow() {
1642 super.onAttachedToWindow();
1643
1644 // Listen for broadcasts related to user-presence
1645 final IntentFilter filter = new IntentFilter();
1646 filter.addAction(Intent.ACTION_SCREEN_OFF);
1647 filter.addAction(Intent.ACTION_USER_PRESENT);
1648 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001649 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001650 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001651 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001652
1653 if (mLauncherCallbacks != null) {
1654 mLauncherCallbacks.onAttachedToWindow();
1655 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001656 }
1657
1658 @Override
1659 public void onDetachedFromWindow() {
1660 super.onDetachedFromWindow();
1661 mVisible = false;
1662
Adam Cohend113e0c2010-11-11 10:48:05 -08001663 if (mAttached) {
1664 unregisterReceiver(mReceiver);
1665 mAttached = false;
1666 }
Winson Chungb745afb2015-03-02 11:51:23 -08001667 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001668
1669 if (mLauncherCallbacks != null) {
1670 mLauncherCallbacks.onDetachedFromWindow();
1671 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001672 }
1673
1674 public void onWindowVisibilityChanged(int visibility) {
1675 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001676 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001677 // The following code used to be in onResume, but it turns out onResume is called when
1678 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1679 // is a more appropriate event to handle
1680 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001681 if (!mWorkspaceLoading) {
1682 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001683 // We want to let Launcher draw itself at least once before we force it to build
1684 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001685 // apps is nice and speedy.
1686 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001687 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001688 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001689 if (mStarted) return;
1690 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001691 // We delay the layer building a bit in order to give
1692 // other message processing a time to run. In particular
1693 // this avoids a delay in hiding the IME if it was
1694 // currently shown, because doing that may involve
1695 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001696 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001697 final ViewTreeObserver.OnDrawListener listener = this;
1698 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001699 public void run() {
1700 if (mWorkspace != null &&
1701 mWorkspace.getViewTreeObserver() != null) {
1702 mWorkspace.getViewTreeObserver().
1703 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001704 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001705 }
1706 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001707 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001708 }
1709 });
1710 }
1711 clearTypedText();
1712 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001713 }
1714
Adam Cohen091440a2015-03-18 14:16:05 -07001715 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001716 mHandler.removeMessages(ADVANCE_MSG);
1717 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1718 mHandler.sendMessageDelayed(msg, delay);
1719 mAutoAdvanceSentTime = System.currentTimeMillis();
1720 }
1721
Adam Cohen091440a2015-03-18 14:16:05 -07001722 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001723 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1724 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1725 mAutoAdvanceRunning = autoAdvanceRunning;
1726 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001727 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001728 sendAdvanceMessage(delay);
1729 } else {
1730 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001731 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001732 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1733 }
1734 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001735 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001736 }
1737 }
1738 }
1739
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001740 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1741
Adam Cohended9f8d2010-11-03 13:25:16 -07001742 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001743 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001744 if (msg.what == ADVANCE_MSG) {
1745 int i = 0;
1746 for (View key: mWidgetsToAdvance.keySet()) {
1747 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001748 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001749 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001750 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001751 public void run() {
1752 ((Advanceable) v).advance();
1753 }
1754 }, delay);
1755 }
1756 i++;
1757 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001758 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001759 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001760 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001761 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001762 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001763
Winsonc0b52fe2015-09-09 16:38:15 -07001764 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001765 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001766 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1767 if (v instanceof Advanceable) {
1768 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001769 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001770 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001771 }
1772 }
1773
Winsonc0b52fe2015-09-09 16:38:15 -07001774 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001775 if (mWidgetsToAdvance.containsKey(hostView)) {
1776 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001777 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001778 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001779 }
1780
Hyunyoung Song3f471442015-04-08 19:01:34 -07001781 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001782 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1783 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001784 }
1785
Winson Chunga6945242014-01-08 14:04:34 -08001786 public DragLayer getDragLayer() {
1787 return mDragLayer;
1788 }
1789
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001790 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001791 return mAppsView;
1792 }
1793
Hyunyoung Song3f471442015-04-08 19:01:34 -07001794 public WidgetsContainerView getWidgetsView() {
1795 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001796 }
1797
Winson Chunga6945242014-01-08 14:04:34 -08001798 public Workspace getWorkspace() {
1799 return mWorkspace;
1800 }
1801
Sunny Goyal6178f132016-07-11 17:30:03 -07001802 public View getQsbContainer() {
1803 return mQsbContainer;
1804 }
1805
Winson Chunga6945242014-01-08 14:04:34 -08001806 public Hotseat getHotseat() {
1807 return mHotseat;
1808 }
1809
Jorim Jaggid017f882014-01-14 17:08:48 -08001810 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001811 return mOverviewPanel;
1812 }
1813
Sunny Goyal47328fd2016-05-25 18:56:41 -07001814 public DropTargetBar getDropTargetBar() {
1815 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001816 }
1817
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001818 public LauncherAppWidgetHost getAppWidgetHost() {
1819 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001820 }
Romain Guycbb89e42009-06-08 15:52:54 -07001821
Winson Chunga9abd0e2010-10-27 17:18:37 -07001822 public LauncherModel getModel() {
1823 return mModel;
1824 }
1825
Adam Cohen79d90c52016-04-22 13:29:20 -07001826 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001827 return mSharedPrefs;
1828 }
1829
Adam Cohen2e6da152015-05-06 11:42:25 -07001830 public DeviceProfile getDeviceProfile() {
1831 return mDeviceProfile;
1832 }
1833
Bjorn Bringertc459e522013-06-07 19:36:01 +01001834 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001835 getWindow().closeAllPanels();
1836
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001837 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001838 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001839 }
1840
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001841 @Override
1842 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001843 long startTime = 0;
1844 if (DEBUG_RESUME_TIME) {
1845 startTime = System.currentTimeMillis();
1846 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 super.onNewIntent(intent);
1848
1849 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001850 Folder openFolder = mWorkspace.getOpenFolder();
1851 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1852 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1853 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1854 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1855 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001856 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001857 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001858
Adam Cohen6fecd412013-10-02 17:41:50 -07001859 if (mWorkspace == null) {
1860 // Can be cases where mWorkspace is null, this prevents a NPE
1861 return;
1862 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001863 // In all these cases, only animate if we're already on home
1864 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001865
Sunny Goyal935fca12015-10-13 10:19:01 -07001866 closeFolder(alreadyOnHome);
Tony Wickham49c8d292016-07-01 11:44:34 -07001867 closeShortcutsContainer();
Adam Cohen6fecd412013-10-02 17:41:50 -07001868 exitSpringLoadedDragMode();
1869
1870 // If we are already on home, then just animate back to the workspace,
1871 // otherwise, just wait until onResume to set the state back to Workspace
1872 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001873 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001874 } else {
1875 mOnResumeState = State.WORKSPACE;
1876 }
1877
1878 final View v = getWindow().peekDecorView();
1879 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001880 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001881 INPUT_METHOD_SERVICE);
1882 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1883 }
1884
Winson Chungb745afb2015-03-02 11:51:23 -08001885 // Reset the apps view
1886 if (!alreadyOnHome && mAppsView != null) {
1887 mAppsView.scrollToTop();
1888 }
1889
Hyunyoung Song3f471442015-04-08 19:01:34 -07001890 // Reset the widgets view
1891 if (!alreadyOnHome && mWidgetsView != null) {
1892 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001893 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001894
Adam Cohen9211d422014-10-07 18:14:53 -07001895 if (mLauncherCallbacks != null) {
1896 mLauncherCallbacks.onHomeIntent();
1897 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 }
Adam Cohened307df2013-10-02 09:37:31 -07001899
Adam Cohen9211d422014-10-07 18:14:53 -07001900 if (mLauncherCallbacks != null) {
1901 mLauncherCallbacks.onNewIntent(intent);
1902 }
Winson15f8b172015-08-19 11:02:39 -07001903
1904 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1905 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1906 // animation.
1907 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001908 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1909 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001910 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1911 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001912
1913 // We use this flag to suppress noisy callbacks above custom content state
1914 // from onResume.
1915 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001916 mWorkspace.post(new Runnable() {
1917 @Override
1918 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001919 if (mWorkspace != null) {
1920 mWorkspace.moveToDefaultScreen(true);
1921 }
Winson15f8b172015-08-19 11:02:39 -07001922 }
1923 });
1924 }
1925 }
1926
1927 if (DEBUG_RESUME_TIME) {
1928 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1929 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001930 }
1931
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001933 public void onRestoreInstanceState(Bundle state) {
1934 super.onRestoreInstanceState(state);
1935 for (int page: mSynchronouslyBoundPages) {
1936 mWorkspace.restoreInstanceStateForChild(page);
1937 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 }
1939
1940 @Override
1941 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001942 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001943 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1944 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001945
Adam Cohen21cd0022013-10-09 18:57:02 -07001946 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001947 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948
Michael Jurka883f55b2010-10-21 15:47:14 -07001949 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001950 // We close any open folder since it will not be re-opened, and we need to make sure
1951 // this state is reflected.
1952 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1953 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954
Tony Wickham49c8d292016-07-01 11:44:34 -07001955 closeShortcutsContainer();
1956
Adam Cohendcd297f2013-06-18 13:13:40 -07001957 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001958 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001959 ContentValues itemValues = new ContentValues();
1960 mPendingAddInfo.writeToValues(itemValues);
1961 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001962 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001963 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 }
1965
Adam Cohen9211d422014-10-07 18:14:53 -07001966 if (mLauncherCallbacks != null) {
1967 mLauncherCallbacks.onSaveInstanceState(outState);
1968 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 }
1970
1971 @Override
1972 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001974
Winson Chunge7a03942011-08-05 15:05:12 -07001975 // Remove all pending runnables
1976 mHandler.removeMessages(ADVANCE_MSG);
1977 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001978 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001979 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001980
Winson Chungcd2b0142011-06-08 16:02:26 -07001981 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001982 // It's possible to receive onDestroy after a new Launcher activity has
1983 // been created. In this case, don't interfere with the new Launcher.
1984 if (mModel.isCurrentCallbacks(this)) {
1985 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001986 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001987 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001988
Sunny Goyal745bad92016-05-02 10:54:12 -07001989 if (mRotationPrefChangeHandler != null) {
1990 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1991 }
1992
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001994 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001996 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001998 mAppWidgetHost = null;
1999
2000 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001
2002 TextKeyListener.getInstance().release();
2003
Tony Wickhame2217252016-03-22 16:34:23 -07002004 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
2005 .removeAccessibilityStateChangeListener(this);
2006
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002007 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002008
Michael Jurka2ecf9952012-06-18 12:52:28 -07002009 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002010
2011 if (mLauncherCallbacks != null) {
2012 mLauncherCallbacks.onDestroy();
2013 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 }
2015
Sunny Goyalae502842016-06-17 08:43:56 -07002016 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
2017 return mAccessibilityDelegate;
2018 }
2019
Adam Cohena9cf38f2011-05-02 15:36:58 -07002020 public DragController getDragController() {
2021 return mDragController;
2022 }
2023
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002024 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002025 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07002026 onStartForResult(requestCode);
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002027 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07002028 }
2029
2030 @Override
2031 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2032 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2033 onStartForResult(requestCode);
2034 try {
2035 super.startIntentSenderForResult(intent, requestCode,
2036 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2037 } catch (IntentSender.SendIntentException e) {
2038 throw new ActivityNotFoundException();
2039 }
2040 }
2041
2042 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002043 if (requestCode >= 0) {
2044 setWaitingForResult(true);
2045 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046 }
2047
Winson Chung70d72102011-08-12 11:24:35 -07002048 /**
2049 * Indicates that we want global search for this activity by setting the globalSearch
2050 * argument for {@link #startSearch} to true.
2051 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002052 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002053 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002055
Karl Rosaen138a0412009-04-23 19:00:21 -07002056 if (initialQuery == null) {
2057 // Use any text typed in the launcher as the initial query
2058 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002059 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 if (appSearchData == null) {
2061 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002062 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002064
2065 // TODO send proper bounds.
2066 Rect sourceBounds = null;
Romain Guycbb89e42009-06-08 15:52:54 -07002067
Ian Parkinson047036e2014-09-01 15:40:53 +01002068 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002069 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002070 if (clearTextImmediately) {
2071 clearTypedText();
2072 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002073
2074 // We need to show the workspace after starting the search
2075 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002076 }
2077
Ian Parkinson047036e2014-09-01 15:40:53 +01002078 /**
2079 * Start a text search.
2080 *
2081 * @return {@code true} if the search will start immediately, so any further keypresses
2082 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2083 * to buffer keypresses.
2084 */
2085 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002086 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002087 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2088 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2089 sourceBounds);
2090 }
2091
Michael Jurkaa33411c2012-06-14 16:18:21 -07002092 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002093 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002094 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002095 }
2096
2097 /**
2098 * Starts the global search activity. This code is a copied from SearchManager
2099 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002100 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002101 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002102 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002103 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2104 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2105 if (globalSearchActivity == null) {
2106 Log.w(TAG, "No global search activity found.");
2107 return;
2108 }
2109 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2110 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2111 intent.setComponent(globalSearchActivity);
2112 // Make sure that we have a Bundle to put source in
2113 if (appSearchData == null) {
2114 appSearchData = new Bundle();
2115 } else {
2116 appSearchData = new Bundle(appSearchData);
2117 }
Adam Cohen9211d422014-10-07 18:14:53 -07002118 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002119 if (!appSearchData.containsKey("source")) {
2120 appSearchData.putString("source", getPackageName());
2121 }
2122 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2123 if (!TextUtils.isEmpty(initialQuery)) {
2124 intent.putExtra(SearchManager.QUERY, initialQuery);
2125 }
2126 if (selectInitialQuery) {
2127 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2128 }
2129 intent.setSourceBounds(sourceBounds);
2130 try {
2131 startActivity(intent);
2132 } catch (ActivityNotFoundException ex) {
2133 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2134 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002135 }
2136
Yura4f93ec62014-02-04 14:15:21 +00002137 public boolean isOnCustomContent() {
2138 return mWorkspace.isOnOrMovingToCustomContent();
2139 }
2140
Winson Chung70d72102011-08-12 11:24:35 -07002141 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002142 public boolean onPrepareOptionsMenu(Menu menu) {
2143 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002144 if (mLauncherCallbacks != null) {
2145 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2146 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002147 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002148 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002149
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002150 @Override
2151 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002152 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002153 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002154 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002155 }
2156
Joe Onorato9c1289c2009-08-17 11:03:03 -04002157 public boolean isWorkspaceLocked() {
2158 return mWorkspaceLoading || mWaitingForResult;
2159 }
2160
Adam Cohen517a7f52014-03-01 12:12:59 -08002161 public boolean isWorkspaceLoading() {
2162 return mWorkspaceLoading;
2163 }
2164
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002165 private void setWorkspaceLoading(boolean value) {
2166 boolean isLocked = isWorkspaceLocked();
2167 mWorkspaceLoading = value;
2168 if (isLocked != isWorkspaceLocked()) {
2169 onWorkspaceLockedChanged();
2170 }
2171 }
2172
2173 private void setWaitingForResult(boolean value) {
2174 boolean isLocked = isWorkspaceLocked();
2175 mWaitingForResult = value;
2176 if (isLocked != isWorkspaceLocked()) {
2177 onWorkspaceLockedChanged();
2178 }
2179 }
2180
Adam Cohen9211d422014-10-07 18:14:53 -07002181 protected void onWorkspaceLockedChanged() {
2182 if (mLauncherCallbacks != null) {
2183 mLauncherCallbacks.onWorkspaceLockedChanged();
2184 }
2185 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002186
Michael Jurka0280c3b2010-09-17 15:00:07 -07002187 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002188 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002189 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002190 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2191 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002192 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002193 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002194
Tony Wickhama0628cc2015-10-14 15:23:04 -07002195 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002196 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002197 if (LOGD) {
2198 Log.d(TAG, "Adding widget from drop");
2199 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002200 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2201 }
2202
Sunny Goyale6b63a32015-01-16 16:14:29 -08002203 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002204 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2205 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002206 if (appWidgetInfo.configure != null) {
2207 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002208 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002209
Bjorn Bringert7984c942009-12-09 15:38:25 +00002210 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002211 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2212 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2213
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002215 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002216 Runnable onComplete = new Runnable() {
2217 @Override
2218 public void run() {
2219 // Exit spring loaded mode if necessary after adding the widget
2220 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2221 null);
2222 }
2223 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002224 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002225 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002226 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002227 }
2228 }
Romain Guycbb89e42009-06-08 15:52:54 -07002229
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002230 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002231 // Close any folders that may be open.
2232 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002233 mWorkspace.moveToCustomContentScreen(animate);
2234 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002235
2236 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2237 int[] cell, int spanX, int spanY) {
2238 switch (info.itemType) {
2239 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2240 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2241 int span[] = new int[2];
2242 span[0] = spanX;
2243 span[1] = spanY;
2244 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2245 container, screenId, cell, span);
2246 break;
2247 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2248 processShortcutFromDrop(info.componentName, container, screenId, cell);
2249 break;
2250 default:
2251 throw new IllegalStateException("Unknown item type: " + info.itemType);
2252 }
2253 }
2254
Adam Cohenfbba09b2011-07-18 21:43:05 -07002255 /**
2256 * Process a shortcut drop.
2257 *
2258 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002259 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002260 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002261 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002262 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2263 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002264 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002265 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002266 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002267
2268 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002269 mPendingAddInfo.cellX = cell[0];
2270 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002271 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002272
2273 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2274 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002275 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002276 }
2277
Adam Cohenfbba09b2011-07-18 21:43:05 -07002278 /**
2279 * Process a widget drop.
2280 *
2281 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002282 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002283 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002284 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002285 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2286 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002287 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002288 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002289 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002290 mPendingAddInfo.minSpanX = info.minSpanX;
2291 mPendingAddInfo.minSpanY = info.minSpanY;
2292
Adam Cohenfbba09b2011-07-18 21:43:05 -07002293 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002294 mPendingAddInfo.cellX = cell[0];
2295 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002296 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002297 if (span != null) {
2298 mPendingAddInfo.spanX = span[0];
2299 mPendingAddInfo.spanY = span[1];
2300 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002301
Adam Cohened66b2b2012-01-23 17:28:51 -08002302 AppWidgetHostView hostView = info.boundWidget;
2303 int appWidgetId;
2304 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002305 // In the case where we've prebound the widget, we remove it from the DragLayer
2306 if (LOGD) {
2307 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2308 }
2309 getDragLayer().removeView(hostView);
2310
Adam Cohened66b2b2012-01-23 17:28:51 -08002311 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002312 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002313
2314 // Clear the boundWidget so that it doesn't get destroyed.
2315 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002316 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002317 // In this case, we either need to start an activity to get permission to bind
2318 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002319 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002320 Bundle options = info.bindOptions;
2321
Sunny Goyalffe83f12014-08-14 17:39:34 -07002322 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2323 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002324 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002325 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002326 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002327 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002328 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2329 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2330 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002331 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2332 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002333 // TODO: we need to make sure that this accounts for the options bundle.
2334 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002335 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2336 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002337 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002338 }
2339
Adam Cohendcd297f2013-06-18 13:13:40 -07002340 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002341 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002342 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002343 folderInfo.title = getText(R.string.folder_name);
2344
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002345 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002346 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2347 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002348
2349 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002350 FolderIcon newFolder =
2351 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002352 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002353 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002354 // Force measure the new folder icon
2355 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2356 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002357 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002358 }
Romain Guycbb89e42009-06-08 15:52:54 -07002359
Winsonc0b52fe2015-09-09 16:38:15 -07002360 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002361 * Unbinds the view for the specified item, and removes the item and all its children.
2362 *
2363 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002364 * @param itemInfo the {@link ItemInfo} for this view.
2365 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002366 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002367 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002368 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002369 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002370 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2371 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002372 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002373 } else {
2374 mWorkspace.removeWorkspaceItem(v);
2375 }
Winsonc0b52fe2015-09-09 16:38:15 -07002376 if (deleteFromDb) {
2377 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2378 }
2379 } else if (itemInfo instanceof FolderInfo) {
2380 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002381 if (v instanceof FolderIcon) {
2382 ((FolderIcon) v).removeListeners();
2383 }
Winsonc0b52fe2015-09-09 16:38:15 -07002384 mWorkspace.removeWorkspaceItem(v);
2385 if (deleteFromDb) {
2386 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2387 }
2388 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2389 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002390 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002391 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002392 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002393 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002394 }
2395 } else {
2396 return false;
2397 }
2398 return true;
2399 }
2400
2401 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002402 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002403 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002404 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002405 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002406 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002407 // Deleting an app widget ID is a void call but writes to disk before returning
2408 // to the caller...
2409 new AsyncTask<Void, Void, Void>() {
2410 public Void doInBackground(Void ... args) {
2411 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2412 return null;
2413 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002414 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002415 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002416 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002417 }
2418
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002419 @Override
2420 public boolean dispatchKeyEvent(KeyEvent event) {
2421 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2422 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002423 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002424 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002425 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002426 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002427 dumpState();
2428 return true;
2429 }
2430 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002431 }
2432 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2433 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002434 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002435 return true;
2436 }
2437 }
2438
2439 return super.dispatchKeyEvent(event);
2440 }
2441
Joe Onorato88ec0992009-11-19 13:16:06 -08002442 @Override
2443 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002444 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2445 return;
2446 }
2447
Sunny Goyal45478022015-06-08 16:52:41 -07002448 if (mDragController.isDragging()) {
2449 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002450 return;
2451 }
2452
Tony Wickham49c8d292016-07-01 11:44:34 -07002453 if (getOpenShortcutsContainer() != null) {
2454 closeShortcutsContainer();
2455 } else if (isAppsViewVisible()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002456 showWorkspace(true);
2457 } else if (isWidgetsViewVisible()) {
2458 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002459 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002460 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002461 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002462 Folder openFolder = mWorkspace.getOpenFolder();
2463 if (openFolder.isEditingName()) {
2464 openFolder.dismissEditingName();
2465 } else {
2466 closeFolder();
2467 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002468 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002469 mWorkspace.exitWidgetResizeMode();
2470
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002471 // Back button is a no-op here, but give at least some feedback for the button press
2472 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002473 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002474 }
2475
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002476 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002477 * Launches the intent referred by the clicked shortcut.
2478 *
2479 * @param v The view representing the clicked shortcut.
2480 */
2481 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002482 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2483 // view has detached (it's possible for this to happen if the view is removed mid touch).
2484 if (v.getWindowToken() == null) {
2485 return;
2486 }
2487
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002488 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002489 return;
2490 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002491
Adam Cohen1697b792013-09-17 19:08:21 -07002492 if (v instanceof Workspace) {
2493 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002494 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002495 }
2496 return;
2497 }
2498
2499 if (v instanceof CellLayout) {
2500 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002501 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2502 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002503 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002504 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002505 }
2506
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002507 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002508 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002509 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002510 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002511 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002512 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002513 }
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002514 } else if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2515 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002516 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002517 } else if (tag instanceof AppInfo) {
2518 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002519 } else if (tag instanceof LauncherAppWidgetInfo) {
2520 if (v instanceof PendingAppWidgetHostView) {
2521 onClickPendingWidget((PendingAppWidgetHostView) v);
2522 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002523 }
2524 }
2525
Sunny Goyal70660032015-05-14 00:07:08 -07002526 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002527 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002528 return false;
2529 }
2530
Michael Jurkaaf442092010-06-10 17:01:57 -07002531 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002532 * Event handler for the app widget view which has not fully restored.
2533 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002534 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002535 if (mIsSafeModeEnabled) {
2536 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2537 return;
2538 }
2539
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002540 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002541 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002542 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2543 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2544 // This should not happen, as we make sure that an Id is allocated during bind.
2545 return;
2546 }
2547 LauncherAppWidgetProviderInfo appWidgetInfo =
2548 mAppWidgetManager.findProvider(info.providerName, info.user);
2549 if (appWidgetInfo != null) {
2550 mPendingAddWidgetId = info.appWidgetId;
2551 mPendingAddInfo.copyFrom(info);
2552 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002553
Sunny Goyald478c832016-04-01 12:04:16 -07002554 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2555 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingAddWidgetId);
2556 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
2557 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2558 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2559 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2560 }
2561 } else {
2562 LauncherAppWidgetProviderInfo appWidgetInfo =
2563 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2564 if (appWidgetInfo != null) {
2565 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2566 }
Sunny Goyalff572272014-07-23 13:58:07 -07002567 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002568 } else if (info.installProgress < 0) {
2569 // The install has not been queued
2570 final String packageName = info.providerName.getPackageName();
2571 showBrokenAppInstallDialog(packageName,
2572 new DialogInterface.OnClickListener() {
2573 public void onClick(DialogInterface dialog, int id) {
2574 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2575 }
2576 });
2577 } else {
2578 // Download has started.
2579 final String packageName = info.providerName.getPackageName();
2580 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002581 }
2582 }
2583
Sunny Goyald478c832016-04-01 12:04:16 -07002584 private void startRestoredWidgetReconfigActivity(
2585 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
2586 mPendingAddWidgetInfo = provider;
2587 mPendingAddInfo.copyFrom(info);
2588 mPendingAddWidgetId = info.appWidgetId;
2589 mAppWidgetManager.startConfigActivity(provider,
2590 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2591 }
2592
Sunny Goyalff572272014-07-23 13:58:07 -07002593 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002594 * Event handler for the "grid" button that appears on the home screen, which
2595 * enters all apps mode.
2596 *
2597 * @param v The view that was clicked.
2598 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002599 protected void onClickAllAppsButton(View v) {
2600 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002601 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002602 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.TAP,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002603 LauncherLogProto.ALL_APPS_BUTTON);
Winson Chung4ac30062015-05-08 17:34:17 -07002604 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002605 true /* updatePredictedApps */, false /* focusSearchBar */);
2606 }
2607 }
2608
2609 protected void onLongClickAllAppsButton(View v) {
2610 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2611 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002612 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.LONGPRESS,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002613 LauncherLogProto.ALL_APPS_BUTTON);
Winson Chung76648c52015-07-10 14:33:23 -07002614 showAppsView(true /* animated */, false /* resetListToTop */,
2615 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002616 }
2617 }
2618
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002619 private void showBrokenAppInstallDialog(final String packageName,
2620 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002621 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002622 .setTitle(R.string.abandoned_promises_title)
2623 .setMessage(R.string.abandoned_promise_explanation)
2624 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2625 .setNeutralButton(R.string.abandoned_clean_this,
2626 new DialogInterface.OnClickListener() {
2627 public void onClick(DialogInterface dialog, int id) {
2628 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2629 mWorkspace.removeAbandonedPromise(packageName, user);
2630 }
2631 })
2632 .create().show();
2633 return;
2634 }
2635
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002636 /**
2637 * Event handler for an app shortcut click.
2638 *
2639 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2640 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002641 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002642 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2643 Object tag = v.getTag();
2644 if (!(tag instanceof ShortcutInfo)) {
2645 throw new IllegalArgumentException("Input must be a Shortcut");
2646 }
2647
2648 // Open shortcut
2649 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002650
2651 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002652 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2653 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002654 // Launch activity anyway, framework will tell the user why the app is suspended.
2655 } else {
2656 int error = R.string.activity_not_available;
2657 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2658 error = R.string.safemode_shortcut_error;
2659 }
2660 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2661 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002662 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002663 }
2664
Chris Wren40c5ed32014-06-24 18:24:23 -04002665 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002666 if ((v instanceof BubbleTextView)
2667 && shortcut.isPromise()
2668 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002669 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002670 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002671 new DialogInterface.OnClickListener() {
2672 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002673 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002674 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002675 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002676 return;
2677 }
2678
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002679 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002680 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002681 }
2682
Sunny Goyala7ce1662016-05-31 15:01:35 -07002683 private void startAppShortcutOrInfoActivity(View v) {
2684 ItemInfo item = (ItemInfo) v.getTag();
2685 Intent intent = item.getIntent();
2686 if (intent == null) {
2687 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002688 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002689 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002690 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002691
2692 if (success && v instanceof BubbleTextView) {
2693 mWaitingForResume = (BubbleTextView) v;
2694 mWaitingForResume.setStayPressed(true);
2695 }
2696 }
2697
2698 /**
2699 * Event handler for a folder icon click.
2700 *
2701 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2702 */
2703 protected void onClickFolderIcon(View v) {
2704 if (LOGD) Log.d(TAG, "onClickFolder");
2705 if (!(v instanceof FolderIcon)){
2706 throw new IllegalArgumentException("Input must be a FolderIcon");
2707 }
2708
2709 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002710 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002711 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002712 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002713 }
Michael Jurka5130e402011-10-13 04:55:35 -07002714 }
2715
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002716 /**
2717 * Event handler for the (Add) Widgets button that appears after a long press
2718 * on the home screen.
2719 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002720 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002721 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002722 if (mIsSafeModeEnabled) {
2723 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2724 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002725 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002726 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002727 }
2728
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002729 /**
2730 * Event handler for the wallpaper picker button that appears after a long press
2731 * on the home screen.
2732 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002733 public void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002734 if (!Utilities.isWallapaperAllowed(this)) {
2735 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2736 return;
2737 }
2738
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002739 String pickerPackage = getString(R.string.wallpaper_picker_package);
2740 if (TextUtils.isEmpty(pickerPackage)) {
2741 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2742 }
2743
Tony Wickham785f7a52015-08-31 17:28:32 -07002744 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2745 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002746
2747 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002748 .setPackage(pickerPackage)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002749 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
2750 intent.setSourceBounds(getViewBounds(v));
2751 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002752 }
2753
2754 /**
2755 * Event handler for a click on the settings button that appears after a long press
2756 * on the home screen.
2757 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002758 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002759 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002760 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2761 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002762 }
2763
Adam Cohen61f560d2013-09-30 15:58:20 -07002764 public View.OnTouchListener getHapticFeedbackTouchListener() {
2765 if (mHapticFeedbackTouchListener == null) {
2766 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002767 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002768 @Override
2769 public boolean onTouch(View v, MotionEvent event) {
2770 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2771 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2772 }
2773 return false;
2774 }
2775 };
2776 }
2777 return mHapticFeedbackTouchListener;
2778 }
2779
Tony Wickhame2217252016-03-22 16:34:23 -07002780 @Override
2781 public void onAccessibilityStateChanged(boolean enabled) {
2782 mDragLayer.onAccessibilityStateChanged(enabled);
2783 }
2784
Adam Cohen9211d422014-10-07 18:14:53 -07002785 public void onDragStarted(View view) {
2786 if (isOnCustomContent()) {
2787 // Custom content screen doesn't participate in drag and drop. If on custom
2788 // content screen, move to default.
2789 moveWorkspaceToDefaultScreen();
2790 }
Adam Cohen9211d422014-10-07 18:14:53 -07002791 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002792
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002793 /**
2794 * Called when the user stops interacting with the launcher.
2795 * This implies that the user is now on the homescreen and is not doing housekeeping.
2796 */
Adam Cohen9211d422014-10-07 18:14:53 -07002797 protected void onInteractionEnd() {
2798 if (mLauncherCallbacks != null) {
2799 mLauncherCallbacks.onInteractionEnd();
2800 }
2801 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002802
2803 /**
2804 * Called when the user starts interacting with the launcher.
2805 * The possible interactions are:
2806 * - open all apps
2807 * - reorder an app shortcut, or a widget
2808 * - open the overview mode.
2809 * This is a good time to stop doing things that only make sense
2810 * when the user is on the homescreen and not doing housekeeping.
2811 */
Adam Cohen9211d422014-10-07 18:14:53 -07002812 protected void onInteractionBegin() {
2813 if (mLauncherCallbacks != null) {
2814 mLauncherCallbacks.onInteractionBegin();
2815 }
2816 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002817
Winson Chungcd99cd32015-04-29 11:03:24 -07002818 /** Updates the interaction state. */
2819 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002820 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002821 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002822 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2823 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002824 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002825 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002826 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002827 onInteractionEnd();
2828 }
2829 }
2830
Sunny Goyala7ce1662016-05-31 15:01:35 -07002831 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002832 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002833 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2834 try {
2835 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2836 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2837 // is enabled by default on NYC.
2838 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2839 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002840
2841 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2842 String id = ((ShortcutInfo) info).getDeepShortcutId();
2843 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002844 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2845 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002846 } else {
2847 // Could be launching some bookkeeping activity
2848 startActivity(intent, optsBundle);
2849 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002850 } finally {
2851 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002852 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002853 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002854 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2855 // corresponding permission. Show the appropriate permission prompt if that
2856 // is the case.
2857 if (intent.getComponent() == null
2858 && Intent.ACTION_CALL.equals(intent.getAction())
2859 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2860 PackageManager.PERMISSION_GRANTED) {
2861 // TODO: Rename sPendingAddItem to a generic name.
2862 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2863 0, info);
2864 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2865 REQUEST_PERMISSION_CALL_PHONE);
2866 } else {
2867 // No idea why this was thrown.
2868 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002869 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002870 }
2871 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002872
Sunny Goyala7ce1662016-05-31 15:01:35 -07002873 private Bundle getActivityLaunchOptions(View v) {
2874 if (Utilities.ATLEAST_MARSHMALLOW) {
2875 int left = 0, top = 0;
2876 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2877 if (v instanceof TextView) {
2878 // Launch from center of icon, not entire view
2879 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2880 if (icon != null) {
2881 Rect bounds = icon.getBounds();
2882 left = (width - bounds.width()) / 2;
2883 top = v.getPaddingTop();
2884 width = bounds.width();
2885 height = bounds.height();
2886 }
2887 }
2888 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2889 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2890 // On L devices, we use the device default slide-up transition.
2891 // On L MR1 devices, we use a custom version of the slide-up transition which
2892 // doesn't have the delay present in the device default.
2893 return ActivityOptions.makeCustomAnimation(
2894 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2895 }
2896 return null;
2897 }
2898
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002899 private Rect getViewBounds(View v) {
2900 int[] pos = new int[2];
2901 v.getLocationOnScreen(pos);
2902 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2903 }
2904
Sunny Goyala7ce1662016-05-31 15:01:35 -07002905 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002906 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2907 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2908 return false;
2909 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002910 // Only launch using the new animation if the shortcut has not opted out (this is a
2911 // private contract between launcher and may be ignored in the future).
2912 boolean useLaunchAnimation = (v != null) &&
2913 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2914 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2915
2916 UserHandleCompat user = null;
2917 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2918 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2919 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002920 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002921
2922 // Prepare intent
2923 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2924 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002925 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002926 }
2927 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002928 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2929 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
2930 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002931 // Shortcuts need some special checks due to legacy reasons.
2932 startShortcutIntentSafely(intent, optsBundle, item);
2933 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2934 // Could be launching some bookkeeping activity
2935 startActivity(intent, optsBundle);
2936 } else {
2937 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2938 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2939 }
2940 return true;
2941 } catch (ActivityNotFoundException|SecurityException e) {
2942 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2943 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2944 }
2945 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002946 }
2947
Adam Cohen268c4752012-06-06 17:47:33 -07002948 /**
2949 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2950 * in the DragLayer in the exact absolute location of the original FolderIcon.
2951 */
2952 private void copyFolderIconToImage(FolderIcon fi) {
2953 final int width = fi.getMeasuredWidth();
2954 final int height = fi.getMeasuredHeight();
2955
2956 // Lazy load ImageView, Bitmap and Canvas
2957 if (mFolderIconImageView == null) {
2958 mFolderIconImageView = new ImageView(this);
2959 }
2960 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2961 mFolderIconBitmap.getHeight() != height) {
2962 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2963 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2964 }
2965
2966 DragLayer.LayoutParams lp;
2967 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2968 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2969 } else {
2970 lp = new DragLayer.LayoutParams(width, height);
2971 }
2972
Adam Cohen307fe232012-08-16 17:55:58 -07002973 // The layout from which the folder is being opened may be scaled, adjust the starting
2974 // view size by this scale factor.
2975 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002976 lp.customPosition = true;
2977 lp.x = mRectForFolderAnimation.left;
2978 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002979 lp.width = (int) (scale * width);
2980 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002981
2982 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2983 fi.draw(mFolderIconCanvas);
2984 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002985 if (fi.getFolder() != null) {
2986 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2987 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002988 }
Adam Cohen268c4752012-06-06 17:47:33 -07002989 // Just in case this image view is still in the drag layer from a previous animation,
2990 // we remove it and re-add it.
2991 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2992 mDragLayer.removeView(mFolderIconImageView);
2993 }
2994 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002995 if (fi.getFolder() != null) {
2996 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002997 }
Adam Cohen268c4752012-06-06 17:47:33 -07002998 }
2999
Adam Cohen37b2a492016-04-06 18:36:06 -07003000 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003001 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07003002 FolderInfo info = (FolderInfo) fi.getTag();
3003 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3004 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003005 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3006 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003007 }
3008
Adam Cohen268c4752012-06-06 17:47:33 -07003009 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3010 copyFolderIconToImage(fi);
3011 fi.setVisibility(View.INVISIBLE);
3012
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003013 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
3014 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003015 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003016 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3017 }
3018 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003019 oa.start();
3020 }
3021
Sunny Goyal935fca12015-10-13 10:19:01 -07003022 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003023 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003024 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003025
Adam Cohen268c4752012-06-06 17:47:33 -07003026 // We remove and re-draw the FolderIcon in-case it has changed
3027 mDragLayer.removeView(mFolderIconImageView);
3028 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08003029
3030 if (cl != null) {
3031 cl.clearFolderLeaveBehind();
3032 }
3033
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003034 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003035 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003036 oa.addListener(new AnimatorListenerAdapter() {
3037 @Override
3038 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003039 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003040 // Remove the ImageView copy of the FolderIcon and make the original visible.
3041 mDragLayer.removeView(mFolderIconImageView);
3042 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003043 }
3044 }
3045 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003046 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003047 if (!animate) {
3048 oa.end();
3049 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003050 }
3051
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003052 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003053 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003054 * is animated relative to the specified View. If the View is null, no animation
3055 * is played.
3056 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003057 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003058 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003059 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003060
Adam Cohenfb91f302012-06-11 15:45:18 -07003061 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003062 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3063 if (openFolder != null && openFolder != folder) {
3064 // Close any open folder before opening a folder.
3065 closeFolder();
3066 }
3067
Adam Cohena9cf38f2011-05-02 15:36:58 -07003068 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003069
Adam Cohena9cf38f2011-05-02 15:36:58 -07003070 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003071
Sunny Goyalf4066152015-04-15 09:42:19 -07003072 // While the folder is open, the position of the icon cannot change.
3073 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3074
Adam Cohen4554ee12011-08-03 16:13:21 -07003075 // Just verify that the folder hasn't already been added to the DragLayer.
3076 // There was a one-off crash where the folder had a parent already.
3077 if (folder.getParent() == null) {
3078 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003079 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003080 } else {
3081 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3082 folder.getParent() + ").");
3083 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003084 folder.animateOpen();
3085
3086 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003087
3088 // Notify the accessibility manager that this folder "window" has appeared and occluded
3089 // the workspace items
3090 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3091 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003092 }
3093
3094 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003095 closeFolder(true);
3096 }
3097
3098 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003099 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003100 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003101 if (folder.isEditingName()) {
3102 folder.dismissEditingName();
3103 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003104 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003105 }
3106 }
3107
Sunny Goyal935fca12015-10-13 10:19:01 -07003108 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003109 animate &= !Utilities.isPowerSaverOn(this);
3110
Adam Cohen4554ee12011-08-03 16:13:21 -07003111 folder.getInfo().opened = false;
3112
3113 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3114 if (parent != null) {
3115 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003116 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003117 if (fi != null) {
3118 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3119 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003120 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003121 if (animate) {
3122 folder.animateClosed();
3123 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003124 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003125 }
Winson Chung83ca4802013-04-12 15:10:52 -07003126
Sunny Goyal935fca12015-10-13 10:19:01 -07003127 // Notify the accessibility manager that this folder "window" has disappeared and no
3128 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003129 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003130 }
3131
Tony Wickham49c8d292016-07-01 11:44:34 -07003132 public void closeShortcutsContainer() {
3133 DeepShortcutsContainer deepShortcutsContainer = getOpenShortcutsContainer();
3134 if (deepShortcutsContainer != null) {
3135 mDragController.removeDragListener(deepShortcutsContainer);
3136 mDragLayer.removeView(deepShortcutsContainer);
3137 }
3138 }
3139
3140 /**
3141 * @return The open shortcuts container, or null if there is none
3142 */
3143 public DeepShortcutsContainer getOpenShortcutsContainer() {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07003144 // Iterate in reverse order. Shortcuts container is added later to the dragLayer,
3145 // and will be one of the last views.
3146 for (int i = mDragLayer.getChildCount() - 1; i >= 0; i--) {
3147 View child = mDragLayer.getChildAt(i);
3148 if (child instanceof DeepShortcutsContainer) {
3149 return (DeepShortcutsContainer) child;
3150 }
3151 }
3152 return null;
Tony Wickham49c8d292016-07-01 11:44:34 -07003153 }
3154
Tony Wickhamdadb3042016-02-24 11:07:00 -08003155 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003156 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003157 if (!isDraggingEnabled()) return false;
3158 if (isWorkspaceLocked()) return false;
3159 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003160
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07003161 if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
3162 (v == mAllAppsButton && mAllAppsButton != null)) {
Winson Chung76648c52015-07-10 14:33:23 -07003163 onLongClickAllAppsButton(v);
3164 return true;
3165 }
3166
Adam Cohen1697b792013-09-17 19:08:21 -07003167 if (v instanceof Workspace) {
3168 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003169 if (!mWorkspace.isTouchActive()) {
3170 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003171 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3172 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3173 return true;
3174 } else {
3175 return false;
3176 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003177 } else {
3178 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003179 }
Adam Cohen1697b792013-09-17 19:08:21 -07003180 }
3181
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003182 CellLayout.CellInfo longClickCellInfo = null;
3183 View itemUnderLongClick = null;
3184 if (v.getTag() instanceof ItemInfo) {
3185 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003186 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003187 itemUnderLongClick = longClickCellInfo.cell;
3188 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003189 }
3190
Winson Chung3d503fb2011-07-13 17:25:49 -07003191 // The hotseat touch handling does not go through Workspace, and we always allow long press
3192 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003193 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003194 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003195 // User long pressed on empty space
3196 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3197 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003198 if (mWorkspace.isInOverviewMode()) {
3199 mWorkspace.startReordering(v);
3200 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003201 showOverviewMode(true);
Hyunyoung Song645764e2016-06-06 14:19:02 -07003202 mHotseat.requestDisallowInterceptTouchEvent(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003203 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003204 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003205 final boolean isAllAppsButton =
3206 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3207 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3208 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003209 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003210 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003211 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003212 }
3213 }
3214 }
3215 return true;
3216 }
3217
Winson Chung3d503fb2011-07-13 17:25:49 -07003218 boolean isHotseatLayout(View layout) {
3219 return mHotseat != null && layout != null &&
3220 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3221 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003222
Winson Chung3d503fb2011-07-13 17:25:49 -07003223 /**
3224 * Returns the CellLayout of the specified container at the specified screen.
3225 */
Sunny Goyal92820592015-03-02 11:31:35 -08003226 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003227 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3228 if (mHotseat != null) {
3229 return mHotseat.getLayout();
3230 } else {
3231 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003232 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003233 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003234 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003235 }
3236 }
3237
Winson Chungb745afb2015-03-02 11:51:23 -08003238 /**
3239 * For overridden classes.
3240 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003241 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003242 return isAppsViewVisible();
3243 }
3244
3245 public boolean isAppsViewVisible() {
3246 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3247 }
3248
3249 public boolean isWidgetsViewVisible() {
3250 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003251 }
3252
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003253 private void setWorkspaceBackground(int background) {
3254 switch (background) {
3255 case WORKSPACE_BACKGROUND_TRANSPARENT:
3256 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3257 break;
3258 case WORKSPACE_BACKGROUND_BLACK:
3259 getWindow().setBackgroundDrawable(null);
3260 break;
3261 default:
3262 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3263 }
Craig Mautner360310b2012-10-26 15:13:08 -07003264 }
3265
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003266 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003267 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3268 int curflags = getWindow().getAttributes().flags
3269 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3270 if (wpflags != curflags) {
3271 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3272 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003273 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003274 }
3275
Michael Jurkae326f182011-11-21 14:05:46 -08003276 @Override
3277 public void onTrimMemory(int level) {
3278 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003279 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3280 // The widget preview db can result in holding onto over
3281 // 3MB of memory for caching which isn't necessary.
3282 SQLiteDatabase.releaseMemory();
3283
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003284 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003285 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003286 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003287 if (mLauncherCallbacks != null) {
3288 mLauncherCallbacks.onTrimMemory(level);
3289 }
Michael Jurkae326f182011-11-21 14:05:46 -08003290 }
3291
Winson5c6bdbb2015-09-03 11:36:19 -07003292 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003293 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003294 }
3295
Winson5c6bdbb2015-09-03 11:36:19 -07003296 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003297 boolean changed = mState != State.WORKSPACE ||
3298 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003299 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003300 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003301 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003302 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003303
Michael Jurkab3e22d92011-10-31 15:58:33 -07003304 // Set focus to the AppsCustomize button
3305 if (mAllAppsButton != null) {
3306 mAllAppsButton.requestFocus();
3307 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003308 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003309
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003310 // Change the state *after* we've called all the transition code
3311 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003312
Winson Chung5fb63472011-02-02 17:03:37 -08003313 // Resume the auto-advance of widgets
3314 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003315 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003316
Winson Chung0f785722015-04-08 10:27:49 -07003317 if (changed) {
3318 // Send an accessibility event to announce the context change
3319 getWindow().getDecorView()
3320 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003321 }
Winson5c6bdbb2015-09-03 11:36:19 -07003322 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003323 }
3324
Winsone9f27272015-10-13 10:47:51 -07003325 /**
3326 * Shows the overview button.
3327 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07003328 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003329 showOverviewMode(animated, false);
3330 }
3331
3332 /**
3333 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3334 * onto one of the overview panel buttons.
3335 */
3336 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3337 Runnable postAnimRunnable = null;
3338 if (requestButtonFocus) {
3339 postAnimRunnable = new Runnable() {
3340 @Override
3341 public void run() {
3342 // Hitting the menu button when in touch mode does not trigger touch mode to
3343 // be disabled, so if requested, force focus on one of the overview panel
3344 // buttons.
3345 mOverviewPanel.requestFocusFromTouch();
3346 }
3347 };
3348 }
Adam Cohened307df2013-10-02 09:37:31 -07003349 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003350 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003351 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003352 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003353 }
3354
Winson Chungb745afb2015-03-02 11:51:23 -08003355 /**
3356 * Shows the apps view.
3357 */
Hyunyoung Song645764e2016-06-06 14:19:02 -07003358 public void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003359 boolean focusSearchBar) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003360 markAppsViewShown();
Winson Chungb745afb2015-03-02 11:51:23 -08003361 if (resetListToTop) {
3362 mAppsView.scrollToTop();
3363 }
Winson Chung4ac30062015-05-08 17:34:17 -07003364 if (updatePredictedApps) {
3365 tryAndUpdatePredictedApps();
3366 }
Winson Chung76648c52015-07-10 14:33:23 -07003367 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003368 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003369
Winson Chungb745afb2015-03-02 11:51:23 -08003370 /**
3371 * Shows the widgets view.
3372 */
3373 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003374 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003375 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003376 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003377 }
Winson Chung76648c52015-07-10 14:33:23 -07003378 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003379
3380 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003381 @Override
3382 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003383 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003384 }
3385 });
Winson Chungb745afb2015-03-02 11:51:23 -08003386 }
3387
3388 /**
3389 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003390 *
3391 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003392 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003393 // TODO: calling method should use the return value so that when {@code false} is returned
3394 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003395 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003396 if (!(mState == State.WORKSPACE ||
3397 mState == State.APPS_SPRING_LOADED ||
3398 mState == State.WIDGETS_SPRING_LOADED ||
3399 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003400 return false;
3401 }
3402 if (toState != State.APPS && toState != State.WIDGETS) {
3403 return false;
3404 }
Winson Chungb745afb2015-03-02 11:51:23 -08003405
3406 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003407 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3408 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003409 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003410 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003411 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003412
Michael Jurkab3e22d92011-10-31 15:58:33 -07003413 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003414 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003415
3416 // Pause the auto-advance of widgets until we are out of AllApps
3417 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003418 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003419 closeFolder();
Tony Wickham49c8d292016-07-01 11:44:34 -07003420 closeShortcutsContainer();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003421
3422 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003423 getWindow().getDecorView()
3424 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003425 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003426 }
3427
Winson Chungcd99cd32015-04-29 11:03:24 -07003428 /**
3429 * Updates the workspace and interaction state on state change, and return the animation to this
3430 * new state.
3431 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003432 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003433 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003434 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003435 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003436 updateInteraction(fromState, toState);
3437 return anim;
3438 }
3439
Hyunyoung Song3f471442015-04-08 19:01:34 -07003440 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003441 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003442 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003443 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003444 }
Winson Chungb745afb2015-03-02 11:51:23 -08003445
Winson Chung006ee262015-08-03 14:40:11 -07003446 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003447 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003448 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003449
3450 if (isAppsViewVisible()) {
3451 mState = State.APPS_SPRING_LOADED;
3452 } else if (isWidgetsViewVisible()) {
3453 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003454 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003455 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003456 } else {
3457 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003458 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003459 }
Adam Cohen7777d962011-08-18 18:58:38 -07003460
Hyunyoung Song3f471442015-04-08 19:01:34 -07003461 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003462 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003463 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003464
Winson Chunge7a03942011-08-05 15:05:12 -07003465 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003466 @Override
3467 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003468 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003469 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3470 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003471 // Before we show workspace, hide all apps again because
3472 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3473 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003474 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003475 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003476 } else {
3477 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003478 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003479 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003480 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003481 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003482
Tony Wickhame0c33232016-02-08 11:37:04 -08003483 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003484 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3485 || mState == State.WIDGETS_SPRING_LOADED;
3486 }
3487
Michael Jurkad3ef3062010-11-23 16:23:58 -08003488 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003489 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003490 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003491 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003492 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003493 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003494 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3495 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003496 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003497 }
3498
Winson Chung4ac30062015-05-08 17:34:17 -07003499 /**
3500 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3501 * resumed.
3502 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003503 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003504 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003505 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003506 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003507 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003508 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003509 }
3510 }
3511 }
3512
Michael Jurkab3e22d92011-10-31 15:58:33 -07003513 void lockAllApps() {
3514 // TODO
3515 }
3516
3517 void unlockAllApps() {
3518 // TODO
3519 }
3520
Winsonf768d932015-09-25 16:12:35 -07003521 public boolean launcherCallbacksProvidesSearch() {
3522 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3523 }
3524
Michael Jurkad7c28052012-04-27 15:43:36 -07003525 @Override
3526 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003527 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003528 final List<CharSequence> text = event.getText();
3529 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003530 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003531 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003532 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003533 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003534 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003535 } else if (mWorkspace != null) {
3536 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003537 } else {
3538 text.add(getString(R.string.all_apps_home_button_label));
3539 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003540 return result;
3541 }
3542
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003543 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003544 * If the activity is currently paused, signal that we need to run the passed Runnable
3545 * in onResume.
3546 *
3547 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003548 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3549 * wrong when we're not running, and if the activity comes back to what the configuration was
3550 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003551 *
3552 * Implementation of the method from LauncherModel.Callbacks.
3553 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003554 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003555 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003556 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003557 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003558 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003559 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003560 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003561 }
3562 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003563 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003564 return true;
3565 } else {
3566 return false;
3567 }
3568 }
3569
Michael Jurkac402cd92013-05-20 15:49:32 +02003570 private boolean waitUntilResume(Runnable run) {
3571 return waitUntilResume(run, false);
3572 }
3573
Michael Jurka1e2f4652013-07-08 18:03:46 -07003574 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003575 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003576 }
3577
Michael Jurka7607c2f2013-04-03 14:33:19 -07003578 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003579 * If the activity is currently paused, signal that we need to re-run the loader
3580 * in onResume.
3581 *
3582 * This needs to be called from incoming places where resources might have been loaded
3583 * while we are paused. That is becaues the Configuration might be wrong
3584 * when we're not running, and if it comes back to what it was when we
3585 * were paused, we are not restarted.
3586 *
3587 * Implementation of the method from LauncherModel.Callbacks.
3588 *
3589 * @return true if we are currently paused. The caller might be able to
3590 * skip some work in that case since we will come back again.
3591 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003592 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003593 public boolean setLoadOnResume() {
3594 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003595 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003596 mOnResumeNeedsLoad = true;
3597 return true;
3598 } else {
3599 return false;
3600 }
3601 }
3602
3603 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003604 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003605 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003606 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003607 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003608 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003609 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003610 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003611 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003612 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003613 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003614
Joe Onorato9c1289c2009-08-17 11:03:03 -04003615 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003616 * Clear any pending bind callbacks. This is called when is loader is planning to
3617 * perform a full rebind from scratch.
3618 */
3619 @Override
3620 public void clearPendingBinds() {
3621 mBindOnResumeCallbacks.clear();
3622 if (mPendingExecutor != null) {
3623 mPendingExecutor.markCompleted();
3624 mPendingExecutor = null;
3625 }
3626 }
3627
3628 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003629 * Refreshes the shortcuts shown on the workspace.
3630 *
3631 * Implementation of the method from LauncherModel.Callbacks.
3632 */
3633 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003634 if (LauncherAppState.PROFILE_STARTUP) {
3635 Trace.beginSection("Starting page bind");
3636 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003637 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003638
Winson Chungd64d1762013-08-20 14:37:16 -07003639 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003640 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003641 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003642
Michael Jurka05bf644e2011-11-30 20:28:53 -08003643 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003644 if (mHotseat != null) {
3645 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003646 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003647 if (LauncherAppState.PROFILE_STARTUP) {
3648 Trace.endSection();
3649 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003650 }
3651
Adam Cohendcd297f2013-06-18 13:13:40 -07003652 @Override
3653 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003654 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003655 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3656 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003657 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3658 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3659 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003660 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3661 // If there are no screens, we need to have an empty screen
3662 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003663 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003664 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003665
3666 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003667 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003668 if (hasCustomContentToLeft()) {
3669 mWorkspace.createCustomContentContainer();
3670 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003671 }
Winson Chung64359a52013-07-08 17:17:08 -07003672 }
3673
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003674 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003675 int count = orderedScreenIds.size();
3676 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003677 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003678 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003679 // No need to bind the first screen, as its always bound.
3680 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3681 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003682 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003683 }
3684
Winson Chungd64d1762013-08-20 14:37:16 -07003685 public void bindAppsAdded(final ArrayList<Long> newScreens,
3686 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003687 final ArrayList<ItemInfo> addAnimated,
3688 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003689 Runnable r = new Runnable() {
3690 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003691 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003692 }
3693 };
3694 if (waitUntilResume(r)) {
3695 return;
3696 }
3697
Winson Chungd64d1762013-08-20 14:37:16 -07003698 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003699 if (newScreens != null) {
3700 bindAddScreens(newScreens);
3701 }
Winson Chungd64d1762013-08-20 14:37:16 -07003702
3703 // We add the items without animation on non-visible pages, and with
3704 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003705 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003706 bindItems(addNotAnimated, 0,
3707 addNotAnimated.size(), false);
3708 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003709 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003710 bindItems(addAnimated, 0,
3711 addAnimated.size(), true);
3712 }
Winson Chungc58497e2013-09-03 17:48:37 -07003713
Winson Chung87412982013-10-03 18:34:14 -07003714 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003715 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003716
Winson Chungb745afb2015-03-02 11:51:23 -08003717 if (addedApps != null && mAppsView != null) {
3718 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003719 }
Winson Chungd64d1762013-08-20 14:37:16 -07003720 }
3721
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003722 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003723 * Bind the items start-end from the list.
3724 *
3725 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003726 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003727 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003728 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3729 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003730 Runnable r = new Runnable() {
3731 public void run() {
3732 bindItems(shortcuts, start, end, forceAnimateIcons);
3733 }
3734 };
3735 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003736 return;
3737 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003738
Winson Chungf0c6ae02012-03-21 16:10:31 -07003739 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003740 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3741 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003742 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003743 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003744 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003745 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003746 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003747
3748 // Short circuit if we are loading dock items for a configuration which has no dock
3749 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3750 mHotseat == null) {
3751 continue;
3752 }
3753
Sunny Goyal639e9062015-08-19 19:17:06 -07003754 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003755 switch (item.itemType) {
3756 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3757 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003758 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003759 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003760 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003761 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003762 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003763 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003764 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003765 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003766 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003767 default:
3768 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003769 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003770
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003771 /*
3772 * Remove colliding items.
3773 */
3774 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3775 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3776 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3777 View v = cl.getChildAt(item.cellX, item.cellY);
3778 Object tag = v.getTag();
3779 String desc = "Collision while binding workspace item: " + item
3780 + ". Collides with " + tag;
3781 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3782 throw (new RuntimeException(desc));
3783 } else {
3784 Log.d(TAG, desc);
3785 LauncherModel.deleteItemFromDatabase(this, item);
3786 continue;
3787 }
3788 }
3789 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003790 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3791 item.cellY, 1, 1);
3792 if (animateIcons) {
3793 // Animate all the applications up now
3794 view.setAlpha(0f);
3795 view.setScaleX(0f);
3796 view.setScaleY(0f);
3797 bounceAnims.add(createNewAppBounceAnimation(view, i));
3798 newShortcutsScreenId = item.screenId;
3799 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003800 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003801
Winson Chung997a9232013-07-24 15:33:46 -07003802 if (animateIcons) {
3803 // Animate to the correct page
3804 if (newShortcutsScreenId > -1) {
3805 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003806 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003807 final Runnable startBounceAnimRunnable = new Runnable() {
3808 public void run() {
3809 anim.playTogether(bounceAnims);
3810 anim.start();
3811 }
3812 };
Winson Chung997a9232013-07-24 15:33:46 -07003813 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003814 // We post the animation slightly delayed to prevent slowdowns
3815 // when we are loading right after we return to launcher.
3816 mWorkspace.postDelayed(new Runnable() {
3817 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003818 if (mWorkspace != null) {
3819 mWorkspace.snapToPage(newScreenIndex);
3820 mWorkspace.postDelayed(startBounceAnimRunnable,
3821 NEW_APPS_ANIMATION_DELAY);
3822 }
Winson Chung94d67682013-09-25 16:29:40 -07003823 }
3824 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003825 } else {
3826 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003827 }
3828 }
Winson Chung64359a52013-07-08 17:17:08 -07003829 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003830 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003831 }
3832
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003833 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3834 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3835 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003836 view.updateAppWidget(null);
3837 view.setOnClickListener(this);
3838 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003839 mWorkspace.requestLayout();
3840 }
3841
Joe Onorato9c1289c2009-08-17 11:03:03 -04003842 /**
3843 * Add the views for a widget to the workspace.
3844 *
3845 * Implementation of the method from LauncherModel.Callbacks.
3846 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003847 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003848 Runnable r = new Runnable() {
3849 public void run() {
3850 bindAppWidget(item);
3851 }
3852 };
3853 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003854 return;
3855 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003856
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003857 if (mIsSafeModeEnabled) {
3858 bindSafeModeWidget(item);
3859 return;
3860 }
3861
Daniel Sandler843e8602010-06-07 14:59:01 -04003862 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3863 if (DEBUG_WIDGETS) {
3864 Log.d(TAG, "bindAppWidget: " + item);
3865 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003866
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003867 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003868
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003869 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3870 // If the provider is not ready, bind as a pending widget.
3871 appWidgetInfo = null;
3872 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3873 // The widget id is not valid. Try to find the widget based on the provider info.
3874 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3875 } else {
3876 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3877 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003878
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003879 // If the provider is ready, but the width is not yet restored, try to restore it.
3880 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3881 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003882 if (appWidgetInfo == null) {
3883 if (DEBUG_WIDGETS) {
3884 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3885 + " belongs to component " + item.providerName
3886 + ", as the povider is null");
3887 }
3888 LauncherModel.deleteItemFromDatabase(this, item);
3889 return;
3890 }
Sunny Goyalff572272014-07-23 13:58:07 -07003891
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003892 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003893 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003894 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3895 // Id has not been allocated yet. Allocate a new id.
3896 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3897 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003898
Sunny Goyald478c832016-04-01 12:04:16 -07003899 // Also try to bind the widget. If the bind fails, the user will be shown
3900 // a click to setup UI, which will ask for the bind permission.
3901 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3902 pendingInfo.spanX = item.spanX;
3903 pendingInfo.spanY = item.spanY;
3904 pendingInfo.minSpanX = item.minSpanX;
3905 pendingInfo.minSpanY = item.minSpanY;
3906 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
3907 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3908 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003909
Sunny Goyald478c832016-04-01 12:04:16 -07003910 // Bind succeeded
3911 if (success) {
3912 // If the widget has a configure activity, it is still needs to set it up,
3913 // otherwise the widget is ready to go.
3914 item.restoreStatus = (appWidgetInfo.configure == null)
3915 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3916 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003917 }
Sunny Goyald478c832016-04-01 12:04:16 -07003918
3919 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003920 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003921 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003922 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003923 // The widget was marked as UI not ready, but there is no configure activity to
3924 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003925 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3926 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003927 }
Sunny Goyalff572272014-07-23 13:58:07 -07003928 }
3929
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003930 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003931 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003932 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3933 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003934 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003935
Sunny Goyal56c73602015-09-25 12:55:01 -07003936 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003937 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003938 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003939 deleteWidgetInfo(item);
3940 return;
3941 }
3942
Sunny Goyal233ee962015-08-03 13:05:01 -07003943 item.minSpanX = appWidgetInfo.minSpanX;
3944 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003945 addAppWidgetToWorkspace(
3946 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3947 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003948 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003949 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003950 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003951 view.updateAppWidget(null);
3952 view.setOnClickListener(this);
3953 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003954 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003955 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003956
Daniel Sandler843e8602010-06-07 14:59:01 -04003957 if (DEBUG_WIDGETS) {
3958 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3959 + (SystemClock.uptimeMillis()-start) + "ms");
3960 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003961 }
3962
Sunny Goyalff572272014-07-23 13:58:07 -07003963 /**
3964 * Restores a pending widget.
3965 *
3966 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003967 */
Sunny Goyald478c832016-04-01 12:04:16 -07003968 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003969 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3970 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3971 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003972 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003973 }
3974
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003975 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003976 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003977
3978 mWorkspace.reinflateWidgetsIfNecessary();
3979 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003980 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003981 }
3982
Adam Cohen1462de32012-07-24 22:34:36 -07003983 public void onPageBoundSynchronously(int page) {
3984 mSynchronouslyBoundPages.add(page);
3985 }
3986
Sunny Goyal527c7d32015-08-28 15:19:36 -07003987 @Override
3988 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3989 if (mPendingExecutor != null) {
3990 mPendingExecutor.markCompleted();
3991 }
3992 mPendingExecutor = executor;
3993 executor.attachTo(this);
3994 }
3995
3996 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3997 if (mPendingExecutor == executor) {
3998 mPendingExecutor = null;
3999 }
4000 }
4001
Joe Onorato9c1289c2009-08-17 11:03:03 -04004002 /**
4003 * Callback saying that there aren't any more items to bind.
4004 *
4005 * Implementation of the method from LauncherModel.Callbacks.
4006 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004007 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004008 Runnable r = new Runnable() {
4009 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004010 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004011 }
4012 };
4013 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004014 return;
4015 }
Sunny Goyale26d1002016-06-20 14:52:14 -07004016 if (LauncherAppState.PROFILE_STARTUP) {
4017 Trace.beginSection("Page bind completed");
4018 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004019 if (mSavedState != null) {
4020 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004021 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004022 }
Sunny Goyal08442b82015-10-21 17:15:08 -07004023
Joe Onorato9c1289c2009-08-17 11:03:03 -04004024 mSavedState = null;
4025 }
4026
Adam Cohen1462de32012-07-24 22:34:36 -07004027 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004028
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004029 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07004030
4031 // If we received the result of any pending adds while the loader was running (e.g. the
4032 // widget configuration forced an orientation change), process them now.
4033 if (sPendingAddItem != null) {
4034 final long screenId = completeAdd(sPendingAddItem);
4035
4036 // TODO: this moves the user to the page where the pending item was added. Ideally,
4037 // the screen would be guaranteed to exist after bind, and the page would be set through
4038 // the workspace restore process.
4039 mWorkspace.post(new Runnable() {
4040 @Override
4041 public void run() {
4042 mWorkspace.snapToScreenId(screenId);
4043 }
4044 });
4045 sPendingAddItem = null;
4046 }
4047
Sunny Goyal756adbc2015-04-16 15:20:51 -07004048 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004049
4050 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004051 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004052 }
Sunny Goyale26d1002016-06-20 14:52:14 -07004053 if (LauncherAppState.PROFILE_STARTUP) {
4054 Trace.endSection();
4055 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004056 }
4057
Winson Chunga2413752012-04-03 14:22:34 -07004058 private boolean canRunNewAppsAnimation() {
4059 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07004060 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07004061 }
4062
Winson Chungc9168342013-06-26 14:54:55 -07004063 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004064 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004065 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4066 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004067 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004068 return bounceAnim;
4069 }
4070
Adam Cohen27772732013-11-11 14:00:56 +00004071 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004072 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004073 }
4074
Tony Wickham55616cd2015-09-23 14:55:17 -07004075 public int getSearchBarHeight() {
4076 if (mLauncherCallbacks != null) {
4077 return mLauncherCallbacks.getSearchBarHeight();
4078 }
4079 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4080 }
4081
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004082 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004083 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4084 * multiple calls to bind the same list.)
4085 */
4086 @Thunk ArrayList<AppInfo> mTmpAppsList;
4087 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4088 public void run() {
4089 bindAllApplications(mTmpAppsList);
4090 mTmpAppsList = null;
4091 }
4092 };
4093
4094 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004095 * Add the icons for all apps.
4096 *
4097 * Implementation of the method from LauncherModel.Callbacks.
4098 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004099 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004100 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4101 mTmpAppsList = apps;
4102 return;
4103 }
4104
Winson Chungb745afb2015-03-02 11:51:23 -08004105 if (mAppsView != null) {
4106 mAppsView.setApps(apps);
4107 }
Adam Cohen9211d422014-10-07 18:14:53 -07004108 if (mLauncherCallbacks != null) {
4109 mLauncherCallbacks.bindAllApplications(apps);
4110 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004111 }
4112
4113 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004114 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
4115 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
4116 */
4117 @Override
4118 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
4119 mDeepShortcutMap = deepShortcutMapCopy;
4120 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
4121 }
4122
Tony Wickham1bce7fd2016-04-28 17:39:03 -07004123 public List<String> getShortcutIdsForItem(ItemInfo info) {
4124 if (!DeepShortcutManager.supportsShortcuts(info)) {
4125 return Collections.EMPTY_LIST;
4126 }
4127 ComponentName component = info.getTargetComponent();
4128 List<String> ids = mDeepShortcutMap.get(new ComponentKey(component, info.user));
4129 return ids == null ? Collections.EMPTY_LIST : ids;
4130 }
4131
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004132 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004133 * A package was updated.
4134 *
4135 * Implementation of the method from LauncherModel.Callbacks.
4136 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004137 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004138 Runnable r = new Runnable() {
4139 public void run() {
4140 bindAppsUpdated(apps);
4141 }
4142 };
4143 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004144 return;
4145 }
4146
Winson Chungb745afb2015-03-02 11:51:23 -08004147 if (mAppsView != null) {
4148 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004149 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004150 }
4151
Sunny Goyal4390ace2014-10-13 11:33:11 -07004152 @Override
4153 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4154 Runnable r = new Runnable() {
4155 public void run() {
4156 bindWidgetsRestored(widgets);
4157 }
4158 };
4159 if (waitUntilResume(r)) {
4160 return;
4161 }
4162 mWorkspace.widgetsRestored(widgets);
4163 }
4164
Joe Onorato9c1289c2009-08-17 11:03:03 -04004165 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004166 * Some shortcuts were updated in the background.
4167 *
4168 * Implementation of the method from LauncherModel.Callbacks.
4169 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004170 @Override
4171 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4172 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004173 Runnable r = new Runnable() {
4174 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004175 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004176 }
4177 };
4178 if (waitUntilResume(r)) {
4179 return;
4180 }
4181
Sunny Goyal4390ace2014-10-13 11:33:11 -07004182 if (!updated.isEmpty()) {
4183 mWorkspace.updateShortcuts(updated);
4184 }
4185
4186 if (!removed.isEmpty()) {
4187 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4188 for (ShortcutInfo si : removed) {
4189 removedComponents.add(si.getTargetComponent());
4190 }
4191 mWorkspace.removeItemsByComponentName(removedComponents, user);
4192 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004193 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004194 }
4195 }
4196
4197 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004198 * Update the state of a package, typically related to install state.
4199 *
4200 * Implementation of the method from LauncherModel.Callbacks.
4201 */
Sunny Goyale755d462014-07-22 13:48:29 -07004202 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004203 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4204 Runnable r = new Runnable() {
4205 public void run() {
4206 bindRestoreItemsChange(updates);
4207 }
4208 };
4209 if (waitUntilResume(r)) {
4210 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004211 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004212
Sunny Goyal756adbc2015-04-16 15:20:51 -07004213 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004214 }
4215
4216 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004217 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004218 * in addition to specific applications to remove, the reason being that
4219 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004220 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004221 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004222 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004223 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004224 public void bindWorkspaceComponentsRemoved(
4225 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4226 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004227 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004228 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004229 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004230 }
Winson Chungd64d1762013-08-20 14:37:16 -07004231 };
4232 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004233 return;
4234 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004235 if (!packageNames.isEmpty()) {
4236 mWorkspace.removeItemsByPackageName(packageNames, user);
4237 }
4238 if (!components.isEmpty()) {
4239 mWorkspace.removeItemsByComponentName(components, user);
4240 }
4241 // Notify the drag controller
4242 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004243
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004244 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004245
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004246 @Override
4247 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4248 Runnable r = new Runnable() {
4249 public void run() {
4250 bindAppInfosRemoved(appInfos);
4251 }
4252 };
4253 if (waitUntilResume(r)) {
4254 return;
Joe Onorato36115782010-06-17 13:28:48 -04004255 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004256
Winson Chungdf95eb12013-10-16 14:57:07 -07004257 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004258 if (mAppsView != null) {
4259 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004260 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004261 }
Joe Onoratobe386092009-11-17 17:32:16 -08004262
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004263 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004264 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004265 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004266 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004267 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004268
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004269 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004270 public void bindWidgetsModel(WidgetsModel model) {
4271 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004272 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004273 return;
4274 }
4275
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004276 if (mWidgetsView != null && model != null) {
4277 mWidgetsView.addWidgets(model);
4278 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004279 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004280 }
4281
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004282 @Override
4283 public void notifyWidgetProvidersChanged() {
4284 if (mWorkspace.getState().shouldUpdateWidget) {
4285 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4286 }
4287 }
4288
Winson Chung400438b2011-01-16 17:53:48 -08004289 private int mapConfigurationOriActivityInfoOri(int configOri) {
4290 final Display d = getWindowManager().getDefaultDisplay();
4291 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4292 switch (d.getRotation()) {
4293 case Surface.ROTATION_0:
4294 case Surface.ROTATION_180:
4295 // We are currently in the same basic orientation as the natural orientation
4296 naturalOri = configOri;
4297 break;
4298 case Surface.ROTATION_90:
4299 case Surface.ROTATION_270:
4300 // We are currently in the other basic orientation to the natural orientation
4301 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4302 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4303 break;
4304 }
4305
4306 int[] oriMap = {
4307 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4308 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4309 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4310 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4311 };
4312 // Since the map starts at portrait, we need to offset if this device's natural orientation
4313 // is landscape.
4314 int indexOffset = 0;
4315 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4316 indexOffset = 1;
4317 }
4318 return oriMap[(d.getRotation() + indexOffset) % 4];
4319 }
Adam Cohen446e9402011-09-15 18:21:21 -07004320
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004321 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004322 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004323 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004324 if (Utilities.ATLEAST_JB_MR2) {
4325 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4326 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004327 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4328 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004329 }
Winson Chung4b919f82012-05-01 10:44:08 -07004330 }
4331 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004332
Winson Chung4b919f82012-05-01 10:44:08 -07004333 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004334 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004335 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004336 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004337 } else {
4338 mHandler.postDelayed(new Runnable() {
4339 public void run() {
4340 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4341 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004342 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004343 }
Winson Chung4b919f82012-05-01 10:44:08 -07004344 }
Winson Chung400438b2011-01-16 17:53:48 -08004345 }
4346
Adam Cohenea90f832014-05-21 15:03:34 -07004347 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004348 * To be overridden by subclasses to indicate that there is an activity to launch
4349 * before showing the standard launcher experience.
4350 */
4351 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004352 if (mLauncherCallbacks != null) {
4353 return mLauncherCallbacks.hasFirstRunActivity();
4354 }
Adam Cohen432609a2014-03-13 17:03:22 -07004355 return false;
4356 }
4357
4358 /**
4359 * To be overridden by subclasses to launch any first run activity
4360 */
4361 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004362 if (mLauncherCallbacks != null) {
4363 return mLauncherCallbacks.getFirstRunActivity();
4364 }
Adam Cohen432609a2014-03-13 17:03:22 -07004365 return null;
4366 }
4367
Winson Chunga6945242014-01-08 14:04:34 -08004368 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004369 return !ActivityManager.isRunningInTestHarness() &&
4370 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004371 }
4372
Adam Cohen432609a2014-03-13 17:03:22 -07004373 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004374 if (shouldRunFirstRunActivity() &&
4375 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004376 Intent firstRunIntent = getFirstRunActivity();
4377 if (firstRunIntent != null) {
4378 startActivity(firstRunIntent);
4379 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004380 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004381 }
4382 }
Adam Cohen432609a2014-03-13 17:03:22 -07004383 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004384 }
4385
4386 private void markFirstRunActivityShown() {
4387 SharedPreferences.Editor editor = mSharedPrefs.edit();
4388 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4389 editor.apply();
4390 }
4391
Hyunyoung Songc001cf52016-07-21 17:32:43 -07004392 private void markAppsViewShown() {
4393 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4394 return;
4395 }
4396 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
4397 }
4398
4399 private boolean shouldShowDiscoveryBounce() {
4400 if (mState != mState.WORKSPACE) {
4401 return false;
4402 }
4403 if (mLauncherCallbacks != null && mLauncherCallbacks.shouldShowDiscoveryBounce()) {
4404 return true;
4405 }
4406 if (!mIsResumeFromActionScreenOff) {
4407 return false;
4408 }
4409 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4410 return false;
4411 }
4412 return true;
4413 }
4414
Adam Cohen432609a2014-03-13 17:03:22 -07004415 /**
4416 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4417 * screen that must be displayed and dismissed.
4418 */
4419 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004420 if (mLauncherCallbacks != null) {
4421 return mLauncherCallbacks.hasDismissableIntroScreen();
4422 }
Adam Cohen432609a2014-03-13 17:03:22 -07004423 return false;
4424 }
4425
4426 /**
4427 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4428 */
4429 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004430 if (mLauncherCallbacks != null) {
4431 return mLauncherCallbacks.getIntroScreen();
4432 }
Adam Cohen432609a2014-03-13 17:03:22 -07004433 return null;
4434 }
4435
4436 /**
4437 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4438 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4439 */
4440 private boolean shouldShowIntroScreen() {
4441 return hasDismissableIntroScreen() &&
4442 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4443 }
4444
4445 protected void showIntroScreen() {
4446 View introScreen = getIntroScreen();
4447 changeWallpaperVisiblity(false);
4448 if (introScreen != null) {
4449 mDragLayer.showOverlayView(introScreen);
4450 }
4451 }
4452
Winson Chung5ffd51d2015-06-25 11:36:50 -07004453 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004454 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004455 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004456 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004457 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004458 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004459 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4460 if (userHandle != null) {
4461 user = UserHandleCompat.fromUser(userHandle);
4462 }
4463 }
4464 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004465 }
4466
4467 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004468 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004469 if (user == null) {
4470 user = UserHandleCompat.myUserHandle();
4471 }
4472
4473 // Called from search suggestion, add the profile extra to the intent to ensure that we
4474 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004475 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004476 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004477 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004478 return null;
4479 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004480 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004481 }
4482
Winson Chung5ffd51d2015-06-25 11:36:50 -07004483 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004484 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4485 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004486 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004487 UserHandleCompat.myUserHandle());
4488 }
4489
Winson Chung5ffd51d2015-06-25 11:36:50 -07004490 protected void moveWorkspaceToDefaultScreen() {
4491 mWorkspace.moveToDefaultScreen(false);
4492 }
4493
Winson Chung80baf5a2010-08-09 16:03:15 -07004494 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004495 * Returns a FastBitmapDrawable with the icon, accurately sized.
4496 */
4497 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4498 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4499 d.setFilterBitmap(true);
4500 resizeIconDrawable(d);
4501 return d;
4502 }
4503
4504 /**
4505 * Resizes an icon drawable to the correct icon size.
4506 */
Winson5fbe0742015-09-30 15:33:00 -07004507 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004508 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004509 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004510 }
4511
4512 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004513 * Prints out out state for debugging.
4514 */
4515 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004516 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004517 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004518 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4519 Log.d(TAG, "mRestoring=" + mRestoring);
4520 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004521 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004522 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004523
Daniel Sandler325dc232013-06-05 22:57:57 -04004524 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004525 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004526
4527 @Override
4528 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4529 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004530 // Dump workspace
4531 writer.println(prefix + "Workspace Items");
4532 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4533 writer.println(prefix + " Homescreen " + i);
4534
4535 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4536 for (int j = 0; j < layout.getChildCount(); j++) {
4537 Object tag = layout.getChildAt(j).getTag();
4538 if (tag != null) {
4539 writer.println(prefix + " " + tag.toString());
4540 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004541 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004542 }
Sunny Goyala1365452015-10-01 15:46:24 -07004543
4544 writer.println(prefix + " Hotseat");
4545 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4546 for (int j = 0; j < layout.getChildCount(); j++) {
4547 Object tag = layout.getChildAt(j).getTag();
4548 if (tag != null) {
4549 writer.println(prefix + " " + tag.toString());
4550 }
4551 }
4552
Sunny Goyal713edfc2016-05-06 09:58:34 -07004553 try {
4554 FileLog.flushAll(writer);
4555 } catch (Exception e) {
4556 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004557 }
4558
Adam Cohen9211d422014-10-07 18:14:53 -07004559 if (mLauncherCallbacks != null) {
4560 mLauncherCallbacks.dump(prefix, fd, writer, args);
4561 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004562 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004563
Adam Cohen59400422014-03-05 18:07:04 -08004564 public static CustomAppWidget getCustomAppWidget(String name) {
4565 return sCustomAppWidgets.get(name);
4566 }
4567
4568 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4569 return sCustomAppWidgets;
4570 }
4571
Sunny Goyal29538332016-02-18 09:10:19 -08004572 public static List<View> getFolderContents(View icon) {
4573 if (icon instanceof FolderIcon) {
4574 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4575 } else {
4576 return Collections.EMPTY_LIST;
4577 }
4578 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004579
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004580 public static Launcher getLauncher(Context context) {
4581 if (context instanceof Launcher) {
4582 return (Launcher) context;
4583 }
4584 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4585 }
4586
Sunny Goyal745bad92016-05-02 10:54:12 -07004587 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4588
4589 @Override
4590 public void onSharedPreferenceChanged(
4591 SharedPreferences sharedPreferences, String key) {
4592 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4593 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4594 if (!waitUntilResume(this, true)) {
4595 run();
4596 }
4597 }
4598 }
4599
4600 @Override
4601 public void run() {
4602 setOrientation();
4603 }
4604 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004605}