blob: a31c8aeb357e8ad3ccca07c355110bf859e627ab [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;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700119import com.android.launcher3.util.ComponentKey;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700120import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700121import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal322d5562015-06-25 19:35:49 -0700122import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700123import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700124import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700125import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700126import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700127import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700128
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700129import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700130import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700131import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700132import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800133import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700134import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700135import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700136import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700137
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138/**
139 * Default launcher application.
140 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100141public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700142 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
143 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700144 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700145 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700146 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700148 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700149 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400150 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700151
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700153 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700154 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700155 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
Michael Jurka8b805b12012-04-18 14:23:14 -0700157 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700158 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700159 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700160
Sunny Goyal28c6b962015-10-12 11:42:05 -0700161 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
162
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700163 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
164 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
165 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
166
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700167 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
168
Mathew Inwood876a8462013-06-14 14:12:41 +0100169 /**
170 * IntentStarter uses request codes starting with this. This must be greater than all activity
171 * request codes used internally.
172 */
173 protected static final int REQUEST_LAST = 100;
174
Michael Jurka0a457bf2012-11-19 14:05:05 -0800175 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700176 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800177 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
Winson Chung2672ff92012-05-04 16:22:30 -0700179 // The Intent extra that defines whether to ignore the launch animation
180 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400181 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700182
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700183 public static final String ACTION_APPWIDGET_HOST_RESET =
184 "com.android.launcher3.intent.ACTION_APPWIDGET_HOST_RESET";
185
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 // Type: int
187 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700188 // Type: int
189 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700190 // Type: Content Values / parcelable
191 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700192 // Type: parcelable
193 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000194 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800195 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
Adam Cohen432609a2014-03-13 17:03:22 -0700197 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800198 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
199
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700200 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700201 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
202 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700203
Adam Cohen091440a2015-03-18 14:16:05 -0700204 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700205 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700206
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700207 private boolean mIsSafeModeEnabled;
208
Joe Onorato9c1289c2009-08-17 11:03:03 -0400209 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800210 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700211 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700212 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213
Winson Chunga2413752012-04-03 14:22:34 -0700214 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700215 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
216 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700217 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700218
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700219 private final BroadcastReceiver mUiBroadcastReceiver = new BroadcastReceiver() {
220
221 @Override
222 public void onReceive(Context context, Intent intent) {
223 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
224 closeSystemDialogs();
225 } else if (ACTION_APPWIDGET_HOST_RESET.equals(intent.getAction())) {
226 if (mAppWidgetHost != null) {
227 mAppWidgetHost.startListening();
228 }
229 }
230 }
231 };
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800232
Adam Cohen091440a2015-03-18 14:16:05 -0700233 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700234 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700235 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800236 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700237
238 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700239
Sunny Goyalffe83f12014-08-14 17:39:34 -0700240 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700241 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700242
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700243 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800244 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800245 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700246
Michael Jurka0280c3b2010-09-17 15:00:07 -0700247 private int[] mTmpAddItemCellCoordinates = new int[2];
248
Sunny Goyal316490e2015-06-02 09:38:28 -0700249 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800250 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700251
Michael Jurka838a4ca2011-02-07 13:33:06 -0800252 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700253 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700254
Sunny Goyal47328fd2016-05-25 18:56:41 -0700255 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700256
257 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700258 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700259 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700260
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700261 // Main container view and the model for the widget tray screen.
262 @Thunk WidgetsContainerView mWidgetsView;
263 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700264
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700266 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
267 // scroll issues (because the workspace may not have been measured yet) and extra work.
268 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
269 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270
271 private SpannableStringBuilder mDefaultKeySsb = null;
272
Adam Cohen091440a2015-03-18 14:16:05 -0700273 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400274
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800275 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 private boolean mRestoring;
277 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700278 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279
Michael Jurka1e2f4652013-07-08 18:03:46 -0700280 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700281 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700282 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700283
Joe Onorato9c1289c2009-08-17 11:03:03 -0400284 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800285 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700286 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700287 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Adam Cohen091440a2015-03-18 14:16:05 -0700288 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800289 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700290 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800291 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400292
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700293 /** Maps launcher activity components to their list of shortcut ids. */
294 private MultiHashMap<ComponentKey, String> mDeepShortcutMap = new MultiHashMap<>();
295
Sunny Goyal639e9062015-08-19 19:17:06 -0700296 private LauncherClings mClings;
297
Adam Cohen61f560d2013-09-30 15:58:20 -0700298 private View.OnTouchListener mHapticFeedbackTouchListener;
299
Adam Cohended9f8d2010-11-03 13:25:16 -0700300 // Related to the auto-advancing of widgets
301 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700302 private static final int ADVANCE_INTERVAL = 20000;
303 private static final int ADVANCE_STAGGER = 250;
304
305 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700306 private long mAutoAdvanceSentTime;
307 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700308 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700309
Winson Chung400438b2011-01-16 17:53:48 -0800310 // Determines how long to wait after a rotation before restoring the screen orientation to
311 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700312 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800313
Adam Cohen091440a2015-03-18 14:16:05 -0700314 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700315
Adam Cohen1462de32012-07-24 22:34:36 -0700316 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700317 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700318
Winson Chung46353de2012-02-16 14:05:10 -0800319 // We only want to get the SharedPreferences once since it does an FS stat each time we get
320 // it from the context.
321 private SharedPreferences mSharedPrefs;
322
Winson Chungf0c6ae02012-03-21 16:10:31 -0700323 // Holds the page that we need to animate to, and the icon views that we need to animate up
324 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700325 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700326 private Bitmap mFolderIconBitmap;
327 private Canvas mFolderIconCanvas;
328 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700329
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700330 private DeviceProfile mDeviceProfile;
331
Adam Cohen4b66bf32015-09-18 12:15:19 -0700332 private boolean mMoveToDefaultScreenFromNewIntent;
333
Winson Chung13eb5272015-05-11 16:30:13 -0700334 // This is set to the view that launched the activity that navigated the user away from
335 // launcher. Since there is no callback for when the activity has finished launching, enable
336 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800337 private BubbleTextView mWaitingForResume;
338
Adam Cohen59400422014-03-05 18:07:04 -0800339 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
340 new HashMap<String, CustomAppWidget>();
341
Adam Cohen59400422014-03-05 18:07:04 -0800342 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700343 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
344 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800345 }
346 }
347
Adam Cohen091440a2015-03-18 14:16:05 -0700348 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700349 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700350 if (mWorkspace != null) {
351 mWorkspace.buildPageHardwareLayers();
352 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700353 }
354 };
355
Adam Cohendb364c32014-05-20 14:23:40 -0700356 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800357
Adam Cohen091440a2015-03-18 14:16:05 -0700358 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800359 int requestCode;
360 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700361 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700362 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800363 int cellX;
364 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700365 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800366 }
367
Hyunyoung Songaa953652016-04-19 18:30:24 -0700368 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800369
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700370 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700371 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400372
373 @Thunk void setOrientation() {
374 if (mRotationEnabled) {
375 unlockScreenOrientation(true);
376 } else {
377 setRequestedOrientation(
378 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700379 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400380 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700381
Sunny Goyal745bad92016-05-02 10:54:12 -0700382 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700383
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 @Override
385 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700386 if (DEBUG_STRICT_MODE) {
387 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
388 .detectDiskReads()
389 .detectDiskWrites()
390 .detectNetwork() // or .detectAll() for all detectable problems
391 .penaltyLog()
392 .build());
393 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
394 .detectLeakedSqlLiteObjects()
395 .detectLeakedClosableObjects()
396 .penaltyLog()
397 .penaltyDeath()
398 .build());
399 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700400 if (LauncherAppState.PROFILE_STARTUP) {
401 Trace.beginSection("Launcher-onCreate");
402 }
Winson Chunga2413752012-04-03 14:22:34 -0700403
Adam Cohen9211d422014-10-07 18:14:53 -0700404 if (mLauncherCallbacks != null) {
405 mLauncherCallbacks.preOnCreate();
406 }
407
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400409
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400410 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700411
Adam Cohen2e6da152015-05-06 11:42:25 -0700412 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700413 mDeviceProfile = getResources().getConfiguration().orientation
414 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700415 app.getInvariantDeviceProfile().landscapeProfile
416 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700417
Sunny Goyalf7258242015-10-19 16:59:07 -0700418 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700419 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800420 mModel = app.setLauncher(this);
421 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700422 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700423
Joe Onorato41a12d22009-10-31 18:30:00 -0400424 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700425 mAllAppsController = new AllAppsTransitionController(this);
426 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700427
Sunny Goyalffe83f12014-08-14 17:39:34 -0700428 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700429
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700430 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
431 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700432
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700433 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
434 // this also ensures that any synchronous binding below doesn't re-trigger another
435 // LauncherModel load.
436 mPaused = false;
437
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700439
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700441 mDeviceProfile.layout(this);
Tony Wickham827cef22016-03-17 15:39:39 -0700442 mExtractedColors = new ExtractedColors();
443 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444
Tony Wickhame2217252016-03-22 16:34:23 -0700445 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
446 .addAccessibilityStateChangeListener(this);
447
Joe Onorato7c312c12009-08-13 21:36:53 -0700448 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700449
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 mSavedState = savedInstanceState;
451 restoreState(mSavedState);
452
Sunny Goyale26d1002016-06-20 14:52:14 -0700453 if (LauncherAppState.PROFILE_STARTUP) {
454 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 }
456
457 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700458 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700459 // If the user leaves launcher, then we should just load items asynchronously when
460 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700461 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700462 } else {
463 // We only load the page synchronously if the user rotates (or triggers a
464 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700465 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700466 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467 }
468
469 // For handling default keys
470 mDefaultKeySsb = new SpannableStringBuilder();
471 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800472
473 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700474 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
475 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800476
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800477 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700478 // In case we are on a device with locked rotation, we should look at preferences to check
479 // if the user has specifically allowed rotation.
480 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700481 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700482 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
483 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700484 }
485
486 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
487 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400488 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700489
Adam Cohen9211d422014-10-07 18:14:53 -0700490 if (mLauncherCallbacks != null) {
491 mLauncherCallbacks.onCreate(savedInstanceState);
492 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700493
494 if (shouldShowIntroScreen()) {
495 showIntroScreen();
496 } else {
497 showFirstRunActivity();
498 showFirstRunClings();
499 }
Adam Cohen9211d422014-10-07 18:14:53 -0700500 }
501
Sunny Goyal7779d622015-06-11 16:18:39 -0700502 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700503 public void onExtractedColorsChanged() {
504 loadExtractedColorsAndColorItems();
505 }
506
507 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700508 // TODO: do this in pre-N as well, once the extraction part is complete.
509 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700510 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700511 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700512 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham827cef22016-03-17 15:39:39 -0700513 }
514 }
515
Adam Cohen9211d422014-10-07 18:14:53 -0700516 private LauncherCallbacks mLauncherCallbacks;
517
518 public void onPostCreate(Bundle savedInstanceState) {
519 super.onPostCreate(savedInstanceState);
520 if (mLauncherCallbacks != null) {
521 mLauncherCallbacks.onPostCreate(savedInstanceState);
522 }
523 }
524
Sunny Goyal32554d12015-12-03 15:31:25 -0800525 /**
526 * Call this after onCreate to set or clear overlay.
527 */
528 public void setLauncherOverlay(LauncherOverlay overlay) {
529 if (overlay != null) {
530 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
531 }
532 mWorkspace.setLauncherOverlay(overlay);
533 }
534
Adam Cohen9211d422014-10-07 18:14:53 -0700535 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
536 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700537 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700538 private boolean mWorkspaceImportanceStored = false;
539 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700540 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800541 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700542 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
543
544 @Override
545 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700546 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700547 return;
548 }
549 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700550 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700551 if (mWorkspace != null) {
552 mWorkspaceImportanceForAccessibility =
553 mWorkspace.getImportantForAccessibility();
554 mWorkspace.setImportantForAccessibility(
555 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
556 mWorkspaceImportanceStored = true;
557 }
558 if (mHotseat != null) {
559 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
560 mHotseat.setImportantForAccessibility(
561 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
562 mHotseatImportanceStored = true;
563 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700564 }
565
566 @Override
567 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700568 if (mWorkspaceImportanceStored && mWorkspace != null) {
569 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
570 }
571 if (mHotseatImportanceStored && mHotseat != null) {
572 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
573 }
574 mWorkspaceImportanceStored = false;
575 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700576 }
577 });
Adam Cohen9211d422014-10-07 18:14:53 -0700578 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700579 }
580
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700581 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700582 public void onLauncherProviderChange() {
583 if (mLauncherCallbacks != null) {
584 mLauncherCallbacks.onLauncherProviderChange();
585 }
586 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700587
Adam Cohen9211d422014-10-07 18:14:53 -0700588 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700589 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700590 if (mLauncherCallbacks != null) {
591 return mLauncherCallbacks.hasCustomContentToLeft();
592 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700593 return false;
594 }
595
Dave Hawkeya8881582013-09-17 15:55:33 -0600596 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500597 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600598 * {@link #addToCustomContentPage}. This will only be invoked if
599 * {@link #hasCustomContentToLeft()} is {@code true}.
600 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500601 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700602 if (mLauncherCallbacks != null) {
603 mLauncherCallbacks.populateCustomContentContainer();
604 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600605 }
606
607 /**
608 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
609 * ensure the custom content page is added or removed if necessary.
610 */
611 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600612 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600613 // Not bound yet, wait for bindScreens to be called.
614 return;
615 }
616
617 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
618 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500619 mWorkspace.createCustomContentContainer();
620 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600621 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
622 mWorkspace.removeCustomContentPage();
623 }
624 }
625
Hyunyoung Songaa953652016-04-19 18:30:24 -0700626 public UserEventDispatcher getUserEventDispatcher() {
627 if (mLauncherCallbacks != null) {
628 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
629 if (dispatcher != null) {
630 return dispatcher;
631 }
632 }
633
Sunny Goyal64976d52016-06-20 14:56:28 -0700634 // Logger object is a singleton and does not have to be coupled with the foreground
635 // activity. Since most user event logging is done on the UI, the object is retrieved
636 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700637 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700638 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700639 }
640 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800641 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100642
Hyunyoung Song3f471442015-04-08 19:01:34 -0700643 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700644 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
645 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700646 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700647 }
648
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000649 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700650 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
651 // This cast is safe as long as the id < 0x00FFFFFF
652 // Since we jail all the dynamically generated views, there should be no clashes
653 // with any other views.
654 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000655 }
656
657 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700658 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
659 * a configuration step, this allows the proper animations to run after other transitions.
660 */
Adam Cohendb364c32014-05-20 14:23:40 -0700661 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700662 long screenId = args.screenId;
663 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
664 // When the screen id represents an actual screen (as opposed to a rank) we make sure
665 // that the drop page actually exists.
666 screenId = ensurePendingDropLayoutExists(args.screenId);
667 }
Adam Cohendb364c32014-05-20 14:23:40 -0700668
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800669 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800670 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700671 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700672 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700673 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800674 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700675 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700676 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700677 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyald478c832016-04-01 12:04:16 -0700678 completeRestoreAppWidget(args.appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700679 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700680 case REQUEST_BIND_PENDING_APPWIDGET: {
681 int widgetId = args.appWidgetId;
682 LauncherAppWidgetInfo info =
683 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
684 if (info != null) {
685 // Since the view was just bound, also launch the configure activity if needed
686 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
687 .getLauncherAppWidgetInfo(widgetId);
688 if (provider != null && provider.configure != null) {
689 startRestoredWidgetReconfigActivity(provider, info);
690 }
691 }
692 break;
693 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800694 }
Michael Jurka27614d22012-04-02 06:40:22 -0700695 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
696 // if you turned the screen off and then back while in All Apps, Launcher would not
697 // return to the workspace. Clearing mAddInfo.container here fixes this issue
698 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700699 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800700 }
701
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800702 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700703 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700704 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700705 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700706 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800707 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700708
Adam Cohenad4e15c2013-10-17 16:21:35 -0700709 Runnable exitSpringLoaded = new Runnable() {
710 @Override
711 public void run() {
712 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
713 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
714 }
715 };
716
Michael Jurka8b805b12012-04-18 14:23:14 -0700717 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700718 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700719 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700720 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
721 if (resultCode == RESULT_CANCELED) {
722 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700723 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700724 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700725 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800726 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700727 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700728 }
729 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200730 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
731 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700732 // User could have free-scrolled between pages before picking a wallpaper; make sure
733 // we move to the closest one now.
734 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700735 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200736 }
737 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700738 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200739
Adam Cohened66b2b2012-01-23 17:28:51 -0800740 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700741 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700742
Adam Cohendb364c32014-05-20 14:23:40 -0700743 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800744 // We have special handling for widgets
745 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800746 final int appWidgetId;
747 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
748 : -1;
749 if (widgetId < 0) {
750 appWidgetId = pendingAddWidgetId;
751 } else {
752 appWidgetId = widgetId;
753 }
754
Adam Cohenad4e15c2013-10-17 16:21:35 -0700755 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800756 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700757 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
758 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700759 result = RESULT_CANCELED;
760 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700761 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700762 @Override
763 public void run() {
764 exitSpringLoadedDragModeDelayed(false, 0, null);
765 }
766 };
Adam Cohendb364c32014-05-20 14:23:40 -0700767 if (workspaceLocked) {
768 // No need to remove the empty screen if we're mid-binding, as the
769 // the bind will not add the empty screen.
770 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
771 } else {
772 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
773 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
774 }
Winson Chung5aaab772012-04-27 15:24:02 -0700775 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700776 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700777 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
778 // When the screen id represents an actual screen (as opposed to a rank)
779 // we make sure that the drop page actually exists.
780 mPendingAddInfo.screenId =
781 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
782 }
Adam Cohendb364c32014-05-20 14:23:40 -0700783 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
784
785 dropLayout.setDropPending(true);
786 final Runnable onComplete = new Runnable() {
787 @Override
788 public void run() {
789 completeTwoStageWidgetDrop(resultCode, appWidgetId);
790 dropLayout.setDropPending(false);
791 }
792 };
793 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
794 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
795 } else {
796 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
797 mPendingAddInfo);
798 sPendingAddItem = args;
799 }
Winson Chung5aaab772012-04-27 15:24:02 -0700800 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800801 return;
802 }
803
Sunny Goyald478c832016-04-01 12:04:16 -0700804 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
805 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700806 if (resultCode == RESULT_OK) {
807 // Update the widget view.
808 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
809 pendingAddWidgetId, mPendingAddInfo);
810 if (workspaceLocked) {
811 sPendingAddItem = args;
812 } else {
813 completeAdd(args);
814 }
815 }
816 // Leave the widget in the pending state if the user canceled the configure.
817 return;
818 }
819
Sunny Goyalaad90582015-07-09 14:22:50 -0700820 if (requestCode == REQUEST_CREATE_SHORTCUT) {
821 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
822 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
823 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
824 mPendingAddInfo);
825 if (isWorkspaceLocked()) {
826 sPendingAddItem = args;
827 } else {
828 completeAdd(args);
829 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
830 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
831 }
832 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700833 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
834 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800837 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800838
839 }
840
841 @Override
842 protected void onActivityResult(
843 final int requestCode, final int resultCode, final Intent data) {
844 handleActivityResult(requestCode, resultCode, data);
845 if (mLauncherCallbacks != null) {
846 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
847 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800848 }
849
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600850 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700851 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600852 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700853 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
854 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
855 View v = null;
856 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
857 if (layout != null) {
858 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
859 }
860 Intent intent = sPendingAddItem.intent;
861 sPendingAddItem = null;
862 if (grantResults.length > 0
863 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700864 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700865 } else {
866 // TODO: Show a snack bar with link to settings
867 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
868 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
869 }
870 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600871 if (mLauncherCallbacks != null) {
872 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
873 grantResults);
874 }
875 }
876
Adam Cohendb364c32014-05-20 14:23:40 -0700877 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
878 appWidgetId, ItemInfo info) {
879 PendingAddArguments args = new PendingAddArguments();
880 args.requestCode = requestCode;
881 args.intent = data;
882 args.container = info.container;
883 args.screenId = info.screenId;
884 args.cellX = info.cellX;
885 args.cellY = info.cellY;
886 args.appWidgetId = appWidgetId;
887 return args;
888 }
889
890 /**
891 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
892 *
893 * @param screenId the screen id to check
894 * @return the new screen, or screenId if it exists
895 */
896 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800897 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700898 if (dropLayout == null) {
899 // it's possible that the add screen was removed because it was
900 // empty and a re-bind occurred
901 mWorkspace.addExtraEmptyScreen();
902 return mWorkspace.commitExtraEmptyScreen();
903 } else {
904 return screenId;
905 }
906 }
907
Adam Cohen091440a2015-03-18 14:16:05 -0700908 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800909 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800910 Runnable onCompleteRunnable = null;
911 int animationType = 0;
912
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700913 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800914 if (resultCode == RESULT_OK) {
915 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
916 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700917 mPendingAddWidgetInfo);
918 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800919 onCompleteRunnable = new Runnable() {
920 @Override
921 public void run() {
922 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700923 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700924 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
925 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800926 }
927 };
928 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800929 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800930 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800931 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700932 if (mDragLayer.getAnimatedView() != null) {
933 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
934 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
935 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700936 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700937 // The animated view may be null in the case of a rotation during widget configuration
938 onCompleteRunnable.run();
939 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 }
941
942 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700943 protected void onStop() {
944 super.onStop();
945 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700946
947 if (mLauncherCallbacks != null) {
948 mLauncherCallbacks.onStop();
949 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700950 }
951
952 @Override
953 protected void onStart() {
954 super.onStart();
955 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700956
957 if (mLauncherCallbacks != null) {
958 mLauncherCallbacks.onStart();
959 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700960 }
961
962 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200964 long startTime = 0;
965 if (DEBUG_RESUME_TIME) {
966 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400967 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200968 }
Adam Cohen9211d422014-10-07 18:14:53 -0700969
970 if (mLauncherCallbacks != null) {
971 mLauncherCallbacks.preOnResume();
972 }
973
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700975 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700976
Winson Chung4a2afa32012-07-19 14:53:05 -0700977 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700978 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700979 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800980 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700981 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700982 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700983 // view after launching an app, as they may be depending on the UI to be static to
984 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700985 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700986 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800987 } else if (mOnResumeState == State.WIDGETS) {
988 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700989 }
990 mOnResumeState = State.NONE;
991
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700992 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700993 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
994 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700995
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800996 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700997 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700998 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700999 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001000 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001001 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001003 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001004 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1005 // execute them here
1006 long startTimeCallbacks = 0;
1007 if (DEBUG_RESUME_TIME) {
1008 startTimeCallbacks = System.currentTimeMillis();
1009 }
1010
Michael Jurka1e2f4652013-07-08 18:03:46 -07001011 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1012 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001013 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001014 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001015 if (DEBUG_RESUME_TIME) {
1016 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1017 (System.currentTimeMillis() - startTimeCallbacks));
1018 }
Michael Jurka447bf842013-05-15 14:52:15 +02001019 }
Michael Jurka54554252013-08-01 12:52:23 +02001020 if (mOnResumeCallbacks.size() > 0) {
1021 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1022 mOnResumeCallbacks.get(i).run();
1023 }
1024 mOnResumeCallbacks.clear();
1025 }
Winson Chunge4e50662012-01-23 14:45:13 -08001026
1027 // Reset the pressed state of icons that were locked in the press state while activities
1028 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001029 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001030 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001031 mWaitingForResume.setStayPressed(false);
1032 }
Winson Chung82963d52013-10-09 11:20:57 -07001033
Adam Cohen06dff352012-06-01 17:17:08 -07001034 // It is possible that widgets can receive updates while launcher is not in the foreground.
1035 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1036 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1037 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001038 if (!isWorkspaceLoading()) {
1039 getWorkspace().reinflateWidgetsIfNecessary();
1040 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001041
Michael Jurka447bf842013-05-15 14:52:15 +02001042 if (DEBUG_RESUME_TIME) {
1043 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1044 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001045
Adam Cohen4b66bf32015-09-18 12:15:19 -07001046 // We want to suppress callbacks about CustomContent being shown if we have just received
1047 // onNewIntent while the user was present within launcher. In that case, we post a call
1048 // to move the user to the main screen (which will occur after onResume). We don't want to
1049 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1050 // suppress here.
1051 if (mWorkspace.getCustomContentCallbacks() != null
1052 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001053 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001054 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001055 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1056 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001057 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001058 }
1059 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001060 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001061 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001062 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001063
Sunny Goyal756adbc2015-04-16 15:20:51 -07001064 if (!isWorkspaceLoading()) {
1065 // Process any items that were added while Launcher was away.
1066 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1067 }
Adam Cohen9211d422014-10-07 18:14:53 -07001068
1069 if (mLauncherCallbacks != null) {
1070 mLauncherCallbacks.onResume();
1071 }
Adam Cohen06dff352012-06-01 17:17:08 -07001072 }
1073
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001075 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001076 // Ensure that items added to Launcher are queued until Launcher returns
1077 InstallShortcutReceiver.enableInstallQueue();
1078
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001079 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001080 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001081 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001082 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001083
1084 // We call onHide() aggressively. The custom content callbacks should be able to
1085 // debounce excess onHide calls.
1086 if (mWorkspace.getCustomContentCallbacks() != null) {
1087 mWorkspace.getCustomContentCallbacks().onHide();
1088 }
Romain Guycbb89e42009-06-08 15:52:54 -07001089
Adam Cohen9211d422014-10-07 18:14:53 -07001090 if (mLauncherCallbacks != null) {
1091 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001092 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001093 }
1094
1095 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001096 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1097 // by a onResume or by scrolling otherwise.
1098 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001099
1100 // Custom content is completely hidden
1101 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001102
1103 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1104 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001105
1106 // Indicates whether the user is allowed to scroll away from the custom content.
1107 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001108 }
1109
Adam Cohenc2d6e892014-10-16 09:49:24 -07001110 public interface LauncherOverlay {
1111
1112 /**
1113 * Touch interaction leading to overscroll has begun
1114 */
1115 public void onScrollInteractionBegin();
1116
1117 /**
1118 * Touch interaction related to overscroll has ended
1119 */
1120 public void onScrollInteractionEnd();
1121
1122 /**
1123 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1124 * screen (or in the case of RTL, the rightmost screen).
1125 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001126 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001127
1128 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001129 * Called when the launcher is ready to use the overlay
1130 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001131 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001132 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001133 }
1134
Jun Mukai8af0cd82015-05-12 12:32:12 -07001135 public interface LauncherSearchCallbacks {
1136 /**
1137 * Called when the search overlay is shown.
1138 */
1139 public void onSearchOverlayOpened();
1140
1141 /**
1142 * Called when the search overlay is dismissed.
1143 */
1144 public void onSearchOverlayClosed();
1145 }
1146
Adam Cohenc2d6e892014-10-16 09:49:24 -07001147 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001148 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001149 }
1150
1151 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1152
Sunny Goyalc86df472016-02-25 09:19:38 -08001153 public void onScrollChanged(float progress) {
1154 if (mWorkspace != null) {
1155 mWorkspace.onOverlayScrollChanged(progress);
1156 }
1157 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001158 }
1159
Jorim Jaggid017f882014-01-14 17:08:48 -08001160 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001161 if (mLauncherCallbacks != null) {
1162 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001163 } else {
1164 // On devices with a locked orientation, we will at least have the allow rotation
1165 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001166 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001167 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001168 }
1169
Adam Cohen9211d422014-10-07 18:14:53 -07001170 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001171 CustomContentCallbacks callbacks, String description) {
1172 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001173 }
1174
Adam Cohenedb40762013-07-18 16:45:45 -07001175 // The custom content needs to offset its content to account for the QSB
1176 public int getTopOffsetForCustomContent() {
1177 return mWorkspace.getPaddingTop();
1178 }
1179
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001180 @Override
1181 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001182 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001183 if (mModel.isCurrentCallbacks(this)) {
1184 mModel.stopLoader();
1185 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001186 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1187
Romain Guy13c2e7b2010-03-10 19:45:00 -08001188 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001189 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001190
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001191 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001192 @Override
1193 public void onWindowFocusChanged(boolean hasFocus) {
1194 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001195 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001196
1197 if (mLauncherCallbacks != null) {
1198 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1199 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001200 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001201
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 private boolean acceptFilter() {
1203 final InputMethodManager inputManager = (InputMethodManager)
1204 getSystemService(Context.INPUT_METHOD_SERVICE);
1205 return !inputManager.isFullscreenMode();
1206 }
1207
1208 @Override
1209 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001210 final int uniChar = event.getUnicodeChar();
1211 final boolean handled = super.onKeyDown(keyCode, event);
1212 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1213 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001214 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1215 keyCode, event);
1216 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001217 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001218 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001219 // showSearchDialog()
1220 // If there are multiple keystrokes before the search dialog takes focus,
1221 // onSearchRequested() will be called for every keystroke,
1222 // but it is idempotent, so it's fine.
1223 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 }
1225 }
1226
Joe Onorato8a9625e2010-01-28 15:55:35 -08001227 // Eat the long press event so the keyboard doesn't come up.
1228 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1229 return true;
1230 }
1231
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 return handled;
1233 }
1234
Winson46163472015-09-22 17:31:56 -07001235 @Override
1236 public boolean onKeyUp(int keyCode, KeyEvent event) {
1237 if (keyCode == KeyEvent.KEYCODE_MENU) {
1238 // Ignore the menu key if we are currently dragging or are on the custom content screen
1239 if (!isOnCustomContent() && !mDragController.isDragging()) {
1240 // Close any open folders
1241 closeFolder();
1242
1243 // Stop resizing any widgets
1244 mWorkspace.exitWidgetResizeMode();
1245
1246 // Show the overview mode if we are on the workspace
1247 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1248 !mWorkspace.isSwitchingState()) {
1249 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001250 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001251 }
1252 }
1253 return true;
1254 }
1255 return super.onKeyUp(keyCode, event);
1256 }
1257
Karl Rosaen138a0412009-04-23 19:00:21 -07001258 private String getTypedText() {
1259 return mDefaultKeySsb.toString();
1260 }
1261
1262 private void clearTypedText() {
1263 mDefaultKeySsb.clear();
1264 mDefaultKeySsb.clearSpans();
1265 Selection.setSelection(mDefaultKeySsb, 0);
1266 }
1267
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001269 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1270 * State
1271 */
1272 private static State intToState(int stateOrdinal) {
1273 State state = State.WORKSPACE;
1274 final State[] stateValues = State.values();
1275 for (int i = 0; i < stateValues.length; i++) {
1276 if (stateValues[i].ordinal() == stateOrdinal) {
1277 state = stateValues[i];
1278 break;
1279 }
1280 }
1281 return state;
1282 }
1283
1284 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 * Restores the previous state, if it exists.
1286 *
1287 * @param savedState The previous state.
1288 */
1289 private void restoreState(Bundle savedState) {
1290 if (savedState == null) {
1291 return;
1292 }
1293
Michael Jurka883f55b2010-10-21 15:47:14 -07001294 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001295 if (state == State.APPS || state == State.WIDGETS) {
1296 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001297 }
1298
Adam Cohen21cd0022013-10-09 18:57:02 -07001299 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1300 PagedView.INVALID_RESTORE_PAGE);
1301 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001302 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 }
1304
Sunny Goyal756cd262015-08-20 12:33:21 -07001305 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1306 if (itemValues != null) {
1307 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001308 AppWidgetProviderInfo info = savedState.getParcelable(
1309 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001310 mPendingAddWidgetInfo = info == null ?
1311 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1312
Adam Cohen4637b5a2013-11-04 18:21:24 -08001313 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001314 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 mRestoring = true;
1316 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 }
1318
1319 /**
1320 * Finds all the views we need and configure them properly.
1321 */
1322 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001323 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001324 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001325 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001326 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001327 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001328
Sunny Goyal784f9c32016-03-23 16:56:54 -07001329 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1330 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1331 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001332 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333
Winson Chung4c98d922011-05-31 16:50:48 -07001334 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001335
1336 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001337
Winson Chung3d503fb2011-07-13 17:25:49 -07001338 // Setup the hotseat
1339 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1340 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001341 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001342 }
1343
Winsone9f27272015-10-13 10:47:51 -07001344 // Setup the overview panel
1345 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001346
Winson Chung4c98d922011-05-31 16:50:48 -07001347 // Setup the workspace
1348 mWorkspace.setHapticFeedbackEnabled(false);
1349 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001350 mWorkspace.setup(mDragController);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001351 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001352 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001353
Tony Wickham34d2c912015-09-11 09:27:58 -07001354 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001355 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001356
Winson Chungef7f8742015-06-04 17:18:17 -07001357 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001358 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001359 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001360 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1361 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1362 } else {
1363 mAppsView.setSearchBarController(new DefaultAppSearchController());
1364 }
Craig Mautner360310b2012-10-26 15:13:08 -07001365
Winson Chung3d503fb2011-07-13 17:25:49 -07001366 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001367 mDragController.setDragScoller(mWorkspace);
1368 mDragController.setScrollView(mDragLayer);
1369 mDragController.setMoveTarget(mWorkspace);
1370 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001371 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001372
Peter Schiller310a9882016-06-30 13:52:36 -07001373 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1374 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1375 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001376
Sunny Goyal322d5562015-06-25 19:35:49 -07001377 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1378 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001379 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 }
1381
Winsone9f27272015-10-13 10:47:51 -07001382 private void setupOverviewPanel() {
1383 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1384
1385 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1386 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1387 @Override
1388 public boolean onLongClick(View v) {
1389 return v.performClick();
1390 }
1391 };
1392
1393 // Bind wallpaper button actions
1394 View wallpaperButton = findViewById(R.id.wallpaper_button);
1395 wallpaperButton.setOnClickListener(new OnClickListener() {
1396 @Override
1397 public void onClick(View view) {
1398 if (!mWorkspace.isSwitchingState()) {
1399 onClickWallpaperPicker(view);
1400 }
1401 }
1402 });
1403 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1404 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1405
1406 // Bind widget button actions
1407 mWidgetsButton = findViewById(R.id.widget_button);
1408 mWidgetsButton.setOnClickListener(new OnClickListener() {
1409 @Override
1410 public void onClick(View view) {
1411 if (!mWorkspace.isSwitchingState()) {
1412 onClickAddWidgetButton(view);
1413 }
1414 }
1415 });
1416 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1417 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1418
1419 // Bind settings actions
1420 View settingsButton = findViewById(R.id.settings_button);
1421 boolean hasSettings = hasSettings();
1422 if (hasSettings) {
1423 settingsButton.setOnClickListener(new OnClickListener() {
1424 @Override
1425 public void onClick(View view) {
1426 if (!mWorkspace.isSwitchingState()) {
1427 onClickSettingsButton(view);
1428 }
1429 }
1430 });
1431 settingsButton.setOnLongClickListener(performClickOnLongClick);
1432 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1433 } else {
1434 settingsButton.setVisibility(View.GONE);
1435 }
1436
1437 mOverviewPanel.setAlpha(0f);
1438 }
1439
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001441 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001442 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001443 */
1444 public void setAllAppsButton(View allAppsButton) {
1445 mAllAppsButton = allAppsButton;
1446 }
1447
Sunny Goyalbb011da2016-06-15 15:42:29 -07001448 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001449 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001450 }
1451
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001452 public View getWidgetsButton() {
1453 return mWidgetsButton;
1454 }
1455
Anjali Koppal5ad44842014-03-10 20:34:39 -07001456 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 * Creates a view representing a shortcut.
1458 *
1459 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001461 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001462 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 }
1464
1465 /**
1466 * Creates a view representing a shortcut inflated from the specified resource.
1467 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 * @param parent The group the shortcut belongs to.
1469 * @param info The data structure describing the shortcut.
1470 *
1471 * @return A View inflated from layoutResId.
1472 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001473 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001474 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001475 parent, false);
1476 favorite.applyFromShortcutInfo(info, mIconCache);
1477 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001479 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 return favorite;
1481 }
1482
1483 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 * Add a shortcut to the workspace.
1485 *
1486 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001488 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001489 int cellY) {
1490 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001491 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001492
Sunny Goyal5c97f512015-05-19 16:03:28 -07001493 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001494 if (info == null) {
1495 return;
1496 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001497 final View view = createShortcut(info);
1498
Sunny Goyal5c97f512015-05-19 16:03:28 -07001499 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001500 // First we check if we already know the exact location where we want to add this item.
1501 if (cellX >= 0 && cellY >= 0) {
1502 cellXY[0] = cellX;
1503 cellXY[1] = cellY;
1504 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001505
1506 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001507 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001508 true, null,null)) {
1509 return;
1510 }
1511 DragObject dragObject = new DragObject();
1512 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001513 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1514 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001515 return;
1516 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001517 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001518 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001519 }
1520
1521 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001522 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001523 return;
1524 }
1525
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001526 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527
1528 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001529 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001530 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 }
1532 }
1533
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001535 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001537 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 */
Adam Cohen091440a2015-03-18 14:16:05 -07001539 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001540 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1541
1542 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001543 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001544 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001545 }
Romain Guycbb89e42009-06-08 15:52:54 -07001546
Adam Cohen59400422014-03-05 18:07:04 -08001547 if (appWidgetInfo.isCustomWidget) {
1548 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001549 }
1550
Adam Cohen59400422014-03-05 18:07:04 -08001551 LauncherAppWidgetInfo launcherInfo;
1552 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1553 launcherInfo.spanX = info.spanX;
1554 launcherInfo.spanY = info.spanY;
1555 launcherInfo.minSpanX = info.minSpanX;
1556 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001557 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001558
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001560 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561
1562 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001563 if (hostView == null) {
1564 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001565 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001566 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001567 hostView.setVisibility(View.VISIBLE);
1568 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001570 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 }
Romain Guycbb89e42009-06-08 15:52:54 -07001572
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001573 private void addAppWidgetToWorkspace(
1574 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001575 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001576 hostView.setTag(item);
1577 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001578
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001579 hostView.setFocusable(true);
1580 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001581
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001582 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001583 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1584
1585 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001586 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001587 }
1588 }
1589
Adam Cohended9f8d2010-11-03 13:25:16 -07001590 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1591 @Override
1592 public void onReceive(Context context, Intent intent) {
1593 final String action = intent.getAction();
1594 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1595 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001596 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001597 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001598
Winson Chungc100e8e2011-08-09 16:02:43 -07001599 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001600 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001601 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001602 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001603 if (!showWorkspace(false)) {
1604 // If we are already on the workspace, then manually reset all apps
1605 mAppsView.reset();
1606 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001607 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001608 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1609 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001610 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001611 }
1612 }
1613 };
1614
1615 @Override
1616 public void onAttachedToWindow() {
1617 super.onAttachedToWindow();
1618
1619 // Listen for broadcasts related to user-presence
1620 final IntentFilter filter = new IntentFilter();
1621 filter.addAction(Intent.ACTION_SCREEN_OFF);
1622 filter.addAction(Intent.ACTION_USER_PRESENT);
1623 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001624 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001625 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001626 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001627
1628 if (mLauncherCallbacks != null) {
1629 mLauncherCallbacks.onAttachedToWindow();
1630 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001631 }
1632
1633 @Override
1634 public void onDetachedFromWindow() {
1635 super.onDetachedFromWindow();
1636 mVisible = false;
1637
Adam Cohend113e0c2010-11-11 10:48:05 -08001638 if (mAttached) {
1639 unregisterReceiver(mReceiver);
1640 mAttached = false;
1641 }
Winson Chungb745afb2015-03-02 11:51:23 -08001642 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001643
1644 if (mLauncherCallbacks != null) {
1645 mLauncherCallbacks.onDetachedFromWindow();
1646 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001647 }
1648
1649 public void onWindowVisibilityChanged(int visibility) {
1650 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001651 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001652 // The following code used to be in onResume, but it turns out onResume is called when
1653 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1654 // is a more appropriate event to handle
1655 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001656 if (!mWorkspaceLoading) {
1657 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001658 // We want to let Launcher draw itself at least once before we force it to build
1659 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001660 // apps is nice and speedy.
1661 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001662 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001663 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001664 if (mStarted) return;
1665 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001666 // We delay the layer building a bit in order to give
1667 // other message processing a time to run. In particular
1668 // this avoids a delay in hiding the IME if it was
1669 // currently shown, because doing that may involve
1670 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001671 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001672 final ViewTreeObserver.OnDrawListener listener = this;
1673 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001674 public void run() {
1675 if (mWorkspace != null &&
1676 mWorkspace.getViewTreeObserver() != null) {
1677 mWorkspace.getViewTreeObserver().
1678 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001679 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001680 }
1681 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001682 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001683 }
1684 });
1685 }
1686 clearTypedText();
1687 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001688 }
1689
Adam Cohen091440a2015-03-18 14:16:05 -07001690 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001691 mHandler.removeMessages(ADVANCE_MSG);
1692 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1693 mHandler.sendMessageDelayed(msg, delay);
1694 mAutoAdvanceSentTime = System.currentTimeMillis();
1695 }
1696
Adam Cohen091440a2015-03-18 14:16:05 -07001697 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001698 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1699 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1700 mAutoAdvanceRunning = autoAdvanceRunning;
1701 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001702 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001703 sendAdvanceMessage(delay);
1704 } else {
1705 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001706 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001707 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1708 }
1709 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001710 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001711 }
1712 }
1713 }
1714
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001715 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1716
Adam Cohended9f8d2010-11-03 13:25:16 -07001717 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001718 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001719 if (msg.what == ADVANCE_MSG) {
1720 int i = 0;
1721 for (View key: mWidgetsToAdvance.keySet()) {
1722 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001723 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001724 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001725 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001726 public void run() {
1727 ((Advanceable) v).advance();
1728 }
1729 }, delay);
1730 }
1731 i++;
1732 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001733 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001734 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001735 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001736 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001737 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001738
Winsonc0b52fe2015-09-09 16:38:15 -07001739 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001740 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001741 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1742 if (v instanceof Advanceable) {
1743 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001744 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001745 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001746 }
1747 }
1748
Winsonc0b52fe2015-09-09 16:38:15 -07001749 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001750 if (mWidgetsToAdvance.containsKey(hostView)) {
1751 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001752 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001753 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001754 }
1755
Hyunyoung Song3f471442015-04-08 19:01:34 -07001756 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001757 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1758 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001759 }
1760
Winson Chunga6945242014-01-08 14:04:34 -08001761 public DragLayer getDragLayer() {
1762 return mDragLayer;
1763 }
1764
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001765 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001766 return mAppsView;
1767 }
1768
Hyunyoung Song3f471442015-04-08 19:01:34 -07001769 public WidgetsContainerView getWidgetsView() {
1770 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001771 }
1772
Winson Chunga6945242014-01-08 14:04:34 -08001773 public Workspace getWorkspace() {
1774 return mWorkspace;
1775 }
1776
1777 public Hotseat getHotseat() {
1778 return mHotseat;
1779 }
1780
Jorim Jaggid017f882014-01-14 17:08:48 -08001781 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001782 return mOverviewPanel;
1783 }
1784
Sunny Goyal47328fd2016-05-25 18:56:41 -07001785 public DropTargetBar getDropTargetBar() {
1786 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001787 }
1788
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001789 public LauncherAppWidgetHost getAppWidgetHost() {
1790 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 }
Romain Guycbb89e42009-06-08 15:52:54 -07001792
Winson Chunga9abd0e2010-10-27 17:18:37 -07001793 public LauncherModel getModel() {
1794 return mModel;
1795 }
1796
Adam Cohen79d90c52016-04-22 13:29:20 -07001797 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001798 return mSharedPrefs;
1799 }
1800
Adam Cohen2e6da152015-05-06 11:42:25 -07001801 public DeviceProfile getDeviceProfile() {
1802 return mDeviceProfile;
1803 }
1804
Bjorn Bringertc459e522013-06-07 19:36:01 +01001805 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001806 getWindow().closeAllPanels();
1807
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001808 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001809 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001810 }
1811
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 @Override
1813 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001814 long startTime = 0;
1815 if (DEBUG_RESUME_TIME) {
1816 startTime = System.currentTimeMillis();
1817 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001818 super.onNewIntent(intent);
1819
1820 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001821 Folder openFolder = mWorkspace.getOpenFolder();
1822 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1823 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1824 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1825 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1826 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001827 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001828 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001829
Adam Cohen6fecd412013-10-02 17:41:50 -07001830 if (mWorkspace == null) {
1831 // Can be cases where mWorkspace is null, this prevents a NPE
1832 return;
1833 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001834 // In all these cases, only animate if we're already on home
1835 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001836
Sunny Goyal935fca12015-10-13 10:19:01 -07001837 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001838 exitSpringLoadedDragMode();
1839
1840 // If we are already on home, then just animate back to the workspace,
1841 // otherwise, just wait until onResume to set the state back to Workspace
1842 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001843 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001844 } else {
1845 mOnResumeState = State.WORKSPACE;
1846 }
1847
1848 final View v = getWindow().peekDecorView();
1849 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001850 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001851 INPUT_METHOD_SERVICE);
1852 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1853 }
1854
Winson Chungb745afb2015-03-02 11:51:23 -08001855 // Reset the apps view
1856 if (!alreadyOnHome && mAppsView != null) {
1857 mAppsView.scrollToTop();
1858 }
1859
Hyunyoung Song3f471442015-04-08 19:01:34 -07001860 // Reset the widgets view
1861 if (!alreadyOnHome && mWidgetsView != null) {
1862 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001863 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001864
Adam Cohen9211d422014-10-07 18:14:53 -07001865 if (mLauncherCallbacks != null) {
1866 mLauncherCallbacks.onHomeIntent();
1867 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001868 }
Adam Cohened307df2013-10-02 09:37:31 -07001869
Adam Cohen9211d422014-10-07 18:14:53 -07001870 if (mLauncherCallbacks != null) {
1871 mLauncherCallbacks.onNewIntent(intent);
1872 }
Winson15f8b172015-08-19 11:02:39 -07001873
1874 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1875 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1876 // animation.
1877 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001878 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1879 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001880 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1881 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001882
1883 // We use this flag to suppress noisy callbacks above custom content state
1884 // from onResume.
1885 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001886 mWorkspace.post(new Runnable() {
1887 @Override
1888 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001889 if (mWorkspace != null) {
1890 mWorkspace.moveToDefaultScreen(true);
1891 }
Winson15f8b172015-08-19 11:02:39 -07001892 }
1893 });
1894 }
1895 }
1896
1897 if (DEBUG_RESUME_TIME) {
1898 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1899 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001900 }
1901
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001903 public void onRestoreInstanceState(Bundle state) {
1904 super.onRestoreInstanceState(state);
1905 for (int page: mSynchronouslyBoundPages) {
1906 mWorkspace.restoreInstanceStateForChild(page);
1907 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 }
1909
1910 @Override
1911 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001912 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001913 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1914 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001915
Adam Cohen21cd0022013-10-09 18:57:02 -07001916 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001917 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918
Michael Jurka883f55b2010-10-21 15:47:14 -07001919 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001920 // We close any open folder since it will not be re-opened, and we need to make sure
1921 // this state is reflected.
1922 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1923 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924
Adam Cohendcd297f2013-06-18 13:13:40 -07001925 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001926 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001927 ContentValues itemValues = new ContentValues();
1928 mPendingAddInfo.writeToValues(itemValues);
1929 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001930 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001931 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 }
1933
Adam Cohen9211d422014-10-07 18:14:53 -07001934 if (mLauncherCallbacks != null) {
1935 mLauncherCallbacks.onSaveInstanceState(outState);
1936 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 }
1938
1939 @Override
1940 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001942
Winson Chunge7a03942011-08-05 15:05:12 -07001943 // Remove all pending runnables
1944 mHandler.removeMessages(ADVANCE_MSG);
1945 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001946 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001947 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001948
Winson Chungcd2b0142011-06-08 16:02:26 -07001949 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001950 // It's possible to receive onDestroy after a new Launcher activity has
1951 // been created. In this case, don't interfere with the new Launcher.
1952 if (mModel.isCurrentCallbacks(this)) {
1953 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001954 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001955 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001956
Sunny Goyal745bad92016-05-02 10:54:12 -07001957 if (mRotationPrefChangeHandler != null) {
1958 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1959 }
1960
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001961 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001962 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001964 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001966 mAppWidgetHost = null;
1967
1968 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969
1970 TextKeyListener.getInstance().release();
1971
Tony Wickhame2217252016-03-22 16:34:23 -07001972 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1973 .removeAccessibilityStateChangeListener(this);
1974
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001975 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001976
Michael Jurka2ecf9952012-06-18 12:52:28 -07001977 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001978
1979 if (mLauncherCallbacks != null) {
1980 mLauncherCallbacks.onDestroy();
1981 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 }
1983
Sunny Goyalae502842016-06-17 08:43:56 -07001984 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1985 return mAccessibilityDelegate;
1986 }
1987
Adam Cohena9cf38f2011-05-02 15:36:58 -07001988 public DragController getDragController() {
1989 return mDragController;
1990 }
1991
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 @Override
1993 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07001994 onStartForResult(requestCode);
1995 super.startActivityForResult(intent, requestCode);
1996 }
1997
1998 @Override
1999 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2000 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2001 onStartForResult(requestCode);
2002 try {
2003 super.startIntentSenderForResult(intent, requestCode,
2004 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2005 } catch (IntentSender.SendIntentException e) {
2006 throw new ActivityNotFoundException();
2007 }
2008 }
2009
2010 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002011 if (requestCode >= 0) {
2012 setWaitingForResult(true);
2013 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 }
2015
Winson Chung70d72102011-08-12 11:24:35 -07002016 /**
2017 * Indicates that we want global search for this activity by setting the globalSearch
2018 * argument for {@link #startSearch} to true.
2019 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002021 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002023
Karl Rosaen138a0412009-04-23 19:00:21 -07002024 if (initialQuery == null) {
2025 // Use any text typed in the launcher as the initial query
2026 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002027 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002028 if (appSearchData == null) {
2029 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002030 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002032
2033 // TODO send proper bounds.
2034 Rect sourceBounds = null;
Romain Guycbb89e42009-06-08 15:52:54 -07002035
Ian Parkinson047036e2014-09-01 15:40:53 +01002036 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002037 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002038 if (clearTextImmediately) {
2039 clearTypedText();
2040 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002041
2042 // We need to show the workspace after starting the search
2043 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002044 }
2045
Ian Parkinson047036e2014-09-01 15:40:53 +01002046 /**
2047 * Start a text search.
2048 *
2049 * @return {@code true} if the search will start immediately, so any further keypresses
2050 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2051 * to buffer keypresses.
2052 */
2053 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002054 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002055 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2056 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2057 sourceBounds);
2058 }
2059
Michael Jurkaa33411c2012-06-14 16:18:21 -07002060 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002061 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002062 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002063 }
2064
2065 /**
2066 * Starts the global search activity. This code is a copied from SearchManager
2067 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002068 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002069 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002070 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002071 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2072 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2073 if (globalSearchActivity == null) {
2074 Log.w(TAG, "No global search activity found.");
2075 return;
2076 }
2077 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2078 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2079 intent.setComponent(globalSearchActivity);
2080 // Make sure that we have a Bundle to put source in
2081 if (appSearchData == null) {
2082 appSearchData = new Bundle();
2083 } else {
2084 appSearchData = new Bundle(appSearchData);
2085 }
Adam Cohen9211d422014-10-07 18:14:53 -07002086 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002087 if (!appSearchData.containsKey("source")) {
2088 appSearchData.putString("source", getPackageName());
2089 }
2090 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2091 if (!TextUtils.isEmpty(initialQuery)) {
2092 intent.putExtra(SearchManager.QUERY, initialQuery);
2093 }
2094 if (selectInitialQuery) {
2095 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2096 }
2097 intent.setSourceBounds(sourceBounds);
2098 try {
2099 startActivity(intent);
2100 } catch (ActivityNotFoundException ex) {
2101 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2102 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 }
2104
Yura4f93ec62014-02-04 14:15:21 +00002105 public boolean isOnCustomContent() {
2106 return mWorkspace.isOnOrMovingToCustomContent();
2107 }
2108
Winson Chung70d72102011-08-12 11:24:35 -07002109 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002110 public boolean onPrepareOptionsMenu(Menu menu) {
2111 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002112 if (mLauncherCallbacks != null) {
2113 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2114 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002115 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002116 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002117
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002118 @Override
2119 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002120 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002121 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002122 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002123 }
2124
Joe Onorato9c1289c2009-08-17 11:03:03 -04002125 public boolean isWorkspaceLocked() {
2126 return mWorkspaceLoading || mWaitingForResult;
2127 }
2128
Adam Cohen517a7f52014-03-01 12:12:59 -08002129 public boolean isWorkspaceLoading() {
2130 return mWorkspaceLoading;
2131 }
2132
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002133 private void setWorkspaceLoading(boolean value) {
2134 boolean isLocked = isWorkspaceLocked();
2135 mWorkspaceLoading = value;
2136 if (isLocked != isWorkspaceLocked()) {
2137 onWorkspaceLockedChanged();
2138 }
2139 }
2140
2141 private void setWaitingForResult(boolean value) {
2142 boolean isLocked = isWorkspaceLocked();
2143 mWaitingForResult = value;
2144 if (isLocked != isWorkspaceLocked()) {
2145 onWorkspaceLockedChanged();
2146 }
2147 }
2148
Adam Cohen9211d422014-10-07 18:14:53 -07002149 protected void onWorkspaceLockedChanged() {
2150 if (mLauncherCallbacks != null) {
2151 mLauncherCallbacks.onWorkspaceLockedChanged();
2152 }
2153 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002154
Michael Jurka0280c3b2010-09-17 15:00:07 -07002155 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002156 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002157 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002158 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2159 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002160 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002161 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002162
Tony Wickhama0628cc2015-10-14 15:23:04 -07002163 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002164 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002165 if (LOGD) {
2166 Log.d(TAG, "Adding widget from drop");
2167 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002168 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2169 }
2170
Sunny Goyale6b63a32015-01-16 16:14:29 -08002171 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002172 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2173 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002174 if (appWidgetInfo.configure != null) {
2175 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002176 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002177
Bjorn Bringert7984c942009-12-09 15:38:25 +00002178 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002179 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2180 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2181
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002182 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002183 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002184 Runnable onComplete = new Runnable() {
2185 @Override
2186 public void run() {
2187 // Exit spring loaded mode if necessary after adding the widget
2188 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2189 null);
2190 }
2191 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002192 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002193 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002194 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002195 }
2196 }
Romain Guycbb89e42009-06-08 15:52:54 -07002197
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002198 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002199 // Close any folders that may be open.
2200 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002201 mWorkspace.moveToCustomContentScreen(animate);
2202 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002203
2204 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2205 int[] cell, int spanX, int spanY) {
2206 switch (info.itemType) {
2207 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2208 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2209 int span[] = new int[2];
2210 span[0] = spanX;
2211 span[1] = spanY;
2212 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2213 container, screenId, cell, span);
2214 break;
2215 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2216 processShortcutFromDrop(info.componentName, container, screenId, cell);
2217 break;
2218 default:
2219 throw new IllegalStateException("Unknown item type: " + info.itemType);
2220 }
2221 }
2222
Adam Cohenfbba09b2011-07-18 21:43:05 -07002223 /**
2224 * Process a shortcut drop.
2225 *
2226 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002227 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002228 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002229 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002230 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2231 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002232 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002233 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002234 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002235
2236 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002237 mPendingAddInfo.cellX = cell[0];
2238 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002239 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002240
2241 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2242 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002243 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002244 }
2245
Adam Cohenfbba09b2011-07-18 21:43:05 -07002246 /**
2247 * Process a widget drop.
2248 *
2249 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002250 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002251 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002252 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002253 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2254 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002255 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002256 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002257 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002258 mPendingAddInfo.minSpanX = info.minSpanX;
2259 mPendingAddInfo.minSpanY = info.minSpanY;
2260
Adam Cohenfbba09b2011-07-18 21:43:05 -07002261 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002262 mPendingAddInfo.cellX = cell[0];
2263 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002264 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002265 if (span != null) {
2266 mPendingAddInfo.spanX = span[0];
2267 mPendingAddInfo.spanY = span[1];
2268 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002269
Adam Cohened66b2b2012-01-23 17:28:51 -08002270 AppWidgetHostView hostView = info.boundWidget;
2271 int appWidgetId;
2272 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002273 // In the case where we've prebound the widget, we remove it from the DragLayer
2274 if (LOGD) {
2275 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2276 }
2277 getDragLayer().removeView(hostView);
2278
Adam Cohened66b2b2012-01-23 17:28:51 -08002279 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002280 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002281
2282 // Clear the boundWidget so that it doesn't get destroyed.
2283 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002284 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002285 // In this case, we either need to start an activity to get permission to bind
2286 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002287 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002288 Bundle options = info.bindOptions;
2289
Sunny Goyalffe83f12014-08-14 17:39:34 -07002290 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2291 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002292 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002293 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002294 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002295 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002296 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2297 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2298 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002299 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2300 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002301 // TODO: we need to make sure that this accounts for the options bundle.
2302 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002303 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2304 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002305 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002306 }
2307
Adam Cohendcd297f2013-06-18 13:13:40 -07002308 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002309 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002310 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002311 folderInfo.title = getText(R.string.folder_name);
2312
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002313 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002314 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2315 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002316
2317 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002318 FolderIcon newFolder =
2319 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002320 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002321 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002322 // Force measure the new folder icon
2323 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2324 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002325 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002326 }
Romain Guycbb89e42009-06-08 15:52:54 -07002327
Winsonc0b52fe2015-09-09 16:38:15 -07002328 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002329 * Unbinds the view for the specified item, and removes the item and all its children.
2330 *
2331 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002332 * @param itemInfo the {@link ItemInfo} for this view.
2333 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002334 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002335 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002336 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002337 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002338 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2339 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002340 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002341 } else {
2342 mWorkspace.removeWorkspaceItem(v);
2343 }
Winsonc0b52fe2015-09-09 16:38:15 -07002344 if (deleteFromDb) {
2345 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2346 }
2347 } else if (itemInfo instanceof FolderInfo) {
2348 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002349 if (v instanceof FolderIcon) {
2350 ((FolderIcon) v).removeListeners();
2351 }
Winsonc0b52fe2015-09-09 16:38:15 -07002352 mWorkspace.removeWorkspaceItem(v);
2353 if (deleteFromDb) {
2354 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2355 }
2356 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2357 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002358 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002359 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002360 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002361 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002362 }
2363 } else {
2364 return false;
2365 }
2366 return true;
2367 }
2368
2369 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002370 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002371 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002372 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002373 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002374 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002375 // Deleting an app widget ID is a void call but writes to disk before returning
2376 // to the caller...
2377 new AsyncTask<Void, Void, Void>() {
2378 public Void doInBackground(Void ... args) {
2379 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2380 return null;
2381 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002382 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002383 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002384 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002385 }
2386
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002387 @Override
2388 public boolean dispatchKeyEvent(KeyEvent event) {
2389 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2390 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002391 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002392 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002393 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002394 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002395 dumpState();
2396 return true;
2397 }
2398 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002399 }
2400 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2401 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002402 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002403 return true;
2404 }
2405 }
2406
2407 return super.dispatchKeyEvent(event);
2408 }
2409
Joe Onorato88ec0992009-11-19 13:16:06 -08002410 @Override
2411 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002412 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2413 return;
2414 }
2415
Sunny Goyal45478022015-06-08 16:52:41 -07002416 if (mDragController.isDragging()) {
2417 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002418 return;
2419 }
2420
Winson Chungb745afb2015-03-02 11:51:23 -08002421 if (isAppsViewVisible()) {
2422 showWorkspace(true);
2423 } else if (isWidgetsViewVisible()) {
2424 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002425 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002426 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002427 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002428 Folder openFolder = mWorkspace.getOpenFolder();
2429 if (openFolder.isEditingName()) {
2430 openFolder.dismissEditingName();
2431 } else {
2432 closeFolder();
2433 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002434 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002435 mWorkspace.exitWidgetResizeMode();
2436
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002437 // Back button is a no-op here, but give at least some feedback for the button press
2438 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002439 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002440 }
2441
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002442 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002443 * Launches the intent referred by the clicked shortcut.
2444 *
2445 * @param v The view representing the clicked shortcut.
2446 */
2447 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002448 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2449 // view has detached (it's possible for this to happen if the view is removed mid touch).
2450 if (v.getWindowToken() == null) {
2451 return;
2452 }
2453
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002454 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002455 return;
2456 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002457
Adam Cohen1697b792013-09-17 19:08:21 -07002458 if (v instanceof Workspace) {
2459 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002460 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002461 }
2462 return;
2463 }
2464
2465 if (v instanceof CellLayout) {
2466 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002467 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2468 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002469 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002470 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002471 }
2472
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002473 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002474 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002475 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002476 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002477 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002478 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002479 }
Sunny Goyalbb011da2016-06-15 15:42:29 -07002480 } else if (v instanceof PageIndicator || (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002481 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002482 } else if (tag instanceof AppInfo) {
2483 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002484 } else if (tag instanceof LauncherAppWidgetInfo) {
2485 if (v instanceof PendingAppWidgetHostView) {
2486 onClickPendingWidget((PendingAppWidgetHostView) v);
2487 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002488 }
2489 }
2490
Sunny Goyal70660032015-05-14 00:07:08 -07002491 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002492 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002493 return false;
2494 }
2495
Michael Jurkaaf442092010-06-10 17:01:57 -07002496 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002497 * Event handler for the app widget view which has not fully restored.
2498 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002499 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002500 if (mIsSafeModeEnabled) {
2501 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2502 return;
2503 }
2504
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002505 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002506 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002507 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2508 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2509 // This should not happen, as we make sure that an Id is allocated during bind.
2510 return;
2511 }
2512 LauncherAppWidgetProviderInfo appWidgetInfo =
2513 mAppWidgetManager.findProvider(info.providerName, info.user);
2514 if (appWidgetInfo != null) {
2515 mPendingAddWidgetId = info.appWidgetId;
2516 mPendingAddInfo.copyFrom(info);
2517 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002518
Sunny Goyald478c832016-04-01 12:04:16 -07002519 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2520 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingAddWidgetId);
2521 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
2522 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2523 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2524 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2525 }
2526 } else {
2527 LauncherAppWidgetProviderInfo appWidgetInfo =
2528 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2529 if (appWidgetInfo != null) {
2530 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2531 }
Sunny Goyalff572272014-07-23 13:58:07 -07002532 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002533 } else if (info.installProgress < 0) {
2534 // The install has not been queued
2535 final String packageName = info.providerName.getPackageName();
2536 showBrokenAppInstallDialog(packageName,
2537 new DialogInterface.OnClickListener() {
2538 public void onClick(DialogInterface dialog, int id) {
2539 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2540 }
2541 });
2542 } else {
2543 // Download has started.
2544 final String packageName = info.providerName.getPackageName();
2545 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002546 }
2547 }
2548
Sunny Goyald478c832016-04-01 12:04:16 -07002549 private void startRestoredWidgetReconfigActivity(
2550 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
2551 mPendingAddWidgetInfo = provider;
2552 mPendingAddInfo.copyFrom(info);
2553 mPendingAddWidgetId = info.appWidgetId;
2554 mAppWidgetManager.startConfigActivity(provider,
2555 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2556 }
2557
Sunny Goyalff572272014-07-23 13:58:07 -07002558 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002559 * Event handler for the "grid" button that appears on the home screen, which
2560 * enters all apps mode.
2561 *
2562 * @param v The view that was clicked.
2563 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002564 protected void onClickAllAppsButton(View v) {
2565 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002566 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002567 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002568 true /* updatePredictedApps */, false /* focusSearchBar */);
2569 }
2570 }
2571
2572 protected void onLongClickAllAppsButton(View v) {
2573 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2574 if (!isAppsViewVisible()) {
2575 showAppsView(true /* animated */, false /* resetListToTop */,
2576 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002577 }
2578 }
2579
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002580 private void showBrokenAppInstallDialog(final String packageName,
2581 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002582 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002583 .setTitle(R.string.abandoned_promises_title)
2584 .setMessage(R.string.abandoned_promise_explanation)
2585 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2586 .setNeutralButton(R.string.abandoned_clean_this,
2587 new DialogInterface.OnClickListener() {
2588 public void onClick(DialogInterface dialog, int id) {
2589 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2590 mWorkspace.removeAbandonedPromise(packageName, user);
2591 }
2592 })
2593 .create().show();
2594 return;
2595 }
2596
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002597 /**
2598 * Event handler for an app shortcut click.
2599 *
2600 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2601 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002602 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002603 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2604 Object tag = v.getTag();
2605 if (!(tag instanceof ShortcutInfo)) {
2606 throw new IllegalArgumentException("Input must be a Shortcut");
2607 }
2608
2609 // Open shortcut
2610 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002611
2612 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002613 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2614 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002615 // Launch activity anyway, framework will tell the user why the app is suspended.
2616 } else {
2617 int error = R.string.activity_not_available;
2618 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2619 error = R.string.safemode_shortcut_error;
2620 }
2621 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2622 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002623 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002624 }
2625
Chris Wren40c5ed32014-06-24 18:24:23 -04002626 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002627 if ((v instanceof BubbleTextView)
2628 && shortcut.isPromise()
2629 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002630 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002631 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002632 new DialogInterface.OnClickListener() {
2633 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002634 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002635 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002636 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002637 return;
2638 }
2639
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002640 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002641 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002642 }
2643
Sunny Goyala7ce1662016-05-31 15:01:35 -07002644 private void startAppShortcutOrInfoActivity(View v) {
2645 ItemInfo item = (ItemInfo) v.getTag();
2646 Intent intent = item.getIntent();
2647 if (intent == null) {
2648 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002649 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002650 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002651 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002652
2653 if (success && v instanceof BubbleTextView) {
2654 mWaitingForResume = (BubbleTextView) v;
2655 mWaitingForResume.setStayPressed(true);
2656 }
2657 }
2658
2659 /**
2660 * Event handler for a folder icon click.
2661 *
2662 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2663 */
2664 protected void onClickFolderIcon(View v) {
2665 if (LOGD) Log.d(TAG, "onClickFolder");
2666 if (!(v instanceof FolderIcon)){
2667 throw new IllegalArgumentException("Input must be a FolderIcon");
2668 }
2669
2670 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002671 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002672 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002673 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002674 }
Michael Jurka5130e402011-10-13 04:55:35 -07002675 }
2676
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002677 /**
2678 * Event handler for the (Add) Widgets button that appears after a long press
2679 * on the home screen.
2680 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002681 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002682 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002683 if (mIsSafeModeEnabled) {
2684 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2685 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002686 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002687 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002688 }
2689
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002690 /**
2691 * Event handler for the wallpaper picker button that appears after a long press
2692 * on the home screen.
2693 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002694 public void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002695 if (!Utilities.isWallapaperAllowed(this)) {
2696 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2697 return;
2698 }
2699
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002700 String pickerPackage = getString(R.string.wallpaper_picker_package);
2701 if (TextUtils.isEmpty(pickerPackage)) {
2702 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2703 }
2704
Tony Wickham785f7a52015-08-31 17:28:32 -07002705 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2706 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002707 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER)
2708 .setPackage(pickerPackage)
2709 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset),
2710 REQUEST_PICK_WALLPAPER);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002711 }
2712
2713 /**
2714 * Event handler for a click on the settings button that appears after a long press
2715 * on the home screen.
2716 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002717 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002718 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002719 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2720 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002721 }
2722
Adam Cohen61f560d2013-09-30 15:58:20 -07002723 public View.OnTouchListener getHapticFeedbackTouchListener() {
2724 if (mHapticFeedbackTouchListener == null) {
2725 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002726 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002727 @Override
2728 public boolean onTouch(View v, MotionEvent event) {
2729 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2730 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2731 }
2732 return false;
2733 }
2734 };
2735 }
2736 return mHapticFeedbackTouchListener;
2737 }
2738
Tony Wickhame2217252016-03-22 16:34:23 -07002739 @Override
2740 public void onAccessibilityStateChanged(boolean enabled) {
2741 mDragLayer.onAccessibilityStateChanged(enabled);
2742 }
2743
Adam Cohen9211d422014-10-07 18:14:53 -07002744 public void onDragStarted(View view) {
2745 if (isOnCustomContent()) {
2746 // Custom content screen doesn't participate in drag and drop. If on custom
2747 // content screen, move to default.
2748 moveWorkspaceToDefaultScreen();
2749 }
Adam Cohen9211d422014-10-07 18:14:53 -07002750 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002751
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002752 /**
2753 * Called when the user stops interacting with the launcher.
2754 * This implies that the user is now on the homescreen and is not doing housekeeping.
2755 */
Adam Cohen9211d422014-10-07 18:14:53 -07002756 protected void onInteractionEnd() {
2757 if (mLauncherCallbacks != null) {
2758 mLauncherCallbacks.onInteractionEnd();
2759 }
2760 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002761
2762 /**
2763 * Called when the user starts interacting with the launcher.
2764 * The possible interactions are:
2765 * - open all apps
2766 * - reorder an app shortcut, or a widget
2767 * - open the overview mode.
2768 * This is a good time to stop doing things that only make sense
2769 * when the user is on the homescreen and not doing housekeeping.
2770 */
Adam Cohen9211d422014-10-07 18:14:53 -07002771 protected void onInteractionBegin() {
2772 if (mLauncherCallbacks != null) {
2773 mLauncherCallbacks.onInteractionBegin();
2774 }
2775 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002776
Winson Chungcd99cd32015-04-29 11:03:24 -07002777 /** Updates the interaction state. */
2778 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002779 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002780 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002781 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2782 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002783 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002784 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002785 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002786 onInteractionEnd();
2787 }
2788 }
2789
Sunny Goyala7ce1662016-05-31 15:01:35 -07002790 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002791 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002792 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2793 try {
2794 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2795 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2796 // is enabled by default on NYC.
2797 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2798 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002799
2800 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2801 String id = ((ShortcutInfo) info).getDeepShortcutId();
2802 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002803 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2804 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002805 } else {
2806 // Could be launching some bookkeeping activity
2807 startActivity(intent, optsBundle);
2808 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002809 } finally {
2810 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002811 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002812 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002813 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2814 // corresponding permission. Show the appropriate permission prompt if that
2815 // is the case.
2816 if (intent.getComponent() == null
2817 && Intent.ACTION_CALL.equals(intent.getAction())
2818 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2819 PackageManager.PERMISSION_GRANTED) {
2820 // TODO: Rename sPendingAddItem to a generic name.
2821 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2822 0, info);
2823 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2824 REQUEST_PERMISSION_CALL_PHONE);
2825 } else {
2826 // No idea why this was thrown.
2827 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002828 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002829 }
2830 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002831
Sunny Goyala7ce1662016-05-31 15:01:35 -07002832 private Bundle getActivityLaunchOptions(View v) {
2833 if (Utilities.ATLEAST_MARSHMALLOW) {
2834 int left = 0, top = 0;
2835 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2836 if (v instanceof TextView) {
2837 // Launch from center of icon, not entire view
2838 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2839 if (icon != null) {
2840 Rect bounds = icon.getBounds();
2841 left = (width - bounds.width()) / 2;
2842 top = v.getPaddingTop();
2843 width = bounds.width();
2844 height = bounds.height();
2845 }
2846 }
2847 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2848 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2849 // On L devices, we use the device default slide-up transition.
2850 // On L MR1 devices, we use a custom version of the slide-up transition which
2851 // doesn't have the delay present in the device default.
2852 return ActivityOptions.makeCustomAnimation(
2853 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2854 }
2855 return null;
2856 }
2857
2858 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002859 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2860 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2861 return false;
2862 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002863 // Only launch using the new animation if the shortcut has not opted out (this is a
2864 // private contract between launcher and may be ignored in the future).
2865 boolean useLaunchAnimation = (v != null) &&
2866 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2867 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2868
2869 UserHandleCompat user = null;
2870 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2871 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2872 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002873 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002874
2875 // Prepare intent
2876 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2877 if (v != null) {
2878 int[] pos = new int[2];
2879 v.getLocationOnScreen(pos);
2880 intent.setSourceBounds(
2881 new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2882 }
2883 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002884 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2885 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
2886 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002887 // Shortcuts need some special checks due to legacy reasons.
2888 startShortcutIntentSafely(intent, optsBundle, item);
2889 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2890 // Could be launching some bookkeeping activity
2891 startActivity(intent, optsBundle);
2892 } else {
2893 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2894 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2895 }
2896 return true;
2897 } catch (ActivityNotFoundException|SecurityException e) {
2898 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2899 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2900 }
2901 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002902 }
2903
Adam Cohen268c4752012-06-06 17:47:33 -07002904 /**
2905 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2906 * in the DragLayer in the exact absolute location of the original FolderIcon.
2907 */
2908 private void copyFolderIconToImage(FolderIcon fi) {
2909 final int width = fi.getMeasuredWidth();
2910 final int height = fi.getMeasuredHeight();
2911
2912 // Lazy load ImageView, Bitmap and Canvas
2913 if (mFolderIconImageView == null) {
2914 mFolderIconImageView = new ImageView(this);
2915 }
2916 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2917 mFolderIconBitmap.getHeight() != height) {
2918 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2919 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2920 }
2921
2922 DragLayer.LayoutParams lp;
2923 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2924 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2925 } else {
2926 lp = new DragLayer.LayoutParams(width, height);
2927 }
2928
Adam Cohen307fe232012-08-16 17:55:58 -07002929 // The layout from which the folder is being opened may be scaled, adjust the starting
2930 // view size by this scale factor.
2931 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002932 lp.customPosition = true;
2933 lp.x = mRectForFolderAnimation.left;
2934 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002935 lp.width = (int) (scale * width);
2936 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002937
2938 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2939 fi.draw(mFolderIconCanvas);
2940 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002941 if (fi.getFolder() != null) {
2942 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2943 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002944 }
Adam Cohen268c4752012-06-06 17:47:33 -07002945 // Just in case this image view is still in the drag layer from a previous animation,
2946 // we remove it and re-add it.
2947 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2948 mDragLayer.removeView(mFolderIconImageView);
2949 }
2950 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002951 if (fi.getFolder() != null) {
2952 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002953 }
Adam Cohen268c4752012-06-06 17:47:33 -07002954 }
2955
Adam Cohen37b2a492016-04-06 18:36:06 -07002956 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002957 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002958 FolderInfo info = (FolderInfo) fi.getTag();
2959 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2960 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002961 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2962 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002963 }
2964
Adam Cohen268c4752012-06-06 17:47:33 -07002965 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2966 copyFolderIconToImage(fi);
2967 fi.setVisibility(View.INVISIBLE);
2968
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002969 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2970 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002971 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002972 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2973 }
2974 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002975 oa.start();
2976 }
2977
Sunny Goyal935fca12015-10-13 10:19:01 -07002978 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002979 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002980 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002981
Adam Cohen268c4752012-06-06 17:47:33 -07002982 // We remove and re-draw the FolderIcon in-case it has changed
2983 mDragLayer.removeView(mFolderIconImageView);
2984 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08002985
2986 if (cl != null) {
2987 cl.clearFolderLeaveBehind();
2988 }
2989
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002990 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002991 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002992 oa.addListener(new AnimatorListenerAdapter() {
2993 @Override
2994 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002995 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07002996 // Remove the ImageView copy of the FolderIcon and make the original visible.
2997 mDragLayer.removeView(mFolderIconImageView);
2998 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002999 }
3000 }
3001 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003002 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003003 if (!animate) {
3004 oa.end();
3005 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003006 }
3007
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003008 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003009 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003010 * is animated relative to the specified View. If the View is null, no animation
3011 * is played.
3012 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003013 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003014 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003015 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003016
Adam Cohenfb91f302012-06-11 15:45:18 -07003017 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003018 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3019 if (openFolder != null && openFolder != folder) {
3020 // Close any open folder before opening a folder.
3021 closeFolder();
3022 }
3023
Adam Cohena9cf38f2011-05-02 15:36:58 -07003024 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003025
Adam Cohena9cf38f2011-05-02 15:36:58 -07003026 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003027
Sunny Goyalf4066152015-04-15 09:42:19 -07003028 // While the folder is open, the position of the icon cannot change.
3029 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3030
Adam Cohen4554ee12011-08-03 16:13:21 -07003031 // Just verify that the folder hasn't already been added to the DragLayer.
3032 // There was a one-off crash where the folder had a parent already.
3033 if (folder.getParent() == null) {
3034 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003035 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003036 } else {
3037 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3038 folder.getParent() + ").");
3039 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003040 folder.animateOpen();
3041
3042 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003043
3044 // Notify the accessibility manager that this folder "window" has appeared and occluded
3045 // the workspace items
3046 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3047 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003048 }
3049
3050 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003051 closeFolder(true);
3052 }
3053
3054 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003055 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003056 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003057 if (folder.isEditingName()) {
3058 folder.dismissEditingName();
3059 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003060 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003061 }
3062 }
3063
Sunny Goyal935fca12015-10-13 10:19:01 -07003064 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003065 animate &= !Utilities.isPowerSaverOn(this);
3066
Adam Cohen4554ee12011-08-03 16:13:21 -07003067 folder.getInfo().opened = false;
3068
3069 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3070 if (parent != null) {
3071 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003072 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003073 if (fi != null) {
3074 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3075 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003076 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003077 if (animate) {
3078 folder.animateClosed();
3079 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003080 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003081 }
Winson Chung83ca4802013-04-12 15:10:52 -07003082
Sunny Goyal935fca12015-10-13 10:19:01 -07003083 // Notify the accessibility manager that this folder "window" has disappeared and no
3084 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003085 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003086 }
3087
Tony Wickhamdadb3042016-02-24 11:07:00 -08003088 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003089 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003090 if (!isDraggingEnabled()) return false;
3091 if (isWorkspaceLocked()) return false;
3092 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003093
Sunny Goyalbb011da2016-06-15 15:42:29 -07003094 if (v == mAllAppsButton && mAllAppsButton != null) {
Winson Chung76648c52015-07-10 14:33:23 -07003095 onLongClickAllAppsButton(v);
3096 return true;
3097 }
3098
Adam Cohen1697b792013-09-17 19:08:21 -07003099 if (v instanceof Workspace) {
3100 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003101 if (!mWorkspace.isTouchActive()) {
3102 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003103 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3104 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3105 return true;
3106 } else {
3107 return false;
3108 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003109 } else {
3110 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003111 }
Adam Cohen1697b792013-09-17 19:08:21 -07003112 }
3113
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003114 CellLayout.CellInfo longClickCellInfo = null;
3115 View itemUnderLongClick = null;
3116 if (v.getTag() instanceof ItemInfo) {
3117 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003118 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003119 itemUnderLongClick = longClickCellInfo.cell;
3120 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003121 }
3122
Winson Chung3d503fb2011-07-13 17:25:49 -07003123 // The hotseat touch handling does not go through Workspace, and we always allow long press
3124 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003125 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003126 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003127 // User long pressed on empty space
3128 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3129 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003130 if (mWorkspace.isInOverviewMode()) {
3131 mWorkspace.startReordering(v);
3132 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003133 showOverviewMode(true);
Hyunyoung Song645764e2016-06-06 14:19:02 -07003134 mHotseat.requestDisallowInterceptTouchEvent(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003135 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003136 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003137 final boolean isAllAppsButton =
3138 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3139 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3140 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003141 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003142 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003143 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003144 }
3145 }
3146 }
3147 return true;
3148 }
3149
Winson Chung3d503fb2011-07-13 17:25:49 -07003150 boolean isHotseatLayout(View layout) {
3151 return mHotseat != null && layout != null &&
3152 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3153 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003154
Winson Chung3d503fb2011-07-13 17:25:49 -07003155 /**
3156 * Returns the CellLayout of the specified container at the specified screen.
3157 */
Sunny Goyal92820592015-03-02 11:31:35 -08003158 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003159 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3160 if (mHotseat != null) {
3161 return mHotseat.getLayout();
3162 } else {
3163 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003164 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003165 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003166 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003167 }
3168 }
3169
Winson Chungb745afb2015-03-02 11:51:23 -08003170 /**
3171 * For overridden classes.
3172 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003173 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003174 return isAppsViewVisible();
3175 }
3176
3177 public boolean isAppsViewVisible() {
3178 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3179 }
3180
3181 public boolean isWidgetsViewVisible() {
3182 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003183 }
3184
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003185 private void setWorkspaceBackground(int background) {
3186 switch (background) {
3187 case WORKSPACE_BACKGROUND_TRANSPARENT:
3188 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3189 break;
3190 case WORKSPACE_BACKGROUND_BLACK:
3191 getWindow().setBackgroundDrawable(null);
3192 break;
3193 default:
3194 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3195 }
Craig Mautner360310b2012-10-26 15:13:08 -07003196 }
3197
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003198 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003199 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3200 int curflags = getWindow().getAttributes().flags
3201 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3202 if (wpflags != curflags) {
3203 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3204 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003205 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003206 }
3207
Michael Jurkae326f182011-11-21 14:05:46 -08003208 @Override
3209 public void onTrimMemory(int level) {
3210 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003211 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3212 // The widget preview db can result in holding onto over
3213 // 3MB of memory for caching which isn't necessary.
3214 SQLiteDatabase.releaseMemory();
3215
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003216 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003217 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003218 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003219 if (mLauncherCallbacks != null) {
3220 mLauncherCallbacks.onTrimMemory(level);
3221 }
Michael Jurkae326f182011-11-21 14:05:46 -08003222 }
3223
Winson5c6bdbb2015-09-03 11:36:19 -07003224 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003225 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003226 }
3227
Winson5c6bdbb2015-09-03 11:36:19 -07003228 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003229 boolean changed = mState != State.WORKSPACE ||
3230 mWorkspace.getState() != Workspace.State.NORMAL;
3231 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003232 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003233 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003234 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003235
Michael Jurkab3e22d92011-10-31 15:58:33 -07003236 // Set focus to the AppsCustomize button
3237 if (mAllAppsButton != null) {
3238 mAllAppsButton.requestFocus();
3239 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003240 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003241
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003242 // Change the state *after* we've called all the transition code
3243 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003244
Winson Chung5fb63472011-02-02 17:03:37 -08003245 // Resume the auto-advance of widgets
3246 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003247 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003248
Winson Chung0f785722015-04-08 10:27:49 -07003249 if (changed) {
3250 // Send an accessibility event to announce the context change
3251 getWindow().getDecorView()
3252 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003253 }
Winson5c6bdbb2015-09-03 11:36:19 -07003254 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003255 }
3256
Winsone9f27272015-10-13 10:47:51 -07003257 /**
3258 * Shows the overview button.
3259 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07003260 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003261 showOverviewMode(animated, false);
3262 }
3263
3264 /**
3265 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3266 * onto one of the overview panel buttons.
3267 */
3268 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3269 Runnable postAnimRunnable = null;
3270 if (requestButtonFocus) {
3271 postAnimRunnable = new Runnable() {
3272 @Override
3273 public void run() {
3274 // Hitting the menu button when in touch mode does not trigger touch mode to
3275 // be disabled, so if requested, force focus on one of the overview panel
3276 // buttons.
3277 mOverviewPanel.requestFocusFromTouch();
3278 }
3279 };
3280 }
Adam Cohened307df2013-10-02 09:37:31 -07003281 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003282 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003283 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003284 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003285 }
3286
Winson Chungb745afb2015-03-02 11:51:23 -08003287 /**
3288 * Shows the apps view.
3289 */
Hyunyoung Song645764e2016-06-06 14:19:02 -07003290 public void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003291 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003292 if (resetListToTop) {
3293 mAppsView.scrollToTop();
3294 }
Winson Chung4ac30062015-05-08 17:34:17 -07003295 if (updatePredictedApps) {
3296 tryAndUpdatePredictedApps();
3297 }
Winson Chung76648c52015-07-10 14:33:23 -07003298 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003299 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003300
Winson Chungb745afb2015-03-02 11:51:23 -08003301 /**
3302 * Shows the widgets view.
3303 */
3304 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003305 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003306 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003307 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003308 }
Winson Chung76648c52015-07-10 14:33:23 -07003309 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003310
3311 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003312 @Override
3313 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003314 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003315 }
3316 });
Winson Chungb745afb2015-03-02 11:51:23 -08003317 }
3318
3319 /**
3320 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003321 *
3322 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003323 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003324 // TODO: calling method should use the return value so that when {@code false} is returned
3325 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003326 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003327 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3328 mState != State.WIDGETS_SPRING_LOADED) {
3329 return false;
3330 }
3331 if (toState != State.APPS && toState != State.WIDGETS) {
3332 return false;
3333 }
Winson Chungb745afb2015-03-02 11:51:23 -08003334
3335 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003336 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3337 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003338 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003339 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003340 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003341
Michael Jurkab3e22d92011-10-31 15:58:33 -07003342 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003343 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003344
3345 // Pause the auto-advance of widgets until we are out of AllApps
3346 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003347 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003348 closeFolder();
3349
3350 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003351 getWindow().getDecorView()
3352 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003353 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003354 }
3355
Winson Chungcd99cd32015-04-29 11:03:24 -07003356 /**
3357 * Updates the workspace and interaction state on state change, and return the animation to this
3358 * new state.
3359 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003360 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003361 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003362 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003363 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003364 updateInteraction(fromState, toState);
3365 return anim;
3366 }
3367
Jun Mukaif0033da2015-08-04 18:34:30 -07003368 public void onLauncherClingShown() {
3369 // When a launcher cling appears, it should cover the underlying layers, so their focus
3370 // should be blocked.
3371 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3372 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3373 }
3374 }
3375
3376 public void onLauncherClingDismissed() {
3377 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3378 }
3379
Hyunyoung Song3f471442015-04-08 19:01:34 -07003380 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003381 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003382 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003383 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003384 }
Winson Chungb745afb2015-03-02 11:51:23 -08003385
Winson Chung006ee262015-08-03 14:40:11 -07003386 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003387 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003388 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003389
3390 if (isAppsViewVisible()) {
3391 mState = State.APPS_SPRING_LOADED;
3392 } else if (isWidgetsViewVisible()) {
3393 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003394 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003395 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003396 } else {
3397 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003398 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003399 }
Adam Cohen7777d962011-08-18 18:58:38 -07003400
Hyunyoung Song3f471442015-04-08 19:01:34 -07003401 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003402 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003403 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003404
Winson Chunge7a03942011-08-05 15:05:12 -07003405 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003406 @Override
3407 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003408 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003409 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3410 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003411 // Before we show workspace, hide all apps again because
3412 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3413 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003414 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003415 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003416 } else {
3417 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003418 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003419 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003420 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003421 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003422
Tony Wickhame0c33232016-02-08 11:37:04 -08003423 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003424 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3425 || mState == State.WIDGETS_SPRING_LOADED;
3426 }
3427
Michael Jurkad3ef3062010-11-23 16:23:58 -08003428 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003429 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003430 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003431 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003432 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003433 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003434 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3435 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003436 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003437 }
3438
Winson Chung4ac30062015-05-08 17:34:17 -07003439 /**
3440 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3441 * resumed.
3442 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003443 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003444 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003445 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003446 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003447 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003448 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003449 }
3450 }
3451 }
3452
Michael Jurkab3e22d92011-10-31 15:58:33 -07003453 void lockAllApps() {
3454 // TODO
3455 }
3456
3457 void unlockAllApps() {
3458 // TODO
3459 }
3460
Winsonf768d932015-09-25 16:12:35 -07003461 public boolean launcherCallbacksProvidesSearch() {
3462 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3463 }
3464
Michael Jurkad7c28052012-04-27 15:43:36 -07003465 @Override
3466 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003467 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003468 final List<CharSequence> text = event.getText();
3469 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003470 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003471 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003472 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003473 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003474 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003475 } else if (mWorkspace != null) {
3476 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003477 } else {
3478 text.add(getString(R.string.all_apps_home_button_label));
3479 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003480 return result;
3481 }
3482
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003483 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003484 * If the activity is currently paused, signal that we need to run the passed Runnable
3485 * in onResume.
3486 *
3487 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003488 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3489 * wrong when we're not running, and if the activity comes back to what the configuration was
3490 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003491 *
3492 * Implementation of the method from LauncherModel.Callbacks.
3493 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003494 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003495 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003496 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003497 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003498 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003499 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003500 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003501 }
3502 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003503 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003504 return true;
3505 } else {
3506 return false;
3507 }
3508 }
3509
Michael Jurkac402cd92013-05-20 15:49:32 +02003510 private boolean waitUntilResume(Runnable run) {
3511 return waitUntilResume(run, false);
3512 }
3513
Michael Jurka1e2f4652013-07-08 18:03:46 -07003514 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003515 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003516 }
3517
Michael Jurka7607c2f2013-04-03 14:33:19 -07003518 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003519 * If the activity is currently paused, signal that we need to re-run the loader
3520 * in onResume.
3521 *
3522 * This needs to be called from incoming places where resources might have been loaded
3523 * while we are paused. That is becaues the Configuration might be wrong
3524 * when we're not running, and if it comes back to what it was when we
3525 * were paused, we are not restarted.
3526 *
3527 * Implementation of the method from LauncherModel.Callbacks.
3528 *
3529 * @return true if we are currently paused. The caller might be able to
3530 * skip some work in that case since we will come back again.
3531 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003532 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003533 public boolean setLoadOnResume() {
3534 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003535 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003536 mOnResumeNeedsLoad = true;
3537 return true;
3538 } else {
3539 return false;
3540 }
3541 }
3542
3543 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003544 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003545 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003546 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003547 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003548 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003549 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003550 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003551 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003552 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003553 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003554
Joe Onorato9c1289c2009-08-17 11:03:03 -04003555 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003556 * Clear any pending bind callbacks. This is called when is loader is planning to
3557 * perform a full rebind from scratch.
3558 */
3559 @Override
3560 public void clearPendingBinds() {
3561 mBindOnResumeCallbacks.clear();
3562 if (mPendingExecutor != null) {
3563 mPendingExecutor.markCompleted();
3564 mPendingExecutor = null;
3565 }
3566 }
3567
3568 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003569 * Refreshes the shortcuts shown on the workspace.
3570 *
3571 * Implementation of the method from LauncherModel.Callbacks.
3572 */
3573 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003574 if (LauncherAppState.PROFILE_STARTUP) {
3575 Trace.beginSection("Starting page bind");
3576 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003577 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003578
Winson Chungd64d1762013-08-20 14:37:16 -07003579 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003580 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003581 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003582
Michael Jurka05bf644e2011-11-30 20:28:53 -08003583 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003584 if (mHotseat != null) {
3585 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003586 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003587 if (LauncherAppState.PROFILE_STARTUP) {
3588 Trace.endSection();
3589 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003590 }
3591
Adam Cohendcd297f2013-06-18 13:13:40 -07003592 @Override
3593 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003594 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003595 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3596 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003597 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3598 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3599 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003600 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3601 // If there are no screens, we need to have an empty screen
3602 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003603 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003604 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003605
3606 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003607 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003608 if (hasCustomContentToLeft()) {
3609 mWorkspace.createCustomContentContainer();
3610 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003611 }
Winson Chung64359a52013-07-08 17:17:08 -07003612 }
3613
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003614 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003615 int count = orderedScreenIds.size();
3616 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003617 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003618 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003619 // No need to bind the first screen, as its always bound.
3620 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3621 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003622 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003623 }
3624
Winson Chungd64d1762013-08-20 14:37:16 -07003625 public void bindAppsAdded(final ArrayList<Long> newScreens,
3626 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003627 final ArrayList<ItemInfo> addAnimated,
3628 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003629 Runnable r = new Runnable() {
3630 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003631 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003632 }
3633 };
3634 if (waitUntilResume(r)) {
3635 return;
3636 }
3637
Winson Chungd64d1762013-08-20 14:37:16 -07003638 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003639 if (newScreens != null) {
3640 bindAddScreens(newScreens);
3641 }
Winson Chungd64d1762013-08-20 14:37:16 -07003642
3643 // We add the items without animation on non-visible pages, and with
3644 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003645 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003646 bindItems(addNotAnimated, 0,
3647 addNotAnimated.size(), false);
3648 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003649 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003650 bindItems(addAnimated, 0,
3651 addAnimated.size(), true);
3652 }
Winson Chungc58497e2013-09-03 17:48:37 -07003653
Winson Chung87412982013-10-03 18:34:14 -07003654 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003655 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003656
Winson Chungb745afb2015-03-02 11:51:23 -08003657 if (addedApps != null && mAppsView != null) {
3658 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003659 }
Winson Chungd64d1762013-08-20 14:37:16 -07003660 }
3661
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003662 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003663 * Bind the items start-end from the list.
3664 *
3665 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003666 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003667 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003668 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3669 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003670 Runnable r = new Runnable() {
3671 public void run() {
3672 bindItems(shortcuts, start, end, forceAnimateIcons);
3673 }
3674 };
3675 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003676 return;
3677 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003678
Winson Chungf0c6ae02012-03-21 16:10:31 -07003679 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003680 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3681 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003682 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003683 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003684 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003685 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003686 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003687
3688 // Short circuit if we are loading dock items for a configuration which has no dock
3689 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3690 mHotseat == null) {
3691 continue;
3692 }
3693
Sunny Goyal639e9062015-08-19 19:17:06 -07003694 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003695 switch (item.itemType) {
3696 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3697 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003698 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003699 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003700 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003701 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003702 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003703 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003704 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003705 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003706 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003707 default:
3708 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003709 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003710
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003711 /*
3712 * Remove colliding items.
3713 */
3714 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3715 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3716 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3717 View v = cl.getChildAt(item.cellX, item.cellY);
3718 Object tag = v.getTag();
3719 String desc = "Collision while binding workspace item: " + item
3720 + ". Collides with " + tag;
3721 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3722 throw (new RuntimeException(desc));
3723 } else {
3724 Log.d(TAG, desc);
3725 LauncherModel.deleteItemFromDatabase(this, item);
3726 continue;
3727 }
3728 }
3729 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003730 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3731 item.cellY, 1, 1);
3732 if (animateIcons) {
3733 // Animate all the applications up now
3734 view.setAlpha(0f);
3735 view.setScaleX(0f);
3736 view.setScaleY(0f);
3737 bounceAnims.add(createNewAppBounceAnimation(view, i));
3738 newShortcutsScreenId = item.screenId;
3739 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003740 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003741
Winson Chung997a9232013-07-24 15:33:46 -07003742 if (animateIcons) {
3743 // Animate to the correct page
3744 if (newShortcutsScreenId > -1) {
3745 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003746 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003747 final Runnable startBounceAnimRunnable = new Runnable() {
3748 public void run() {
3749 anim.playTogether(bounceAnims);
3750 anim.start();
3751 }
3752 };
Winson Chung997a9232013-07-24 15:33:46 -07003753 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003754 // We post the animation slightly delayed to prevent slowdowns
3755 // when we are loading right after we return to launcher.
3756 mWorkspace.postDelayed(new Runnable() {
3757 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003758 if (mWorkspace != null) {
3759 mWorkspace.snapToPage(newScreenIndex);
3760 mWorkspace.postDelayed(startBounceAnimRunnable,
3761 NEW_APPS_ANIMATION_DELAY);
3762 }
Winson Chung94d67682013-09-25 16:29:40 -07003763 }
3764 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003765 } else {
3766 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003767 }
3768 }
Winson Chung64359a52013-07-08 17:17:08 -07003769 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003770 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003771 }
3772
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003773 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3774 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3775 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003776 view.updateAppWidget(null);
3777 view.setOnClickListener(this);
3778 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003779 mWorkspace.requestLayout();
3780 }
3781
Joe Onorato9c1289c2009-08-17 11:03:03 -04003782 /**
3783 * Add the views for a widget to the workspace.
3784 *
3785 * Implementation of the method from LauncherModel.Callbacks.
3786 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003787 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003788 Runnable r = new Runnable() {
3789 public void run() {
3790 bindAppWidget(item);
3791 }
3792 };
3793 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003794 return;
3795 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003796
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003797 if (mIsSafeModeEnabled) {
3798 bindSafeModeWidget(item);
3799 return;
3800 }
3801
Daniel Sandler843e8602010-06-07 14:59:01 -04003802 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3803 if (DEBUG_WIDGETS) {
3804 Log.d(TAG, "bindAppWidget: " + item);
3805 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003806
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003807 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003808
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003809 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3810 // If the provider is not ready, bind as a pending widget.
3811 appWidgetInfo = null;
3812 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3813 // The widget id is not valid. Try to find the widget based on the provider info.
3814 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3815 } else {
3816 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3817 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003818
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003819 // If the provider is ready, but the width is not yet restored, try to restore it.
3820 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3821 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003822 if (appWidgetInfo == null) {
3823 if (DEBUG_WIDGETS) {
3824 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3825 + " belongs to component " + item.providerName
3826 + ", as the povider is null");
3827 }
3828 LauncherModel.deleteItemFromDatabase(this, item);
3829 return;
3830 }
Sunny Goyalff572272014-07-23 13:58:07 -07003831
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003832 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003833 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003834 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3835 // Id has not been allocated yet. Allocate a new id.
3836 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3837 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003838
Sunny Goyald478c832016-04-01 12:04:16 -07003839 // Also try to bind the widget. If the bind fails, the user will be shown
3840 // a click to setup UI, which will ask for the bind permission.
3841 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3842 pendingInfo.spanX = item.spanX;
3843 pendingInfo.spanY = item.spanY;
3844 pendingInfo.minSpanX = item.minSpanX;
3845 pendingInfo.minSpanY = item.minSpanY;
3846 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
3847 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3848 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003849
Sunny Goyald478c832016-04-01 12:04:16 -07003850 // Bind succeeded
3851 if (success) {
3852 // If the widget has a configure activity, it is still needs to set it up,
3853 // otherwise the widget is ready to go.
3854 item.restoreStatus = (appWidgetInfo.configure == null)
3855 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3856 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003857 }
Sunny Goyald478c832016-04-01 12:04:16 -07003858
3859 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003860 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003861 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003862 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003863 // The widget was marked as UI not ready, but there is no configure activity to
3864 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003865 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3866 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003867 }
Sunny Goyalff572272014-07-23 13:58:07 -07003868 }
3869
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003870 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003871 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003872 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3873 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003874 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003875
Sunny Goyal56c73602015-09-25 12:55:01 -07003876 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003877 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003878 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003879 deleteWidgetInfo(item);
3880 return;
3881 }
3882
Sunny Goyal233ee962015-08-03 13:05:01 -07003883 item.minSpanX = appWidgetInfo.minSpanX;
3884 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003885 addAppWidgetToWorkspace(
3886 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3887 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003888 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003889 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003890 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003891 view.updateAppWidget(null);
3892 view.setOnClickListener(this);
3893 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003894 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003895 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003896
Daniel Sandler843e8602010-06-07 14:59:01 -04003897 if (DEBUG_WIDGETS) {
3898 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3899 + (SystemClock.uptimeMillis()-start) + "ms");
3900 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003901 }
3902
Sunny Goyalff572272014-07-23 13:58:07 -07003903 /**
3904 * Restores a pending widget.
3905 *
3906 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003907 */
Sunny Goyald478c832016-04-01 12:04:16 -07003908 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003909 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3910 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3911 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003912 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003913 }
3914
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003915 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003916 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003917
3918 mWorkspace.reinflateWidgetsIfNecessary();
3919 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003920 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003921 }
3922
Adam Cohen1462de32012-07-24 22:34:36 -07003923 public void onPageBoundSynchronously(int page) {
3924 mSynchronouslyBoundPages.add(page);
3925 }
3926
Sunny Goyal527c7d32015-08-28 15:19:36 -07003927 @Override
3928 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3929 if (mPendingExecutor != null) {
3930 mPendingExecutor.markCompleted();
3931 }
3932 mPendingExecutor = executor;
3933 executor.attachTo(this);
3934 }
3935
3936 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3937 if (mPendingExecutor == executor) {
3938 mPendingExecutor = null;
3939 }
3940 }
3941
Joe Onorato9c1289c2009-08-17 11:03:03 -04003942 /**
3943 * Callback saying that there aren't any more items to bind.
3944 *
3945 * Implementation of the method from LauncherModel.Callbacks.
3946 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003947 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003948 Runnable r = new Runnable() {
3949 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003950 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003951 }
3952 };
3953 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003954 return;
3955 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003956 if (LauncherAppState.PROFILE_STARTUP) {
3957 Trace.beginSection("Page bind completed");
3958 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003959 if (mSavedState != null) {
3960 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003961 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003962 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003963
Joe Onorato9c1289c2009-08-17 11:03:03 -04003964 mSavedState = null;
3965 }
3966
Adam Cohen1462de32012-07-24 22:34:36 -07003967 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003968
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003969 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003970
3971 // If we received the result of any pending adds while the loader was running (e.g. the
3972 // widget configuration forced an orientation change), process them now.
3973 if (sPendingAddItem != null) {
3974 final long screenId = completeAdd(sPendingAddItem);
3975
3976 // TODO: this moves the user to the page where the pending item was added. Ideally,
3977 // the screen would be guaranteed to exist after bind, and the page would be set through
3978 // the workspace restore process.
3979 mWorkspace.post(new Runnable() {
3980 @Override
3981 public void run() {
3982 mWorkspace.snapToScreenId(screenId);
3983 }
3984 });
3985 sPendingAddItem = null;
3986 }
3987
Sunny Goyal756adbc2015-04-16 15:20:51 -07003988 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003989
3990 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003991 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003992 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003993 if (LauncherAppState.PROFILE_STARTUP) {
3994 Trace.endSection();
3995 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003996 }
3997
Winson Chunga2413752012-04-03 14:22:34 -07003998 private boolean canRunNewAppsAnimation() {
3999 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004000 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4001 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004002 }
4003
Winson Chungc9168342013-06-26 14:54:55 -07004004 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004005 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004006 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4007 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004008 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004009 return bounceAnim;
4010 }
4011
Adam Cohen27772732013-11-11 14:00:56 +00004012 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004013 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004014 }
4015
Tony Wickham55616cd2015-09-23 14:55:17 -07004016 public int getSearchBarHeight() {
4017 if (mLauncherCallbacks != null) {
4018 return mLauncherCallbacks.getSearchBarHeight();
4019 }
4020 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4021 }
4022
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004023 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004024 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4025 * multiple calls to bind the same list.)
4026 */
4027 @Thunk ArrayList<AppInfo> mTmpAppsList;
4028 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4029 public void run() {
4030 bindAllApplications(mTmpAppsList);
4031 mTmpAppsList = null;
4032 }
4033 };
4034
4035 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004036 * Add the icons for all apps.
4037 *
4038 * Implementation of the method from LauncherModel.Callbacks.
4039 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004040 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004041 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4042 mTmpAppsList = apps;
4043 return;
4044 }
4045
Winson Chungb745afb2015-03-02 11:51:23 -08004046 if (mAppsView != null) {
4047 mAppsView.setApps(apps);
4048 }
Adam Cohen9211d422014-10-07 18:14:53 -07004049 if (mLauncherCallbacks != null) {
4050 mLauncherCallbacks.bindAllApplications(apps);
4051 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004052 }
4053
4054 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004055 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
4056 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
4057 */
4058 @Override
4059 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
4060 mDeepShortcutMap = deepShortcutMapCopy;
4061 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
4062 }
4063
Tony Wickham1bce7fd2016-04-28 17:39:03 -07004064 public List<String> getShortcutIdsForItem(ItemInfo info) {
4065 if (!DeepShortcutManager.supportsShortcuts(info)) {
4066 return Collections.EMPTY_LIST;
4067 }
4068 ComponentName component = info.getTargetComponent();
4069 List<String> ids = mDeepShortcutMap.get(new ComponentKey(component, info.user));
4070 return ids == null ? Collections.EMPTY_LIST : ids;
4071 }
4072
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004073 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004074 * A package was updated.
4075 *
4076 * Implementation of the method from LauncherModel.Callbacks.
4077 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004078 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004079 Runnable r = new Runnable() {
4080 public void run() {
4081 bindAppsUpdated(apps);
4082 }
4083 };
4084 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004085 return;
4086 }
4087
Winson Chungb745afb2015-03-02 11:51:23 -08004088 if (mAppsView != null) {
4089 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004090 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004091 }
4092
Sunny Goyal4390ace2014-10-13 11:33:11 -07004093 @Override
4094 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4095 Runnable r = new Runnable() {
4096 public void run() {
4097 bindWidgetsRestored(widgets);
4098 }
4099 };
4100 if (waitUntilResume(r)) {
4101 return;
4102 }
4103 mWorkspace.widgetsRestored(widgets);
4104 }
4105
Joe Onorato9c1289c2009-08-17 11:03:03 -04004106 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004107 * Some shortcuts were updated in the background.
4108 *
4109 * Implementation of the method from LauncherModel.Callbacks.
4110 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004111 @Override
4112 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4113 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004114 Runnable r = new Runnable() {
4115 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004116 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004117 }
4118 };
4119 if (waitUntilResume(r)) {
4120 return;
4121 }
4122
Sunny Goyal4390ace2014-10-13 11:33:11 -07004123 if (!updated.isEmpty()) {
4124 mWorkspace.updateShortcuts(updated);
4125 }
4126
4127 if (!removed.isEmpty()) {
4128 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4129 for (ShortcutInfo si : removed) {
4130 removedComponents.add(si.getTargetComponent());
4131 }
4132 mWorkspace.removeItemsByComponentName(removedComponents, user);
4133 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004134 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004135 }
4136 }
4137
4138 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004139 * Update the state of a package, typically related to install state.
4140 *
4141 * Implementation of the method from LauncherModel.Callbacks.
4142 */
Sunny Goyale755d462014-07-22 13:48:29 -07004143 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004144 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4145 Runnable r = new Runnable() {
4146 public void run() {
4147 bindRestoreItemsChange(updates);
4148 }
4149 };
4150 if (waitUntilResume(r)) {
4151 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004152 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004153
Sunny Goyal756adbc2015-04-16 15:20:51 -07004154 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004155 }
4156
4157 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004158 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004159 * in addition to specific applications to remove, the reason being that
4160 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004161 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004162 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004163 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004164 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004165 public void bindWorkspaceComponentsRemoved(
4166 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4167 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004168 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004169 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004170 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004171 }
Winson Chungd64d1762013-08-20 14:37:16 -07004172 };
4173 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004174 return;
4175 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004176 if (!packageNames.isEmpty()) {
4177 mWorkspace.removeItemsByPackageName(packageNames, user);
4178 }
4179 if (!components.isEmpty()) {
4180 mWorkspace.removeItemsByComponentName(components, user);
4181 }
4182 // Notify the drag controller
4183 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004184
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004185 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004186
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004187 @Override
4188 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4189 Runnable r = new Runnable() {
4190 public void run() {
4191 bindAppInfosRemoved(appInfos);
4192 }
4193 };
4194 if (waitUntilResume(r)) {
4195 return;
Joe Onorato36115782010-06-17 13:28:48 -04004196 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004197
Winson Chungdf95eb12013-10-16 14:57:07 -07004198 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004199 if (mAppsView != null) {
4200 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004201 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004202 }
Joe Onoratobe386092009-11-17 17:32:16 -08004203
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004204 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004205 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004206 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004207 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004208 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004209
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004210 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004211 public void bindWidgetsModel(WidgetsModel model) {
4212 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004213 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004214 return;
4215 }
4216
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004217 if (mWidgetsView != null && model != null) {
4218 mWidgetsView.addWidgets(model);
4219 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004220 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004221 }
4222
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004223 @Override
4224 public void notifyWidgetProvidersChanged() {
4225 if (mWorkspace.getState().shouldUpdateWidget) {
4226 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4227 }
4228 }
4229
Winson Chung400438b2011-01-16 17:53:48 -08004230 private int mapConfigurationOriActivityInfoOri(int configOri) {
4231 final Display d = getWindowManager().getDefaultDisplay();
4232 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4233 switch (d.getRotation()) {
4234 case Surface.ROTATION_0:
4235 case Surface.ROTATION_180:
4236 // We are currently in the same basic orientation as the natural orientation
4237 naturalOri = configOri;
4238 break;
4239 case Surface.ROTATION_90:
4240 case Surface.ROTATION_270:
4241 // We are currently in the other basic orientation to the natural orientation
4242 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4243 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4244 break;
4245 }
4246
4247 int[] oriMap = {
4248 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4249 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4250 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4251 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4252 };
4253 // Since the map starts at portrait, we need to offset if this device's natural orientation
4254 // is landscape.
4255 int indexOffset = 0;
4256 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4257 indexOffset = 1;
4258 }
4259 return oriMap[(d.getRotation() + indexOffset) % 4];
4260 }
Adam Cohen446e9402011-09-15 18:21:21 -07004261
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004262 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004263 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004264 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004265 if (Utilities.ATLEAST_JB_MR2) {
4266 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4267 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004268 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4269 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004270 }
Winson Chung4b919f82012-05-01 10:44:08 -07004271 }
4272 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004273
Winson Chung4b919f82012-05-01 10:44:08 -07004274 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004275 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004276 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004277 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004278 } else {
4279 mHandler.postDelayed(new Runnable() {
4280 public void run() {
4281 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4282 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004283 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004284 }
Winson Chung4b919f82012-05-01 10:44:08 -07004285 }
Winson Chung400438b2011-01-16 17:53:48 -08004286 }
4287
Adam Cohenea90f832014-05-21 15:03:34 -07004288 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004289 * To be overridden by subclasses to indicate that there is an activity to launch
4290 * before showing the standard launcher experience.
4291 */
4292 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004293 if (mLauncherCallbacks != null) {
4294 return mLauncherCallbacks.hasFirstRunActivity();
4295 }
Adam Cohen432609a2014-03-13 17:03:22 -07004296 return false;
4297 }
4298
4299 /**
4300 * To be overridden by subclasses to launch any first run activity
4301 */
4302 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004303 if (mLauncherCallbacks != null) {
4304 return mLauncherCallbacks.getFirstRunActivity();
4305 }
Adam Cohen432609a2014-03-13 17:03:22 -07004306 return null;
4307 }
4308
Winson Chunga6945242014-01-08 14:04:34 -08004309 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004310 return !ActivityManager.isRunningInTestHarness() &&
4311 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004312 }
4313
Adam Cohen4a9423d2014-03-28 14:22:31 -07004314 protected boolean hasRunFirstRunActivity() {
4315 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4316 }
4317
Adam Cohen432609a2014-03-13 17:03:22 -07004318 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004319 if (shouldRunFirstRunActivity() &&
4320 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004321 Intent firstRunIntent = getFirstRunActivity();
4322 if (firstRunIntent != null) {
4323 startActivity(firstRunIntent);
4324 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004325 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004326 }
4327 }
Adam Cohen432609a2014-03-13 17:03:22 -07004328 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004329 }
4330
4331 private void markFirstRunActivityShown() {
4332 SharedPreferences.Editor editor = mSharedPrefs.edit();
4333 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4334 editor.apply();
4335 }
4336
Adam Cohen432609a2014-03-13 17:03:22 -07004337 /**
4338 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4339 * screen that must be displayed and dismissed.
4340 */
4341 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004342 if (mLauncherCallbacks != null) {
4343 return mLauncherCallbacks.hasDismissableIntroScreen();
4344 }
Adam Cohen432609a2014-03-13 17:03:22 -07004345 return false;
4346 }
4347
4348 /**
4349 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4350 */
4351 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004352 if (mLauncherCallbacks != null) {
4353 return mLauncherCallbacks.getIntroScreen();
4354 }
Adam Cohen432609a2014-03-13 17:03:22 -07004355 return null;
4356 }
4357
4358 /**
4359 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4360 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4361 */
4362 private boolean shouldShowIntroScreen() {
4363 return hasDismissableIntroScreen() &&
4364 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4365 }
4366
4367 protected void showIntroScreen() {
4368 View introScreen = getIntroScreen();
4369 changeWallpaperVisiblity(false);
4370 if (introScreen != null) {
4371 mDragLayer.showOverlayView(introScreen);
4372 }
4373 }
4374
4375 public void dismissIntroScreen() {
4376 markIntroScreenDismissed();
4377 if (showFirstRunActivity()) {
4378 // We delay hiding the intro view until the first run activity is showing. This
4379 // avoids a blip.
4380 mWorkspace.postDelayed(new Runnable() {
4381 @Override
4382 public void run() {
4383 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004384 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004385 }
4386 }, ACTIVITY_START_DELAY);
4387 } else {
4388 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004389 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004390 }
4391 changeWallpaperVisiblity(true);
4392 }
4393
4394 private void markIntroScreenDismissed() {
4395 SharedPreferences.Editor editor = mSharedPrefs.edit();
4396 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4397 editor.apply();
4398 }
4399
Adam Cohen091440a2015-03-18 14:16:05 -07004400 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004401 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4402 // on the device, then we always show the first run cling experience (or if there is no
4403 // launcher2). Otherwise, we prompt the user upon started for migration
4404 LauncherClings launcherClings = new LauncherClings(this);
4405 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004406 mClings = launcherClings;
Sunny Goyalded0fdb2016-05-23 15:55:41 -07004407 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004408 }
4409 }
4410
Winson Chung5ffd51d2015-06-25 11:36:50 -07004411 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004412 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004413 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004414 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004415 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004416 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004417 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4418 if (userHandle != null) {
4419 user = UserHandleCompat.fromUser(userHandle);
4420 }
4421 }
4422 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004423 }
4424
4425 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004426 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004427 if (user == null) {
4428 user = UserHandleCompat.myUserHandle();
4429 }
4430
4431 // Called from search suggestion, add the profile extra to the intent to ensure that we
4432 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004433 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004434 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004435 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004436 return null;
4437 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004438 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004439 }
4440
Winson Chung5ffd51d2015-06-25 11:36:50 -07004441 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004442 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4443 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004444 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004445 UserHandleCompat.myUserHandle());
4446 }
4447
Winson Chung5ffd51d2015-06-25 11:36:50 -07004448 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004449 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4450 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004451 mWorkspace.onExternalDragStartedWithItem(dragView);
4452 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004453 }
4454
Winson Chung5ffd51d2015-06-25 11:36:50 -07004455 protected void moveWorkspaceToDefaultScreen() {
4456 mWorkspace.moveToDefaultScreen(false);
4457 }
4458
Winson Chung80baf5a2010-08-09 16:03:15 -07004459 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004460 * Returns a FastBitmapDrawable with the icon, accurately sized.
4461 */
4462 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4463 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4464 d.setFilterBitmap(true);
4465 resizeIconDrawable(d);
4466 return d;
4467 }
4468
4469 /**
4470 * Resizes an icon drawable to the correct icon size.
4471 */
Winson5fbe0742015-09-30 15:33:00 -07004472 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004473 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004474 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004475 }
4476
4477 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004478 * Prints out out state for debugging.
4479 */
4480 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004481 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004482 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004483 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4484 Log.d(TAG, "mRestoring=" + mRestoring);
4485 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004486 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004487 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004488
Daniel Sandler325dc232013-06-05 22:57:57 -04004489 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004490 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004491
4492 @Override
4493 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4494 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004495 // Dump workspace
4496 writer.println(prefix + "Workspace Items");
4497 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4498 writer.println(prefix + " Homescreen " + i);
4499
4500 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4501 for (int j = 0; j < layout.getChildCount(); j++) {
4502 Object tag = layout.getChildAt(j).getTag();
4503 if (tag != null) {
4504 writer.println(prefix + " " + tag.toString());
4505 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004506 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004507 }
Sunny Goyala1365452015-10-01 15:46:24 -07004508
4509 writer.println(prefix + " Hotseat");
4510 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4511 for (int j = 0; j < layout.getChildCount(); j++) {
4512 Object tag = layout.getChildAt(j).getTag();
4513 if (tag != null) {
4514 writer.println(prefix + " " + tag.toString());
4515 }
4516 }
4517
Sunny Goyal713edfc2016-05-06 09:58:34 -07004518 try {
4519 FileLog.flushAll(writer);
4520 } catch (Exception e) {
4521 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004522 }
4523
Adam Cohen9211d422014-10-07 18:14:53 -07004524 if (mLauncherCallbacks != null) {
4525 mLauncherCallbacks.dump(prefix, fd, writer, args);
4526 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004527 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004528
Adam Cohen59400422014-03-05 18:07:04 -08004529 public static CustomAppWidget getCustomAppWidget(String name) {
4530 return sCustomAppWidgets.get(name);
4531 }
4532
4533 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4534 return sCustomAppWidgets;
4535 }
4536
Sunny Goyal29538332016-02-18 09:10:19 -08004537 public static List<View> getFolderContents(View icon) {
4538 if (icon instanceof FolderIcon) {
4539 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4540 } else {
4541 return Collections.EMPTY_LIST;
4542 }
4543 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004544
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004545 public static Launcher getLauncher(Context context) {
4546 if (context instanceof Launcher) {
4547 return (Launcher) context;
4548 }
4549 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4550 }
4551
Sunny Goyal745bad92016-05-02 10:54:12 -07004552 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4553
4554 @Override
4555 public void onSharedPreferenceChanged(
4556 SharedPreferences sharedPreferences, String key) {
4557 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4558 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4559 if (!waitUntilResume(this, true)) {
4560 run();
4561 }
4562 }
4563 }
4564
4565 @Override
4566 public void run() {
4567 setOrientation();
4568 }
4569 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004570}