blob: 5ce98e2e82c8427b4203543f594ff0ae3ac2086b [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;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700120import com.android.launcher3.shortcuts.ShortcutKey;
Hyunyoung Song5aa27142016-07-21 11:48:37 -0700121import com.android.launcher3.userevent.nano.LauncherLogProto;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700122import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700123import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700124import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700125import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal322d5562015-06-25 19:35:49 -0700126import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700127import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700128import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700129import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700130import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700131import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700132
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700133import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700134import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700135import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700136import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800137import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700138import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700139import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700140import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700141
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142/**
143 * Default launcher application.
144 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100145public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700146 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
147 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700148 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700149 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700150 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700152 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700153 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400154 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700155
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700157 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700158 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700159 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
Michael Jurka8b805b12012-04-18 14:23:14 -0700161 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700162 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700163 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700164
Sunny Goyal28c6b962015-10-12 11:42:05 -0700165 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
166
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700167 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
168 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
169 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
170
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700171 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
172
Mathew Inwood876a8462013-06-14 14:12:41 +0100173 /**
174 * IntentStarter uses request codes starting with this. This must be greater than all activity
175 * request codes used internally.
176 */
177 protected static final int REQUEST_LAST = 100;
178
Michael Jurka0a457bf2012-11-19 14:05:05 -0800179 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700180 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800181 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182
Winson Chung2672ff92012-05-04 16:22:30 -0700183 // The Intent extra that defines whether to ignore the launch animation
184 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400185 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700186
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700187 public static final String ACTION_APPWIDGET_HOST_RESET =
188 "com.android.launcher3.intent.ACTION_APPWIDGET_HOST_RESET";
189
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 // Type: int
191 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700192 // Type: int
193 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700194 // Type: Content Values / parcelable
195 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700196 // Type: parcelable
197 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000198 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800199 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200
Adam Cohen432609a2014-03-13 17:03:22 -0700201 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800202 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700203 static final String APPS_VIEW_SHOWN = "launcher.apps_view_shown";
Adam Cohenb54a5982014-01-08 15:21:04 -0800204
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700205 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700206 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
207 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700208
Adam Cohen091440a2015-03-18 14:16:05 -0700209 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700210 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700211
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700212 private boolean mIsSafeModeEnabled;
213
Joe Onorato9c1289c2009-08-17 11:03:03 -0400214 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800215 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700216 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700217 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218
Winson Chunga2413752012-04-03 14:22:34 -0700219 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700220 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
221 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700222 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700223
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700224 private final BroadcastReceiver mUiBroadcastReceiver = new BroadcastReceiver() {
225
226 @Override
227 public void onReceive(Context context, Intent intent) {
228 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
229 closeSystemDialogs();
230 } else if (ACTION_APPWIDGET_HOST_RESET.equals(intent.getAction())) {
231 if (mAppWidgetHost != null) {
232 mAppWidgetHost.startListening();
233 }
234 }
235 }
236 };
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800237
Adam Cohen091440a2015-03-18 14:16:05 -0700238 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700239 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700240 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800241 private DragController mDragController;
Sunny Goyal6178f132016-07-11 17:30:03 -0700242 private View mQsbContainer;
Sunny Goyal322d5562015-06-25 19:35:49 -0700243
244 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700245
Sunny Goyalffe83f12014-08-14 17:39:34 -0700246 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700247 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700248
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700249 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800250 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800251 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700252
Michael Jurka0280c3b2010-09-17 15:00:07 -0700253 private int[] mTmpAddItemCellCoordinates = new int[2];
254
Sunny Goyal316490e2015-06-02 09:38:28 -0700255 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800256 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700257
Michael Jurka838a4ca2011-02-07 13:33:06 -0800258 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700259 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700260
Sunny Goyal47328fd2016-05-25 18:56:41 -0700261 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700262
263 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700264 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700265 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700266
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700267 // Main container view and the model for the widget tray screen.
268 @Thunk WidgetsContainerView mWidgetsView;
269 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700270
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700272 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
273 // scroll issues (because the workspace may not have been measured yet) and extra work.
274 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
275 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276
277 private SpannableStringBuilder mDefaultKeySsb = null;
278
Adam Cohen091440a2015-03-18 14:16:05 -0700279 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400280
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800281 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 private boolean mRestoring;
283 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700284 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285
Michael Jurka1e2f4652013-07-08 18:03:46 -0700286 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700287 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700288 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700289
Joe Onorato9c1289c2009-08-17 11:03:03 -0400290 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800291 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700292 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700293 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700294 private boolean mIsResumeFromActionScreenOff;
Adam Cohen091440a2015-03-18 14:16:05 -0700295 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800296 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700297 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800298 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400299
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700300 /** Maps launcher activity components to their list of shortcut ids. */
301 private MultiHashMap<ComponentKey, String> mDeepShortcutMap = new MultiHashMap<>();
302
Adam Cohen61f560d2013-09-30 15:58:20 -0700303 private View.OnTouchListener mHapticFeedbackTouchListener;
304
Adam Cohended9f8d2010-11-03 13:25:16 -0700305 // Related to the auto-advancing of widgets
306 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700307 private static final int ADVANCE_INTERVAL = 20000;
308 private static final int ADVANCE_STAGGER = 250;
309
310 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700311 private long mAutoAdvanceSentTime;
312 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700313 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700314
Winson Chung400438b2011-01-16 17:53:48 -0800315 // Determines how long to wait after a rotation before restoring the screen orientation to
316 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700317 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800318
Adam Cohen091440a2015-03-18 14:16:05 -0700319 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700320
Adam Cohen1462de32012-07-24 22:34:36 -0700321 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700322 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700323
Winson Chung46353de2012-02-16 14:05:10 -0800324 // We only want to get the SharedPreferences once since it does an FS stat each time we get
325 // it from the context.
326 private SharedPreferences mSharedPrefs;
327
Winson Chungf0c6ae02012-03-21 16:10:31 -0700328 // Holds the page that we need to animate to, and the icon views that we need to animate up
329 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700330 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700331 private Bitmap mFolderIconBitmap;
332 private Canvas mFolderIconCanvas;
333 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700334
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700335 private DeviceProfile mDeviceProfile;
336
Adam Cohen4b66bf32015-09-18 12:15:19 -0700337 private boolean mMoveToDefaultScreenFromNewIntent;
338
Winson Chung13eb5272015-05-11 16:30:13 -0700339 // This is set to the view that launched the activity that navigated the user away from
340 // launcher. Since there is no callback for when the activity has finished launching, enable
341 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800342 private BubbleTextView mWaitingForResume;
343
Adam Cohen59400422014-03-05 18:07:04 -0800344 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
345 new HashMap<String, CustomAppWidget>();
346
Adam Cohen59400422014-03-05 18:07:04 -0800347 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700348 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
349 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800350 }
351 }
352
Adam Cohen091440a2015-03-18 14:16:05 -0700353 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700354 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700355 if (mWorkspace != null) {
356 mWorkspace.buildPageHardwareLayers();
357 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700358 }
359 };
360
Adam Cohendb364c32014-05-20 14:23:40 -0700361 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800362
Adam Cohen091440a2015-03-18 14:16:05 -0700363 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800364 int requestCode;
365 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700366 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700367 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800368 int cellX;
369 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700370 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800371 }
372
Hyunyoung Songaa953652016-04-19 18:30:24 -0700373 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800374
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700375 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700376 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400377
378 @Thunk void setOrientation() {
379 if (mRotationEnabled) {
380 unlockScreenOrientation(true);
381 } else {
382 setRequestedOrientation(
383 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700384 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400385 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700386
Sunny Goyal745bad92016-05-02 10:54:12 -0700387 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700388
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 @Override
390 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700391 if (DEBUG_STRICT_MODE) {
392 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
393 .detectDiskReads()
394 .detectDiskWrites()
395 .detectNetwork() // or .detectAll() for all detectable problems
396 .penaltyLog()
397 .build());
398 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
399 .detectLeakedSqlLiteObjects()
400 .detectLeakedClosableObjects()
401 .penaltyLog()
402 .penaltyDeath()
403 .build());
404 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700405 if (LauncherAppState.PROFILE_STARTUP) {
406 Trace.beginSection("Launcher-onCreate");
407 }
Winson Chunga2413752012-04-03 14:22:34 -0700408
Adam Cohen9211d422014-10-07 18:14:53 -0700409 if (mLauncherCallbacks != null) {
410 mLauncherCallbacks.preOnCreate();
411 }
412
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800413 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400414
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400415 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700416
Adam Cohen2e6da152015-05-06 11:42:25 -0700417 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700418 mDeviceProfile = getResources().getConfiguration().orientation
419 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700420 app.getInvariantDeviceProfile().landscapeProfile
421 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700422
Sunny Goyalf7258242015-10-19 16:59:07 -0700423 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700424 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800425 mModel = app.setLauncher(this);
426 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700427 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700428
Joe Onorato41a12d22009-10-31 18:30:00 -0400429 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700430 mAllAppsController = new AllAppsTransitionController(this);
431 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700432
Sunny Goyalffe83f12014-08-14 17:39:34 -0700433 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700434
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700435 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
436 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700437
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700438 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
439 // this also ensures that any synchronous binding below doesn't re-trigger another
440 // LauncherModel load.
441 mPaused = false;
442
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700444
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 setupViews();
Winson1f064272016-07-18 17:18:02 -0700446 mDeviceProfile.layout(this, false /* notifyListeners */);
Tony Wickham827cef22016-03-17 15:39:39 -0700447 mExtractedColors = new ExtractedColors();
448 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449
Tony Wickhame2217252016-03-22 16:34:23 -0700450 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
451 .addAccessibilityStateChangeListener(this);
452
Joe Onorato7c312c12009-08-13 21:36:53 -0700453 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700454
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 mSavedState = savedInstanceState;
456 restoreState(mSavedState);
457
Sunny Goyale26d1002016-06-20 14:52:14 -0700458 if (LauncherAppState.PROFILE_STARTUP) {
459 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 }
461
462 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700463 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700464 // If the user leaves launcher, then we should just load items asynchronously when
465 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700466 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700467 } else {
468 // We only load the page synchronously if the user rotates (or triggers a
469 // configuration change) while launcher is in the foreground
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700470 if (!mModel.startLoader(mWorkspace.getRestorePage())) {
471 // If we are not binding synchronously, show a fade in animation when
472 // the first page bind completes.
473 mDragLayer.setAlpha(0);
474 }
Winson Chung4a2afa32012-07-19 14:53:05 -0700475 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800476 }
477
478 // For handling default keys
479 mDefaultKeySsb = new SpannableStringBuilder();
480 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800481
482 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700483 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
484 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800485
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800486 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700487 // In case we are on a device with locked rotation, we should look at preferences to check
488 // if the user has specifically allowed rotation.
489 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700490 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700491 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
492 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700493 }
494
495 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
496 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400497 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700498
Adam Cohen9211d422014-10-07 18:14:53 -0700499 if (mLauncherCallbacks != null) {
500 mLauncherCallbacks.onCreate(savedInstanceState);
501 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700502
503 if (shouldShowIntroScreen()) {
504 showIntroScreen();
505 } else {
506 showFirstRunActivity();
Adam Cohenc3e12c72014-10-31 16:15:36 -0700507 }
Adam Cohen9211d422014-10-07 18:14:53 -0700508 }
509
Sunny Goyal7779d622015-06-11 16:18:39 -0700510 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700511 public void onExtractedColorsChanged() {
512 loadExtractedColorsAndColorItems();
513 }
514
515 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700516 // TODO: do this in pre-N as well, once the extraction part is complete.
517 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700518 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700519 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700520 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham827cef22016-03-17 15:39:39 -0700521 }
522 }
523
Adam Cohen9211d422014-10-07 18:14:53 -0700524 private LauncherCallbacks mLauncherCallbacks;
525
526 public void onPostCreate(Bundle savedInstanceState) {
527 super.onPostCreate(savedInstanceState);
528 if (mLauncherCallbacks != null) {
529 mLauncherCallbacks.onPostCreate(savedInstanceState);
530 }
531 }
532
Winson1f064272016-07-18 17:18:02 -0700533 public void onInsetsChanged(Rect insets) {
534 mDeviceProfile.updateInsets(insets);
535 mDeviceProfile.layout(this, true /* notifyListeners */);
536 }
537
Sunny Goyal32554d12015-12-03 15:31:25 -0800538 /**
539 * Call this after onCreate to set or clear overlay.
540 */
541 public void setLauncherOverlay(LauncherOverlay overlay) {
542 if (overlay != null) {
543 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
544 }
545 mWorkspace.setLauncherOverlay(overlay);
546 }
547
Adam Cohen9211d422014-10-07 18:14:53 -0700548 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
549 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700550 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700551 private boolean mWorkspaceImportanceStored = false;
552 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700553 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800554 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700555 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
556
557 @Override
558 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700559 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700560 return;
561 }
562 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700563 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700564 if (mWorkspace != null) {
565 mWorkspaceImportanceForAccessibility =
566 mWorkspace.getImportantForAccessibility();
567 mWorkspace.setImportantForAccessibility(
568 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
569 mWorkspaceImportanceStored = true;
570 }
571 if (mHotseat != null) {
572 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
573 mHotseat.setImportantForAccessibility(
574 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
575 mHotseatImportanceStored = true;
576 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700577 }
578
579 @Override
580 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700581 if (mWorkspaceImportanceStored && mWorkspace != null) {
582 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
583 }
584 if (mHotseatImportanceStored && mHotseat != null) {
585 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
586 }
587 mWorkspaceImportanceStored = false;
588 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700589 }
590 });
Adam Cohen9211d422014-10-07 18:14:53 -0700591 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700592 }
593
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700594 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700595 public void onLauncherProviderChange() {
596 if (mLauncherCallbacks != null) {
597 mLauncherCallbacks.onLauncherProviderChange();
598 }
599 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700600
Adam Cohen9211d422014-10-07 18:14:53 -0700601 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700602 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700603 if (mLauncherCallbacks != null) {
604 return mLauncherCallbacks.hasCustomContentToLeft();
605 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700606 return false;
607 }
608
Dave Hawkeya8881582013-09-17 15:55:33 -0600609 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500610 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600611 * {@link #addToCustomContentPage}. This will only be invoked if
612 * {@link #hasCustomContentToLeft()} is {@code true}.
613 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500614 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700615 if (mLauncherCallbacks != null) {
616 mLauncherCallbacks.populateCustomContentContainer();
617 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600618 }
619
620 /**
621 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
622 * ensure the custom content page is added or removed if necessary.
623 */
624 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600625 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600626 // Not bound yet, wait for bindScreens to be called.
627 return;
628 }
629
630 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
631 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500632 mWorkspace.createCustomContentContainer();
633 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600634 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
635 mWorkspace.removeCustomContentPage();
636 }
637 }
638
Hyunyoung Songaa953652016-04-19 18:30:24 -0700639 public UserEventDispatcher getUserEventDispatcher() {
640 if (mLauncherCallbacks != null) {
641 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
642 if (dispatcher != null) {
643 return dispatcher;
644 }
645 }
646
Sunny Goyal64976d52016-06-20 14:56:28 -0700647 // Logger object is a singleton and does not have to be coupled with the foreground
648 // activity. Since most user event logging is done on the UI, the object is retrieved
649 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700650 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700651 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700652 }
653 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800654 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100655
Hyunyoung Song3f471442015-04-08 19:01:34 -0700656 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700657 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
658 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700659 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700660 }
661
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000662 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700663 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
664 // This cast is safe as long as the id < 0x00FFFFFF
665 // Since we jail all the dynamically generated views, there should be no clashes
666 // with any other views.
667 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000668 }
669
670 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700671 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
672 * a configuration step, this allows the proper animations to run after other transitions.
673 */
Adam Cohendb364c32014-05-20 14:23:40 -0700674 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700675 long screenId = args.screenId;
676 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
677 // When the screen id represents an actual screen (as opposed to a rank) we make sure
678 // that the drop page actually exists.
679 screenId = ensurePendingDropLayoutExists(args.screenId);
680 }
Adam Cohendb364c32014-05-20 14:23:40 -0700681
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800682 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800683 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700684 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700685 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700686 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800687 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700688 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700689 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700690 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyald478c832016-04-01 12:04:16 -0700691 completeRestoreAppWidget(args.appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700692 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700693 case REQUEST_BIND_PENDING_APPWIDGET: {
694 int widgetId = args.appWidgetId;
695 LauncherAppWidgetInfo info =
696 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
697 if (info != null) {
698 // Since the view was just bound, also launch the configure activity if needed
699 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
700 .getLauncherAppWidgetInfo(widgetId);
701 if (provider != null && provider.configure != null) {
702 startRestoredWidgetReconfigActivity(provider, info);
703 }
704 }
705 break;
706 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800707 }
Michael Jurka27614d22012-04-02 06:40:22 -0700708 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
709 // if you turned the screen off and then back while in All Apps, Launcher would not
710 // return to the workspace. Clearing mAddInfo.container here fixes this issue
711 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700712 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800713 }
714
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800715 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700716 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700717 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700718 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700719 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800720 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700721
Adam Cohenad4e15c2013-10-17 16:21:35 -0700722 Runnable exitSpringLoaded = new Runnable() {
723 @Override
724 public void run() {
725 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
726 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
727 }
728 };
729
Michael Jurka8b805b12012-04-18 14:23:14 -0700730 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700731 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700732 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700733 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
734 if (resultCode == RESULT_CANCELED) {
735 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700736 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700737 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700738 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800739 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700740 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700741 }
742 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200743 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
744 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700745 // User could have free-scrolled between pages before picking a wallpaper; make sure
746 // we move to the closest one now.
747 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700748 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200749 }
750 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700751 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200752
Adam Cohened66b2b2012-01-23 17:28:51 -0800753 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700754 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700755
Adam Cohendb364c32014-05-20 14:23:40 -0700756 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800757 // We have special handling for widgets
758 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800759 final int appWidgetId;
760 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
761 : -1;
762 if (widgetId < 0) {
763 appWidgetId = pendingAddWidgetId;
764 } else {
765 appWidgetId = widgetId;
766 }
767
Adam Cohenad4e15c2013-10-17 16:21:35 -0700768 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800769 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700770 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
771 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700772 result = RESULT_CANCELED;
773 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700774 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700775 @Override
776 public void run() {
777 exitSpringLoadedDragModeDelayed(false, 0, null);
778 }
779 };
Adam Cohendb364c32014-05-20 14:23:40 -0700780 if (workspaceLocked) {
781 // No need to remove the empty screen if we're mid-binding, as the
782 // the bind will not add the empty screen.
783 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
784 } else {
785 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
786 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
787 }
Winson Chung5aaab772012-04-27 15:24:02 -0700788 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700789 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700790 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
791 // When the screen id represents an actual screen (as opposed to a rank)
792 // we make sure that the drop page actually exists.
793 mPendingAddInfo.screenId =
794 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
795 }
Adam Cohendb364c32014-05-20 14:23:40 -0700796 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
797
798 dropLayout.setDropPending(true);
799 final Runnable onComplete = new Runnable() {
800 @Override
801 public void run() {
802 completeTwoStageWidgetDrop(resultCode, appWidgetId);
803 dropLayout.setDropPending(false);
804 }
805 };
806 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
807 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
808 } else {
809 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
810 mPendingAddInfo);
811 sPendingAddItem = args;
812 }
Winson Chung5aaab772012-04-27 15:24:02 -0700813 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800814 return;
815 }
816
Sunny Goyald478c832016-04-01 12:04:16 -0700817 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
818 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700819 if (resultCode == RESULT_OK) {
820 // Update the widget view.
821 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
822 pendingAddWidgetId, mPendingAddInfo);
823 if (workspaceLocked) {
824 sPendingAddItem = args;
825 } else {
826 completeAdd(args);
827 }
828 }
829 // Leave the widget in the pending state if the user canceled the configure.
830 return;
831 }
832
Sunny Goyalaad90582015-07-09 14:22:50 -0700833 if (requestCode == REQUEST_CREATE_SHORTCUT) {
834 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
835 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
836 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
837 mPendingAddInfo);
838 if (isWorkspaceLocked()) {
839 sPendingAddItem = args;
840 } else {
841 completeAdd(args);
842 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
843 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
844 }
845 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700846 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
847 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800850 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800851
852 }
853
854 @Override
855 protected void onActivityResult(
856 final int requestCode, final int resultCode, final Intent data) {
857 handleActivityResult(requestCode, resultCode, data);
858 if (mLauncherCallbacks != null) {
859 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
860 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800861 }
862
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600863 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700864 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600865 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700866 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
867 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
868 View v = null;
869 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
870 if (layout != null) {
871 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
872 }
873 Intent intent = sPendingAddItem.intent;
874 sPendingAddItem = null;
875 if (grantResults.length > 0
876 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700877 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700878 } else {
879 // TODO: Show a snack bar with link to settings
880 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
881 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
882 }
883 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600884 if (mLauncherCallbacks != null) {
885 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
886 grantResults);
887 }
888 }
889
Adam Cohendb364c32014-05-20 14:23:40 -0700890 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
891 appWidgetId, ItemInfo info) {
892 PendingAddArguments args = new PendingAddArguments();
893 args.requestCode = requestCode;
894 args.intent = data;
895 args.container = info.container;
896 args.screenId = info.screenId;
897 args.cellX = info.cellX;
898 args.cellY = info.cellY;
899 args.appWidgetId = appWidgetId;
900 return args;
901 }
902
903 /**
904 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
905 *
906 * @param screenId the screen id to check
907 * @return the new screen, or screenId if it exists
908 */
909 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800910 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700911 if (dropLayout == null) {
912 // it's possible that the add screen was removed because it was
913 // empty and a re-bind occurred
914 mWorkspace.addExtraEmptyScreen();
915 return mWorkspace.commitExtraEmptyScreen();
916 } else {
917 return screenId;
918 }
919 }
920
Adam Cohen091440a2015-03-18 14:16:05 -0700921 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800922 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800923 Runnable onCompleteRunnable = null;
924 int animationType = 0;
925
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700926 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800927 if (resultCode == RESULT_OK) {
928 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
929 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700930 mPendingAddWidgetInfo);
931 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800932 onCompleteRunnable = new Runnable() {
933 @Override
934 public void run() {
935 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700936 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700937 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
938 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800939 }
940 };
941 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800942 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800943 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800944 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700945 if (mDragLayer.getAnimatedView() != null) {
946 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
947 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
948 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700949 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700950 // The animated view may be null in the case of a rotation during widget configuration
951 onCompleteRunnable.run();
952 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 }
954
955 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700956 protected void onStop() {
957 super.onStop();
958 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700959
960 if (mLauncherCallbacks != null) {
961 mLauncherCallbacks.onStop();
962 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700963
964 if (Utilities.isNycMR1OrAbove()) {
965 mAppWidgetHost.stopListening();
966 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700967 }
968
969 @Override
970 protected void onStart() {
971 super.onStart();
972 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700973
974 if (mLauncherCallbacks != null) {
975 mLauncherCallbacks.onStart();
976 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700977
978 if (Utilities.isNycMR1OrAbove()) {
979 mAppWidgetHost.startListening();
980 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700981 }
982
983 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200985 long startTime = 0;
986 if (DEBUG_RESUME_TIME) {
987 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400988 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200989 }
Adam Cohen9211d422014-10-07 18:14:53 -0700990
991 if (mLauncherCallbacks != null) {
992 mLauncherCallbacks.preOnResume();
993 }
994
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700996 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700997
Winson Chung4a2afa32012-07-19 14:53:05 -0700998 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700999 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001000 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001001 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -07001002 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -07001003 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -07001004 // view after launching an app, as they may be depending on the UI to be static to
1005 // switch to another app, otherwise, if it was
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07001006 showAppsView(false /* animated */, !launchedFromApp /* updatePredictedApps */,
1007 false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08001008 } else if (mOnResumeState == State.WIDGETS) {
1009 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001010 }
1011 mOnResumeState = State.NONE;
1012
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001013 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001014 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1015 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001016
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001017 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001018 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001019 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001020 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001021 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001022 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001024 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001025 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1026 // execute them here
1027 long startTimeCallbacks = 0;
1028 if (DEBUG_RESUME_TIME) {
1029 startTimeCallbacks = System.currentTimeMillis();
1030 }
1031
Michael Jurka1e2f4652013-07-08 18:03:46 -07001032 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1033 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001034 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001035 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001036 if (DEBUG_RESUME_TIME) {
1037 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1038 (System.currentTimeMillis() - startTimeCallbacks));
1039 }
Michael Jurka447bf842013-05-15 14:52:15 +02001040 }
Michael Jurka54554252013-08-01 12:52:23 +02001041 if (mOnResumeCallbacks.size() > 0) {
1042 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1043 mOnResumeCallbacks.get(i).run();
1044 }
1045 mOnResumeCallbacks.clear();
1046 }
Winson Chunge4e50662012-01-23 14:45:13 -08001047
1048 // Reset the pressed state of icons that were locked in the press state while activities
1049 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001050 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001051 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001052 mWaitingForResume.setStayPressed(false);
1053 }
Winson Chung82963d52013-10-09 11:20:57 -07001054
Adam Cohen06dff352012-06-01 17:17:08 -07001055 // It is possible that widgets can receive updates while launcher is not in the foreground.
1056 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1057 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1058 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001059 if (!isWorkspaceLoading()) {
1060 getWorkspace().reinflateWidgetsIfNecessary();
1061 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001062
Michael Jurka447bf842013-05-15 14:52:15 +02001063 if (DEBUG_RESUME_TIME) {
1064 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1065 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001066
Adam Cohen4b66bf32015-09-18 12:15:19 -07001067 // We want to suppress callbacks about CustomContent being shown if we have just received
1068 // onNewIntent while the user was present within launcher. In that case, we post a call
1069 // to move the user to the main screen (which will occur after onResume). We don't want to
1070 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1071 // suppress here.
1072 if (mWorkspace.getCustomContentCallbacks() != null
1073 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001074 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001075 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001076 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1077 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001078 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001079 }
1080 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001081 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001082 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001083 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001084
Sunny Goyal756adbc2015-04-16 15:20:51 -07001085 if (!isWorkspaceLoading()) {
1086 // Process any items that were added while Launcher was away.
1087 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1088 }
Adam Cohen9211d422014-10-07 18:14:53 -07001089
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001090 if (shouldShowDiscoveryBounce()) {
1091 mAllAppsController.showDiscoveryBounce();
1092 }
1093 mIsResumeFromActionScreenOff = false;
Adam Cohen9211d422014-10-07 18:14:53 -07001094 if (mLauncherCallbacks != null) {
1095 mLauncherCallbacks.onResume();
1096 }
Adam Cohen06dff352012-06-01 17:17:08 -07001097 }
1098
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001100 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001101 // Ensure that items added to Launcher are queued until Launcher returns
1102 InstallShortcutReceiver.enableInstallQueue();
1103
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001104 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001105 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001106 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001107 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001108
1109 // We call onHide() aggressively. The custom content callbacks should be able to
1110 // debounce excess onHide calls.
1111 if (mWorkspace.getCustomContentCallbacks() != null) {
1112 mWorkspace.getCustomContentCallbacks().onHide();
1113 }
Romain Guycbb89e42009-06-08 15:52:54 -07001114
Adam Cohen9211d422014-10-07 18:14:53 -07001115 if (mLauncherCallbacks != null) {
1116 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001117 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001118 }
1119
1120 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001121 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1122 // by a onResume or by scrolling otherwise.
1123 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001124
1125 // Custom content is completely hidden
1126 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001127
1128 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1129 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001130
1131 // Indicates whether the user is allowed to scroll away from the custom content.
1132 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001133 }
1134
Adam Cohenc2d6e892014-10-16 09:49:24 -07001135 public interface LauncherOverlay {
1136
1137 /**
1138 * Touch interaction leading to overscroll has begun
1139 */
1140 public void onScrollInteractionBegin();
1141
1142 /**
1143 * Touch interaction related to overscroll has ended
1144 */
1145 public void onScrollInteractionEnd();
1146
1147 /**
1148 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1149 * screen (or in the case of RTL, the rightmost screen).
1150 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001151 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001152
1153 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001154 * Called when the launcher is ready to use the overlay
1155 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001156 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001157 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001158 }
1159
Jun Mukai8af0cd82015-05-12 12:32:12 -07001160 public interface LauncherSearchCallbacks {
1161 /**
1162 * Called when the search overlay is shown.
1163 */
1164 public void onSearchOverlayOpened();
1165
1166 /**
1167 * Called when the search overlay is dismissed.
1168 */
1169 public void onSearchOverlayClosed();
1170 }
1171
Adam Cohenc2d6e892014-10-16 09:49:24 -07001172 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001173 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001174 }
1175
1176 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1177
Sunny Goyalc86df472016-02-25 09:19:38 -08001178 public void onScrollChanged(float progress) {
1179 if (mWorkspace != null) {
1180 mWorkspace.onOverlayScrollChanged(progress);
1181 }
1182 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001183 }
1184
Jorim Jaggid017f882014-01-14 17:08:48 -08001185 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001186 if (mLauncherCallbacks != null) {
1187 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001188 } else {
1189 // On devices with a locked orientation, we will at least have the allow rotation
1190 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001191 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001192 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001193 }
1194
Adam Cohen9211d422014-10-07 18:14:53 -07001195 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001196 CustomContentCallbacks callbacks, String description) {
1197 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001198 }
1199
Adam Cohenedb40762013-07-18 16:45:45 -07001200 // The custom content needs to offset its content to account for the QSB
1201 public int getTopOffsetForCustomContent() {
1202 return mWorkspace.getPaddingTop();
1203 }
1204
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001205 @Override
1206 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001207 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001208 if (mModel.isCurrentCallbacks(this)) {
1209 mModel.stopLoader();
1210 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001211 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1212
Romain Guy13c2e7b2010-03-10 19:45:00 -08001213 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001214 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001215
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001216 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001217 @Override
1218 public void onWindowFocusChanged(boolean hasFocus) {
1219 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001220 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001221
1222 if (mLauncherCallbacks != null) {
1223 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1224 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001225 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001226
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 private boolean acceptFilter() {
1228 final InputMethodManager inputManager = (InputMethodManager)
1229 getSystemService(Context.INPUT_METHOD_SERVICE);
1230 return !inputManager.isFullscreenMode();
1231 }
1232
1233 @Override
1234 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001235 final int uniChar = event.getUnicodeChar();
1236 final boolean handled = super.onKeyDown(keyCode, event);
1237 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1238 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001239 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1240 keyCode, event);
1241 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001242 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001243 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001244 // showSearchDialog()
1245 // If there are multiple keystrokes before the search dialog takes focus,
1246 // onSearchRequested() will be called for every keystroke,
1247 // but it is idempotent, so it's fine.
1248 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 }
1250 }
1251
Joe Onorato8a9625e2010-01-28 15:55:35 -08001252 // Eat the long press event so the keyboard doesn't come up.
1253 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1254 return true;
1255 }
1256
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257 return handled;
1258 }
1259
Winson46163472015-09-22 17:31:56 -07001260 @Override
1261 public boolean onKeyUp(int keyCode, KeyEvent event) {
1262 if (keyCode == KeyEvent.KEYCODE_MENU) {
1263 // Ignore the menu key if we are currently dragging or are on the custom content screen
1264 if (!isOnCustomContent() && !mDragController.isDragging()) {
1265 // Close any open folders
1266 closeFolder();
1267
Tony Wickham49c8d292016-07-01 11:44:34 -07001268 // Close any shortcuts containers
1269 closeShortcutsContainer();
1270
Winson46163472015-09-22 17:31:56 -07001271 // Stop resizing any widgets
1272 mWorkspace.exitWidgetResizeMode();
1273
1274 // Show the overview mode if we are on the workspace
1275 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1276 !mWorkspace.isSwitchingState()) {
1277 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001278 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001279 }
1280 }
1281 return true;
1282 }
1283 return super.onKeyUp(keyCode, event);
1284 }
1285
Karl Rosaen138a0412009-04-23 19:00:21 -07001286 private String getTypedText() {
1287 return mDefaultKeySsb.toString();
1288 }
1289
1290 private void clearTypedText() {
1291 mDefaultKeySsb.clear();
1292 mDefaultKeySsb.clearSpans();
1293 Selection.setSelection(mDefaultKeySsb, 0);
1294 }
1295
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001297 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1298 * State
1299 */
1300 private static State intToState(int stateOrdinal) {
1301 State state = State.WORKSPACE;
1302 final State[] stateValues = State.values();
1303 for (int i = 0; i < stateValues.length; i++) {
1304 if (stateValues[i].ordinal() == stateOrdinal) {
1305 state = stateValues[i];
1306 break;
1307 }
1308 }
1309 return state;
1310 }
1311
1312 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 * Restores the previous state, if it exists.
1314 *
1315 * @param savedState The previous state.
1316 */
1317 private void restoreState(Bundle savedState) {
1318 if (savedState == null) {
1319 return;
1320 }
1321
Michael Jurka883f55b2010-10-21 15:47:14 -07001322 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001323 if (state == State.APPS || state == State.WIDGETS) {
1324 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001325 }
1326
Adam Cohen21cd0022013-10-09 18:57:02 -07001327 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1328 PagedView.INVALID_RESTORE_PAGE);
1329 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001330 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 }
1332
Sunny Goyal756cd262015-08-20 12:33:21 -07001333 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1334 if (itemValues != null) {
1335 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001336 AppWidgetProviderInfo info = savedState.getParcelable(
1337 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001338 mPendingAddWidgetInfo = info == null ?
1339 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1340
Adam Cohen4637b5a2013-11-04 18:21:24 -08001341 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001342 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 mRestoring = true;
1344 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 }
1346
1347 /**
1348 * Finds all the views we need and configure them properly.
1349 */
1350 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001351 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001352 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001353 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001354 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyal6178f132016-07-11 17:30:03 -07001355 mQsbContainer = mDragLayer.findViewById(mDeviceProfile.isVerticalBarLayout()
1356 ? R.id.workspace_blocked_row : R.id.qsb_container);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001357 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001358
Sunny Goyal784f9c32016-03-23 16:56:54 -07001359 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1360 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1361 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001362 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363
Winson Chung4c98d922011-05-31 16:50:48 -07001364 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001365
1366 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001367
Winson Chung3d503fb2011-07-13 17:25:49 -07001368 // Setup the hotseat
1369 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1370 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001371 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001372 }
1373
Winsone9f27272015-10-13 10:47:51 -07001374 // Setup the overview panel
1375 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001376
Winson Chung4c98d922011-05-31 16:50:48 -07001377 // Setup the workspace
1378 mWorkspace.setHapticFeedbackEnabled(false);
1379 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001380 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001381 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1382 // default state, otherwise we will update to the wrong offsets in RTL
1383 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001384 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001385 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001386
Tony Wickham34d2c912015-09-11 09:27:58 -07001387 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001388 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001389
Winson Chungef7f8742015-06-04 17:18:17 -07001390 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001391 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001392 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001393 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1394 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1395 } else {
1396 mAppsView.setSearchBarController(new DefaultAppSearchController());
1397 }
Craig Mautner360310b2012-10-26 15:13:08 -07001398
Winson Chung3d503fb2011-07-13 17:25:49 -07001399 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001400 mDragController.setDragScoller(mWorkspace);
1401 mDragController.setScrollView(mDragLayer);
1402 mDragController.setMoveTarget(mWorkspace);
1403 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001404 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001405
Peter Schiller310a9882016-06-30 13:52:36 -07001406 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1407 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1408 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001409
Sunny Goyal322d5562015-06-25 19:35:49 -07001410 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1411 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001412 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001413 }
1414
Winsone9f27272015-10-13 10:47:51 -07001415 private void setupOverviewPanel() {
1416 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1417
1418 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1419 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1420 @Override
1421 public boolean onLongClick(View v) {
1422 return v.performClick();
1423 }
1424 };
1425
1426 // Bind wallpaper button actions
1427 View wallpaperButton = findViewById(R.id.wallpaper_button);
1428 wallpaperButton.setOnClickListener(new OnClickListener() {
1429 @Override
1430 public void onClick(View view) {
1431 if (!mWorkspace.isSwitchingState()) {
1432 onClickWallpaperPicker(view);
1433 }
1434 }
1435 });
1436 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1437 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1438
1439 // Bind widget button actions
1440 mWidgetsButton = findViewById(R.id.widget_button);
1441 mWidgetsButton.setOnClickListener(new OnClickListener() {
1442 @Override
1443 public void onClick(View view) {
1444 if (!mWorkspace.isSwitchingState()) {
1445 onClickAddWidgetButton(view);
1446 }
1447 }
1448 });
1449 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1450 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1451
1452 // Bind settings actions
1453 View settingsButton = findViewById(R.id.settings_button);
1454 boolean hasSettings = hasSettings();
1455 if (hasSettings) {
1456 settingsButton.setOnClickListener(new OnClickListener() {
1457 @Override
1458 public void onClick(View view) {
1459 if (!mWorkspace.isSwitchingState()) {
1460 onClickSettingsButton(view);
1461 }
1462 }
1463 });
1464 settingsButton.setOnLongClickListener(performClickOnLongClick);
1465 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1466 } else {
1467 settingsButton.setVisibility(View.GONE);
1468 }
1469
1470 mOverviewPanel.setAlpha(0f);
1471 }
1472
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001474 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001475 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001476 */
1477 public void setAllAppsButton(View allAppsButton) {
1478 mAllAppsButton = allAppsButton;
1479 }
1480
Sunny Goyalbb011da2016-06-15 15:42:29 -07001481 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001482 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001483 }
1484
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001485 public View getWidgetsButton() {
1486 return mWidgetsButton;
1487 }
1488
Anjali Koppal5ad44842014-03-10 20:34:39 -07001489 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 * Creates a view representing a shortcut.
1491 *
1492 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001494 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001495 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 }
1497
1498 /**
1499 * Creates a view representing a shortcut inflated from the specified resource.
1500 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 * @param parent The group the shortcut belongs to.
1502 * @param info The data structure describing the shortcut.
1503 *
1504 * @return A View inflated from layoutResId.
1505 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001506 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001507 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001508 parent, false);
1509 favorite.applyFromShortcutInfo(info, mIconCache);
1510 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001512 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 return favorite;
1514 }
1515
1516 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 * Add a shortcut to the workspace.
1518 *
1519 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001521 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001522 int cellY) {
1523 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001524 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001525
Sunny Goyal5c97f512015-05-19 16:03:28 -07001526 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001527 if (info == null) {
1528 return;
1529 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001530 final View view = createShortcut(info);
1531
Sunny Goyal5c97f512015-05-19 16:03:28 -07001532 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001533 // First we check if we already know the exact location where we want to add this item.
1534 if (cellX >= 0 && cellY >= 0) {
1535 cellXY[0] = cellX;
1536 cellXY[1] = cellY;
1537 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001538
1539 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001540 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001541 true, null,null)) {
1542 return;
1543 }
1544 DragObject dragObject = new DragObject();
1545 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001546 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1547 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001548 return;
1549 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001550 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001551 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001552 }
1553
1554 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001555 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001556 return;
1557 }
1558
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001559 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560
1561 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001562 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001563 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 }
1565 }
1566
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001568 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001570 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 */
Adam Cohen091440a2015-03-18 14:16:05 -07001572 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001573 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1574
1575 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001576 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001577 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001578 }
Romain Guycbb89e42009-06-08 15:52:54 -07001579
Adam Cohen59400422014-03-05 18:07:04 -08001580 if (appWidgetInfo.isCustomWidget) {
1581 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001582 }
1583
Adam Cohen59400422014-03-05 18:07:04 -08001584 LauncherAppWidgetInfo launcherInfo;
1585 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1586 launcherInfo.spanX = info.spanX;
1587 launcherInfo.spanY = info.spanY;
1588 launcherInfo.minSpanX = info.minSpanX;
1589 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001590 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001591
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001593 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594
1595 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001596 if (hostView == null) {
1597 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001598 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001599 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001600 hostView.setVisibility(View.VISIBLE);
1601 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001603 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001604 }
Romain Guycbb89e42009-06-08 15:52:54 -07001605
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001606 private void addAppWidgetToWorkspace(
1607 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001608 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001609 hostView.setTag(item);
1610 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001611
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001612 hostView.setFocusable(true);
1613 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001614
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001615 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001616 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1617
1618 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001619 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001620 }
1621 }
1622
Adam Cohended9f8d2010-11-03 13:25:16 -07001623 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1624 @Override
1625 public void onReceive(Context context, Intent intent) {
1626 final String action = intent.getAction();
1627 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1628 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001629 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001630 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001631
Winson Chungc100e8e2011-08-09 16:02:43 -07001632 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001633 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001634 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001635 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001636 if (!showWorkspace(false)) {
1637 // If we are already on the workspace, then manually reset all apps
1638 mAppsView.reset();
1639 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001640 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001641 mIsResumeFromActionScreenOff = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001642 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1643 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001644 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001645 }
1646 }
1647 };
1648
1649 @Override
1650 public void onAttachedToWindow() {
1651 super.onAttachedToWindow();
1652
1653 // Listen for broadcasts related to user-presence
1654 final IntentFilter filter = new IntentFilter();
1655 filter.addAction(Intent.ACTION_SCREEN_OFF);
1656 filter.addAction(Intent.ACTION_USER_PRESENT);
1657 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001658 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001659 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001660 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001661
1662 if (mLauncherCallbacks != null) {
1663 mLauncherCallbacks.onAttachedToWindow();
1664 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001665 }
1666
1667 @Override
1668 public void onDetachedFromWindow() {
1669 super.onDetachedFromWindow();
1670 mVisible = false;
1671
Adam Cohend113e0c2010-11-11 10:48:05 -08001672 if (mAttached) {
1673 unregisterReceiver(mReceiver);
1674 mAttached = false;
1675 }
Winson Chungb745afb2015-03-02 11:51:23 -08001676 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001677
1678 if (mLauncherCallbacks != null) {
1679 mLauncherCallbacks.onDetachedFromWindow();
1680 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001681 }
1682
1683 public void onWindowVisibilityChanged(int visibility) {
1684 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001685 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001686 // The following code used to be in onResume, but it turns out onResume is called when
1687 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1688 // is a more appropriate event to handle
1689 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001690 if (!mWorkspaceLoading) {
1691 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001692 // We want to let Launcher draw itself at least once before we force it to build
1693 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001694 // apps is nice and speedy.
1695 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001696 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001697 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001698 if (mStarted) return;
1699 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001700 // We delay the layer building a bit in order to give
1701 // other message processing a time to run. In particular
1702 // this avoids a delay in hiding the IME if it was
1703 // currently shown, because doing that may involve
1704 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001705 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001706 final ViewTreeObserver.OnDrawListener listener = this;
1707 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001708 public void run() {
1709 if (mWorkspace != null &&
1710 mWorkspace.getViewTreeObserver() != null) {
1711 mWorkspace.getViewTreeObserver().
1712 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001713 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001714 }
1715 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001716 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001717 }
1718 });
1719 }
1720 clearTypedText();
1721 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001722 }
1723
Adam Cohen091440a2015-03-18 14:16:05 -07001724 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001725 mHandler.removeMessages(ADVANCE_MSG);
1726 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1727 mHandler.sendMessageDelayed(msg, delay);
1728 mAutoAdvanceSentTime = System.currentTimeMillis();
1729 }
1730
Adam Cohen091440a2015-03-18 14:16:05 -07001731 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001732 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1733 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1734 mAutoAdvanceRunning = autoAdvanceRunning;
1735 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001736 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001737 sendAdvanceMessage(delay);
1738 } else {
1739 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001740 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001741 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1742 }
1743 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001744 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001745 }
1746 }
1747 }
1748
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001749 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1750
Adam Cohended9f8d2010-11-03 13:25:16 -07001751 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001752 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001753 if (msg.what == ADVANCE_MSG) {
1754 int i = 0;
1755 for (View key: mWidgetsToAdvance.keySet()) {
1756 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001757 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001758 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001759 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001760 public void run() {
1761 ((Advanceable) v).advance();
1762 }
1763 }, delay);
1764 }
1765 i++;
1766 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001767 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001768 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001769 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001770 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001771 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001772
Winsonc0b52fe2015-09-09 16:38:15 -07001773 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001774 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001775 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1776 if (v instanceof Advanceable) {
1777 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001778 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001779 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001780 }
1781 }
1782
Winsonc0b52fe2015-09-09 16:38:15 -07001783 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001784 if (mWidgetsToAdvance.containsKey(hostView)) {
1785 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001786 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001787 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001788 }
1789
Hyunyoung Song3f471442015-04-08 19:01:34 -07001790 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001791 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1792 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001793 }
1794
Winson Chunga6945242014-01-08 14:04:34 -08001795 public DragLayer getDragLayer() {
1796 return mDragLayer;
1797 }
1798
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001799 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001800 return mAppsView;
1801 }
1802
Hyunyoung Song3f471442015-04-08 19:01:34 -07001803 public WidgetsContainerView getWidgetsView() {
1804 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001805 }
1806
Winson Chunga6945242014-01-08 14:04:34 -08001807 public Workspace getWorkspace() {
1808 return mWorkspace;
1809 }
1810
Sunny Goyal6178f132016-07-11 17:30:03 -07001811 public View getQsbContainer() {
1812 return mQsbContainer;
1813 }
1814
Winson Chunga6945242014-01-08 14:04:34 -08001815 public Hotseat getHotseat() {
1816 return mHotseat;
1817 }
1818
Jorim Jaggid017f882014-01-14 17:08:48 -08001819 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001820 return mOverviewPanel;
1821 }
1822
Sunny Goyal47328fd2016-05-25 18:56:41 -07001823 public DropTargetBar getDropTargetBar() {
1824 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001825 }
1826
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001827 public LauncherAppWidgetHost getAppWidgetHost() {
1828 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001829 }
Romain Guycbb89e42009-06-08 15:52:54 -07001830
Winson Chunga9abd0e2010-10-27 17:18:37 -07001831 public LauncherModel getModel() {
1832 return mModel;
1833 }
1834
Adam Cohen79d90c52016-04-22 13:29:20 -07001835 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001836 return mSharedPrefs;
1837 }
1838
Adam Cohen2e6da152015-05-06 11:42:25 -07001839 public DeviceProfile getDeviceProfile() {
1840 return mDeviceProfile;
1841 }
1842
Bjorn Bringertc459e522013-06-07 19:36:01 +01001843 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001844 getWindow().closeAllPanels();
1845
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001846 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001847 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001848 }
1849
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 @Override
1851 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001852 long startTime = 0;
1853 if (DEBUG_RESUME_TIME) {
1854 startTime = System.currentTimeMillis();
1855 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001856 super.onNewIntent(intent);
1857
1858 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001859 Folder openFolder = mWorkspace.getOpenFolder();
1860 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1861 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1862 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1863 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1864 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001865 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001866 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001867
Adam Cohen6fecd412013-10-02 17:41:50 -07001868 if (mWorkspace == null) {
1869 // Can be cases where mWorkspace is null, this prevents a NPE
1870 return;
1871 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001872 // In all these cases, only animate if we're already on home
1873 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001874
Sunny Goyal935fca12015-10-13 10:19:01 -07001875 closeFolder(alreadyOnHome);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001876 closeShortcutsContainer(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001877 exitSpringLoadedDragMode();
1878
1879 // If we are already on home, then just animate back to the workspace,
1880 // otherwise, just wait until onResume to set the state back to Workspace
1881 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001882 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001883 } else {
1884 mOnResumeState = State.WORKSPACE;
1885 }
1886
1887 final View v = getWindow().peekDecorView();
1888 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001889 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001890 INPUT_METHOD_SERVICE);
1891 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1892 }
1893
Winson Chungb745afb2015-03-02 11:51:23 -08001894 // Reset the apps view
1895 if (!alreadyOnHome && mAppsView != null) {
1896 mAppsView.scrollToTop();
1897 }
1898
Hyunyoung Song3f471442015-04-08 19:01:34 -07001899 // Reset the widgets view
1900 if (!alreadyOnHome && mWidgetsView != null) {
1901 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001902 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001903
Adam Cohen9211d422014-10-07 18:14:53 -07001904 if (mLauncherCallbacks != null) {
1905 mLauncherCallbacks.onHomeIntent();
1906 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001907 }
Adam Cohened307df2013-10-02 09:37:31 -07001908
Adam Cohen9211d422014-10-07 18:14:53 -07001909 if (mLauncherCallbacks != null) {
1910 mLauncherCallbacks.onNewIntent(intent);
1911 }
Winson15f8b172015-08-19 11:02:39 -07001912
1913 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1914 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1915 // animation.
1916 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001917 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1918 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001919 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1920 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001921
1922 // We use this flag to suppress noisy callbacks above custom content state
1923 // from onResume.
1924 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001925 mWorkspace.post(new Runnable() {
1926 @Override
1927 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001928 if (mWorkspace != null) {
1929 mWorkspace.moveToDefaultScreen(true);
1930 }
Winson15f8b172015-08-19 11:02:39 -07001931 }
1932 });
1933 }
1934 }
1935
1936 if (DEBUG_RESUME_TIME) {
1937 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1938 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001939 }
1940
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001942 public void onRestoreInstanceState(Bundle state) {
1943 super.onRestoreInstanceState(state);
1944 for (int page: mSynchronouslyBoundPages) {
1945 mWorkspace.restoreInstanceStateForChild(page);
1946 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947 }
1948
1949 @Override
1950 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001951 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001952 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1953 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001954
Adam Cohen21cd0022013-10-09 18:57:02 -07001955 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001956 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957
Michael Jurka883f55b2010-10-21 15:47:14 -07001958 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001959 // We close any open folder since it will not be re-opened, and we need to make sure
1960 // this state is reflected.
1961 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1962 closeFolder(false);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001963 closeShortcutsContainer(false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001964
Adam Cohendcd297f2013-06-18 13:13:40 -07001965 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001966 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001967 ContentValues itemValues = new ContentValues();
1968 mPendingAddInfo.writeToValues(itemValues);
1969 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001970 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001971 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 }
1973
Adam Cohen9211d422014-10-07 18:14:53 -07001974 if (mLauncherCallbacks != null) {
1975 mLauncherCallbacks.onSaveInstanceState(outState);
1976 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 }
1978
1979 @Override
1980 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001982
Winson Chunge7a03942011-08-05 15:05:12 -07001983 // Remove all pending runnables
1984 mHandler.removeMessages(ADVANCE_MSG);
1985 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001986 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001987 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001988
Winson Chungcd2b0142011-06-08 16:02:26 -07001989 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001990 // It's possible to receive onDestroy after a new Launcher activity has
1991 // been created. In this case, don't interfere with the new Launcher.
1992 if (mModel.isCurrentCallbacks(this)) {
1993 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001994 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001995 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001996
Sunny Goyal745bad92016-05-02 10:54:12 -07001997 if (mRotationPrefChangeHandler != null) {
1998 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1999 }
2000
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002002 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002004 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002006 mAppWidgetHost = null;
2007
2008 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009
2010 TextKeyListener.getInstance().release();
2011
Tony Wickhame2217252016-03-22 16:34:23 -07002012 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
2013 .removeAccessibilityStateChangeListener(this);
2014
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002015 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002016
Michael Jurka2ecf9952012-06-18 12:52:28 -07002017 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002018
2019 if (mLauncherCallbacks != null) {
2020 mLauncherCallbacks.onDestroy();
2021 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 }
2023
Sunny Goyalae502842016-06-17 08:43:56 -07002024 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
2025 return mAccessibilityDelegate;
2026 }
2027
Adam Cohena9cf38f2011-05-02 15:36:58 -07002028 public DragController getDragController() {
2029 return mDragController;
2030 }
2031
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002033 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07002034 onStartForResult(requestCode);
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002035 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07002036 }
2037
2038 @Override
2039 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2040 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2041 onStartForResult(requestCode);
2042 try {
2043 super.startIntentSenderForResult(intent, requestCode,
2044 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2045 } catch (IntentSender.SendIntentException e) {
2046 throw new ActivityNotFoundException();
2047 }
2048 }
2049
2050 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002051 if (requestCode >= 0) {
2052 setWaitingForResult(true);
2053 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 }
2055
Winson Chung70d72102011-08-12 11:24:35 -07002056 /**
2057 * Indicates that we want global search for this activity by setting the globalSearch
2058 * argument for {@link #startSearch} to true.
2059 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002061 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002063
Karl Rosaen138a0412009-04-23 19:00:21 -07002064 if (initialQuery == null) {
2065 // Use any text typed in the launcher as the initial query
2066 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002067 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068 if (appSearchData == null) {
2069 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002070 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002072
2073 // TODO send proper bounds.
2074 Rect sourceBounds = null;
Romain Guycbb89e42009-06-08 15:52:54 -07002075
Ian Parkinson047036e2014-09-01 15:40:53 +01002076 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002077 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002078 if (clearTextImmediately) {
2079 clearTypedText();
2080 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002081
2082 // We need to show the workspace after starting the search
2083 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002084 }
2085
Ian Parkinson047036e2014-09-01 15:40:53 +01002086 /**
2087 * Start a text search.
2088 *
2089 * @return {@code true} if the search will start immediately, so any further keypresses
2090 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2091 * to buffer keypresses.
2092 */
2093 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002094 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002095 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2096 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2097 sourceBounds);
2098 }
2099
Michael Jurkaa33411c2012-06-14 16:18:21 -07002100 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002101 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002102 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002103 }
2104
2105 /**
2106 * Starts the global search activity. This code is a copied from SearchManager
2107 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002108 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002109 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002110 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002111 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2112 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2113 if (globalSearchActivity == null) {
2114 Log.w(TAG, "No global search activity found.");
2115 return;
2116 }
2117 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2118 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2119 intent.setComponent(globalSearchActivity);
2120 // Make sure that we have a Bundle to put source in
2121 if (appSearchData == null) {
2122 appSearchData = new Bundle();
2123 } else {
2124 appSearchData = new Bundle(appSearchData);
2125 }
Adam Cohen9211d422014-10-07 18:14:53 -07002126 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002127 if (!appSearchData.containsKey("source")) {
2128 appSearchData.putString("source", getPackageName());
2129 }
2130 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2131 if (!TextUtils.isEmpty(initialQuery)) {
2132 intent.putExtra(SearchManager.QUERY, initialQuery);
2133 }
2134 if (selectInitialQuery) {
2135 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2136 }
2137 intent.setSourceBounds(sourceBounds);
2138 try {
2139 startActivity(intent);
2140 } catch (ActivityNotFoundException ex) {
2141 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2142 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002143 }
2144
Yura4f93ec62014-02-04 14:15:21 +00002145 public boolean isOnCustomContent() {
2146 return mWorkspace.isOnOrMovingToCustomContent();
2147 }
2148
Winson Chung70d72102011-08-12 11:24:35 -07002149 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002150 public boolean onPrepareOptionsMenu(Menu menu) {
2151 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002152 if (mLauncherCallbacks != null) {
2153 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2154 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002155 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002156 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002157
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002158 @Override
2159 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002160 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002161 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002162 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002163 }
2164
Joe Onorato9c1289c2009-08-17 11:03:03 -04002165 public boolean isWorkspaceLocked() {
2166 return mWorkspaceLoading || mWaitingForResult;
2167 }
2168
Adam Cohen517a7f52014-03-01 12:12:59 -08002169 public boolean isWorkspaceLoading() {
2170 return mWorkspaceLoading;
2171 }
2172
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002173 private void setWorkspaceLoading(boolean value) {
2174 boolean isLocked = isWorkspaceLocked();
2175 mWorkspaceLoading = value;
2176 if (isLocked != isWorkspaceLocked()) {
2177 onWorkspaceLockedChanged();
2178 }
2179 }
2180
2181 private void setWaitingForResult(boolean value) {
2182 boolean isLocked = isWorkspaceLocked();
2183 mWaitingForResult = value;
2184 if (isLocked != isWorkspaceLocked()) {
2185 onWorkspaceLockedChanged();
2186 }
2187 }
2188
Adam Cohen9211d422014-10-07 18:14:53 -07002189 protected void onWorkspaceLockedChanged() {
2190 if (mLauncherCallbacks != null) {
2191 mLauncherCallbacks.onWorkspaceLockedChanged();
2192 }
2193 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002194
Michael Jurka0280c3b2010-09-17 15:00:07 -07002195 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002196 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002197 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002198 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2199 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002200 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002201 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002202
Tony Wickhama0628cc2015-10-14 15:23:04 -07002203 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002204 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002205 if (LOGD) {
2206 Log.d(TAG, "Adding widget from drop");
2207 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002208 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2209 }
2210
Sunny Goyale6b63a32015-01-16 16:14:29 -08002211 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002212 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2213 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002214 if (appWidgetInfo.configure != null) {
2215 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002216 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002217
Bjorn Bringert7984c942009-12-09 15:38:25 +00002218 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002219 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2220 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2221
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002222 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002223 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002224 Runnable onComplete = new Runnable() {
2225 @Override
2226 public void run() {
2227 // Exit spring loaded mode if necessary after adding the widget
2228 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2229 null);
2230 }
2231 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002232 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002233 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002234 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002235 }
2236 }
Romain Guycbb89e42009-06-08 15:52:54 -07002237
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002238 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002239 // Close any folders that may be open.
2240 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002241 mWorkspace.moveToCustomContentScreen(animate);
2242 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002243
2244 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2245 int[] cell, int spanX, int spanY) {
2246 switch (info.itemType) {
2247 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2248 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2249 int span[] = new int[2];
2250 span[0] = spanX;
2251 span[1] = spanY;
2252 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2253 container, screenId, cell, span);
2254 break;
2255 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2256 processShortcutFromDrop(info.componentName, container, screenId, cell);
2257 break;
2258 default:
2259 throw new IllegalStateException("Unknown item type: " + info.itemType);
2260 }
2261 }
2262
Adam Cohenfbba09b2011-07-18 21:43:05 -07002263 /**
2264 * Process a shortcut drop.
2265 *
2266 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002267 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002268 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002269 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002270 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2271 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002272 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002273 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002274 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002275
2276 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002277 mPendingAddInfo.cellX = cell[0];
2278 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002279 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002280
2281 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2282 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002283 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002284 }
2285
Adam Cohenfbba09b2011-07-18 21:43:05 -07002286 /**
2287 * Process a widget drop.
2288 *
2289 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002290 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002291 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002292 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002293 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2294 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002295 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002296 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002297 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002298 mPendingAddInfo.minSpanX = info.minSpanX;
2299 mPendingAddInfo.minSpanY = info.minSpanY;
2300
Adam Cohenfbba09b2011-07-18 21:43:05 -07002301 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002302 mPendingAddInfo.cellX = cell[0];
2303 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002304 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002305 if (span != null) {
2306 mPendingAddInfo.spanX = span[0];
2307 mPendingAddInfo.spanY = span[1];
2308 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002309
Adam Cohened66b2b2012-01-23 17:28:51 -08002310 AppWidgetHostView hostView = info.boundWidget;
2311 int appWidgetId;
2312 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002313 // In the case where we've prebound the widget, we remove it from the DragLayer
2314 if (LOGD) {
2315 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2316 }
2317 getDragLayer().removeView(hostView);
2318
Adam Cohened66b2b2012-01-23 17:28:51 -08002319 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002320 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002321
2322 // Clear the boundWidget so that it doesn't get destroyed.
2323 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002324 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002325 // In this case, we either need to start an activity to get permission to bind
2326 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002327 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002328 Bundle options = info.bindOptions;
2329
Sunny Goyalffe83f12014-08-14 17:39:34 -07002330 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2331 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002332 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002333 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002334 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002335 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002336 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2337 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2338 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002339 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2340 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002341 // TODO: we need to make sure that this accounts for the options bundle.
2342 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002343 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2344 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002345 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002346 }
2347
Adam Cohendcd297f2013-06-18 13:13:40 -07002348 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002349 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002350 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002351 folderInfo.title = getText(R.string.folder_name);
2352
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002353 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002354 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2355 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002356
2357 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002358 FolderIcon newFolder =
2359 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002360 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002361 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002362 // Force measure the new folder icon
2363 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2364 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002365 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002366 }
Romain Guycbb89e42009-06-08 15:52:54 -07002367
Winsonc0b52fe2015-09-09 16:38:15 -07002368 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002369 * Unbinds the view for the specified item, and removes the item and all its children.
2370 *
2371 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002372 * @param itemInfo the {@link ItemInfo} for this view.
2373 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002374 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002375 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002376 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002377 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002378 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2379 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002380 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002381 } else {
2382 mWorkspace.removeWorkspaceItem(v);
2383 }
Winsonc0b52fe2015-09-09 16:38:15 -07002384 if (deleteFromDb) {
2385 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2386 }
2387 } else if (itemInfo instanceof FolderInfo) {
2388 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002389 if (v instanceof FolderIcon) {
2390 ((FolderIcon) v).removeListeners();
2391 }
Winsonc0b52fe2015-09-09 16:38:15 -07002392 mWorkspace.removeWorkspaceItem(v);
2393 if (deleteFromDb) {
2394 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2395 }
2396 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2397 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002398 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002399 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002400 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002401 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002402 }
2403 } else {
2404 return false;
2405 }
2406 return true;
2407 }
2408
2409 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002410 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002411 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002412 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002413 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002414 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002415 // Deleting an app widget ID is a void call but writes to disk before returning
2416 // to the caller...
2417 new AsyncTask<Void, Void, Void>() {
2418 public Void doInBackground(Void ... args) {
2419 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2420 return null;
2421 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002422 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002423 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002424 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002425 }
2426
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002427 @Override
2428 public boolean dispatchKeyEvent(KeyEvent event) {
2429 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2430 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002431 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002432 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002433 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002434 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002435 dumpState();
2436 return true;
2437 }
2438 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002439 }
2440 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2441 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002442 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002443 return true;
2444 }
2445 }
2446
2447 return super.dispatchKeyEvent(event);
2448 }
2449
Joe Onorato88ec0992009-11-19 13:16:06 -08002450 @Override
2451 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002452 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2453 return;
2454 }
2455
Sunny Goyal45478022015-06-08 16:52:41 -07002456 if (mDragController.isDragging()) {
2457 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002458 return;
2459 }
2460
Tony Wickham49c8d292016-07-01 11:44:34 -07002461 if (getOpenShortcutsContainer() != null) {
2462 closeShortcutsContainer();
2463 } else if (isAppsViewVisible()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002464 showWorkspace(true);
2465 } else if (isWidgetsViewVisible()) {
2466 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002467 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002468 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002469 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002470 Folder openFolder = mWorkspace.getOpenFolder();
2471 if (openFolder.isEditingName()) {
2472 openFolder.dismissEditingName();
2473 } else {
2474 closeFolder();
2475 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002476 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002477 mWorkspace.exitWidgetResizeMode();
2478
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002479 // Back button is a no-op here, but give at least some feedback for the button press
2480 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002481 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002482 }
2483
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002484 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002485 * Launches the intent referred by the clicked shortcut.
2486 *
2487 * @param v The view representing the clicked shortcut.
2488 */
2489 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002490 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2491 // view has detached (it's possible for this to happen if the view is removed mid touch).
2492 if (v.getWindowToken() == null) {
2493 return;
2494 }
2495
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002496 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002497 return;
2498 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002499
Adam Cohen1697b792013-09-17 19:08:21 -07002500 if (v instanceof Workspace) {
2501 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002502 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002503 }
2504 return;
2505 }
2506
2507 if (v instanceof CellLayout) {
2508 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002509 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2510 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002511 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002512 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002513 }
2514
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002515 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002516 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002517 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002518 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002519 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002520 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002521 }
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002522 } else if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2523 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002524 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002525 } else if (tag instanceof AppInfo) {
2526 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002527 } else if (tag instanceof LauncherAppWidgetInfo) {
2528 if (v instanceof PendingAppWidgetHostView) {
2529 onClickPendingWidget((PendingAppWidgetHostView) v);
2530 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002531 }
2532 }
2533
Sunny Goyal70660032015-05-14 00:07:08 -07002534 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002535 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002536 return false;
2537 }
2538
Michael Jurkaaf442092010-06-10 17:01:57 -07002539 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002540 * Event handler for the app widget view which has not fully restored.
2541 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002542 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002543 if (mIsSafeModeEnabled) {
2544 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2545 return;
2546 }
2547
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002548 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002549 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002550 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2551 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2552 // This should not happen, as we make sure that an Id is allocated during bind.
2553 return;
2554 }
2555 LauncherAppWidgetProviderInfo appWidgetInfo =
2556 mAppWidgetManager.findProvider(info.providerName, info.user);
2557 if (appWidgetInfo != null) {
2558 mPendingAddWidgetId = info.appWidgetId;
2559 mPendingAddInfo.copyFrom(info);
2560 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002561
Sunny Goyald478c832016-04-01 12:04:16 -07002562 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2563 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingAddWidgetId);
2564 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
2565 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2566 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2567 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2568 }
2569 } else {
2570 LauncherAppWidgetProviderInfo appWidgetInfo =
2571 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2572 if (appWidgetInfo != null) {
2573 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2574 }
Sunny Goyalff572272014-07-23 13:58:07 -07002575 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002576 } else if (info.installProgress < 0) {
2577 // The install has not been queued
2578 final String packageName = info.providerName.getPackageName();
2579 showBrokenAppInstallDialog(packageName,
2580 new DialogInterface.OnClickListener() {
2581 public void onClick(DialogInterface dialog, int id) {
2582 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2583 }
2584 });
2585 } else {
2586 // Download has started.
2587 final String packageName = info.providerName.getPackageName();
2588 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002589 }
2590 }
2591
Sunny Goyald478c832016-04-01 12:04:16 -07002592 private void startRestoredWidgetReconfigActivity(
2593 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
2594 mPendingAddWidgetInfo = provider;
2595 mPendingAddInfo.copyFrom(info);
2596 mPendingAddWidgetId = info.appWidgetId;
2597 mAppWidgetManager.startConfigActivity(provider,
2598 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2599 }
2600
Sunny Goyalff572272014-07-23 13:58:07 -07002601 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002602 * Event handler for the "grid" button that appears on the home screen, which
2603 * enters all apps mode.
2604 *
2605 * @param v The view that was clicked.
2606 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002607 protected void onClickAllAppsButton(View v) {
2608 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002609 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002610 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.TAP,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002611 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002612 showAppsView(true /* animated */, true /* updatePredictedApps */,
2613 false /* focusSearchBar */);
Winson Chung76648c52015-07-10 14:33:23 -07002614 }
2615 }
2616
2617 protected void onLongClickAllAppsButton(View v) {
2618 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2619 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002620 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.LONGPRESS,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002621 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002622 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07002623 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002624 }
2625 }
2626
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002627 private void showBrokenAppInstallDialog(final String packageName,
2628 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002629 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002630 .setTitle(R.string.abandoned_promises_title)
2631 .setMessage(R.string.abandoned_promise_explanation)
2632 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2633 .setNeutralButton(R.string.abandoned_clean_this,
2634 new DialogInterface.OnClickListener() {
2635 public void onClick(DialogInterface dialog, int id) {
2636 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2637 mWorkspace.removeAbandonedPromise(packageName, user);
2638 }
2639 })
2640 .create().show();
2641 return;
2642 }
2643
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002644 /**
2645 * Event handler for an app shortcut click.
2646 *
2647 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2648 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002649 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002650 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2651 Object tag = v.getTag();
2652 if (!(tag instanceof ShortcutInfo)) {
2653 throw new IllegalArgumentException("Input must be a Shortcut");
2654 }
2655
2656 // Open shortcut
2657 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002658
2659 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002660 if ((shortcut.isDisabled &
2661 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2662 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2663 // If the app is only disabled because of the above flags, launch activity anyway.
2664 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002665 } else {
2666 int error = R.string.activity_not_available;
2667 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2668 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002669 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2670 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002671 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002672 }
2673 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2674 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002675 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002676 }
2677
Chris Wren40c5ed32014-06-24 18:24:23 -04002678 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002679 if ((v instanceof BubbleTextView)
2680 && shortcut.isPromise()
2681 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002682 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002683 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002684 new DialogInterface.OnClickListener() {
2685 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002686 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002687 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002688 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002689 return;
2690 }
2691
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002692 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002693 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002694 }
2695
Sunny Goyala7ce1662016-05-31 15:01:35 -07002696 private void startAppShortcutOrInfoActivity(View v) {
2697 ItemInfo item = (ItemInfo) v.getTag();
2698 Intent intent = item.getIntent();
2699 if (intent == null) {
2700 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002701 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002702 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002703 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002704
2705 if (success && v instanceof BubbleTextView) {
2706 mWaitingForResume = (BubbleTextView) v;
2707 mWaitingForResume.setStayPressed(true);
2708 }
2709 }
2710
2711 /**
2712 * Event handler for a folder icon click.
2713 *
2714 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2715 */
2716 protected void onClickFolderIcon(View v) {
2717 if (LOGD) Log.d(TAG, "onClickFolder");
2718 if (!(v instanceof FolderIcon)){
2719 throw new IllegalArgumentException("Input must be a FolderIcon");
2720 }
2721
2722 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002723 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002724 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002725 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002726 }
Michael Jurka5130e402011-10-13 04:55:35 -07002727 }
2728
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002729 /**
2730 * Event handler for the (Add) Widgets button that appears after a long press
2731 * on the home screen.
2732 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002733 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002734 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002735 if (mIsSafeModeEnabled) {
2736 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2737 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002738 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002739 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002740 }
2741
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002742 /**
2743 * Event handler for the wallpaper picker button that appears after a long press
2744 * on the home screen.
2745 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002746 public void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002747 if (!Utilities.isWallapaperAllowed(this)) {
2748 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2749 return;
2750 }
2751
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002752 String pickerPackage = getString(R.string.wallpaper_picker_package);
2753 if (TextUtils.isEmpty(pickerPackage)) {
2754 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2755 }
2756
Tony Wickham785f7a52015-08-31 17:28:32 -07002757 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2758 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002759
2760 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002761 .setPackage(pickerPackage)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002762 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
2763 intent.setSourceBounds(getViewBounds(v));
2764 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002765 }
2766
2767 /**
2768 * Event handler for a click on the settings button that appears after a long press
2769 * on the home screen.
2770 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002771 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002772 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002773 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2774 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002775 }
2776
Adam Cohen61f560d2013-09-30 15:58:20 -07002777 public View.OnTouchListener getHapticFeedbackTouchListener() {
2778 if (mHapticFeedbackTouchListener == null) {
2779 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002780 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002781 @Override
2782 public boolean onTouch(View v, MotionEvent event) {
2783 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2784 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2785 }
2786 return false;
2787 }
2788 };
2789 }
2790 return mHapticFeedbackTouchListener;
2791 }
2792
Tony Wickhame2217252016-03-22 16:34:23 -07002793 @Override
2794 public void onAccessibilityStateChanged(boolean enabled) {
2795 mDragLayer.onAccessibilityStateChanged(enabled);
2796 }
2797
Adam Cohen9211d422014-10-07 18:14:53 -07002798 public void onDragStarted(View view) {
2799 if (isOnCustomContent()) {
2800 // Custom content screen doesn't participate in drag and drop. If on custom
2801 // content screen, move to default.
2802 moveWorkspaceToDefaultScreen();
2803 }
Adam Cohen9211d422014-10-07 18:14:53 -07002804 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002805
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002806 /**
2807 * Called when the user stops interacting with the launcher.
2808 * This implies that the user is now on the homescreen and is not doing housekeeping.
2809 */
Adam Cohen9211d422014-10-07 18:14:53 -07002810 protected void onInteractionEnd() {
2811 if (mLauncherCallbacks != null) {
2812 mLauncherCallbacks.onInteractionEnd();
2813 }
2814 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002815
2816 /**
2817 * Called when the user starts interacting with the launcher.
2818 * The possible interactions are:
2819 * - open all apps
2820 * - reorder an app shortcut, or a widget
2821 * - open the overview mode.
2822 * This is a good time to stop doing things that only make sense
2823 * when the user is on the homescreen and not doing housekeeping.
2824 */
Adam Cohen9211d422014-10-07 18:14:53 -07002825 protected void onInteractionBegin() {
2826 if (mLauncherCallbacks != null) {
2827 mLauncherCallbacks.onInteractionBegin();
2828 }
2829 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002830
Winson Chungcd99cd32015-04-29 11:03:24 -07002831 /** Updates the interaction state. */
2832 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002833 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002834 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002835 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2836 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002837 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002838 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002839 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002840 onInteractionEnd();
2841 }
2842 }
2843
Sunny Goyala7ce1662016-05-31 15:01:35 -07002844 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002845 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002846 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2847 try {
2848 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2849 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2850 // is enabled by default on NYC.
2851 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2852 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002853
2854 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2855 String id = ((ShortcutInfo) info).getDeepShortcutId();
2856 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002857 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2858 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002859 } else {
2860 // Could be launching some bookkeeping activity
2861 startActivity(intent, optsBundle);
2862 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002863 } finally {
2864 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002865 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002866 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002867 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2868 // corresponding permission. Show the appropriate permission prompt if that
2869 // is the case.
2870 if (intent.getComponent() == null
2871 && Intent.ACTION_CALL.equals(intent.getAction())
2872 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2873 PackageManager.PERMISSION_GRANTED) {
2874 // TODO: Rename sPendingAddItem to a generic name.
2875 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2876 0, info);
2877 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2878 REQUEST_PERMISSION_CALL_PHONE);
2879 } else {
2880 // No idea why this was thrown.
2881 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002882 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002883 }
2884 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002885
Sunny Goyala7ce1662016-05-31 15:01:35 -07002886 private Bundle getActivityLaunchOptions(View v) {
2887 if (Utilities.ATLEAST_MARSHMALLOW) {
2888 int left = 0, top = 0;
2889 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2890 if (v instanceof TextView) {
2891 // Launch from center of icon, not entire view
2892 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2893 if (icon != null) {
2894 Rect bounds = icon.getBounds();
2895 left = (width - bounds.width()) / 2;
2896 top = v.getPaddingTop();
2897 width = bounds.width();
2898 height = bounds.height();
2899 }
2900 }
2901 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2902 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2903 // On L devices, we use the device default slide-up transition.
2904 // On L MR1 devices, we use a custom version of the slide-up transition which
2905 // doesn't have the delay present in the device default.
2906 return ActivityOptions.makeCustomAnimation(
2907 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2908 }
2909 return null;
2910 }
2911
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002912 private Rect getViewBounds(View v) {
2913 int[] pos = new int[2];
2914 v.getLocationOnScreen(pos);
2915 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2916 }
2917
Sunny Goyala7ce1662016-05-31 15:01:35 -07002918 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002919 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2920 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2921 return false;
2922 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002923 // Only launch using the new animation if the shortcut has not opted out (this is a
2924 // private contract between launcher and may be ignored in the future).
2925 boolean useLaunchAnimation = (v != null) &&
2926 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2927 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2928
2929 UserHandleCompat user = null;
2930 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2931 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2932 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002933 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002934
2935 // Prepare intent
2936 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2937 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002938 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002939 }
2940 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002941 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2942 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
2943 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002944 // Shortcuts need some special checks due to legacy reasons.
2945 startShortcutIntentSafely(intent, optsBundle, item);
2946 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2947 // Could be launching some bookkeeping activity
2948 startActivity(intent, optsBundle);
2949 } else {
2950 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2951 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2952 }
2953 return true;
2954 } catch (ActivityNotFoundException|SecurityException e) {
2955 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2956 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2957 }
2958 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002959 }
2960
Adam Cohen268c4752012-06-06 17:47:33 -07002961 /**
2962 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2963 * in the DragLayer in the exact absolute location of the original FolderIcon.
2964 */
2965 private void copyFolderIconToImage(FolderIcon fi) {
2966 final int width = fi.getMeasuredWidth();
2967 final int height = fi.getMeasuredHeight();
2968
2969 // Lazy load ImageView, Bitmap and Canvas
2970 if (mFolderIconImageView == null) {
2971 mFolderIconImageView = new ImageView(this);
2972 }
2973 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2974 mFolderIconBitmap.getHeight() != height) {
2975 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2976 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2977 }
2978
2979 DragLayer.LayoutParams lp;
2980 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2981 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2982 } else {
2983 lp = new DragLayer.LayoutParams(width, height);
2984 }
2985
Adam Cohen307fe232012-08-16 17:55:58 -07002986 // The layout from which the folder is being opened may be scaled, adjust the starting
2987 // view size by this scale factor.
2988 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002989 lp.customPosition = true;
2990 lp.x = mRectForFolderAnimation.left;
2991 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002992 lp.width = (int) (scale * width);
2993 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002994
2995 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2996 fi.draw(mFolderIconCanvas);
2997 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002998 if (fi.getFolder() != null) {
2999 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3000 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003001 }
Adam Cohen268c4752012-06-06 17:47:33 -07003002 // Just in case this image view is still in the drag layer from a previous animation,
3003 // we remove it and re-add it.
3004 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3005 mDragLayer.removeView(mFolderIconImageView);
3006 }
3007 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003008 if (fi.getFolder() != null) {
3009 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003010 }
Adam Cohen268c4752012-06-06 17:47:33 -07003011 }
3012
Adam Cohen37b2a492016-04-06 18:36:06 -07003013 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003014 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07003015 FolderInfo info = (FolderInfo) fi.getTag();
3016 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3017 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003018 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3019 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003020 }
3021
Adam Cohen268c4752012-06-06 17:47:33 -07003022 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3023 copyFolderIconToImage(fi);
3024 fi.setVisibility(View.INVISIBLE);
3025
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003026 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
3027 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003028 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003029 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3030 }
3031 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003032 oa.start();
3033 }
3034
Sunny Goyal935fca12015-10-13 10:19:01 -07003035 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003036 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003037 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003038
Adam Cohen268c4752012-06-06 17:47:33 -07003039 // We remove and re-draw the FolderIcon in-case it has changed
3040 mDragLayer.removeView(mFolderIconImageView);
3041 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08003042
3043 if (cl != null) {
3044 cl.clearFolderLeaveBehind();
3045 }
3046
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003047 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003048 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003049 oa.addListener(new AnimatorListenerAdapter() {
3050 @Override
3051 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003052 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003053 // Remove the ImageView copy of the FolderIcon and make the original visible.
3054 mDragLayer.removeView(mFolderIconImageView);
3055 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003056 }
3057 }
3058 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003059 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003060 if (!animate) {
3061 oa.end();
3062 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003063 }
3064
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003065 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003066 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003067 * is animated relative to the specified View. If the View is null, no animation
3068 * is played.
3069 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003070 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003071 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003072 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003073
Adam Cohenfb91f302012-06-11 15:45:18 -07003074 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003075 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3076 if (openFolder != null && openFolder != folder) {
3077 // Close any open folder before opening a folder.
3078 closeFolder();
3079 }
3080
Adam Cohena9cf38f2011-05-02 15:36:58 -07003081 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003082
Adam Cohena9cf38f2011-05-02 15:36:58 -07003083 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003084
Sunny Goyalf4066152015-04-15 09:42:19 -07003085 // While the folder is open, the position of the icon cannot change.
3086 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3087
Adam Cohen4554ee12011-08-03 16:13:21 -07003088 // Just verify that the folder hasn't already been added to the DragLayer.
3089 // There was a one-off crash where the folder had a parent already.
3090 if (folder.getParent() == null) {
3091 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003092 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003093 } else {
3094 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3095 folder.getParent() + ").");
3096 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003097 folder.animateOpen();
3098
3099 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003100
3101 // Notify the accessibility manager that this folder "window" has appeared and occluded
3102 // the workspace items
3103 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3104 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003105 }
3106
3107 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003108 closeFolder(true);
3109 }
3110
3111 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003112 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003113 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003114 if (folder.isEditingName()) {
3115 folder.dismissEditingName();
3116 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003117 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003118 }
3119 }
3120
Sunny Goyal935fca12015-10-13 10:19:01 -07003121 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003122 animate &= !Utilities.isPowerSaverOn(this);
3123
Adam Cohen4554ee12011-08-03 16:13:21 -07003124 folder.getInfo().opened = false;
3125
3126 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3127 if (parent != null) {
3128 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003129 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003130 if (fi != null) {
3131 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3132 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003133 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003134 if (animate) {
3135 folder.animateClosed();
3136 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003137 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003138 }
Winson Chung83ca4802013-04-12 15:10:52 -07003139
Sunny Goyal935fca12015-10-13 10:19:01 -07003140 // Notify the accessibility manager that this folder "window" has disappeared and no
3141 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003142 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003143 }
3144
Tony Wickham49c8d292016-07-01 11:44:34 -07003145 public void closeShortcutsContainer() {
Sunny Goyala2454ad2016-07-22 10:50:11 -07003146 closeShortcutsContainer(true);
3147 }
3148
3149 public void closeShortcutsContainer(boolean animate) {
Tony Wickham49c8d292016-07-01 11:44:34 -07003150 DeepShortcutsContainer deepShortcutsContainer = getOpenShortcutsContainer();
3151 if (deepShortcutsContainer != null) {
Sunny Goyala2454ad2016-07-22 10:50:11 -07003152 if (animate) {
3153 deepShortcutsContainer.animateClose();
3154 } else {
3155 deepShortcutsContainer.close();
3156 }
Tony Wickham49c8d292016-07-01 11:44:34 -07003157 }
3158 }
3159
3160 /**
3161 * @return The open shortcuts container, or null if there is none
3162 */
3163 public DeepShortcutsContainer getOpenShortcutsContainer() {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07003164 // Iterate in reverse order. Shortcuts container is added later to the dragLayer,
3165 // and will be one of the last views.
3166 for (int i = mDragLayer.getChildCount() - 1; i >= 0; i--) {
3167 View child = mDragLayer.getChildAt(i);
Sunny Goyala2454ad2016-07-22 10:50:11 -07003168 if (child instanceof DeepShortcutsContainer
3169 && ((DeepShortcutsContainer) child).isOpen()) {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07003170 return (DeepShortcutsContainer) child;
3171 }
3172 }
3173 return null;
Tony Wickham49c8d292016-07-01 11:44:34 -07003174 }
3175
Tony Wickhamdadb3042016-02-24 11:07:00 -08003176 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003177 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003178 if (!isDraggingEnabled()) return false;
3179 if (isWorkspaceLocked()) return false;
3180 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003181
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07003182 if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
3183 (v == mAllAppsButton && mAllAppsButton != null)) {
Winson Chung76648c52015-07-10 14:33:23 -07003184 onLongClickAllAppsButton(v);
3185 return true;
3186 }
3187
Adam Cohen1697b792013-09-17 19:08:21 -07003188 if (v instanceof Workspace) {
3189 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003190 if (!mWorkspace.isTouchActive()) {
3191 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003192 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3193 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3194 return true;
3195 } else {
3196 return false;
3197 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003198 } else {
3199 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003200 }
Adam Cohen1697b792013-09-17 19:08:21 -07003201 }
3202
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003203 CellLayout.CellInfo longClickCellInfo = null;
3204 View itemUnderLongClick = null;
3205 if (v.getTag() instanceof ItemInfo) {
3206 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003207 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003208 itemUnderLongClick = longClickCellInfo.cell;
3209 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003210 }
3211
Winson Chung3d503fb2011-07-13 17:25:49 -07003212 // The hotseat touch handling does not go through Workspace, and we always allow long press
3213 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003214 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003215 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003216 // User long pressed on empty space
3217 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3218 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003219 if (mWorkspace.isInOverviewMode()) {
3220 mWorkspace.startReordering(v);
3221 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003222 showOverviewMode(true);
Hyunyoung Song645764e2016-06-06 14:19:02 -07003223 mHotseat.requestDisallowInterceptTouchEvent(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003224 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003225 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003226 final boolean isAllAppsButton =
3227 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3228 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3229 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003230 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003231 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003232 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003233 }
3234 }
3235 }
3236 return true;
3237 }
3238
Winson Chung3d503fb2011-07-13 17:25:49 -07003239 boolean isHotseatLayout(View layout) {
3240 return mHotseat != null && layout != null &&
3241 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3242 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003243
Winson Chung3d503fb2011-07-13 17:25:49 -07003244 /**
3245 * Returns the CellLayout of the specified container at the specified screen.
3246 */
Sunny Goyal92820592015-03-02 11:31:35 -08003247 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003248 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3249 if (mHotseat != null) {
3250 return mHotseat.getLayout();
3251 } else {
3252 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003253 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003254 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003255 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003256 }
3257 }
3258
Winson Chungb745afb2015-03-02 11:51:23 -08003259 /**
3260 * For overridden classes.
3261 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003262 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003263 return isAppsViewVisible();
3264 }
3265
3266 public boolean isAppsViewVisible() {
3267 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3268 }
3269
3270 public boolean isWidgetsViewVisible() {
3271 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003272 }
3273
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003274 private void setWorkspaceBackground(int background) {
3275 switch (background) {
3276 case WORKSPACE_BACKGROUND_TRANSPARENT:
3277 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3278 break;
3279 case WORKSPACE_BACKGROUND_BLACK:
3280 getWindow().setBackgroundDrawable(null);
3281 break;
3282 default:
3283 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3284 }
Craig Mautner360310b2012-10-26 15:13:08 -07003285 }
3286
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003287 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003288 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3289 int curflags = getWindow().getAttributes().flags
3290 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3291 if (wpflags != curflags) {
3292 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3293 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003294 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003295 }
3296
Michael Jurkae326f182011-11-21 14:05:46 -08003297 @Override
3298 public void onTrimMemory(int level) {
3299 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003300 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3301 // The widget preview db can result in holding onto over
3302 // 3MB of memory for caching which isn't necessary.
3303 SQLiteDatabase.releaseMemory();
3304
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003305 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003306 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003307 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003308 if (mLauncherCallbacks != null) {
3309 mLauncherCallbacks.onTrimMemory(level);
3310 }
Michael Jurkae326f182011-11-21 14:05:46 -08003311 }
3312
Winson5c6bdbb2015-09-03 11:36:19 -07003313 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003314 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003315 }
3316
Winson5c6bdbb2015-09-03 11:36:19 -07003317 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003318 boolean changed = mState != State.WORKSPACE ||
3319 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003320 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003321 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003322 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003323 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003324
Michael Jurkab3e22d92011-10-31 15:58:33 -07003325 // Set focus to the AppsCustomize button
3326 if (mAllAppsButton != null) {
3327 mAllAppsButton.requestFocus();
3328 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003329 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003330
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003331 // Change the state *after* we've called all the transition code
3332 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003333
Winson Chung5fb63472011-02-02 17:03:37 -08003334 // Resume the auto-advance of widgets
3335 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003336 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003337
Winson Chung0f785722015-04-08 10:27:49 -07003338 if (changed) {
3339 // Send an accessibility event to announce the context change
3340 getWindow().getDecorView()
3341 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003342 }
Winson5c6bdbb2015-09-03 11:36:19 -07003343 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003344 }
3345
Winsone9f27272015-10-13 10:47:51 -07003346 /**
3347 * Shows the overview button.
3348 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07003349 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003350 showOverviewMode(animated, false);
3351 }
3352
3353 /**
3354 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3355 * onto one of the overview panel buttons.
3356 */
3357 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3358 Runnable postAnimRunnable = null;
3359 if (requestButtonFocus) {
3360 postAnimRunnable = new Runnable() {
3361 @Override
3362 public void run() {
3363 // Hitting the menu button when in touch mode does not trigger touch mode to
3364 // be disabled, so if requested, force focus on one of the overview panel
3365 // buttons.
3366 mOverviewPanel.requestFocusFromTouch();
3367 }
3368 };
3369 }
Adam Cohened307df2013-10-02 09:37:31 -07003370 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003371 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003372 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003373 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003374 }
3375
Winson Chungb745afb2015-03-02 11:51:23 -08003376 /**
3377 * Shows the apps view.
3378 */
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003379 public void showAppsView(boolean animated, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003380 boolean focusSearchBar) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003381 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07003382 if (updatePredictedApps) {
3383 tryAndUpdatePredictedApps();
3384 }
Winson Chung76648c52015-07-10 14:33:23 -07003385 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003386 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003387
Winson Chungb745afb2015-03-02 11:51:23 -08003388 /**
3389 * Shows the widgets view.
3390 */
3391 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003392 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003393 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003394 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003395 }
Winson Chung76648c52015-07-10 14:33:23 -07003396 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003397
3398 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003399 @Override
3400 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003401 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003402 }
3403 });
Winson Chungb745afb2015-03-02 11:51:23 -08003404 }
3405
3406 /**
3407 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003408 *
3409 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003410 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003411 // TODO: calling method should use the return value so that when {@code false} is returned
3412 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003413 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003414 if (!(mState == State.WORKSPACE ||
3415 mState == State.APPS_SPRING_LOADED ||
3416 mState == State.WIDGETS_SPRING_LOADED ||
3417 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003418 return false;
3419 }
3420 if (toState != State.APPS && toState != State.WIDGETS) {
3421 return false;
3422 }
Winson Chungb745afb2015-03-02 11:51:23 -08003423
3424 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003425 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3426 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003427 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003428 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003429 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003430
Michael Jurkab3e22d92011-10-31 15:58:33 -07003431 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003432 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003433
3434 // Pause the auto-advance of widgets until we are out of AllApps
3435 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003436 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003437 closeFolder();
Tony Wickham49c8d292016-07-01 11:44:34 -07003438 closeShortcutsContainer();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003439
3440 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003441 getWindow().getDecorView()
3442 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003443 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003444 }
3445
Winson Chungcd99cd32015-04-29 11:03:24 -07003446 /**
3447 * Updates the workspace and interaction state on state change, and return the animation to this
3448 * new state.
3449 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003450 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003451 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003452 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003453 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003454 updateInteraction(fromState, toState);
3455 return anim;
3456 }
3457
Hyunyoung Song3f471442015-04-08 19:01:34 -07003458 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003459 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003460 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003461 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003462 }
Winson Chungb745afb2015-03-02 11:51:23 -08003463
Winson Chung006ee262015-08-03 14:40:11 -07003464 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003465 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003466 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003467
3468 if (isAppsViewVisible()) {
3469 mState = State.APPS_SPRING_LOADED;
3470 } else if (isWidgetsViewVisible()) {
3471 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003472 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003473 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003474 } else {
3475 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003476 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003477 }
Adam Cohen7777d962011-08-18 18:58:38 -07003478
Hyunyoung Song3f471442015-04-08 19:01:34 -07003479 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003480 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003481 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003482
Winson Chunge7a03942011-08-05 15:05:12 -07003483 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003484 @Override
3485 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003486 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003487 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3488 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003489 // Before we show workspace, hide all apps again because
3490 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3491 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003492 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003493 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003494 } else {
3495 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003496 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003497 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003498 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003499 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003500
Tony Wickhame0c33232016-02-08 11:37:04 -08003501 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003502 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3503 || mState == State.WIDGETS_SPRING_LOADED;
3504 }
3505
Michael Jurkad3ef3062010-11-23 16:23:58 -08003506 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003507 if (mState == State.APPS_SPRING_LOADED) {
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003508 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07003509 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003510 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003511 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003512 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3513 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003514 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003515 }
3516
Winson Chung4ac30062015-05-08 17:34:17 -07003517 /**
3518 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3519 * resumed.
3520 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003521 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003522 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003523 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003524 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003525 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003526 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003527 }
3528 }
3529 }
3530
Michael Jurkab3e22d92011-10-31 15:58:33 -07003531 void lockAllApps() {
3532 // TODO
3533 }
3534
3535 void unlockAllApps() {
3536 // TODO
3537 }
3538
Winsonf768d932015-09-25 16:12:35 -07003539 public boolean launcherCallbacksProvidesSearch() {
3540 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3541 }
3542
Michael Jurkad7c28052012-04-27 15:43:36 -07003543 @Override
3544 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003545 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003546 final List<CharSequence> text = event.getText();
3547 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003548 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003549 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003550 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003551 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003552 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003553 } else if (mWorkspace != null) {
3554 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003555 } else {
3556 text.add(getString(R.string.all_apps_home_button_label));
3557 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003558 return result;
3559 }
3560
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003561 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003562 * If the activity is currently paused, signal that we need to run the passed Runnable
3563 * in onResume.
3564 *
3565 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003566 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3567 * wrong when we're not running, and if the activity comes back to what the configuration was
3568 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003569 *
3570 * Implementation of the method from LauncherModel.Callbacks.
3571 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003572 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003573 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003574 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003575 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003576 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003577 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003578 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003579 }
3580 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003581 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003582 return true;
3583 } else {
3584 return false;
3585 }
3586 }
3587
Michael Jurkac402cd92013-05-20 15:49:32 +02003588 private boolean waitUntilResume(Runnable run) {
3589 return waitUntilResume(run, false);
3590 }
3591
Michael Jurka1e2f4652013-07-08 18:03:46 -07003592 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003593 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003594 }
3595
Michael Jurka7607c2f2013-04-03 14:33:19 -07003596 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003597 * If the activity is currently paused, signal that we need to re-run the loader
3598 * in onResume.
3599 *
3600 * This needs to be called from incoming places where resources might have been loaded
3601 * while we are paused. That is becaues the Configuration might be wrong
3602 * when we're not running, and if it comes back to what it was when we
3603 * were paused, we are not restarted.
3604 *
3605 * Implementation of the method from LauncherModel.Callbacks.
3606 *
3607 * @return true if we are currently paused. The caller might be able to
3608 * skip some work in that case since we will come back again.
3609 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003610 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003611 public boolean setLoadOnResume() {
3612 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003613 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003614 mOnResumeNeedsLoad = true;
3615 return true;
3616 } else {
3617 return false;
3618 }
3619 }
3620
3621 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003622 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003623 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003624 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003625 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003626 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003627 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003628 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003629 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003630 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003631 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003632
Joe Onorato9c1289c2009-08-17 11:03:03 -04003633 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003634 * Clear any pending bind callbacks. This is called when is loader is planning to
3635 * perform a full rebind from scratch.
3636 */
3637 @Override
3638 public void clearPendingBinds() {
3639 mBindOnResumeCallbacks.clear();
3640 if (mPendingExecutor != null) {
3641 mPendingExecutor.markCompleted();
3642 mPendingExecutor = null;
3643 }
3644 }
3645
3646 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003647 * Refreshes the shortcuts shown on the workspace.
3648 *
3649 * Implementation of the method from LauncherModel.Callbacks.
3650 */
3651 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003652 if (LauncherAppState.PROFILE_STARTUP) {
3653 Trace.beginSection("Starting page bind");
3654 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003655 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003656
Winson Chungd64d1762013-08-20 14:37:16 -07003657 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003658 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003659 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003660
Michael Jurka05bf644e2011-11-30 20:28:53 -08003661 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003662 if (mHotseat != null) {
3663 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003664 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003665 if (LauncherAppState.PROFILE_STARTUP) {
3666 Trace.endSection();
3667 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003668 }
3669
Adam Cohendcd297f2013-06-18 13:13:40 -07003670 @Override
3671 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003672 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003673 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3674 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003675 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3676 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3677 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003678 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3679 // If there are no screens, we need to have an empty screen
3680 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003681 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003682 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003683
3684 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003685 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003686 if (hasCustomContentToLeft()) {
3687 mWorkspace.createCustomContentContainer();
3688 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003689 }
Winsonc7d2e832016-07-28 12:24:55 -07003690
3691 // After we have added all the screens, if the wallpaper was locked to the default state,
3692 // then notify to indicate that it can be released and a proper wallpaper offset can be
3693 // computed before the next layout
3694 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07003695 }
3696
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003697 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003698 int count = orderedScreenIds.size();
3699 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003700 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003701 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003702 // No need to bind the first screen, as its always bound.
3703 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3704 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003705 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003706 }
3707
Winson Chungd64d1762013-08-20 14:37:16 -07003708 public void bindAppsAdded(final ArrayList<Long> newScreens,
3709 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003710 final ArrayList<ItemInfo> addAnimated,
3711 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003712 Runnable r = new Runnable() {
3713 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003714 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003715 }
3716 };
3717 if (waitUntilResume(r)) {
3718 return;
3719 }
3720
Winson Chungd64d1762013-08-20 14:37:16 -07003721 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003722 if (newScreens != null) {
3723 bindAddScreens(newScreens);
3724 }
Winson Chungd64d1762013-08-20 14:37:16 -07003725
3726 // We add the items without animation on non-visible pages, and with
3727 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003728 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003729 bindItems(addNotAnimated, 0,
3730 addNotAnimated.size(), false);
3731 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003732 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003733 bindItems(addAnimated, 0,
3734 addAnimated.size(), true);
3735 }
Winson Chungc58497e2013-09-03 17:48:37 -07003736
Winson Chung87412982013-10-03 18:34:14 -07003737 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003738 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003739
Winson Chungb745afb2015-03-02 11:51:23 -08003740 if (addedApps != null && mAppsView != null) {
3741 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003742 }
Winson Chungd64d1762013-08-20 14:37:16 -07003743 }
3744
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003745 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003746 * Bind the items start-end from the list.
3747 *
3748 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003749 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003750 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003751 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3752 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003753 Runnable r = new Runnable() {
3754 public void run() {
3755 bindItems(shortcuts, start, end, forceAnimateIcons);
3756 }
3757 };
3758 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003759 return;
3760 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003761
Winson Chungf0c6ae02012-03-21 16:10:31 -07003762 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003763 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3764 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003765 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003766 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003767 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003768 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003769 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003770
3771 // Short circuit if we are loading dock items for a configuration which has no dock
3772 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3773 mHotseat == null) {
3774 continue;
3775 }
3776
Sunny Goyal639e9062015-08-19 19:17:06 -07003777 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003778 switch (item.itemType) {
3779 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3780 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003781 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003782 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003783 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003784 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003785 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003786 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003787 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003788 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003789 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003790 default:
3791 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003792 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003793
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003794 /*
3795 * Remove colliding items.
3796 */
3797 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3798 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3799 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3800 View v = cl.getChildAt(item.cellX, item.cellY);
3801 Object tag = v.getTag();
3802 String desc = "Collision while binding workspace item: " + item
3803 + ". Collides with " + tag;
3804 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3805 throw (new RuntimeException(desc));
3806 } else {
3807 Log.d(TAG, desc);
3808 LauncherModel.deleteItemFromDatabase(this, item);
3809 continue;
3810 }
3811 }
3812 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003813 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3814 item.cellY, 1, 1);
3815 if (animateIcons) {
3816 // Animate all the applications up now
3817 view.setAlpha(0f);
3818 view.setScaleX(0f);
3819 view.setScaleY(0f);
3820 bounceAnims.add(createNewAppBounceAnimation(view, i));
3821 newShortcutsScreenId = item.screenId;
3822 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003823 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003824
Winson Chung997a9232013-07-24 15:33:46 -07003825 if (animateIcons) {
3826 // Animate to the correct page
3827 if (newShortcutsScreenId > -1) {
3828 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003829 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003830 final Runnable startBounceAnimRunnable = new Runnable() {
3831 public void run() {
3832 anim.playTogether(bounceAnims);
3833 anim.start();
3834 }
3835 };
Winson Chung997a9232013-07-24 15:33:46 -07003836 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003837 // We post the animation slightly delayed to prevent slowdowns
3838 // when we are loading right after we return to launcher.
3839 mWorkspace.postDelayed(new Runnable() {
3840 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003841 if (mWorkspace != null) {
3842 mWorkspace.snapToPage(newScreenIndex);
3843 mWorkspace.postDelayed(startBounceAnimRunnable,
3844 NEW_APPS_ANIMATION_DELAY);
3845 }
Winson Chung94d67682013-09-25 16:29:40 -07003846 }
3847 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003848 } else {
3849 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003850 }
3851 }
Winson Chung64359a52013-07-08 17:17:08 -07003852 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003853 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003854 }
3855
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003856 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3857 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3858 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003859 view.updateAppWidget(null);
3860 view.setOnClickListener(this);
3861 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003862 mWorkspace.requestLayout();
3863 }
3864
Joe Onorato9c1289c2009-08-17 11:03:03 -04003865 /**
3866 * Add the views for a widget to the workspace.
3867 *
3868 * Implementation of the method from LauncherModel.Callbacks.
3869 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003870 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003871 Runnable r = new Runnable() {
3872 public void run() {
3873 bindAppWidget(item);
3874 }
3875 };
3876 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003877 return;
3878 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003879
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003880 if (mIsSafeModeEnabled) {
3881 bindSafeModeWidget(item);
3882 return;
3883 }
3884
Daniel Sandler843e8602010-06-07 14:59:01 -04003885 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3886 if (DEBUG_WIDGETS) {
3887 Log.d(TAG, "bindAppWidget: " + item);
3888 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003889
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003890 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003891
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003892 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3893 // If the provider is not ready, bind as a pending widget.
3894 appWidgetInfo = null;
3895 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3896 // The widget id is not valid. Try to find the widget based on the provider info.
3897 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3898 } else {
3899 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3900 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003901
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003902 // If the provider is ready, but the width is not yet restored, try to restore it.
3903 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3904 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003905 if (appWidgetInfo == null) {
3906 if (DEBUG_WIDGETS) {
3907 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3908 + " belongs to component " + item.providerName
3909 + ", as the povider is null");
3910 }
3911 LauncherModel.deleteItemFromDatabase(this, item);
3912 return;
3913 }
Sunny Goyalff572272014-07-23 13:58:07 -07003914
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003915 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003916 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003917 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3918 // Id has not been allocated yet. Allocate a new id.
3919 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3920 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003921
Sunny Goyald478c832016-04-01 12:04:16 -07003922 // Also try to bind the widget. If the bind fails, the user will be shown
3923 // a click to setup UI, which will ask for the bind permission.
3924 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3925 pendingInfo.spanX = item.spanX;
3926 pendingInfo.spanY = item.spanY;
3927 pendingInfo.minSpanX = item.minSpanX;
3928 pendingInfo.minSpanY = item.minSpanY;
3929 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
3930 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3931 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003932
Sunny Goyald478c832016-04-01 12:04:16 -07003933 // Bind succeeded
3934 if (success) {
3935 // If the widget has a configure activity, it is still needs to set it up,
3936 // otherwise the widget is ready to go.
3937 item.restoreStatus = (appWidgetInfo.configure == null)
3938 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3939 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003940 }
Sunny Goyald478c832016-04-01 12:04:16 -07003941
3942 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003943 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003944 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003945 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003946 // The widget was marked as UI not ready, but there is no configure activity to
3947 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003948 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3949 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003950 }
Sunny Goyalff572272014-07-23 13:58:07 -07003951 }
3952
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003953 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003954 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003955 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3956 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003957 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003958
Sunny Goyal56c73602015-09-25 12:55:01 -07003959 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003960 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003961 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003962 deleteWidgetInfo(item);
3963 return;
3964 }
3965
Sunny Goyal233ee962015-08-03 13:05:01 -07003966 item.minSpanX = appWidgetInfo.minSpanX;
3967 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003968 addAppWidgetToWorkspace(
3969 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3970 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003971 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003972 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003973 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003974 view.updateAppWidget(null);
3975 view.setOnClickListener(this);
3976 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003977 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003978 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003979
Daniel Sandler843e8602010-06-07 14:59:01 -04003980 if (DEBUG_WIDGETS) {
3981 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3982 + (SystemClock.uptimeMillis()-start) + "ms");
3983 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003984 }
3985
Sunny Goyalff572272014-07-23 13:58:07 -07003986 /**
3987 * Restores a pending widget.
3988 *
3989 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003990 */
Sunny Goyald478c832016-04-01 12:04:16 -07003991 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003992 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3993 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3994 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003995 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003996 }
3997
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003998 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003999 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07004000
4001 mWorkspace.reinflateWidgetsIfNecessary();
4002 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07004003 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07004004 }
4005
Adam Cohen1462de32012-07-24 22:34:36 -07004006 public void onPageBoundSynchronously(int page) {
4007 mSynchronouslyBoundPages.add(page);
4008 }
4009
Sunny Goyal527c7d32015-08-28 15:19:36 -07004010 @Override
4011 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
4012 if (mPendingExecutor != null) {
4013 mPendingExecutor.markCompleted();
4014 }
4015 mPendingExecutor = executor;
4016 executor.attachTo(this);
4017 }
4018
4019 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4020 if (mPendingExecutor == executor) {
4021 mPendingExecutor = null;
4022 }
4023 }
4024
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07004025 @Override
4026 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
4027 Runnable r = new Runnable() {
4028 public void run() {
4029 finishFirstPageBind(executor);
4030 }
4031 };
4032 if (waitUntilResume(r)) {
4033 return;
4034 }
4035
4036 Runnable onComplete = new Runnable() {
4037 @Override
4038 public void run() {
4039 if (executor != null) {
4040 executor.onLoadAnimationCompleted();
4041 }
4042 }
4043 };
4044 if (mDragLayer.getAlpha() < 1) {
4045 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
4046 } else {
4047 onComplete.run();
4048 }
4049 }
4050
Joe Onorato9c1289c2009-08-17 11:03:03 -04004051 /**
4052 * Callback saying that there aren't any more items to bind.
4053 *
4054 * Implementation of the method from LauncherModel.Callbacks.
4055 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004056 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004057 Runnable r = new Runnable() {
4058 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004059 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004060 }
4061 };
4062 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004063 return;
4064 }
Sunny Goyale26d1002016-06-20 14:52:14 -07004065 if (LauncherAppState.PROFILE_STARTUP) {
4066 Trace.beginSection("Page bind completed");
4067 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004068 if (mSavedState != null) {
4069 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004070 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004071 }
Sunny Goyal08442b82015-10-21 17:15:08 -07004072
Joe Onorato9c1289c2009-08-17 11:03:03 -04004073 mSavedState = null;
4074 }
4075
Adam Cohen1462de32012-07-24 22:34:36 -07004076 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004077
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004078 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07004079
4080 // If we received the result of any pending adds while the loader was running (e.g. the
4081 // widget configuration forced an orientation change), process them now.
4082 if (sPendingAddItem != null) {
4083 final long screenId = completeAdd(sPendingAddItem);
4084
4085 // TODO: this moves the user to the page where the pending item was added. Ideally,
4086 // the screen would be guaranteed to exist after bind, and the page would be set through
4087 // the workspace restore process.
4088 mWorkspace.post(new Runnable() {
4089 @Override
4090 public void run() {
4091 mWorkspace.snapToScreenId(screenId);
4092 }
4093 });
4094 sPendingAddItem = null;
4095 }
4096
Sunny Goyal756adbc2015-04-16 15:20:51 -07004097 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004098
4099 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004100 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004101 }
Sunny Goyale26d1002016-06-20 14:52:14 -07004102 if (LauncherAppState.PROFILE_STARTUP) {
4103 Trace.endSection();
4104 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004105 }
4106
Winson Chunga2413752012-04-03 14:22:34 -07004107 private boolean canRunNewAppsAnimation() {
4108 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07004109 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07004110 }
4111
Winson Chungc9168342013-06-26 14:54:55 -07004112 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004113 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004114 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4115 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004116 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004117 return bounceAnim;
4118 }
4119
Adam Cohen27772732013-11-11 14:00:56 +00004120 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004121 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004122 }
4123
Tony Wickham55616cd2015-09-23 14:55:17 -07004124 public int getSearchBarHeight() {
4125 if (mLauncherCallbacks != null) {
4126 return mLauncherCallbacks.getSearchBarHeight();
4127 }
4128 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4129 }
4130
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004131 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004132 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4133 * multiple calls to bind the same list.)
4134 */
4135 @Thunk ArrayList<AppInfo> mTmpAppsList;
4136 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4137 public void run() {
4138 bindAllApplications(mTmpAppsList);
4139 mTmpAppsList = null;
4140 }
4141 };
4142
4143 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004144 * Add the icons for all apps.
4145 *
4146 * Implementation of the method from LauncherModel.Callbacks.
4147 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004148 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004149 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4150 mTmpAppsList = apps;
4151 return;
4152 }
4153
Winson Chungb745afb2015-03-02 11:51:23 -08004154 if (mAppsView != null) {
4155 mAppsView.setApps(apps);
4156 }
Adam Cohen9211d422014-10-07 18:14:53 -07004157 if (mLauncherCallbacks != null) {
4158 mLauncherCallbacks.bindAllApplications(apps);
4159 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004160 }
4161
4162 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004163 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
4164 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
4165 */
4166 @Override
4167 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
4168 mDeepShortcutMap = deepShortcutMapCopy;
4169 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
4170 }
4171
Tony Wickham1bce7fd2016-04-28 17:39:03 -07004172 public List<String> getShortcutIdsForItem(ItemInfo info) {
4173 if (!DeepShortcutManager.supportsShortcuts(info)) {
4174 return Collections.EMPTY_LIST;
4175 }
4176 ComponentName component = info.getTargetComponent();
4177 List<String> ids = mDeepShortcutMap.get(new ComponentKey(component, info.user));
4178 return ids == null ? Collections.EMPTY_LIST : ids;
4179 }
4180
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004181 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004182 * A package was updated.
4183 *
4184 * Implementation of the method from LauncherModel.Callbacks.
4185 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004186 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004187 Runnable r = new Runnable() {
4188 public void run() {
4189 bindAppsUpdated(apps);
4190 }
4191 };
4192 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004193 return;
4194 }
4195
Winson Chungb745afb2015-03-02 11:51:23 -08004196 if (mAppsView != null) {
4197 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004198 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004199 }
4200
Sunny Goyal4390ace2014-10-13 11:33:11 -07004201 @Override
4202 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4203 Runnable r = new Runnable() {
4204 public void run() {
4205 bindWidgetsRestored(widgets);
4206 }
4207 };
4208 if (waitUntilResume(r)) {
4209 return;
4210 }
4211 mWorkspace.widgetsRestored(widgets);
4212 }
4213
Joe Onorato9c1289c2009-08-17 11:03:03 -04004214 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004215 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004216 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004217 *
4218 * @param updated list of shortcuts which have changed.
4219 * @param removed list of shortcuts which were deleted in the background. This can happen when
4220 * an app gets removed from the system or some of its components are no longer
4221 * available.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004222 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004223 @Override
4224 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4225 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004226 Runnable r = new Runnable() {
4227 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004228 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004229 }
4230 };
4231 if (waitUntilResume(r)) {
4232 return;
4233 }
4234
Sunny Goyal4390ace2014-10-13 11:33:11 -07004235 if (!updated.isEmpty()) {
4236 mWorkspace.updateShortcuts(updated);
4237 }
4238
4239 if (!removed.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004240 HashSet<ComponentName> removedComponents = new HashSet<>();
4241 HashSet<ShortcutKey> removedDeepShortcuts = new HashSet<>();
4242
Sunny Goyal4390ace2014-10-13 11:33:11 -07004243 for (ShortcutInfo si : removed) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004244 if (si.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
4245 removedDeepShortcuts.add(ShortcutKey.fromItemInfo(si));
4246 } else {
4247 removedComponents.add(si.getTargetComponent());
4248 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004249 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004250
4251 if (!removedComponents.isEmpty()) {
4252 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(removedComponents, user);
4253 mWorkspace.removeItemsByMatcher(matcher);
4254 mDragController.onAppsRemoved(matcher);
4255 }
4256
4257 if (!removedDeepShortcuts.isEmpty()) {
4258 ItemInfoMatcher matcher = ItemInfoMatcher.ofShortcutKeys(removedDeepShortcuts);
4259 mWorkspace.removeItemsByMatcher(matcher);
4260 mDragController.onAppsRemoved(matcher);
4261 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004262 }
4263 }
4264
4265 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004266 * Update the state of a package, typically related to install state.
4267 *
4268 * Implementation of the method from LauncherModel.Callbacks.
4269 */
Sunny Goyale755d462014-07-22 13:48:29 -07004270 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004271 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4272 Runnable r = new Runnable() {
4273 public void run() {
4274 bindRestoreItemsChange(updates);
4275 }
4276 };
4277 if (waitUntilResume(r)) {
4278 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004279 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004280
Sunny Goyal756adbc2015-04-16 15:20:51 -07004281 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004282 }
4283
4284 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004285 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004286 * in addition to specific applications to remove, the reason being that
4287 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004288 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004289 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004290 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004291 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004292 public void bindWorkspaceComponentsRemoved(
4293 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4294 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004295 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004296 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004297 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004298 }
Winson Chungd64d1762013-08-20 14:37:16 -07004299 };
4300 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004301 return;
4302 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004303 if (!packageNames.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004304 ItemInfoMatcher matcher = ItemInfoMatcher.ofPackages(packageNames, user);
4305 mWorkspace.removeItemsByMatcher(matcher);
4306 mDragController.onAppsRemoved(matcher);
4307
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004308 }
4309 if (!components.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004310 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(components, user);
4311 mWorkspace.removeItemsByMatcher(matcher);
4312 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004313 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004314 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004315
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004316 @Override
4317 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4318 Runnable r = new Runnable() {
4319 public void run() {
4320 bindAppInfosRemoved(appInfos);
4321 }
4322 };
4323 if (waitUntilResume(r)) {
4324 return;
Joe Onorato36115782010-06-17 13:28:48 -04004325 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004326
Winson Chungdf95eb12013-10-16 14:57:07 -07004327 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004328 if (mAppsView != null) {
4329 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004330 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004331 }
Joe Onoratobe386092009-11-17 17:32:16 -08004332
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004333 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004334 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004335 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004336 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004337 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004338
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004339 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004340 public void bindWidgetsModel(WidgetsModel model) {
4341 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004342 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004343 return;
4344 }
4345
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004346 if (mWidgetsView != null && model != null) {
4347 mWidgetsView.addWidgets(model);
4348 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004349 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004350 }
4351
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004352 @Override
4353 public void notifyWidgetProvidersChanged() {
4354 if (mWorkspace.getState().shouldUpdateWidget) {
4355 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4356 }
4357 }
4358
Winson Chung400438b2011-01-16 17:53:48 -08004359 private int mapConfigurationOriActivityInfoOri(int configOri) {
4360 final Display d = getWindowManager().getDefaultDisplay();
4361 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4362 switch (d.getRotation()) {
4363 case Surface.ROTATION_0:
4364 case Surface.ROTATION_180:
4365 // We are currently in the same basic orientation as the natural orientation
4366 naturalOri = configOri;
4367 break;
4368 case Surface.ROTATION_90:
4369 case Surface.ROTATION_270:
4370 // We are currently in the other basic orientation to the natural orientation
4371 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4372 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4373 break;
4374 }
4375
4376 int[] oriMap = {
4377 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4378 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4379 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4380 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4381 };
4382 // Since the map starts at portrait, we need to offset if this device's natural orientation
4383 // is landscape.
4384 int indexOffset = 0;
4385 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4386 indexOffset = 1;
4387 }
4388 return oriMap[(d.getRotation() + indexOffset) % 4];
4389 }
Adam Cohen446e9402011-09-15 18:21:21 -07004390
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004391 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004392 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004393 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004394 if (Utilities.ATLEAST_JB_MR2) {
4395 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4396 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004397 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4398 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004399 }
Winson Chung4b919f82012-05-01 10:44:08 -07004400 }
4401 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004402
Winson Chung4b919f82012-05-01 10:44:08 -07004403 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004404 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004405 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004406 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004407 } else {
4408 mHandler.postDelayed(new Runnable() {
4409 public void run() {
4410 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4411 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004412 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004413 }
Winson Chung4b919f82012-05-01 10:44:08 -07004414 }
Winson Chung400438b2011-01-16 17:53:48 -08004415 }
4416
Adam Cohenea90f832014-05-21 15:03:34 -07004417 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004418 * To be overridden by subclasses to indicate that there is an activity to launch
4419 * before showing the standard launcher experience.
4420 */
4421 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004422 if (mLauncherCallbacks != null) {
4423 return mLauncherCallbacks.hasFirstRunActivity();
4424 }
Adam Cohen432609a2014-03-13 17:03:22 -07004425 return false;
4426 }
4427
4428 /**
4429 * To be overridden by subclasses to launch any first run activity
4430 */
4431 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004432 if (mLauncherCallbacks != null) {
4433 return mLauncherCallbacks.getFirstRunActivity();
4434 }
Adam Cohen432609a2014-03-13 17:03:22 -07004435 return null;
4436 }
4437
Winson Chunga6945242014-01-08 14:04:34 -08004438 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004439 return !ActivityManager.isRunningInTestHarness() &&
4440 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004441 }
4442
Adam Cohen432609a2014-03-13 17:03:22 -07004443 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004444 if (shouldRunFirstRunActivity() &&
4445 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004446 Intent firstRunIntent = getFirstRunActivity();
4447 if (firstRunIntent != null) {
4448 startActivity(firstRunIntent);
4449 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004450 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004451 }
4452 }
Adam Cohen432609a2014-03-13 17:03:22 -07004453 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004454 }
4455
4456 private void markFirstRunActivityShown() {
4457 SharedPreferences.Editor editor = mSharedPrefs.edit();
4458 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4459 editor.apply();
4460 }
4461
Hyunyoung Songc001cf52016-07-21 17:32:43 -07004462 private void markAppsViewShown() {
4463 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4464 return;
4465 }
4466 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
4467 }
4468
4469 private boolean shouldShowDiscoveryBounce() {
4470 if (mState != mState.WORKSPACE) {
4471 return false;
4472 }
4473 if (mLauncherCallbacks != null && mLauncherCallbacks.shouldShowDiscoveryBounce()) {
4474 return true;
4475 }
4476 if (!mIsResumeFromActionScreenOff) {
4477 return false;
4478 }
4479 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4480 return false;
4481 }
4482 return true;
4483 }
4484
Adam Cohen432609a2014-03-13 17:03:22 -07004485 /**
4486 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4487 * screen that must be displayed and dismissed.
4488 */
4489 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004490 if (mLauncherCallbacks != null) {
4491 return mLauncherCallbacks.hasDismissableIntroScreen();
4492 }
Adam Cohen432609a2014-03-13 17:03:22 -07004493 return false;
4494 }
4495
4496 /**
4497 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4498 */
4499 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004500 if (mLauncherCallbacks != null) {
4501 return mLauncherCallbacks.getIntroScreen();
4502 }
Adam Cohen432609a2014-03-13 17:03:22 -07004503 return null;
4504 }
4505
4506 /**
4507 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4508 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4509 */
4510 private boolean shouldShowIntroScreen() {
4511 return hasDismissableIntroScreen() &&
4512 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4513 }
4514
4515 protected void showIntroScreen() {
4516 View introScreen = getIntroScreen();
4517 changeWallpaperVisiblity(false);
4518 if (introScreen != null) {
4519 mDragLayer.showOverlayView(introScreen);
4520 }
4521 }
4522
Winson Chung5ffd51d2015-06-25 11:36:50 -07004523 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004524 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004525 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004526 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004527 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004528 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004529 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4530 if (userHandle != null) {
4531 user = UserHandleCompat.fromUser(userHandle);
4532 }
4533 }
4534 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004535 }
4536
4537 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004538 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004539 if (user == null) {
4540 user = UserHandleCompat.myUserHandle();
4541 }
4542
4543 // Called from search suggestion, add the profile extra to the intent to ensure that we
4544 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004545 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004546 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004547 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004548 return null;
4549 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004550 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004551 }
4552
Winson Chung5ffd51d2015-06-25 11:36:50 -07004553 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004554 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4555 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004556 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004557 UserHandleCompat.myUserHandle());
4558 }
4559
Winson Chung5ffd51d2015-06-25 11:36:50 -07004560 protected void moveWorkspaceToDefaultScreen() {
4561 mWorkspace.moveToDefaultScreen(false);
4562 }
4563
Winson Chung80baf5a2010-08-09 16:03:15 -07004564 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004565 * Returns a FastBitmapDrawable with the icon, accurately sized.
4566 */
4567 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4568 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4569 d.setFilterBitmap(true);
4570 resizeIconDrawable(d);
4571 return d;
4572 }
4573
4574 /**
4575 * Resizes an icon drawable to the correct icon size.
4576 */
Winson5fbe0742015-09-30 15:33:00 -07004577 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004578 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004579 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004580 }
4581
4582 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004583 * Prints out out state for debugging.
4584 */
4585 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004586 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004587 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004588 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4589 Log.d(TAG, "mRestoring=" + mRestoring);
4590 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004591 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004592 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004593
Daniel Sandler325dc232013-06-05 22:57:57 -04004594 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004595 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004596
4597 @Override
4598 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4599 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004600 // Dump workspace
4601 writer.println(prefix + "Workspace Items");
4602 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4603 writer.println(prefix + " Homescreen " + i);
4604
4605 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4606 for (int j = 0; j < layout.getChildCount(); j++) {
4607 Object tag = layout.getChildAt(j).getTag();
4608 if (tag != null) {
4609 writer.println(prefix + " " + tag.toString());
4610 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004611 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004612 }
Sunny Goyala1365452015-10-01 15:46:24 -07004613
4614 writer.println(prefix + " Hotseat");
4615 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4616 for (int j = 0; j < layout.getChildCount(); j++) {
4617 Object tag = layout.getChildAt(j).getTag();
4618 if (tag != null) {
4619 writer.println(prefix + " " + tag.toString());
4620 }
4621 }
4622
Sunny Goyal713edfc2016-05-06 09:58:34 -07004623 try {
4624 FileLog.flushAll(writer);
4625 } catch (Exception e) {
4626 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004627 }
4628
Adam Cohen9211d422014-10-07 18:14:53 -07004629 if (mLauncherCallbacks != null) {
4630 mLauncherCallbacks.dump(prefix, fd, writer, args);
4631 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004632 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004633
Adam Cohen59400422014-03-05 18:07:04 -08004634 public static CustomAppWidget getCustomAppWidget(String name) {
4635 return sCustomAppWidgets.get(name);
4636 }
4637
4638 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4639 return sCustomAppWidgets;
4640 }
4641
Sunny Goyal29538332016-02-18 09:10:19 -08004642 public static List<View> getFolderContents(View icon) {
4643 if (icon instanceof FolderIcon) {
4644 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4645 } else {
4646 return Collections.EMPTY_LIST;
4647 }
4648 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004649
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004650 public static Launcher getLauncher(Context context) {
4651 if (context instanceof Launcher) {
4652 return (Launcher) context;
4653 }
4654 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4655 }
4656
Sunny Goyal745bad92016-05-02 10:54:12 -07004657 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4658
4659 @Override
4660 public void onSharedPreferenceChanged(
4661 SharedPreferences sharedPreferences, String key) {
4662 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4663 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4664 if (!waitUntilResume(this, true)) {
4665 run();
4666 }
4667 }
4668 }
4669
4670 @Override
4671 public void run() {
4672 setOrientation();
4673 }
4674 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004675}