blob: 9e3dfb4533d4df102bde74b5100b8f4f78a553f1 [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
Adam Cohen61f560d2013-09-30 15:58:20 -0700296 private View.OnTouchListener mHapticFeedbackTouchListener;
297
Adam Cohended9f8d2010-11-03 13:25:16 -0700298 // Related to the auto-advancing of widgets
299 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700300 private static final int ADVANCE_INTERVAL = 20000;
301 private static final int ADVANCE_STAGGER = 250;
302
303 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700304 private long mAutoAdvanceSentTime;
305 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700306 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700307
Winson Chung400438b2011-01-16 17:53:48 -0800308 // Determines how long to wait after a rotation before restoring the screen orientation to
309 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700310 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800311
Adam Cohen091440a2015-03-18 14:16:05 -0700312 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700313
Adam Cohen1462de32012-07-24 22:34:36 -0700314 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700315 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700316
Winson Chung46353de2012-02-16 14:05:10 -0800317 // We only want to get the SharedPreferences once since it does an FS stat each time we get
318 // it from the context.
319 private SharedPreferences mSharedPrefs;
320
Winson Chungf0c6ae02012-03-21 16:10:31 -0700321 // Holds the page that we need to animate to, and the icon views that we need to animate up
322 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700323 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700324 private Bitmap mFolderIconBitmap;
325 private Canvas mFolderIconCanvas;
326 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700327
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700328 private DeviceProfile mDeviceProfile;
329
Adam Cohen4b66bf32015-09-18 12:15:19 -0700330 private boolean mMoveToDefaultScreenFromNewIntent;
331
Winson Chung13eb5272015-05-11 16:30:13 -0700332 // This is set to the view that launched the activity that navigated the user away from
333 // launcher. Since there is no callback for when the activity has finished launching, enable
334 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800335 private BubbleTextView mWaitingForResume;
336
Adam Cohen59400422014-03-05 18:07:04 -0800337 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
338 new HashMap<String, CustomAppWidget>();
339
Adam Cohen59400422014-03-05 18:07:04 -0800340 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700341 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
342 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800343 }
344 }
345
Adam Cohen091440a2015-03-18 14:16:05 -0700346 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700347 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700348 if (mWorkspace != null) {
349 mWorkspace.buildPageHardwareLayers();
350 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700351 }
352 };
353
Adam Cohendb364c32014-05-20 14:23:40 -0700354 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800355
Adam Cohen091440a2015-03-18 14:16:05 -0700356 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800357 int requestCode;
358 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700359 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700360 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800361 int cellX;
362 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700363 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800364 }
365
Hyunyoung Songaa953652016-04-19 18:30:24 -0700366 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800367
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700368 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700369 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400370
371 @Thunk void setOrientation() {
372 if (mRotationEnabled) {
373 unlockScreenOrientation(true);
374 } else {
375 setRequestedOrientation(
376 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700377 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400378 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700379
Sunny Goyal745bad92016-05-02 10:54:12 -0700380 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700381
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 @Override
383 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700384 if (DEBUG_STRICT_MODE) {
385 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
386 .detectDiskReads()
387 .detectDiskWrites()
388 .detectNetwork() // or .detectAll() for all detectable problems
389 .penaltyLog()
390 .build());
391 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
392 .detectLeakedSqlLiteObjects()
393 .detectLeakedClosableObjects()
394 .penaltyLog()
395 .penaltyDeath()
396 .build());
397 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700398 if (LauncherAppState.PROFILE_STARTUP) {
399 Trace.beginSection("Launcher-onCreate");
400 }
Winson Chunga2413752012-04-03 14:22:34 -0700401
Adam Cohen9211d422014-10-07 18:14:53 -0700402 if (mLauncherCallbacks != null) {
403 mLauncherCallbacks.preOnCreate();
404 }
405
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400407
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400408 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700409
Adam Cohen2e6da152015-05-06 11:42:25 -0700410 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700411 mDeviceProfile = getResources().getConfiguration().orientation
412 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700413 app.getInvariantDeviceProfile().landscapeProfile
414 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700415
Sunny Goyalf7258242015-10-19 16:59:07 -0700416 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700417 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800418 mModel = app.setLauncher(this);
419 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700420 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700421
Joe Onorato41a12d22009-10-31 18:30:00 -0400422 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700423 mAllAppsController = new AllAppsTransitionController(this);
424 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700425
Sunny Goyalffe83f12014-08-14 17:39:34 -0700426 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700427
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700428 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
429 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700430
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700431 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
432 // this also ensures that any synchronous binding below doesn't re-trigger another
433 // LauncherModel load.
434 mPaused = false;
435
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700437
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700439 mDeviceProfile.layout(this);
Tony Wickham827cef22016-03-17 15:39:39 -0700440 mExtractedColors = new ExtractedColors();
441 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442
Tony Wickhame2217252016-03-22 16:34:23 -0700443 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
444 .addAccessibilityStateChangeListener(this);
445
Joe Onorato7c312c12009-08-13 21:36:53 -0700446 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700447
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 mSavedState = savedInstanceState;
449 restoreState(mSavedState);
450
Sunny Goyale26d1002016-06-20 14:52:14 -0700451 if (LauncherAppState.PROFILE_STARTUP) {
452 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 }
454
455 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700456 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700457 // If the user leaves launcher, then we should just load items asynchronously when
458 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700459 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700460 } else {
461 // We only load the page synchronously if the user rotates (or triggers a
462 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700463 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700464 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800465 }
466
467 // For handling default keys
468 mDefaultKeySsb = new SpannableStringBuilder();
469 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800470
471 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700472 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
473 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800474
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800475 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700476 // In case we are on a device with locked rotation, we should look at preferences to check
477 // if the user has specifically allowed rotation.
478 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700479 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700480 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
481 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700482 }
483
484 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
485 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400486 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700487
Adam Cohen9211d422014-10-07 18:14:53 -0700488 if (mLauncherCallbacks != null) {
489 mLauncherCallbacks.onCreate(savedInstanceState);
490 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700491
492 if (shouldShowIntroScreen()) {
493 showIntroScreen();
494 } else {
495 showFirstRunActivity();
Adam Cohenc3e12c72014-10-31 16:15:36 -0700496 }
Adam Cohen9211d422014-10-07 18:14:53 -0700497 }
498
Sunny Goyal7779d622015-06-11 16:18:39 -0700499 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700500 public void onExtractedColorsChanged() {
501 loadExtractedColorsAndColorItems();
502 }
503
504 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700505 // TODO: do this in pre-N as well, once the extraction part is complete.
506 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700507 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700508 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700509 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham827cef22016-03-17 15:39:39 -0700510 }
511 }
512
Adam Cohen9211d422014-10-07 18:14:53 -0700513 private LauncherCallbacks mLauncherCallbacks;
514
515 public void onPostCreate(Bundle savedInstanceState) {
516 super.onPostCreate(savedInstanceState);
517 if (mLauncherCallbacks != null) {
518 mLauncherCallbacks.onPostCreate(savedInstanceState);
519 }
520 }
521
Sunny Goyal32554d12015-12-03 15:31:25 -0800522 /**
523 * Call this after onCreate to set or clear overlay.
524 */
525 public void setLauncherOverlay(LauncherOverlay overlay) {
526 if (overlay != null) {
527 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
528 }
529 mWorkspace.setLauncherOverlay(overlay);
530 }
531
Adam Cohen9211d422014-10-07 18:14:53 -0700532 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
533 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700534 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700535 private boolean mWorkspaceImportanceStored = false;
536 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700537 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800538 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700539 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
540
541 @Override
542 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700543 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700544 return;
545 }
546 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700547 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700548 if (mWorkspace != null) {
549 mWorkspaceImportanceForAccessibility =
550 mWorkspace.getImportantForAccessibility();
551 mWorkspace.setImportantForAccessibility(
552 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
553 mWorkspaceImportanceStored = true;
554 }
555 if (mHotseat != null) {
556 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
557 mHotseat.setImportantForAccessibility(
558 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
559 mHotseatImportanceStored = true;
560 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700561 }
562
563 @Override
564 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700565 if (mWorkspaceImportanceStored && mWorkspace != null) {
566 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
567 }
568 if (mHotseatImportanceStored && mHotseat != null) {
569 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
570 }
571 mWorkspaceImportanceStored = false;
572 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700573 }
574 });
Adam Cohen9211d422014-10-07 18:14:53 -0700575 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700576 }
577
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700578 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700579 public void onLauncherProviderChange() {
580 if (mLauncherCallbacks != null) {
581 mLauncherCallbacks.onLauncherProviderChange();
582 }
583 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700584
Adam Cohen9211d422014-10-07 18:14:53 -0700585 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700586 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700587 if (mLauncherCallbacks != null) {
588 return mLauncherCallbacks.hasCustomContentToLeft();
589 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700590 return false;
591 }
592
Dave Hawkeya8881582013-09-17 15:55:33 -0600593 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500594 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600595 * {@link #addToCustomContentPage}. This will only be invoked if
596 * {@link #hasCustomContentToLeft()} is {@code true}.
597 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500598 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700599 if (mLauncherCallbacks != null) {
600 mLauncherCallbacks.populateCustomContentContainer();
601 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600602 }
603
604 /**
605 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
606 * ensure the custom content page is added or removed if necessary.
607 */
608 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600609 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600610 // Not bound yet, wait for bindScreens to be called.
611 return;
612 }
613
614 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
615 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500616 mWorkspace.createCustomContentContainer();
617 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600618 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
619 mWorkspace.removeCustomContentPage();
620 }
621 }
622
Hyunyoung Songaa953652016-04-19 18:30:24 -0700623 public UserEventDispatcher getUserEventDispatcher() {
624 if (mLauncherCallbacks != null) {
625 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
626 if (dispatcher != null) {
627 return dispatcher;
628 }
629 }
630
Sunny Goyal64976d52016-06-20 14:56:28 -0700631 // Logger object is a singleton and does not have to be coupled with the foreground
632 // activity. Since most user event logging is done on the UI, the object is retrieved
633 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700634 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700635 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700636 }
637 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800638 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100639
Hyunyoung Song3f471442015-04-08 19:01:34 -0700640 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700641 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
642 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700643 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700644 }
645
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000646 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700647 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
648 // This cast is safe as long as the id < 0x00FFFFFF
649 // Since we jail all the dynamically generated views, there should be no clashes
650 // with any other views.
651 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000652 }
653
654 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700655 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
656 * a configuration step, this allows the proper animations to run after other transitions.
657 */
Adam Cohendb364c32014-05-20 14:23:40 -0700658 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700659 long screenId = args.screenId;
660 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
661 // When the screen id represents an actual screen (as opposed to a rank) we make sure
662 // that the drop page actually exists.
663 screenId = ensurePendingDropLayoutExists(args.screenId);
664 }
Adam Cohendb364c32014-05-20 14:23:40 -0700665
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800666 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800667 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700668 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700669 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700670 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800671 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700672 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700673 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700674 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyald478c832016-04-01 12:04:16 -0700675 completeRestoreAppWidget(args.appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700676 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700677 case REQUEST_BIND_PENDING_APPWIDGET: {
678 int widgetId = args.appWidgetId;
679 LauncherAppWidgetInfo info =
680 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
681 if (info != null) {
682 // Since the view was just bound, also launch the configure activity if needed
683 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
684 .getLauncherAppWidgetInfo(widgetId);
685 if (provider != null && provider.configure != null) {
686 startRestoredWidgetReconfigActivity(provider, info);
687 }
688 }
689 break;
690 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800691 }
Michael Jurka27614d22012-04-02 06:40:22 -0700692 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
693 // if you turned the screen off and then back while in All Apps, Launcher would not
694 // return to the workspace. Clearing mAddInfo.container here fixes this issue
695 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700696 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800697 }
698
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800699 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700700 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700701 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700702 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700703 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800704 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700705
Adam Cohenad4e15c2013-10-17 16:21:35 -0700706 Runnable exitSpringLoaded = new Runnable() {
707 @Override
708 public void run() {
709 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
710 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
711 }
712 };
713
Michael Jurka8b805b12012-04-18 14:23:14 -0700714 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700715 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700716 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700717 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
718 if (resultCode == RESULT_CANCELED) {
719 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700720 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700721 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700722 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800723 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700724 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700725 }
726 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200727 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
728 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700729 // User could have free-scrolled between pages before picking a wallpaper; make sure
730 // we move to the closest one now.
731 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700732 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200733 }
734 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700735 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200736
Adam Cohened66b2b2012-01-23 17:28:51 -0800737 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700738 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700739
Adam Cohendb364c32014-05-20 14:23:40 -0700740 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800741 // We have special handling for widgets
742 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800743 final int appWidgetId;
744 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
745 : -1;
746 if (widgetId < 0) {
747 appWidgetId = pendingAddWidgetId;
748 } else {
749 appWidgetId = widgetId;
750 }
751
Adam Cohenad4e15c2013-10-17 16:21:35 -0700752 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800753 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700754 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
755 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700756 result = RESULT_CANCELED;
757 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700758 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700759 @Override
760 public void run() {
761 exitSpringLoadedDragModeDelayed(false, 0, null);
762 }
763 };
Adam Cohendb364c32014-05-20 14:23:40 -0700764 if (workspaceLocked) {
765 // No need to remove the empty screen if we're mid-binding, as the
766 // the bind will not add the empty screen.
767 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
768 } else {
769 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
770 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
771 }
Winson Chung5aaab772012-04-27 15:24:02 -0700772 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700773 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700774 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
775 // When the screen id represents an actual screen (as opposed to a rank)
776 // we make sure that the drop page actually exists.
777 mPendingAddInfo.screenId =
778 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
779 }
Adam Cohendb364c32014-05-20 14:23:40 -0700780 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
781
782 dropLayout.setDropPending(true);
783 final Runnable onComplete = new Runnable() {
784 @Override
785 public void run() {
786 completeTwoStageWidgetDrop(resultCode, appWidgetId);
787 dropLayout.setDropPending(false);
788 }
789 };
790 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
791 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
792 } else {
793 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
794 mPendingAddInfo);
795 sPendingAddItem = args;
796 }
Winson Chung5aaab772012-04-27 15:24:02 -0700797 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800798 return;
799 }
800
Sunny Goyald478c832016-04-01 12:04:16 -0700801 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
802 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700803 if (resultCode == RESULT_OK) {
804 // Update the widget view.
805 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
806 pendingAddWidgetId, mPendingAddInfo);
807 if (workspaceLocked) {
808 sPendingAddItem = args;
809 } else {
810 completeAdd(args);
811 }
812 }
813 // Leave the widget in the pending state if the user canceled the configure.
814 return;
815 }
816
Sunny Goyalaad90582015-07-09 14:22:50 -0700817 if (requestCode == REQUEST_CREATE_SHORTCUT) {
818 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
819 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
820 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
821 mPendingAddInfo);
822 if (isWorkspaceLocked()) {
823 sPendingAddItem = args;
824 } else {
825 completeAdd(args);
826 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
827 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
828 }
829 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700830 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
831 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800834 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800835
836 }
837
838 @Override
839 protected void onActivityResult(
840 final int requestCode, final int resultCode, final Intent data) {
841 handleActivityResult(requestCode, resultCode, data);
842 if (mLauncherCallbacks != null) {
843 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
844 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800845 }
846
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600847 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700848 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600849 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700850 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
851 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
852 View v = null;
853 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
854 if (layout != null) {
855 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
856 }
857 Intent intent = sPendingAddItem.intent;
858 sPendingAddItem = null;
859 if (grantResults.length > 0
860 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700861 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700862 } else {
863 // TODO: Show a snack bar with link to settings
864 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
865 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
866 }
867 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600868 if (mLauncherCallbacks != null) {
869 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
870 grantResults);
871 }
872 }
873
Adam Cohendb364c32014-05-20 14:23:40 -0700874 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
875 appWidgetId, ItemInfo info) {
876 PendingAddArguments args = new PendingAddArguments();
877 args.requestCode = requestCode;
878 args.intent = data;
879 args.container = info.container;
880 args.screenId = info.screenId;
881 args.cellX = info.cellX;
882 args.cellY = info.cellY;
883 args.appWidgetId = appWidgetId;
884 return args;
885 }
886
887 /**
888 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
889 *
890 * @param screenId the screen id to check
891 * @return the new screen, or screenId if it exists
892 */
893 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800894 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700895 if (dropLayout == null) {
896 // it's possible that the add screen was removed because it was
897 // empty and a re-bind occurred
898 mWorkspace.addExtraEmptyScreen();
899 return mWorkspace.commitExtraEmptyScreen();
900 } else {
901 return screenId;
902 }
903 }
904
Adam Cohen091440a2015-03-18 14:16:05 -0700905 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800906 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800907 Runnable onCompleteRunnable = null;
908 int animationType = 0;
909
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700910 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800911 if (resultCode == RESULT_OK) {
912 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
913 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700914 mPendingAddWidgetInfo);
915 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800916 onCompleteRunnable = new Runnable() {
917 @Override
918 public void run() {
919 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700920 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700921 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
922 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800923 }
924 };
925 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800926 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800927 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800928 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700929 if (mDragLayer.getAnimatedView() != null) {
930 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
931 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
932 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700933 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700934 // The animated view may be null in the case of a rotation during widget configuration
935 onCompleteRunnable.run();
936 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937 }
938
939 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700940 protected void onStop() {
941 super.onStop();
942 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700943
944 if (mLauncherCallbacks != null) {
945 mLauncherCallbacks.onStop();
946 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700947 }
948
949 @Override
950 protected void onStart() {
951 super.onStart();
952 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700953
954 if (mLauncherCallbacks != null) {
955 mLauncherCallbacks.onStart();
956 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700957 }
958
959 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200961 long startTime = 0;
962 if (DEBUG_RESUME_TIME) {
963 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400964 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200965 }
Adam Cohen9211d422014-10-07 18:14:53 -0700966
967 if (mLauncherCallbacks != null) {
968 mLauncherCallbacks.preOnResume();
969 }
970
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700972 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700973
Winson Chung4a2afa32012-07-19 14:53:05 -0700974 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700975 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700976 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800977 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700978 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700979 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700980 // view after launching an app, as they may be depending on the UI to be static to
981 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700982 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700983 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800984 } else if (mOnResumeState == State.WIDGETS) {
985 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700986 }
987 mOnResumeState = State.NONE;
988
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700989 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700990 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
991 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700992
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800993 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700994 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700995 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700996 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -0400997 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700998 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001000 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001001 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1002 // execute them here
1003 long startTimeCallbacks = 0;
1004 if (DEBUG_RESUME_TIME) {
1005 startTimeCallbacks = System.currentTimeMillis();
1006 }
1007
Michael Jurka1e2f4652013-07-08 18:03:46 -07001008 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1009 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001010 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001011 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001012 if (DEBUG_RESUME_TIME) {
1013 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1014 (System.currentTimeMillis() - startTimeCallbacks));
1015 }
Michael Jurka447bf842013-05-15 14:52:15 +02001016 }
Michael Jurka54554252013-08-01 12:52:23 +02001017 if (mOnResumeCallbacks.size() > 0) {
1018 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1019 mOnResumeCallbacks.get(i).run();
1020 }
1021 mOnResumeCallbacks.clear();
1022 }
Winson Chunge4e50662012-01-23 14:45:13 -08001023
1024 // Reset the pressed state of icons that were locked in the press state while activities
1025 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001026 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001027 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001028 mWaitingForResume.setStayPressed(false);
1029 }
Winson Chung82963d52013-10-09 11:20:57 -07001030
Adam Cohen06dff352012-06-01 17:17:08 -07001031 // It is possible that widgets can receive updates while launcher is not in the foreground.
1032 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1033 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1034 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001035 if (!isWorkspaceLoading()) {
1036 getWorkspace().reinflateWidgetsIfNecessary();
1037 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001038
Michael Jurka447bf842013-05-15 14:52:15 +02001039 if (DEBUG_RESUME_TIME) {
1040 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1041 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001042
Adam Cohen4b66bf32015-09-18 12:15:19 -07001043 // We want to suppress callbacks about CustomContent being shown if we have just received
1044 // onNewIntent while the user was present within launcher. In that case, we post a call
1045 // to move the user to the main screen (which will occur after onResume). We don't want to
1046 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1047 // suppress here.
1048 if (mWorkspace.getCustomContentCallbacks() != null
1049 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001050 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001051 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001052 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1053 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001054 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001055 }
1056 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001057 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001058 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001059 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001060
Sunny Goyal756adbc2015-04-16 15:20:51 -07001061 if (!isWorkspaceLoading()) {
1062 // Process any items that were added while Launcher was away.
1063 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1064 }
Adam Cohen9211d422014-10-07 18:14:53 -07001065
1066 if (mLauncherCallbacks != null) {
1067 mLauncherCallbacks.onResume();
1068 }
Adam Cohen06dff352012-06-01 17:17:08 -07001069 }
1070
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001072 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001073 // Ensure that items added to Launcher are queued until Launcher returns
1074 InstallShortcutReceiver.enableInstallQueue();
1075
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001076 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001077 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001078 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001079 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001080
1081 // We call onHide() aggressively. The custom content callbacks should be able to
1082 // debounce excess onHide calls.
1083 if (mWorkspace.getCustomContentCallbacks() != null) {
1084 mWorkspace.getCustomContentCallbacks().onHide();
1085 }
Romain Guycbb89e42009-06-08 15:52:54 -07001086
Adam Cohen9211d422014-10-07 18:14:53 -07001087 if (mLauncherCallbacks != null) {
1088 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001089 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001090 }
1091
1092 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001093 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1094 // by a onResume or by scrolling otherwise.
1095 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001096
1097 // Custom content is completely hidden
1098 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001099
1100 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1101 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001102
1103 // Indicates whether the user is allowed to scroll away from the custom content.
1104 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001105 }
1106
Adam Cohenc2d6e892014-10-16 09:49:24 -07001107 public interface LauncherOverlay {
1108
1109 /**
1110 * Touch interaction leading to overscroll has begun
1111 */
1112 public void onScrollInteractionBegin();
1113
1114 /**
1115 * Touch interaction related to overscroll has ended
1116 */
1117 public void onScrollInteractionEnd();
1118
1119 /**
1120 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1121 * screen (or in the case of RTL, the rightmost screen).
1122 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001123 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001124
1125 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001126 * Called when the launcher is ready to use the overlay
1127 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001128 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001129 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001130 }
1131
Jun Mukai8af0cd82015-05-12 12:32:12 -07001132 public interface LauncherSearchCallbacks {
1133 /**
1134 * Called when the search overlay is shown.
1135 */
1136 public void onSearchOverlayOpened();
1137
1138 /**
1139 * Called when the search overlay is dismissed.
1140 */
1141 public void onSearchOverlayClosed();
1142 }
1143
Adam Cohenc2d6e892014-10-16 09:49:24 -07001144 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001145 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001146 }
1147
1148 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1149
Sunny Goyalc86df472016-02-25 09:19:38 -08001150 public void onScrollChanged(float progress) {
1151 if (mWorkspace != null) {
1152 mWorkspace.onOverlayScrollChanged(progress);
1153 }
1154 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001155 }
1156
Jorim Jaggid017f882014-01-14 17:08:48 -08001157 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001158 if (mLauncherCallbacks != null) {
1159 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001160 } else {
1161 // On devices with a locked orientation, we will at least have the allow rotation
1162 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001163 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001164 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001165 }
1166
Adam Cohen9211d422014-10-07 18:14:53 -07001167 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001168 CustomContentCallbacks callbacks, String description) {
1169 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001170 }
1171
Adam Cohenedb40762013-07-18 16:45:45 -07001172 // The custom content needs to offset its content to account for the QSB
1173 public int getTopOffsetForCustomContent() {
1174 return mWorkspace.getPaddingTop();
1175 }
1176
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001177 @Override
1178 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001179 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001180 if (mModel.isCurrentCallbacks(this)) {
1181 mModel.stopLoader();
1182 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001183 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1184
Romain Guy13c2e7b2010-03-10 19:45:00 -08001185 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001186 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001187
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001188 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001189 @Override
1190 public void onWindowFocusChanged(boolean hasFocus) {
1191 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001192 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001193
1194 if (mLauncherCallbacks != null) {
1195 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1196 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001197 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001198
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 private boolean acceptFilter() {
1200 final InputMethodManager inputManager = (InputMethodManager)
1201 getSystemService(Context.INPUT_METHOD_SERVICE);
1202 return !inputManager.isFullscreenMode();
1203 }
1204
1205 @Override
1206 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001207 final int uniChar = event.getUnicodeChar();
1208 final boolean handled = super.onKeyDown(keyCode, event);
1209 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1210 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001211 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1212 keyCode, event);
1213 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001214 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001215 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001216 // showSearchDialog()
1217 // If there are multiple keystrokes before the search dialog takes focus,
1218 // onSearchRequested() will be called for every keystroke,
1219 // but it is idempotent, so it's fine.
1220 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221 }
1222 }
1223
Joe Onorato8a9625e2010-01-28 15:55:35 -08001224 // Eat the long press event so the keyboard doesn't come up.
1225 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1226 return true;
1227 }
1228
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229 return handled;
1230 }
1231
Winson46163472015-09-22 17:31:56 -07001232 @Override
1233 public boolean onKeyUp(int keyCode, KeyEvent event) {
1234 if (keyCode == KeyEvent.KEYCODE_MENU) {
1235 // Ignore the menu key if we are currently dragging or are on the custom content screen
1236 if (!isOnCustomContent() && !mDragController.isDragging()) {
1237 // Close any open folders
1238 closeFolder();
1239
1240 // Stop resizing any widgets
1241 mWorkspace.exitWidgetResizeMode();
1242
1243 // Show the overview mode if we are on the workspace
1244 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1245 !mWorkspace.isSwitchingState()) {
1246 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001247 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001248 }
1249 }
1250 return true;
1251 }
1252 return super.onKeyUp(keyCode, event);
1253 }
1254
Karl Rosaen138a0412009-04-23 19:00:21 -07001255 private String getTypedText() {
1256 return mDefaultKeySsb.toString();
1257 }
1258
1259 private void clearTypedText() {
1260 mDefaultKeySsb.clear();
1261 mDefaultKeySsb.clearSpans();
1262 Selection.setSelection(mDefaultKeySsb, 0);
1263 }
1264
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001266 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1267 * State
1268 */
1269 private static State intToState(int stateOrdinal) {
1270 State state = State.WORKSPACE;
1271 final State[] stateValues = State.values();
1272 for (int i = 0; i < stateValues.length; i++) {
1273 if (stateValues[i].ordinal() == stateOrdinal) {
1274 state = stateValues[i];
1275 break;
1276 }
1277 }
1278 return state;
1279 }
1280
1281 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 * Restores the previous state, if it exists.
1283 *
1284 * @param savedState The previous state.
1285 */
1286 private void restoreState(Bundle savedState) {
1287 if (savedState == null) {
1288 return;
1289 }
1290
Michael Jurka883f55b2010-10-21 15:47:14 -07001291 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001292 if (state == State.APPS || state == State.WIDGETS) {
1293 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001294 }
1295
Adam Cohen21cd0022013-10-09 18:57:02 -07001296 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1297 PagedView.INVALID_RESTORE_PAGE);
1298 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001299 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 }
1301
Sunny Goyal756cd262015-08-20 12:33:21 -07001302 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1303 if (itemValues != null) {
1304 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001305 AppWidgetProviderInfo info = savedState.getParcelable(
1306 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001307 mPendingAddWidgetInfo = info == null ?
1308 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1309
Adam Cohen4637b5a2013-11-04 18:21:24 -08001310 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001311 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 mRestoring = true;
1313 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314 }
1315
1316 /**
1317 * Finds all the views we need and configure them properly.
1318 */
1319 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001320 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001321 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001322 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001323 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001324 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001325
Sunny Goyal784f9c32016-03-23 16:56:54 -07001326 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1327 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1328 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001329 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330
Winson Chung4c98d922011-05-31 16:50:48 -07001331 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001332
1333 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001334
Winson Chung3d503fb2011-07-13 17:25:49 -07001335 // Setup the hotseat
1336 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1337 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001338 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001339 }
1340
Winsone9f27272015-10-13 10:47:51 -07001341 // Setup the overview panel
1342 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001343
Winson Chung4c98d922011-05-31 16:50:48 -07001344 // Setup the workspace
1345 mWorkspace.setHapticFeedbackEnabled(false);
1346 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001347 mWorkspace.setup(mDragController);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001348 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001349 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001350
Tony Wickham34d2c912015-09-11 09:27:58 -07001351 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001352 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001353
Winson Chungef7f8742015-06-04 17:18:17 -07001354 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001355 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001356 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001357 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1358 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1359 } else {
1360 mAppsView.setSearchBarController(new DefaultAppSearchController());
1361 }
Craig Mautner360310b2012-10-26 15:13:08 -07001362
Winson Chung3d503fb2011-07-13 17:25:49 -07001363 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001364 mDragController.setDragScoller(mWorkspace);
1365 mDragController.setScrollView(mDragLayer);
1366 mDragController.setMoveTarget(mWorkspace);
1367 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001368 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001369
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001370 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1371
Sunny Goyal322d5562015-06-25 19:35:49 -07001372 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1373 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001374 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375 }
1376
Winsone9f27272015-10-13 10:47:51 -07001377 private void setupOverviewPanel() {
1378 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1379
1380 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1381 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1382 @Override
1383 public boolean onLongClick(View v) {
1384 return v.performClick();
1385 }
1386 };
1387
1388 // Bind wallpaper button actions
1389 View wallpaperButton = findViewById(R.id.wallpaper_button);
1390 wallpaperButton.setOnClickListener(new OnClickListener() {
1391 @Override
1392 public void onClick(View view) {
1393 if (!mWorkspace.isSwitchingState()) {
1394 onClickWallpaperPicker(view);
1395 }
1396 }
1397 });
1398 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1399 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1400
1401 // Bind widget button actions
1402 mWidgetsButton = findViewById(R.id.widget_button);
1403 mWidgetsButton.setOnClickListener(new OnClickListener() {
1404 @Override
1405 public void onClick(View view) {
1406 if (!mWorkspace.isSwitchingState()) {
1407 onClickAddWidgetButton(view);
1408 }
1409 }
1410 });
1411 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1412 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1413
1414 // Bind settings actions
1415 View settingsButton = findViewById(R.id.settings_button);
1416 boolean hasSettings = hasSettings();
1417 if (hasSettings) {
1418 settingsButton.setOnClickListener(new OnClickListener() {
1419 @Override
1420 public void onClick(View view) {
1421 if (!mWorkspace.isSwitchingState()) {
1422 onClickSettingsButton(view);
1423 }
1424 }
1425 });
1426 settingsButton.setOnLongClickListener(performClickOnLongClick);
1427 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1428 } else {
1429 settingsButton.setVisibility(View.GONE);
1430 }
1431
1432 mOverviewPanel.setAlpha(0f);
1433 }
1434
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001436 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001437 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001438 */
1439 public void setAllAppsButton(View allAppsButton) {
1440 mAllAppsButton = allAppsButton;
1441 }
1442
Sunny Goyalbb011da2016-06-15 15:42:29 -07001443 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001444 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001445 }
1446
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001447 public View getWidgetsButton() {
1448 return mWidgetsButton;
1449 }
1450
Anjali Koppal5ad44842014-03-10 20:34:39 -07001451 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 * Creates a view representing a shortcut.
1453 *
1454 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001456 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001457 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 }
1459
1460 /**
1461 * Creates a view representing a shortcut inflated from the specified resource.
1462 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 * @param parent The group the shortcut belongs to.
1464 * @param info The data structure describing the shortcut.
1465 *
1466 * @return A View inflated from layoutResId.
1467 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001468 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001469 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001470 parent, false);
1471 favorite.applyFromShortcutInfo(info, mIconCache);
1472 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001474 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 return favorite;
1476 }
1477
1478 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 * Add a shortcut to the workspace.
1480 *
1481 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001483 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001484 int cellY) {
1485 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001486 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001487
Sunny Goyal5c97f512015-05-19 16:03:28 -07001488 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001489 if (info == null) {
1490 return;
1491 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001492 final View view = createShortcut(info);
1493
Sunny Goyal5c97f512015-05-19 16:03:28 -07001494 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001495 // First we check if we already know the exact location where we want to add this item.
1496 if (cellX >= 0 && cellY >= 0) {
1497 cellXY[0] = cellX;
1498 cellXY[1] = cellY;
1499 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001500
1501 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001502 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001503 true, null,null)) {
1504 return;
1505 }
1506 DragObject dragObject = new DragObject();
1507 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001508 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1509 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001510 return;
1511 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001512 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001513 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001514 }
1515
1516 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001517 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001518 return;
1519 }
1520
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001521 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522
1523 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001524 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001525 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 }
1527 }
1528
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001530 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001532 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 */
Adam Cohen091440a2015-03-18 14:16:05 -07001534 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001535 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1536
1537 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001538 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001539 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001540 }
Romain Guycbb89e42009-06-08 15:52:54 -07001541
Adam Cohen59400422014-03-05 18:07:04 -08001542 if (appWidgetInfo.isCustomWidget) {
1543 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001544 }
1545
Adam Cohen59400422014-03-05 18:07:04 -08001546 LauncherAppWidgetInfo launcherInfo;
1547 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1548 launcherInfo.spanX = info.spanX;
1549 launcherInfo.spanY = info.spanY;
1550 launcherInfo.minSpanX = info.minSpanX;
1551 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001552 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001553
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001555 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556
1557 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001558 if (hostView == null) {
1559 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001560 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001561 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001562 hostView.setVisibility(View.VISIBLE);
1563 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001565 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566 }
Romain Guycbb89e42009-06-08 15:52:54 -07001567
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001568 private void addAppWidgetToWorkspace(
1569 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001570 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001571 hostView.setTag(item);
1572 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001573
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001574 hostView.setFocusable(true);
1575 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001576
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001577 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001578 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1579
1580 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001581 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001582 }
1583 }
1584
Adam Cohended9f8d2010-11-03 13:25:16 -07001585 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1586 @Override
1587 public void onReceive(Context context, Intent intent) {
1588 final String action = intent.getAction();
1589 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1590 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001591 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001592 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001593
Winson Chungc100e8e2011-08-09 16:02:43 -07001594 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001595 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001596 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001597 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001598 if (!showWorkspace(false)) {
1599 // If we are already on the workspace, then manually reset all apps
1600 mAppsView.reset();
1601 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001602 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001603 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1604 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001605 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001606 }
1607 }
1608 };
1609
1610 @Override
1611 public void onAttachedToWindow() {
1612 super.onAttachedToWindow();
1613
1614 // Listen for broadcasts related to user-presence
1615 final IntentFilter filter = new IntentFilter();
1616 filter.addAction(Intent.ACTION_SCREEN_OFF);
1617 filter.addAction(Intent.ACTION_USER_PRESENT);
1618 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001619 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001620 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001621 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001622
1623 if (mLauncherCallbacks != null) {
1624 mLauncherCallbacks.onAttachedToWindow();
1625 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001626 }
1627
1628 @Override
1629 public void onDetachedFromWindow() {
1630 super.onDetachedFromWindow();
1631 mVisible = false;
1632
Adam Cohend113e0c2010-11-11 10:48:05 -08001633 if (mAttached) {
1634 unregisterReceiver(mReceiver);
1635 mAttached = false;
1636 }
Winson Chungb745afb2015-03-02 11:51:23 -08001637 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001638
1639 if (mLauncherCallbacks != null) {
1640 mLauncherCallbacks.onDetachedFromWindow();
1641 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001642 }
1643
1644 public void onWindowVisibilityChanged(int visibility) {
1645 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001646 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001647 // The following code used to be in onResume, but it turns out onResume is called when
1648 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1649 // is a more appropriate event to handle
1650 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001651 if (!mWorkspaceLoading) {
1652 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001653 // We want to let Launcher draw itself at least once before we force it to build
1654 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001655 // apps is nice and speedy.
1656 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001657 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001658 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001659 if (mStarted) return;
1660 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001661 // We delay the layer building a bit in order to give
1662 // other message processing a time to run. In particular
1663 // this avoids a delay in hiding the IME if it was
1664 // currently shown, because doing that may involve
1665 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001666 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001667 final ViewTreeObserver.OnDrawListener listener = this;
1668 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001669 public void run() {
1670 if (mWorkspace != null &&
1671 mWorkspace.getViewTreeObserver() != null) {
1672 mWorkspace.getViewTreeObserver().
1673 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001674 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001675 }
1676 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001677 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001678 }
1679 });
1680 }
1681 clearTypedText();
1682 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001683 }
1684
Adam Cohen091440a2015-03-18 14:16:05 -07001685 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001686 mHandler.removeMessages(ADVANCE_MSG);
1687 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1688 mHandler.sendMessageDelayed(msg, delay);
1689 mAutoAdvanceSentTime = System.currentTimeMillis();
1690 }
1691
Adam Cohen091440a2015-03-18 14:16:05 -07001692 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001693 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1694 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1695 mAutoAdvanceRunning = autoAdvanceRunning;
1696 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001697 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001698 sendAdvanceMessage(delay);
1699 } else {
1700 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001701 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001702 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1703 }
1704 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001705 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001706 }
1707 }
1708 }
1709
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001710 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1711
Adam Cohended9f8d2010-11-03 13:25:16 -07001712 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001713 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001714 if (msg.what == ADVANCE_MSG) {
1715 int i = 0;
1716 for (View key: mWidgetsToAdvance.keySet()) {
1717 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001718 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001719 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001720 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001721 public void run() {
1722 ((Advanceable) v).advance();
1723 }
1724 }, delay);
1725 }
1726 i++;
1727 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001728 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001729 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001730 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001731 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001732 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001733
Winsonc0b52fe2015-09-09 16:38:15 -07001734 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001735 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001736 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1737 if (v instanceof Advanceable) {
1738 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001739 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001740 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001741 }
1742 }
1743
Winsonc0b52fe2015-09-09 16:38:15 -07001744 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001745 if (mWidgetsToAdvance.containsKey(hostView)) {
1746 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001747 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001748 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001749 }
1750
Hyunyoung Song3f471442015-04-08 19:01:34 -07001751 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001752 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1753 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001754 }
1755
Winson Chunga6945242014-01-08 14:04:34 -08001756 public DragLayer getDragLayer() {
1757 return mDragLayer;
1758 }
1759
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001760 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001761 return mAppsView;
1762 }
1763
Hyunyoung Song3f471442015-04-08 19:01:34 -07001764 public WidgetsContainerView getWidgetsView() {
1765 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001766 }
1767
Winson Chunga6945242014-01-08 14:04:34 -08001768 public Workspace getWorkspace() {
1769 return mWorkspace;
1770 }
1771
1772 public Hotseat getHotseat() {
1773 return mHotseat;
1774 }
1775
Jorim Jaggid017f882014-01-14 17:08:48 -08001776 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001777 return mOverviewPanel;
1778 }
1779
Sunny Goyal47328fd2016-05-25 18:56:41 -07001780 public DropTargetBar getDropTargetBar() {
1781 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001782 }
1783
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001784 public LauncherAppWidgetHost getAppWidgetHost() {
1785 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 }
Romain Guycbb89e42009-06-08 15:52:54 -07001787
Winson Chunga9abd0e2010-10-27 17:18:37 -07001788 public LauncherModel getModel() {
1789 return mModel;
1790 }
1791
Adam Cohen79d90c52016-04-22 13:29:20 -07001792 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001793 return mSharedPrefs;
1794 }
1795
Adam Cohen2e6da152015-05-06 11:42:25 -07001796 public DeviceProfile getDeviceProfile() {
1797 return mDeviceProfile;
1798 }
1799
Bjorn Bringertc459e522013-06-07 19:36:01 +01001800 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001801 getWindow().closeAllPanels();
1802
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001803 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001804 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001805 }
1806
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807 @Override
1808 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001809 long startTime = 0;
1810 if (DEBUG_RESUME_TIME) {
1811 startTime = System.currentTimeMillis();
1812 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001813 super.onNewIntent(intent);
1814
1815 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001816 Folder openFolder = mWorkspace.getOpenFolder();
1817 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1818 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1819 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1820 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1821 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001822 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001823 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001824
Adam Cohen6fecd412013-10-02 17:41:50 -07001825 if (mWorkspace == null) {
1826 // Can be cases where mWorkspace is null, this prevents a NPE
1827 return;
1828 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001829 // In all these cases, only animate if we're already on home
1830 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001831
Sunny Goyal935fca12015-10-13 10:19:01 -07001832 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001833 exitSpringLoadedDragMode();
1834
1835 // If we are already on home, then just animate back to the workspace,
1836 // otherwise, just wait until onResume to set the state back to Workspace
1837 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001838 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001839 } else {
1840 mOnResumeState = State.WORKSPACE;
1841 }
1842
1843 final View v = getWindow().peekDecorView();
1844 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001845 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001846 INPUT_METHOD_SERVICE);
1847 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1848 }
1849
Winson Chungb745afb2015-03-02 11:51:23 -08001850 // Reset the apps view
1851 if (!alreadyOnHome && mAppsView != null) {
1852 mAppsView.scrollToTop();
1853 }
1854
Hyunyoung Song3f471442015-04-08 19:01:34 -07001855 // Reset the widgets view
1856 if (!alreadyOnHome && mWidgetsView != null) {
1857 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001858 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001859
Adam Cohen9211d422014-10-07 18:14:53 -07001860 if (mLauncherCallbacks != null) {
1861 mLauncherCallbacks.onHomeIntent();
1862 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 }
Adam Cohened307df2013-10-02 09:37:31 -07001864
Adam Cohen9211d422014-10-07 18:14:53 -07001865 if (mLauncherCallbacks != null) {
1866 mLauncherCallbacks.onNewIntent(intent);
1867 }
Winson15f8b172015-08-19 11:02:39 -07001868
1869 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1870 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1871 // animation.
1872 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001873 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1874 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001875 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1876 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001877
1878 // We use this flag to suppress noisy callbacks above custom content state
1879 // from onResume.
1880 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001881 mWorkspace.post(new Runnable() {
1882 @Override
1883 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001884 if (mWorkspace != null) {
1885 mWorkspace.moveToDefaultScreen(true);
1886 }
Winson15f8b172015-08-19 11:02:39 -07001887 }
1888 });
1889 }
1890 }
1891
1892 if (DEBUG_RESUME_TIME) {
1893 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1894 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001895 }
1896
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001898 public void onRestoreInstanceState(Bundle state) {
1899 super.onRestoreInstanceState(state);
1900 for (int page: mSynchronouslyBoundPages) {
1901 mWorkspace.restoreInstanceStateForChild(page);
1902 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 }
1904
1905 @Override
1906 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001907 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001908 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1909 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001910
Adam Cohen21cd0022013-10-09 18:57:02 -07001911 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001912 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913
Michael Jurka883f55b2010-10-21 15:47:14 -07001914 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001915 // We close any open folder since it will not be re-opened, and we need to make sure
1916 // this state is reflected.
1917 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1918 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919
Adam Cohendcd297f2013-06-18 13:13:40 -07001920 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001921 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001922 ContentValues itemValues = new ContentValues();
1923 mPendingAddInfo.writeToValues(itemValues);
1924 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001925 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001926 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 }
1928
Adam Cohen9211d422014-10-07 18:14:53 -07001929 if (mLauncherCallbacks != null) {
1930 mLauncherCallbacks.onSaveInstanceState(outState);
1931 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 }
1933
1934 @Override
1935 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001937
Winson Chunge7a03942011-08-05 15:05:12 -07001938 // Remove all pending runnables
1939 mHandler.removeMessages(ADVANCE_MSG);
1940 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001941 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001942 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001943
Winson Chungcd2b0142011-06-08 16:02:26 -07001944 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001945 // It's possible to receive onDestroy after a new Launcher activity has
1946 // been created. In this case, don't interfere with the new Launcher.
1947 if (mModel.isCurrentCallbacks(this)) {
1948 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001949 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001950 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001951
Sunny Goyal745bad92016-05-02 10:54:12 -07001952 if (mRotationPrefChangeHandler != null) {
1953 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1954 }
1955
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001957 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001959 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001961 mAppWidgetHost = null;
1962
1963 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964
1965 TextKeyListener.getInstance().release();
1966
Tony Wickhame2217252016-03-22 16:34:23 -07001967 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1968 .removeAccessibilityStateChangeListener(this);
1969
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001970 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001971
Michael Jurka2ecf9952012-06-18 12:52:28 -07001972 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001973
1974 if (mLauncherCallbacks != null) {
1975 mLauncherCallbacks.onDestroy();
1976 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 }
1978
Sunny Goyalae502842016-06-17 08:43:56 -07001979 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1980 return mAccessibilityDelegate;
1981 }
1982
Adam Cohena9cf38f2011-05-02 15:36:58 -07001983 public DragController getDragController() {
1984 return mDragController;
1985 }
1986
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 @Override
1988 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07001989 onStartForResult(requestCode);
1990 super.startActivityForResult(intent, requestCode);
1991 }
1992
1993 @Override
1994 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1995 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
1996 onStartForResult(requestCode);
1997 try {
1998 super.startIntentSenderForResult(intent, requestCode,
1999 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2000 } catch (IntentSender.SendIntentException e) {
2001 throw new ActivityNotFoundException();
2002 }
2003 }
2004
2005 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002006 if (requestCode >= 0) {
2007 setWaitingForResult(true);
2008 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 }
2010
Winson Chung70d72102011-08-12 11:24:35 -07002011 /**
2012 * Indicates that we want global search for this activity by setting the globalSearch
2013 * argument for {@link #startSearch} to true.
2014 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002016 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002018
Karl Rosaen138a0412009-04-23 19:00:21 -07002019 if (initialQuery == null) {
2020 // Use any text typed in the launcher as the initial query
2021 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002022 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 if (appSearchData == null) {
2024 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002025 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002027
2028 // TODO send proper bounds.
2029 Rect sourceBounds = null;
Romain Guycbb89e42009-06-08 15:52:54 -07002030
Ian Parkinson047036e2014-09-01 15:40:53 +01002031 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002032 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002033 if (clearTextImmediately) {
2034 clearTypedText();
2035 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002036
2037 // We need to show the workspace after starting the search
2038 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002039 }
2040
Ian Parkinson047036e2014-09-01 15:40:53 +01002041 /**
2042 * Start a text search.
2043 *
2044 * @return {@code true} if the search will start immediately, so any further keypresses
2045 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2046 * to buffer keypresses.
2047 */
2048 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002049 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002050 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2051 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2052 sourceBounds);
2053 }
2054
Michael Jurkaa33411c2012-06-14 16:18:21 -07002055 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002056 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002057 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002058 }
2059
2060 /**
2061 * Starts the global search activity. This code is a copied from SearchManager
2062 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002063 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002064 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002065 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002066 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2067 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2068 if (globalSearchActivity == null) {
2069 Log.w(TAG, "No global search activity found.");
2070 return;
2071 }
2072 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2073 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2074 intent.setComponent(globalSearchActivity);
2075 // Make sure that we have a Bundle to put source in
2076 if (appSearchData == null) {
2077 appSearchData = new Bundle();
2078 } else {
2079 appSearchData = new Bundle(appSearchData);
2080 }
Adam Cohen9211d422014-10-07 18:14:53 -07002081 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002082 if (!appSearchData.containsKey("source")) {
2083 appSearchData.putString("source", getPackageName());
2084 }
2085 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2086 if (!TextUtils.isEmpty(initialQuery)) {
2087 intent.putExtra(SearchManager.QUERY, initialQuery);
2088 }
2089 if (selectInitialQuery) {
2090 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2091 }
2092 intent.setSourceBounds(sourceBounds);
2093 try {
2094 startActivity(intent);
2095 } catch (ActivityNotFoundException ex) {
2096 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2097 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002098 }
2099
Yura4f93ec62014-02-04 14:15:21 +00002100 public boolean isOnCustomContent() {
2101 return mWorkspace.isOnOrMovingToCustomContent();
2102 }
2103
Winson Chung70d72102011-08-12 11:24:35 -07002104 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002105 public boolean onPrepareOptionsMenu(Menu menu) {
2106 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002107 if (mLauncherCallbacks != null) {
2108 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2109 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002110 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002111 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002112
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002113 @Override
2114 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002115 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002116 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002117 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002118 }
2119
Joe Onorato9c1289c2009-08-17 11:03:03 -04002120 public boolean isWorkspaceLocked() {
2121 return mWorkspaceLoading || mWaitingForResult;
2122 }
2123
Adam Cohen517a7f52014-03-01 12:12:59 -08002124 public boolean isWorkspaceLoading() {
2125 return mWorkspaceLoading;
2126 }
2127
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002128 private void setWorkspaceLoading(boolean value) {
2129 boolean isLocked = isWorkspaceLocked();
2130 mWorkspaceLoading = value;
2131 if (isLocked != isWorkspaceLocked()) {
2132 onWorkspaceLockedChanged();
2133 }
2134 }
2135
2136 private void setWaitingForResult(boolean value) {
2137 boolean isLocked = isWorkspaceLocked();
2138 mWaitingForResult = value;
2139 if (isLocked != isWorkspaceLocked()) {
2140 onWorkspaceLockedChanged();
2141 }
2142 }
2143
Adam Cohen9211d422014-10-07 18:14:53 -07002144 protected void onWorkspaceLockedChanged() {
2145 if (mLauncherCallbacks != null) {
2146 mLauncherCallbacks.onWorkspaceLockedChanged();
2147 }
2148 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002149
Michael Jurka0280c3b2010-09-17 15:00:07 -07002150 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002151 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002152 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002153 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2154 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002155 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002156 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002157
Tony Wickhama0628cc2015-10-14 15:23:04 -07002158 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002159 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002160 if (LOGD) {
2161 Log.d(TAG, "Adding widget from drop");
2162 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002163 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2164 }
2165
Sunny Goyale6b63a32015-01-16 16:14:29 -08002166 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002167 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2168 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002169 if (appWidgetInfo.configure != null) {
2170 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002171 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002172
Bjorn Bringert7984c942009-12-09 15:38:25 +00002173 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002174 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2175 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2176
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002177 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002178 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002179 Runnable onComplete = new Runnable() {
2180 @Override
2181 public void run() {
2182 // Exit spring loaded mode if necessary after adding the widget
2183 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2184 null);
2185 }
2186 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002187 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002188 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002189 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002190 }
2191 }
Romain Guycbb89e42009-06-08 15:52:54 -07002192
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002193 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002194 // Close any folders that may be open.
2195 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002196 mWorkspace.moveToCustomContentScreen(animate);
2197 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002198
2199 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2200 int[] cell, int spanX, int spanY) {
2201 switch (info.itemType) {
2202 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2203 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2204 int span[] = new int[2];
2205 span[0] = spanX;
2206 span[1] = spanY;
2207 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2208 container, screenId, cell, span);
2209 break;
2210 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2211 processShortcutFromDrop(info.componentName, container, screenId, cell);
2212 break;
2213 default:
2214 throw new IllegalStateException("Unknown item type: " + info.itemType);
2215 }
2216 }
2217
Adam Cohenfbba09b2011-07-18 21:43:05 -07002218 /**
2219 * Process a shortcut drop.
2220 *
2221 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002222 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002223 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002224 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002225 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2226 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002227 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002228 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002229 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002230
2231 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002232 mPendingAddInfo.cellX = cell[0];
2233 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002234 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002235
2236 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2237 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002238 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002239 }
2240
Adam Cohenfbba09b2011-07-18 21:43:05 -07002241 /**
2242 * Process a widget drop.
2243 *
2244 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002245 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002246 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002247 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002248 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2249 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002250 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002251 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002252 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002253 mPendingAddInfo.minSpanX = info.minSpanX;
2254 mPendingAddInfo.minSpanY = info.minSpanY;
2255
Adam Cohenfbba09b2011-07-18 21:43:05 -07002256 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002257 mPendingAddInfo.cellX = cell[0];
2258 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002259 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002260 if (span != null) {
2261 mPendingAddInfo.spanX = span[0];
2262 mPendingAddInfo.spanY = span[1];
2263 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002264
Adam Cohened66b2b2012-01-23 17:28:51 -08002265 AppWidgetHostView hostView = info.boundWidget;
2266 int appWidgetId;
2267 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002268 // In the case where we've prebound the widget, we remove it from the DragLayer
2269 if (LOGD) {
2270 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2271 }
2272 getDragLayer().removeView(hostView);
2273
Adam Cohened66b2b2012-01-23 17:28:51 -08002274 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002275 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002276
2277 // Clear the boundWidget so that it doesn't get destroyed.
2278 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002279 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002280 // In this case, we either need to start an activity to get permission to bind
2281 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002282 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002283 Bundle options = info.bindOptions;
2284
Sunny Goyalffe83f12014-08-14 17:39:34 -07002285 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2286 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002287 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002288 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002289 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002290 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002291 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2292 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2293 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002294 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2295 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002296 // TODO: we need to make sure that this accounts for the options bundle.
2297 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002298 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2299 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002300 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002301 }
2302
Adam Cohendcd297f2013-06-18 13:13:40 -07002303 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002304 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002305 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002306 folderInfo.title = getText(R.string.folder_name);
2307
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002308 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002309 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2310 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002311
2312 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002313 FolderIcon newFolder =
2314 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002315 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002316 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002317 // Force measure the new folder icon
2318 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2319 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002320 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002321 }
Romain Guycbb89e42009-06-08 15:52:54 -07002322
Winsonc0b52fe2015-09-09 16:38:15 -07002323 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002324 * Unbinds the view for the specified item, and removes the item and all its children.
2325 *
2326 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002327 * @param itemInfo the {@link ItemInfo} for this view.
2328 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002329 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002330 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002331 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002332 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002333 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2334 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002335 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002336 } else {
2337 mWorkspace.removeWorkspaceItem(v);
2338 }
Winsonc0b52fe2015-09-09 16:38:15 -07002339 if (deleteFromDb) {
2340 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2341 }
2342 } else if (itemInfo instanceof FolderInfo) {
2343 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002344 if (v instanceof FolderIcon) {
2345 ((FolderIcon) v).removeListeners();
2346 }
Winsonc0b52fe2015-09-09 16:38:15 -07002347 mWorkspace.removeWorkspaceItem(v);
2348 if (deleteFromDb) {
2349 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2350 }
2351 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2352 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002353 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002354 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002355 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002356 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002357 }
2358 } else {
2359 return false;
2360 }
2361 return true;
2362 }
2363
2364 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002365 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002366 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002367 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002368 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002369 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002370 // Deleting an app widget ID is a void call but writes to disk before returning
2371 // to the caller...
2372 new AsyncTask<Void, Void, Void>() {
2373 public Void doInBackground(Void ... args) {
2374 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2375 return null;
2376 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002377 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002378 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002379 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002380 }
2381
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002382 @Override
2383 public boolean dispatchKeyEvent(KeyEvent event) {
2384 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2385 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002386 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002387 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002388 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002389 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002390 dumpState();
2391 return true;
2392 }
2393 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002394 }
2395 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2396 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002397 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002398 return true;
2399 }
2400 }
2401
2402 return super.dispatchKeyEvent(event);
2403 }
2404
Joe Onorato88ec0992009-11-19 13:16:06 -08002405 @Override
2406 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002407 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2408 return;
2409 }
2410
Sunny Goyal45478022015-06-08 16:52:41 -07002411 if (mDragController.isDragging()) {
2412 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002413 return;
2414 }
2415
Winson Chungb745afb2015-03-02 11:51:23 -08002416 if (isAppsViewVisible()) {
2417 showWorkspace(true);
2418 } else if (isWidgetsViewVisible()) {
2419 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002420 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002421 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002422 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002423 Folder openFolder = mWorkspace.getOpenFolder();
2424 if (openFolder.isEditingName()) {
2425 openFolder.dismissEditingName();
2426 } else {
2427 closeFolder();
2428 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002429 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002430 mWorkspace.exitWidgetResizeMode();
2431
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002432 // Back button is a no-op here, but give at least some feedback for the button press
2433 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002434 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002435 }
2436
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002437 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438 * Launches the intent referred by the clicked shortcut.
2439 *
2440 * @param v The view representing the clicked shortcut.
2441 */
2442 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002443 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2444 // view has detached (it's possible for this to happen if the view is removed mid touch).
2445 if (v.getWindowToken() == null) {
2446 return;
2447 }
2448
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002449 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002450 return;
2451 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002452
Adam Cohen1697b792013-09-17 19:08:21 -07002453 if (v instanceof Workspace) {
2454 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002455 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002456 }
2457 return;
2458 }
2459
2460 if (v instanceof CellLayout) {
2461 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002462 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2463 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002464 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002465 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002466 }
2467
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002468 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002469 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002470 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002471 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002472 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002473 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002474 }
Sunny Goyalbb011da2016-06-15 15:42:29 -07002475 } else if (v instanceof PageIndicator || (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002476 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002477 } else if (tag instanceof AppInfo) {
2478 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002479 } else if (tag instanceof LauncherAppWidgetInfo) {
2480 if (v instanceof PendingAppWidgetHostView) {
2481 onClickPendingWidget((PendingAppWidgetHostView) v);
2482 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002483 }
2484 }
2485
Sunny Goyal70660032015-05-14 00:07:08 -07002486 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002487 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002488 return false;
2489 }
2490
Michael Jurkaaf442092010-06-10 17:01:57 -07002491 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002492 * Event handler for the app widget view which has not fully restored.
2493 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002494 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002495 if (mIsSafeModeEnabled) {
2496 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2497 return;
2498 }
2499
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002500 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002501 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002502 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2503 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2504 // This should not happen, as we make sure that an Id is allocated during bind.
2505 return;
2506 }
2507 LauncherAppWidgetProviderInfo appWidgetInfo =
2508 mAppWidgetManager.findProvider(info.providerName, info.user);
2509 if (appWidgetInfo != null) {
2510 mPendingAddWidgetId = info.appWidgetId;
2511 mPendingAddInfo.copyFrom(info);
2512 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002513
Sunny Goyald478c832016-04-01 12:04:16 -07002514 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2515 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingAddWidgetId);
2516 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
2517 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2518 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2519 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2520 }
2521 } else {
2522 LauncherAppWidgetProviderInfo appWidgetInfo =
2523 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2524 if (appWidgetInfo != null) {
2525 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2526 }
Sunny Goyalff572272014-07-23 13:58:07 -07002527 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002528 } else if (info.installProgress < 0) {
2529 // The install has not been queued
2530 final String packageName = info.providerName.getPackageName();
2531 showBrokenAppInstallDialog(packageName,
2532 new DialogInterface.OnClickListener() {
2533 public void onClick(DialogInterface dialog, int id) {
2534 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2535 }
2536 });
2537 } else {
2538 // Download has started.
2539 final String packageName = info.providerName.getPackageName();
2540 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002541 }
2542 }
2543
Sunny Goyald478c832016-04-01 12:04:16 -07002544 private void startRestoredWidgetReconfigActivity(
2545 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
2546 mPendingAddWidgetInfo = provider;
2547 mPendingAddInfo.copyFrom(info);
2548 mPendingAddWidgetId = info.appWidgetId;
2549 mAppWidgetManager.startConfigActivity(provider,
2550 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2551 }
2552
Sunny Goyalff572272014-07-23 13:58:07 -07002553 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002554 * Event handler for the "grid" button that appears on the home screen, which
2555 * enters all apps mode.
2556 *
2557 * @param v The view that was clicked.
2558 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002559 protected void onClickAllAppsButton(View v) {
2560 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002561 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002562 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002563 true /* updatePredictedApps */, false /* focusSearchBar */);
2564 }
2565 }
2566
2567 protected void onLongClickAllAppsButton(View v) {
2568 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2569 if (!isAppsViewVisible()) {
2570 showAppsView(true /* animated */, false /* resetListToTop */,
2571 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002572 }
2573 }
2574
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002575 private void showBrokenAppInstallDialog(final String packageName,
2576 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002577 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002578 .setTitle(R.string.abandoned_promises_title)
2579 .setMessage(R.string.abandoned_promise_explanation)
2580 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2581 .setNeutralButton(R.string.abandoned_clean_this,
2582 new DialogInterface.OnClickListener() {
2583 public void onClick(DialogInterface dialog, int id) {
2584 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2585 mWorkspace.removeAbandonedPromise(packageName, user);
2586 }
2587 })
2588 .create().show();
2589 return;
2590 }
2591
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002592 /**
2593 * Event handler for an app shortcut click.
2594 *
2595 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2596 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002597 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002598 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2599 Object tag = v.getTag();
2600 if (!(tag instanceof ShortcutInfo)) {
2601 throw new IllegalArgumentException("Input must be a Shortcut");
2602 }
2603
2604 // Open shortcut
2605 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002606
2607 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002608 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2609 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002610 // Launch activity anyway, framework will tell the user why the app is suspended.
2611 } else {
2612 int error = R.string.activity_not_available;
2613 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2614 error = R.string.safemode_shortcut_error;
2615 }
2616 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2617 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002618 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002619 }
2620
Chris Wren40c5ed32014-06-24 18:24:23 -04002621 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002622 if ((v instanceof BubbleTextView)
2623 && shortcut.isPromise()
2624 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002625 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002626 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002627 new DialogInterface.OnClickListener() {
2628 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002629 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002630 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002631 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002632 return;
2633 }
2634
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002635 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002636 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002637 }
2638
Sunny Goyala7ce1662016-05-31 15:01:35 -07002639 private void startAppShortcutOrInfoActivity(View v) {
2640 ItemInfo item = (ItemInfo) v.getTag();
2641 Intent intent = item.getIntent();
2642 if (intent == null) {
2643 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002644 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002645 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002646 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002647
2648 if (success && v instanceof BubbleTextView) {
2649 mWaitingForResume = (BubbleTextView) v;
2650 mWaitingForResume.setStayPressed(true);
2651 }
2652 }
2653
2654 /**
2655 * Event handler for a folder icon click.
2656 *
2657 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2658 */
2659 protected void onClickFolderIcon(View v) {
2660 if (LOGD) Log.d(TAG, "onClickFolder");
2661 if (!(v instanceof FolderIcon)){
2662 throw new IllegalArgumentException("Input must be a FolderIcon");
2663 }
2664
2665 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002666 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002667 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002668 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002669 }
Michael Jurka5130e402011-10-13 04:55:35 -07002670 }
2671
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002672 /**
2673 * Event handler for the (Add) Widgets button that appears after a long press
2674 * on the home screen.
2675 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002676 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002677 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002678 if (mIsSafeModeEnabled) {
2679 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2680 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002681 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002682 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002683 }
2684
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002685 /**
2686 * Event handler for the wallpaper picker button that appears after a long press
2687 * on the home screen.
2688 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002689 public void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002690 if (!Utilities.isWallapaperAllowed(this)) {
2691 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2692 return;
2693 }
2694
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002695 String pickerPackage = getString(R.string.wallpaper_picker_package);
2696 if (TextUtils.isEmpty(pickerPackage)) {
2697 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2698 }
2699
Tony Wickham785f7a52015-08-31 17:28:32 -07002700 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2701 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002702 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER)
2703 .setPackage(pickerPackage)
2704 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset),
2705 REQUEST_PICK_WALLPAPER);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002706 }
2707
2708 /**
2709 * Event handler for a click on the settings button that appears after a long press
2710 * on the home screen.
2711 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002712 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002713 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002714 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2715 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002716 }
2717
Adam Cohen61f560d2013-09-30 15:58:20 -07002718 public View.OnTouchListener getHapticFeedbackTouchListener() {
2719 if (mHapticFeedbackTouchListener == null) {
2720 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002721 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002722 @Override
2723 public boolean onTouch(View v, MotionEvent event) {
2724 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2725 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2726 }
2727 return false;
2728 }
2729 };
2730 }
2731 return mHapticFeedbackTouchListener;
2732 }
2733
Tony Wickhame2217252016-03-22 16:34:23 -07002734 @Override
2735 public void onAccessibilityStateChanged(boolean enabled) {
2736 mDragLayer.onAccessibilityStateChanged(enabled);
2737 }
2738
Adam Cohen9211d422014-10-07 18:14:53 -07002739 public void onDragStarted(View view) {
2740 if (isOnCustomContent()) {
2741 // Custom content screen doesn't participate in drag and drop. If on custom
2742 // content screen, move to default.
2743 moveWorkspaceToDefaultScreen();
2744 }
Adam Cohen9211d422014-10-07 18:14:53 -07002745 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002746
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002747 /**
2748 * Called when the user stops interacting with the launcher.
2749 * This implies that the user is now on the homescreen and is not doing housekeeping.
2750 */
Adam Cohen9211d422014-10-07 18:14:53 -07002751 protected void onInteractionEnd() {
2752 if (mLauncherCallbacks != null) {
2753 mLauncherCallbacks.onInteractionEnd();
2754 }
2755 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002756
2757 /**
2758 * Called when the user starts interacting with the launcher.
2759 * The possible interactions are:
2760 * - open all apps
2761 * - reorder an app shortcut, or a widget
2762 * - open the overview mode.
2763 * This is a good time to stop doing things that only make sense
2764 * when the user is on the homescreen and not doing housekeeping.
2765 */
Adam Cohen9211d422014-10-07 18:14:53 -07002766 protected void onInteractionBegin() {
2767 if (mLauncherCallbacks != null) {
2768 mLauncherCallbacks.onInteractionBegin();
2769 }
2770 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002771
Winson Chungcd99cd32015-04-29 11:03:24 -07002772 /** Updates the interaction state. */
2773 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002774 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002775 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002776 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2777 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002778 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002779 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002780 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002781 onInteractionEnd();
2782 }
2783 }
2784
Sunny Goyala7ce1662016-05-31 15:01:35 -07002785 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002786 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002787 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2788 try {
2789 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2790 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2791 // is enabled by default on NYC.
2792 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2793 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002794
2795 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2796 String id = ((ShortcutInfo) info).getDeepShortcutId();
2797 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002798 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2799 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002800 } else {
2801 // Could be launching some bookkeeping activity
2802 startActivity(intent, optsBundle);
2803 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002804 } finally {
2805 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002806 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002807 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002808 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2809 // corresponding permission. Show the appropriate permission prompt if that
2810 // is the case.
2811 if (intent.getComponent() == null
2812 && Intent.ACTION_CALL.equals(intent.getAction())
2813 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2814 PackageManager.PERMISSION_GRANTED) {
2815 // TODO: Rename sPendingAddItem to a generic name.
2816 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2817 0, info);
2818 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2819 REQUEST_PERMISSION_CALL_PHONE);
2820 } else {
2821 // No idea why this was thrown.
2822 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002823 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002824 }
2825 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002826
Sunny Goyala7ce1662016-05-31 15:01:35 -07002827 private Bundle getActivityLaunchOptions(View v) {
2828 if (Utilities.ATLEAST_MARSHMALLOW) {
2829 int left = 0, top = 0;
2830 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2831 if (v instanceof TextView) {
2832 // Launch from center of icon, not entire view
2833 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2834 if (icon != null) {
2835 Rect bounds = icon.getBounds();
2836 left = (width - bounds.width()) / 2;
2837 top = v.getPaddingTop();
2838 width = bounds.width();
2839 height = bounds.height();
2840 }
2841 }
2842 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2843 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2844 // On L devices, we use the device default slide-up transition.
2845 // On L MR1 devices, we use a custom version of the slide-up transition which
2846 // doesn't have the delay present in the device default.
2847 return ActivityOptions.makeCustomAnimation(
2848 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2849 }
2850 return null;
2851 }
2852
2853 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002854 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2855 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2856 return false;
2857 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002858 // Only launch using the new animation if the shortcut has not opted out (this is a
2859 // private contract between launcher and may be ignored in the future).
2860 boolean useLaunchAnimation = (v != null) &&
2861 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2862 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2863
2864 UserHandleCompat user = null;
2865 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2866 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2867 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002868 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002869
2870 // Prepare intent
2871 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2872 if (v != null) {
2873 int[] pos = new int[2];
2874 v.getLocationOnScreen(pos);
2875 intent.setSourceBounds(
2876 new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2877 }
2878 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002879 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2880 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
2881 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002882 // Shortcuts need some special checks due to legacy reasons.
2883 startShortcutIntentSafely(intent, optsBundle, item);
2884 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2885 // Could be launching some bookkeeping activity
2886 startActivity(intent, optsBundle);
2887 } else {
2888 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2889 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2890 }
2891 return true;
2892 } catch (ActivityNotFoundException|SecurityException e) {
2893 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2894 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2895 }
2896 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002897 }
2898
Adam Cohen268c4752012-06-06 17:47:33 -07002899 /**
2900 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2901 * in the DragLayer in the exact absolute location of the original FolderIcon.
2902 */
2903 private void copyFolderIconToImage(FolderIcon fi) {
2904 final int width = fi.getMeasuredWidth();
2905 final int height = fi.getMeasuredHeight();
2906
2907 // Lazy load ImageView, Bitmap and Canvas
2908 if (mFolderIconImageView == null) {
2909 mFolderIconImageView = new ImageView(this);
2910 }
2911 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2912 mFolderIconBitmap.getHeight() != height) {
2913 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2914 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2915 }
2916
2917 DragLayer.LayoutParams lp;
2918 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2919 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2920 } else {
2921 lp = new DragLayer.LayoutParams(width, height);
2922 }
2923
Adam Cohen307fe232012-08-16 17:55:58 -07002924 // The layout from which the folder is being opened may be scaled, adjust the starting
2925 // view size by this scale factor.
2926 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002927 lp.customPosition = true;
2928 lp.x = mRectForFolderAnimation.left;
2929 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002930 lp.width = (int) (scale * width);
2931 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002932
2933 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2934 fi.draw(mFolderIconCanvas);
2935 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002936 if (fi.getFolder() != null) {
2937 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2938 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002939 }
Adam Cohen268c4752012-06-06 17:47:33 -07002940 // Just in case this image view is still in the drag layer from a previous animation,
2941 // we remove it and re-add it.
2942 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2943 mDragLayer.removeView(mFolderIconImageView);
2944 }
2945 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002946 if (fi.getFolder() != null) {
2947 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002948 }
Adam Cohen268c4752012-06-06 17:47:33 -07002949 }
2950
Adam Cohen37b2a492016-04-06 18:36:06 -07002951 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002952 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002953 FolderInfo info = (FolderInfo) fi.getTag();
2954 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2955 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002956 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2957 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002958 }
2959
Adam Cohen268c4752012-06-06 17:47:33 -07002960 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2961 copyFolderIconToImage(fi);
2962 fi.setVisibility(View.INVISIBLE);
2963
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002964 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2965 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002966 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002967 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2968 }
2969 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002970 oa.start();
2971 }
2972
Sunny Goyal935fca12015-10-13 10:19:01 -07002973 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002974 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002975 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002976
Adam Cohen268c4752012-06-06 17:47:33 -07002977 // We remove and re-draw the FolderIcon in-case it has changed
2978 mDragLayer.removeView(mFolderIconImageView);
2979 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08002980
2981 if (cl != null) {
2982 cl.clearFolderLeaveBehind();
2983 }
2984
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002985 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002986 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002987 oa.addListener(new AnimatorListenerAdapter() {
2988 @Override
2989 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002990 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07002991 // Remove the ImageView copy of the FolderIcon and make the original visible.
2992 mDragLayer.removeView(mFolderIconImageView);
2993 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002994 }
2995 }
2996 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002997 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07002998 if (!animate) {
2999 oa.end();
3000 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003001 }
3002
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003003 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003004 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003005 * is animated relative to the specified View. If the View is null, no animation
3006 * is played.
3007 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003008 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003009 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003010 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003011
Adam Cohenfb91f302012-06-11 15:45:18 -07003012 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003013 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3014 if (openFolder != null && openFolder != folder) {
3015 // Close any open folder before opening a folder.
3016 closeFolder();
3017 }
3018
Adam Cohena9cf38f2011-05-02 15:36:58 -07003019 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003020
Adam Cohena9cf38f2011-05-02 15:36:58 -07003021 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003022
Sunny Goyalf4066152015-04-15 09:42:19 -07003023 // While the folder is open, the position of the icon cannot change.
3024 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3025
Adam Cohen4554ee12011-08-03 16:13:21 -07003026 // Just verify that the folder hasn't already been added to the DragLayer.
3027 // There was a one-off crash where the folder had a parent already.
3028 if (folder.getParent() == null) {
3029 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003030 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003031 } else {
3032 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3033 folder.getParent() + ").");
3034 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003035 folder.animateOpen();
3036
3037 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003038
3039 // Notify the accessibility manager that this folder "window" has appeared and occluded
3040 // the workspace items
3041 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3042 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003043 }
3044
3045 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003046 closeFolder(true);
3047 }
3048
3049 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003050 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003051 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003052 if (folder.isEditingName()) {
3053 folder.dismissEditingName();
3054 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003055 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003056 }
3057 }
3058
Sunny Goyal935fca12015-10-13 10:19:01 -07003059 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003060 animate &= !Utilities.isPowerSaverOn(this);
3061
Adam Cohen4554ee12011-08-03 16:13:21 -07003062 folder.getInfo().opened = false;
3063
3064 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3065 if (parent != null) {
3066 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003067 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003068 if (fi != null) {
3069 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3070 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003071 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003072 if (animate) {
3073 folder.animateClosed();
3074 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003075 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003076 }
Winson Chung83ca4802013-04-12 15:10:52 -07003077
Sunny Goyal935fca12015-10-13 10:19:01 -07003078 // Notify the accessibility manager that this folder "window" has disappeared and no
3079 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003080 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003081 }
3082
Tony Wickhamdadb3042016-02-24 11:07:00 -08003083 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003084 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003085 if (!isDraggingEnabled()) return false;
3086 if (isWorkspaceLocked()) return false;
3087 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003088
Sunny Goyalbb011da2016-06-15 15:42:29 -07003089 if (v == mAllAppsButton && mAllAppsButton != null) {
Winson Chung76648c52015-07-10 14:33:23 -07003090 onLongClickAllAppsButton(v);
3091 return true;
3092 }
3093
Adam Cohen1697b792013-09-17 19:08:21 -07003094 if (v instanceof Workspace) {
3095 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003096 if (!mWorkspace.isTouchActive()) {
3097 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003098 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3099 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3100 return true;
3101 } else {
3102 return false;
3103 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003104 } else {
3105 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003106 }
Adam Cohen1697b792013-09-17 19:08:21 -07003107 }
3108
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003109 CellLayout.CellInfo longClickCellInfo = null;
3110 View itemUnderLongClick = null;
3111 if (v.getTag() instanceof ItemInfo) {
3112 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003113 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003114 itemUnderLongClick = longClickCellInfo.cell;
3115 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003116 }
3117
Winson Chung3d503fb2011-07-13 17:25:49 -07003118 // The hotseat touch handling does not go through Workspace, and we always allow long press
3119 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003120 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003121 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003122 // User long pressed on empty space
3123 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3124 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003125 if (mWorkspace.isInOverviewMode()) {
3126 mWorkspace.startReordering(v);
3127 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003128 showOverviewMode(true);
Hyunyoung Song645764e2016-06-06 14:19:02 -07003129 mHotseat.requestDisallowInterceptTouchEvent(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003130 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003131 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003132 final boolean isAllAppsButton =
3133 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3134 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3135 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003136 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003137 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003138 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003139 }
3140 }
3141 }
3142 return true;
3143 }
3144
Winson Chung3d503fb2011-07-13 17:25:49 -07003145 boolean isHotseatLayout(View layout) {
3146 return mHotseat != null && layout != null &&
3147 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3148 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003149
Winson Chung3d503fb2011-07-13 17:25:49 -07003150 /**
3151 * Returns the CellLayout of the specified container at the specified screen.
3152 */
Sunny Goyal92820592015-03-02 11:31:35 -08003153 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003154 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3155 if (mHotseat != null) {
3156 return mHotseat.getLayout();
3157 } else {
3158 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003159 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003160 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003161 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003162 }
3163 }
3164
Winson Chungb745afb2015-03-02 11:51:23 -08003165 /**
3166 * For overridden classes.
3167 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003168 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003169 return isAppsViewVisible();
3170 }
3171
3172 public boolean isAppsViewVisible() {
3173 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3174 }
3175
3176 public boolean isWidgetsViewVisible() {
3177 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003178 }
3179
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003180 private void setWorkspaceBackground(int background) {
3181 switch (background) {
3182 case WORKSPACE_BACKGROUND_TRANSPARENT:
3183 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3184 break;
3185 case WORKSPACE_BACKGROUND_BLACK:
3186 getWindow().setBackgroundDrawable(null);
3187 break;
3188 default:
3189 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3190 }
Craig Mautner360310b2012-10-26 15:13:08 -07003191 }
3192
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003193 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003194 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3195 int curflags = getWindow().getAttributes().flags
3196 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3197 if (wpflags != curflags) {
3198 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3199 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003200 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003201 }
3202
Michael Jurkae326f182011-11-21 14:05:46 -08003203 @Override
3204 public void onTrimMemory(int level) {
3205 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003206 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3207 // The widget preview db can result in holding onto over
3208 // 3MB of memory for caching which isn't necessary.
3209 SQLiteDatabase.releaseMemory();
3210
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003211 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003212 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003213 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003214 if (mLauncherCallbacks != null) {
3215 mLauncherCallbacks.onTrimMemory(level);
3216 }
Michael Jurkae326f182011-11-21 14:05:46 -08003217 }
3218
Winson5c6bdbb2015-09-03 11:36:19 -07003219 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003220 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003221 }
3222
Winson5c6bdbb2015-09-03 11:36:19 -07003223 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003224 boolean changed = mState != State.WORKSPACE ||
3225 mWorkspace.getState() != Workspace.State.NORMAL;
3226 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003227 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003228 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003229 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003230
Michael Jurkab3e22d92011-10-31 15:58:33 -07003231 // Set focus to the AppsCustomize button
3232 if (mAllAppsButton != null) {
3233 mAllAppsButton.requestFocus();
3234 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003235 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003236
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003237 // Change the state *after* we've called all the transition code
3238 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003239
Winson Chung5fb63472011-02-02 17:03:37 -08003240 // Resume the auto-advance of widgets
3241 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003242 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003243
Winson Chung0f785722015-04-08 10:27:49 -07003244 if (changed) {
3245 // Send an accessibility event to announce the context change
3246 getWindow().getDecorView()
3247 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003248 }
Winson5c6bdbb2015-09-03 11:36:19 -07003249 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003250 }
3251
Winsone9f27272015-10-13 10:47:51 -07003252 /**
3253 * Shows the overview button.
3254 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07003255 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003256 showOverviewMode(animated, false);
3257 }
3258
3259 /**
3260 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3261 * onto one of the overview panel buttons.
3262 */
3263 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3264 Runnable postAnimRunnable = null;
3265 if (requestButtonFocus) {
3266 postAnimRunnable = new Runnable() {
3267 @Override
3268 public void run() {
3269 // Hitting the menu button when in touch mode does not trigger touch mode to
3270 // be disabled, so if requested, force focus on one of the overview panel
3271 // buttons.
3272 mOverviewPanel.requestFocusFromTouch();
3273 }
3274 };
3275 }
Adam Cohened307df2013-10-02 09:37:31 -07003276 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003277 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003278 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003279 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003280 }
3281
Winson Chungb745afb2015-03-02 11:51:23 -08003282 /**
3283 * Shows the apps view.
3284 */
Hyunyoung Song645764e2016-06-06 14:19:02 -07003285 public void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003286 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003287 if (resetListToTop) {
3288 mAppsView.scrollToTop();
3289 }
Winson Chung4ac30062015-05-08 17:34:17 -07003290 if (updatePredictedApps) {
3291 tryAndUpdatePredictedApps();
3292 }
Winson Chung76648c52015-07-10 14:33:23 -07003293 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003294 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003295
Winson Chungb745afb2015-03-02 11:51:23 -08003296 /**
3297 * Shows the widgets view.
3298 */
3299 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003300 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003301 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003302 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003303 }
Winson Chung76648c52015-07-10 14:33:23 -07003304 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003305
3306 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003307 @Override
3308 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003309 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003310 }
3311 });
Winson Chungb745afb2015-03-02 11:51:23 -08003312 }
3313
3314 /**
3315 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003316 *
3317 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003318 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003319 // TODO: calling method should use the return value so that when {@code false} is returned
3320 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003321 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003322 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3323 mState != State.WIDGETS_SPRING_LOADED) {
3324 return false;
3325 }
3326 if (toState != State.APPS && toState != State.WIDGETS) {
3327 return false;
3328 }
Winson Chungb745afb2015-03-02 11:51:23 -08003329
3330 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003331 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3332 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003333 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003334 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003335 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003336
Michael Jurkab3e22d92011-10-31 15:58:33 -07003337 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003338 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003339
3340 // Pause the auto-advance of widgets until we are out of AllApps
3341 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003342 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003343 closeFolder();
3344
3345 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003346 getWindow().getDecorView()
3347 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003348 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003349 }
3350
Winson Chungcd99cd32015-04-29 11:03:24 -07003351 /**
3352 * Updates the workspace and interaction state on state change, and return the animation to this
3353 * new state.
3354 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003355 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003356 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003357 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003358 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003359 updateInteraction(fromState, toState);
3360 return anim;
3361 }
3362
Hyunyoung Song3f471442015-04-08 19:01:34 -07003363 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003364 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003365 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003366 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003367 }
Winson Chungb745afb2015-03-02 11:51:23 -08003368
Winson Chung006ee262015-08-03 14:40:11 -07003369 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003370 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003371 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003372
3373 if (isAppsViewVisible()) {
3374 mState = State.APPS_SPRING_LOADED;
3375 } else if (isWidgetsViewVisible()) {
3376 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003377 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003378 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003379 } else {
3380 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003381 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003382 }
Adam Cohen7777d962011-08-18 18:58:38 -07003383
Hyunyoung Song3f471442015-04-08 19:01:34 -07003384 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003385 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003386 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003387
Winson Chunge7a03942011-08-05 15:05:12 -07003388 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003389 @Override
3390 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003391 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003392 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3393 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003394 // Before we show workspace, hide all apps again because
3395 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3396 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003397 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003398 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003399 } else {
3400 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003401 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003402 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003403 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003404 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003405
Tony Wickhame0c33232016-02-08 11:37:04 -08003406 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003407 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3408 || mState == State.WIDGETS_SPRING_LOADED;
3409 }
3410
Michael Jurkad3ef3062010-11-23 16:23:58 -08003411 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003412 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003413 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003414 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003415 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003416 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003417 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3418 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003419 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003420 }
3421
Winson Chung4ac30062015-05-08 17:34:17 -07003422 /**
3423 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3424 * resumed.
3425 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003426 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003427 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003428 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003429 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003430 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003431 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003432 }
3433 }
3434 }
3435
Michael Jurkab3e22d92011-10-31 15:58:33 -07003436 void lockAllApps() {
3437 // TODO
3438 }
3439
3440 void unlockAllApps() {
3441 // TODO
3442 }
3443
Winsonf768d932015-09-25 16:12:35 -07003444 public boolean launcherCallbacksProvidesSearch() {
3445 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3446 }
3447
Michael Jurkad7c28052012-04-27 15:43:36 -07003448 @Override
3449 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003450 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003451 final List<CharSequence> text = event.getText();
3452 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003453 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003454 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003455 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003456 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003457 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003458 } else if (mWorkspace != null) {
3459 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003460 } else {
3461 text.add(getString(R.string.all_apps_home_button_label));
3462 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003463 return result;
3464 }
3465
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003466 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003467 * If the activity is currently paused, signal that we need to run the passed Runnable
3468 * in onResume.
3469 *
3470 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003471 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3472 * wrong when we're not running, and if the activity comes back to what the configuration was
3473 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003474 *
3475 * Implementation of the method from LauncherModel.Callbacks.
3476 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003477 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003478 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003479 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003480 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003481 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003482 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003483 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003484 }
3485 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003486 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003487 return true;
3488 } else {
3489 return false;
3490 }
3491 }
3492
Michael Jurkac402cd92013-05-20 15:49:32 +02003493 private boolean waitUntilResume(Runnable run) {
3494 return waitUntilResume(run, false);
3495 }
3496
Michael Jurka1e2f4652013-07-08 18:03:46 -07003497 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003498 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003499 }
3500
Michael Jurka7607c2f2013-04-03 14:33:19 -07003501 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003502 * If the activity is currently paused, signal that we need to re-run the loader
3503 * in onResume.
3504 *
3505 * This needs to be called from incoming places where resources might have been loaded
3506 * while we are paused. That is becaues the Configuration might be wrong
3507 * when we're not running, and if it comes back to what it was when we
3508 * were paused, we are not restarted.
3509 *
3510 * Implementation of the method from LauncherModel.Callbacks.
3511 *
3512 * @return true if we are currently paused. The caller might be able to
3513 * skip some work in that case since we will come back again.
3514 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003515 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003516 public boolean setLoadOnResume() {
3517 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003518 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003519 mOnResumeNeedsLoad = true;
3520 return true;
3521 } else {
3522 return false;
3523 }
3524 }
3525
3526 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003527 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003528 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003529 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003530 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003531 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003532 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003533 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003534 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003535 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003536 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003537
Joe Onorato9c1289c2009-08-17 11:03:03 -04003538 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003539 * Clear any pending bind callbacks. This is called when is loader is planning to
3540 * perform a full rebind from scratch.
3541 */
3542 @Override
3543 public void clearPendingBinds() {
3544 mBindOnResumeCallbacks.clear();
3545 if (mPendingExecutor != null) {
3546 mPendingExecutor.markCompleted();
3547 mPendingExecutor = null;
3548 }
3549 }
3550
3551 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003552 * Refreshes the shortcuts shown on the workspace.
3553 *
3554 * Implementation of the method from LauncherModel.Callbacks.
3555 */
3556 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003557 if (LauncherAppState.PROFILE_STARTUP) {
3558 Trace.beginSection("Starting page bind");
3559 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003560 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003561
Winson Chungd64d1762013-08-20 14:37:16 -07003562 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003563 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003564 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003565
Michael Jurka05bf644e2011-11-30 20:28:53 -08003566 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003567 if (mHotseat != null) {
3568 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003569 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003570 if (LauncherAppState.PROFILE_STARTUP) {
3571 Trace.endSection();
3572 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003573 }
3574
Adam Cohendcd297f2013-06-18 13:13:40 -07003575 @Override
3576 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003577 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003578 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3579 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003580 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3581 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3582 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003583 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3584 // If there are no screens, we need to have an empty screen
3585 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003586 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003587 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003588
3589 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003590 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003591 if (hasCustomContentToLeft()) {
3592 mWorkspace.createCustomContentContainer();
3593 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003594 }
Winson Chung64359a52013-07-08 17:17:08 -07003595 }
3596
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003597 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003598 int count = orderedScreenIds.size();
3599 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003600 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003601 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003602 // No need to bind the first screen, as its always bound.
3603 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3604 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003605 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003606 }
3607
Winson Chungd64d1762013-08-20 14:37:16 -07003608 public void bindAppsAdded(final ArrayList<Long> newScreens,
3609 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003610 final ArrayList<ItemInfo> addAnimated,
3611 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003612 Runnable r = new Runnable() {
3613 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003614 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003615 }
3616 };
3617 if (waitUntilResume(r)) {
3618 return;
3619 }
3620
Winson Chungd64d1762013-08-20 14:37:16 -07003621 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003622 if (newScreens != null) {
3623 bindAddScreens(newScreens);
3624 }
Winson Chungd64d1762013-08-20 14:37:16 -07003625
3626 // We add the items without animation on non-visible pages, and with
3627 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003628 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003629 bindItems(addNotAnimated, 0,
3630 addNotAnimated.size(), false);
3631 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003632 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003633 bindItems(addAnimated, 0,
3634 addAnimated.size(), true);
3635 }
Winson Chungc58497e2013-09-03 17:48:37 -07003636
Winson Chung87412982013-10-03 18:34:14 -07003637 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003638 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003639
Winson Chungb745afb2015-03-02 11:51:23 -08003640 if (addedApps != null && mAppsView != null) {
3641 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003642 }
Winson Chungd64d1762013-08-20 14:37:16 -07003643 }
3644
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003645 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003646 * Bind the items start-end from the list.
3647 *
3648 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003649 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003650 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003651 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3652 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003653 Runnable r = new Runnable() {
3654 public void run() {
3655 bindItems(shortcuts, start, end, forceAnimateIcons);
3656 }
3657 };
3658 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003659 return;
3660 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003661
Winson Chungf0c6ae02012-03-21 16:10:31 -07003662 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003663 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3664 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003665 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003666 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003667 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003668 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003669 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003670
3671 // Short circuit if we are loading dock items for a configuration which has no dock
3672 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3673 mHotseat == null) {
3674 continue;
3675 }
3676
Sunny Goyal639e9062015-08-19 19:17:06 -07003677 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003678 switch (item.itemType) {
3679 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3680 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003681 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003682 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003683 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003684 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003685 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003686 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003687 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003688 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003689 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003690 default:
3691 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003692 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003693
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003694 /*
3695 * Remove colliding items.
3696 */
3697 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3698 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3699 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3700 View v = cl.getChildAt(item.cellX, item.cellY);
3701 Object tag = v.getTag();
3702 String desc = "Collision while binding workspace item: " + item
3703 + ". Collides with " + tag;
3704 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3705 throw (new RuntimeException(desc));
3706 } else {
3707 Log.d(TAG, desc);
3708 LauncherModel.deleteItemFromDatabase(this, item);
3709 continue;
3710 }
3711 }
3712 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003713 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3714 item.cellY, 1, 1);
3715 if (animateIcons) {
3716 // Animate all the applications up now
3717 view.setAlpha(0f);
3718 view.setScaleX(0f);
3719 view.setScaleY(0f);
3720 bounceAnims.add(createNewAppBounceAnimation(view, i));
3721 newShortcutsScreenId = item.screenId;
3722 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003723 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003724
Winson Chung997a9232013-07-24 15:33:46 -07003725 if (animateIcons) {
3726 // Animate to the correct page
3727 if (newShortcutsScreenId > -1) {
3728 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003729 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003730 final Runnable startBounceAnimRunnable = new Runnable() {
3731 public void run() {
3732 anim.playTogether(bounceAnims);
3733 anim.start();
3734 }
3735 };
Winson Chung997a9232013-07-24 15:33:46 -07003736 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003737 // We post the animation slightly delayed to prevent slowdowns
3738 // when we are loading right after we return to launcher.
3739 mWorkspace.postDelayed(new Runnable() {
3740 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003741 if (mWorkspace != null) {
3742 mWorkspace.snapToPage(newScreenIndex);
3743 mWorkspace.postDelayed(startBounceAnimRunnable,
3744 NEW_APPS_ANIMATION_DELAY);
3745 }
Winson Chung94d67682013-09-25 16:29:40 -07003746 }
3747 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003748 } else {
3749 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003750 }
3751 }
Winson Chung64359a52013-07-08 17:17:08 -07003752 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003753 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003754 }
3755
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003756 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3757 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3758 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003759 view.updateAppWidget(null);
3760 view.setOnClickListener(this);
3761 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003762 mWorkspace.requestLayout();
3763 }
3764
Joe Onorato9c1289c2009-08-17 11:03:03 -04003765 /**
3766 * Add the views for a widget to the workspace.
3767 *
3768 * Implementation of the method from LauncherModel.Callbacks.
3769 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003770 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003771 Runnable r = new Runnable() {
3772 public void run() {
3773 bindAppWidget(item);
3774 }
3775 };
3776 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003777 return;
3778 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003779
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003780 if (mIsSafeModeEnabled) {
3781 bindSafeModeWidget(item);
3782 return;
3783 }
3784
Daniel Sandler843e8602010-06-07 14:59:01 -04003785 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3786 if (DEBUG_WIDGETS) {
3787 Log.d(TAG, "bindAppWidget: " + item);
3788 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003789
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003790 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003791
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003792 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3793 // If the provider is not ready, bind as a pending widget.
3794 appWidgetInfo = null;
3795 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3796 // The widget id is not valid. Try to find the widget based on the provider info.
3797 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3798 } else {
3799 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3800 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003801
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003802 // If the provider is ready, but the width is not yet restored, try to restore it.
3803 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3804 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003805 if (appWidgetInfo == null) {
3806 if (DEBUG_WIDGETS) {
3807 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3808 + " belongs to component " + item.providerName
3809 + ", as the povider is null");
3810 }
3811 LauncherModel.deleteItemFromDatabase(this, item);
3812 return;
3813 }
Sunny Goyalff572272014-07-23 13:58:07 -07003814
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003815 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003816 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003817 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3818 // Id has not been allocated yet. Allocate a new id.
3819 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3820 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003821
Sunny Goyald478c832016-04-01 12:04:16 -07003822 // Also try to bind the widget. If the bind fails, the user will be shown
3823 // a click to setup UI, which will ask for the bind permission.
3824 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3825 pendingInfo.spanX = item.spanX;
3826 pendingInfo.spanY = item.spanY;
3827 pendingInfo.minSpanX = item.minSpanX;
3828 pendingInfo.minSpanY = item.minSpanY;
3829 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
3830 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3831 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003832
Sunny Goyald478c832016-04-01 12:04:16 -07003833 // Bind succeeded
3834 if (success) {
3835 // If the widget has a configure activity, it is still needs to set it up,
3836 // otherwise the widget is ready to go.
3837 item.restoreStatus = (appWidgetInfo.configure == null)
3838 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3839 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003840 }
Sunny Goyald478c832016-04-01 12:04:16 -07003841
3842 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003843 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003844 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003845 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003846 // The widget was marked as UI not ready, but there is no configure activity to
3847 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003848 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3849 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003850 }
Sunny Goyalff572272014-07-23 13:58:07 -07003851 }
3852
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003853 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003854 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003855 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3856 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003857 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003858
Sunny Goyal56c73602015-09-25 12:55:01 -07003859 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003860 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003861 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003862 deleteWidgetInfo(item);
3863 return;
3864 }
3865
Sunny Goyal233ee962015-08-03 13:05:01 -07003866 item.minSpanX = appWidgetInfo.minSpanX;
3867 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003868 addAppWidgetToWorkspace(
3869 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3870 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003871 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003872 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003873 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003874 view.updateAppWidget(null);
3875 view.setOnClickListener(this);
3876 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003877 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003878 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003879
Daniel Sandler843e8602010-06-07 14:59:01 -04003880 if (DEBUG_WIDGETS) {
3881 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3882 + (SystemClock.uptimeMillis()-start) + "ms");
3883 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003884 }
3885
Sunny Goyalff572272014-07-23 13:58:07 -07003886 /**
3887 * Restores a pending widget.
3888 *
3889 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003890 */
Sunny Goyald478c832016-04-01 12:04:16 -07003891 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003892 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3893 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3894 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003895 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003896 }
3897
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003898 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003899 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003900
3901 mWorkspace.reinflateWidgetsIfNecessary();
3902 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003903 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003904 }
3905
Adam Cohen1462de32012-07-24 22:34:36 -07003906 public void onPageBoundSynchronously(int page) {
3907 mSynchronouslyBoundPages.add(page);
3908 }
3909
Sunny Goyal527c7d32015-08-28 15:19:36 -07003910 @Override
3911 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3912 if (mPendingExecutor != null) {
3913 mPendingExecutor.markCompleted();
3914 }
3915 mPendingExecutor = executor;
3916 executor.attachTo(this);
3917 }
3918
3919 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3920 if (mPendingExecutor == executor) {
3921 mPendingExecutor = null;
3922 }
3923 }
3924
Joe Onorato9c1289c2009-08-17 11:03:03 -04003925 /**
3926 * Callback saying that there aren't any more items to bind.
3927 *
3928 * Implementation of the method from LauncherModel.Callbacks.
3929 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003930 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003931 Runnable r = new Runnable() {
3932 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003933 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003934 }
3935 };
3936 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003937 return;
3938 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003939 if (LauncherAppState.PROFILE_STARTUP) {
3940 Trace.beginSection("Page bind completed");
3941 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003942 if (mSavedState != null) {
3943 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003944 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003945 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003946
Joe Onorato9c1289c2009-08-17 11:03:03 -04003947 mSavedState = null;
3948 }
3949
Adam Cohen1462de32012-07-24 22:34:36 -07003950 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003951
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003952 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003953
3954 // If we received the result of any pending adds while the loader was running (e.g. the
3955 // widget configuration forced an orientation change), process them now.
3956 if (sPendingAddItem != null) {
3957 final long screenId = completeAdd(sPendingAddItem);
3958
3959 // TODO: this moves the user to the page where the pending item was added. Ideally,
3960 // the screen would be guaranteed to exist after bind, and the page would be set through
3961 // the workspace restore process.
3962 mWorkspace.post(new Runnable() {
3963 @Override
3964 public void run() {
3965 mWorkspace.snapToScreenId(screenId);
3966 }
3967 });
3968 sPendingAddItem = null;
3969 }
3970
Sunny Goyal756adbc2015-04-16 15:20:51 -07003971 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003972
3973 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003974 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003975 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003976 if (LauncherAppState.PROFILE_STARTUP) {
3977 Trace.endSection();
3978 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003979 }
3980
Winson Chunga2413752012-04-03 14:22:34 -07003981 private boolean canRunNewAppsAnimation() {
3982 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003983 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003984 }
3985
Winson Chungc9168342013-06-26 14:54:55 -07003986 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003987 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003988 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3989 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003990 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003991 return bounceAnim;
3992 }
3993
Adam Cohen27772732013-11-11 14:00:56 +00003994 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07003995 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00003996 }
3997
Tony Wickham55616cd2015-09-23 14:55:17 -07003998 public int getSearchBarHeight() {
3999 if (mLauncherCallbacks != null) {
4000 return mLauncherCallbacks.getSearchBarHeight();
4001 }
4002 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4003 }
4004
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004005 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004006 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4007 * multiple calls to bind the same list.)
4008 */
4009 @Thunk ArrayList<AppInfo> mTmpAppsList;
4010 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4011 public void run() {
4012 bindAllApplications(mTmpAppsList);
4013 mTmpAppsList = null;
4014 }
4015 };
4016
4017 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004018 * Add the icons for all apps.
4019 *
4020 * Implementation of the method from LauncherModel.Callbacks.
4021 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004022 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004023 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4024 mTmpAppsList = apps;
4025 return;
4026 }
4027
Winson Chungb745afb2015-03-02 11:51:23 -08004028 if (mAppsView != null) {
4029 mAppsView.setApps(apps);
4030 }
Adam Cohen9211d422014-10-07 18:14:53 -07004031 if (mLauncherCallbacks != null) {
4032 mLauncherCallbacks.bindAllApplications(apps);
4033 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004034 }
4035
4036 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004037 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
4038 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
4039 */
4040 @Override
4041 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
4042 mDeepShortcutMap = deepShortcutMapCopy;
4043 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
4044 }
4045
Tony Wickham1bce7fd2016-04-28 17:39:03 -07004046 public List<String> getShortcutIdsForItem(ItemInfo info) {
4047 if (!DeepShortcutManager.supportsShortcuts(info)) {
4048 return Collections.EMPTY_LIST;
4049 }
4050 ComponentName component = info.getTargetComponent();
4051 List<String> ids = mDeepShortcutMap.get(new ComponentKey(component, info.user));
4052 return ids == null ? Collections.EMPTY_LIST : ids;
4053 }
4054
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004055 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004056 * A package was updated.
4057 *
4058 * Implementation of the method from LauncherModel.Callbacks.
4059 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004060 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004061 Runnable r = new Runnable() {
4062 public void run() {
4063 bindAppsUpdated(apps);
4064 }
4065 };
4066 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004067 return;
4068 }
4069
Winson Chungb745afb2015-03-02 11:51:23 -08004070 if (mAppsView != null) {
4071 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004072 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004073 }
4074
Sunny Goyal4390ace2014-10-13 11:33:11 -07004075 @Override
4076 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4077 Runnable r = new Runnable() {
4078 public void run() {
4079 bindWidgetsRestored(widgets);
4080 }
4081 };
4082 if (waitUntilResume(r)) {
4083 return;
4084 }
4085 mWorkspace.widgetsRestored(widgets);
4086 }
4087
Joe Onorato9c1289c2009-08-17 11:03:03 -04004088 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004089 * Some shortcuts were updated in the background.
4090 *
4091 * Implementation of the method from LauncherModel.Callbacks.
4092 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004093 @Override
4094 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4095 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004096 Runnable r = new Runnable() {
4097 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004098 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004099 }
4100 };
4101 if (waitUntilResume(r)) {
4102 return;
4103 }
4104
Sunny Goyal4390ace2014-10-13 11:33:11 -07004105 if (!updated.isEmpty()) {
4106 mWorkspace.updateShortcuts(updated);
4107 }
4108
4109 if (!removed.isEmpty()) {
4110 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4111 for (ShortcutInfo si : removed) {
4112 removedComponents.add(si.getTargetComponent());
4113 }
4114 mWorkspace.removeItemsByComponentName(removedComponents, user);
4115 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004116 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004117 }
4118 }
4119
4120 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004121 * Update the state of a package, typically related to install state.
4122 *
4123 * Implementation of the method from LauncherModel.Callbacks.
4124 */
Sunny Goyale755d462014-07-22 13:48:29 -07004125 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004126 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4127 Runnable r = new Runnable() {
4128 public void run() {
4129 bindRestoreItemsChange(updates);
4130 }
4131 };
4132 if (waitUntilResume(r)) {
4133 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004134 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004135
Sunny Goyal756adbc2015-04-16 15:20:51 -07004136 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004137 }
4138
4139 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004140 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004141 * in addition to specific applications to remove, the reason being that
4142 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004143 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004144 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004145 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004146 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004147 public void bindWorkspaceComponentsRemoved(
4148 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4149 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004150 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004151 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004152 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004153 }
Winson Chungd64d1762013-08-20 14:37:16 -07004154 };
4155 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004156 return;
4157 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004158 if (!packageNames.isEmpty()) {
4159 mWorkspace.removeItemsByPackageName(packageNames, user);
4160 }
4161 if (!components.isEmpty()) {
4162 mWorkspace.removeItemsByComponentName(components, user);
4163 }
4164 // Notify the drag controller
4165 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004166
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004167 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004168
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004169 @Override
4170 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4171 Runnable r = new Runnable() {
4172 public void run() {
4173 bindAppInfosRemoved(appInfos);
4174 }
4175 };
4176 if (waitUntilResume(r)) {
4177 return;
Joe Onorato36115782010-06-17 13:28:48 -04004178 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004179
Winson Chungdf95eb12013-10-16 14:57:07 -07004180 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004181 if (mAppsView != null) {
4182 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004183 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004184 }
Joe Onoratobe386092009-11-17 17:32:16 -08004185
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004186 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004187 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004188 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004189 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004190 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004191
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004192 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004193 public void bindWidgetsModel(WidgetsModel model) {
4194 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004195 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004196 return;
4197 }
4198
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004199 if (mWidgetsView != null && model != null) {
4200 mWidgetsView.addWidgets(model);
4201 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004202 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004203 }
4204
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004205 @Override
4206 public void notifyWidgetProvidersChanged() {
4207 if (mWorkspace.getState().shouldUpdateWidget) {
4208 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4209 }
4210 }
4211
Winson Chung400438b2011-01-16 17:53:48 -08004212 private int mapConfigurationOriActivityInfoOri(int configOri) {
4213 final Display d = getWindowManager().getDefaultDisplay();
4214 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4215 switch (d.getRotation()) {
4216 case Surface.ROTATION_0:
4217 case Surface.ROTATION_180:
4218 // We are currently in the same basic orientation as the natural orientation
4219 naturalOri = configOri;
4220 break;
4221 case Surface.ROTATION_90:
4222 case Surface.ROTATION_270:
4223 // We are currently in the other basic orientation to the natural orientation
4224 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4225 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4226 break;
4227 }
4228
4229 int[] oriMap = {
4230 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4231 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4232 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4233 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4234 };
4235 // Since the map starts at portrait, we need to offset if this device's natural orientation
4236 // is landscape.
4237 int indexOffset = 0;
4238 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4239 indexOffset = 1;
4240 }
4241 return oriMap[(d.getRotation() + indexOffset) % 4];
4242 }
Adam Cohen446e9402011-09-15 18:21:21 -07004243
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004244 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004245 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004246 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004247 if (Utilities.ATLEAST_JB_MR2) {
4248 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4249 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004250 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4251 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004252 }
Winson Chung4b919f82012-05-01 10:44:08 -07004253 }
4254 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004255
Winson Chung4b919f82012-05-01 10:44:08 -07004256 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004257 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004258 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004259 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004260 } else {
4261 mHandler.postDelayed(new Runnable() {
4262 public void run() {
4263 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4264 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004265 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004266 }
Winson Chung4b919f82012-05-01 10:44:08 -07004267 }
Winson Chung400438b2011-01-16 17:53:48 -08004268 }
4269
Adam Cohenea90f832014-05-21 15:03:34 -07004270 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004271 * To be overridden by subclasses to indicate that there is an activity to launch
4272 * before showing the standard launcher experience.
4273 */
4274 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004275 if (mLauncherCallbacks != null) {
4276 return mLauncherCallbacks.hasFirstRunActivity();
4277 }
Adam Cohen432609a2014-03-13 17:03:22 -07004278 return false;
4279 }
4280
4281 /**
4282 * To be overridden by subclasses to launch any first run activity
4283 */
4284 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004285 if (mLauncherCallbacks != null) {
4286 return mLauncherCallbacks.getFirstRunActivity();
4287 }
Adam Cohen432609a2014-03-13 17:03:22 -07004288 return null;
4289 }
4290
Winson Chunga6945242014-01-08 14:04:34 -08004291 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004292 return !ActivityManager.isRunningInTestHarness() &&
4293 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004294 }
4295
Adam Cohen4a9423d2014-03-28 14:22:31 -07004296 protected boolean hasRunFirstRunActivity() {
4297 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4298 }
4299
Adam Cohen432609a2014-03-13 17:03:22 -07004300 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004301 if (shouldRunFirstRunActivity() &&
4302 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004303 Intent firstRunIntent = getFirstRunActivity();
4304 if (firstRunIntent != null) {
4305 startActivity(firstRunIntent);
4306 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004307 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004308 }
4309 }
Adam Cohen432609a2014-03-13 17:03:22 -07004310 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004311 }
4312
4313 private void markFirstRunActivityShown() {
4314 SharedPreferences.Editor editor = mSharedPrefs.edit();
4315 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4316 editor.apply();
4317 }
4318
Adam Cohen432609a2014-03-13 17:03:22 -07004319 /**
4320 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4321 * screen that must be displayed and dismissed.
4322 */
4323 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004324 if (mLauncherCallbacks != null) {
4325 return mLauncherCallbacks.hasDismissableIntroScreen();
4326 }
Adam Cohen432609a2014-03-13 17:03:22 -07004327 return false;
4328 }
4329
4330 /**
4331 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4332 */
4333 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004334 if (mLauncherCallbacks != null) {
4335 return mLauncherCallbacks.getIntroScreen();
4336 }
Adam Cohen432609a2014-03-13 17:03:22 -07004337 return null;
4338 }
4339
4340 /**
4341 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4342 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4343 */
4344 private boolean shouldShowIntroScreen() {
4345 return hasDismissableIntroScreen() &&
4346 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4347 }
4348
4349 protected void showIntroScreen() {
4350 View introScreen = getIntroScreen();
4351 changeWallpaperVisiblity(false);
4352 if (introScreen != null) {
4353 mDragLayer.showOverlayView(introScreen);
4354 }
4355 }
4356
Winson Chung5ffd51d2015-06-25 11:36:50 -07004357 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004358 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004359 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004360 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004361 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004362 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004363 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4364 if (userHandle != null) {
4365 user = UserHandleCompat.fromUser(userHandle);
4366 }
4367 }
4368 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004369 }
4370
4371 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004372 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004373 if (user == null) {
4374 user = UserHandleCompat.myUserHandle();
4375 }
4376
4377 // Called from search suggestion, add the profile extra to the intent to ensure that we
4378 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004379 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004380 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004381 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004382 return null;
4383 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004384 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004385 }
4386
Winson Chung5ffd51d2015-06-25 11:36:50 -07004387 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004388 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4389 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004390 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004391 UserHandleCompat.myUserHandle());
4392 }
4393
Winson Chung5ffd51d2015-06-25 11:36:50 -07004394 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004395 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4396 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004397 mWorkspace.onExternalDragStartedWithItem(dragView);
4398 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004399 }
4400
Winson Chung5ffd51d2015-06-25 11:36:50 -07004401 protected void moveWorkspaceToDefaultScreen() {
4402 mWorkspace.moveToDefaultScreen(false);
4403 }
4404
Winson Chung80baf5a2010-08-09 16:03:15 -07004405 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004406 * Returns a FastBitmapDrawable with the icon, accurately sized.
4407 */
4408 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4409 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4410 d.setFilterBitmap(true);
4411 resizeIconDrawable(d);
4412 return d;
4413 }
4414
4415 /**
4416 * Resizes an icon drawable to the correct icon size.
4417 */
Winson5fbe0742015-09-30 15:33:00 -07004418 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004419 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004420 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004421 }
4422
4423 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004424 * Prints out out state for debugging.
4425 */
4426 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004427 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004428 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004429 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4430 Log.d(TAG, "mRestoring=" + mRestoring);
4431 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004432 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004433 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004434
Daniel Sandler325dc232013-06-05 22:57:57 -04004435 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004436 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004437
4438 @Override
4439 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4440 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004441 // Dump workspace
4442 writer.println(prefix + "Workspace Items");
4443 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4444 writer.println(prefix + " Homescreen " + i);
4445
4446 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4447 for (int j = 0; j < layout.getChildCount(); j++) {
4448 Object tag = layout.getChildAt(j).getTag();
4449 if (tag != null) {
4450 writer.println(prefix + " " + tag.toString());
4451 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004452 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004453 }
Sunny Goyala1365452015-10-01 15:46:24 -07004454
4455 writer.println(prefix + " Hotseat");
4456 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4457 for (int j = 0; j < layout.getChildCount(); j++) {
4458 Object tag = layout.getChildAt(j).getTag();
4459 if (tag != null) {
4460 writer.println(prefix + " " + tag.toString());
4461 }
4462 }
4463
Sunny Goyal713edfc2016-05-06 09:58:34 -07004464 try {
4465 FileLog.flushAll(writer);
4466 } catch (Exception e) {
4467 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004468 }
4469
Adam Cohen9211d422014-10-07 18:14:53 -07004470 if (mLauncherCallbacks != null) {
4471 mLauncherCallbacks.dump(prefix, fd, writer, args);
4472 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004473 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004474
Adam Cohen59400422014-03-05 18:07:04 -08004475 public static CustomAppWidget getCustomAppWidget(String name) {
4476 return sCustomAppWidgets.get(name);
4477 }
4478
4479 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4480 return sCustomAppWidgets;
4481 }
4482
Sunny Goyal29538332016-02-18 09:10:19 -08004483 public static List<View> getFolderContents(View icon) {
4484 if (icon instanceof FolderIcon) {
4485 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4486 } else {
4487 return Collections.EMPTY_LIST;
4488 }
4489 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004490
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004491 public static Launcher getLauncher(Context context) {
4492 if (context instanceof Launcher) {
4493 return (Launcher) context;
4494 }
4495 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4496 }
4497
Sunny Goyal745bad92016-05-02 10:54:12 -07004498 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4499
4500 @Override
4501 public void onSharedPreferenceChanged(
4502 SharedPreferences sharedPreferences, String key) {
4503 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4504 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4505 if (!waitUntilResume(this, true)) {
4506 run();
4507 }
4508 }
4509 }
4510
4511 @Override
4512 public void run() {
4513 setOrientation();
4514 }
4515 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004516}