blob: 97af37c2eb8861a7fde308b9e71546be20d78a93 [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;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020062import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080063import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070064import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070065import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040066import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070067import android.os.Trace;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070068import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.Selection;
70import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070071import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070077import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.MotionEvent;
79import android.view.Surface;
80import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070081import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.ViewGroup;
84import android.view.ViewTreeObserver;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080086import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070087import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070088import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080092import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070093import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070094
Sunny Goyal651077b2014-06-30 14:15:31 -070095import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070096import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyalae502842016-06-17 08:43:56 -070097import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070098import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070099import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -0800100import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700101import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100102import com.android.launcher3.compat.LauncherActivityInfoCompat;
103import com.android.launcher3.compat.LauncherAppsCompat;
104import com.android.launcher3.compat.UserHandleCompat;
105import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800106import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700107import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700108import com.android.launcher3.dragndrop.DragController;
109import com.android.launcher3.dragndrop.DragLayer;
110import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -0700111import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000112import com.android.launcher3.folder.Folder;
113import com.android.launcher3.folder.FolderIcon;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700114import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700115import com.android.launcher3.logging.FileLog;
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800116import com.android.launcher3.logging.LoggerUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700117import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700118import com.android.launcher3.model.WidgetsModel;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700119import com.android.launcher3.pageindicators.PageIndicator;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800120import com.android.launcher3.userevent.nano.LauncherLogProto;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700121import com.android.launcher3.util.ComponentKey;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700122import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700123import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal322d5562015-06-25 19:35:49 -0700124import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700125import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700126import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700127import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700128import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700129import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700130
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700131import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700132import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700133import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700134import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800135import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700136import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700137import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700138import java.util.List;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800139import java.util.Locale;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700140
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141/**
142 * Default launcher application.
143 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100144public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700145 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
146 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700147 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700148 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700149 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700151 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700152 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400153 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700156 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700157 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700158 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159
Michael Jurka8b805b12012-04-18 14:23:14 -0700160 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700161 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700162 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700163
Sunny Goyal28c6b962015-10-12 11:42:05 -0700164 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
165
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700166 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
167 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
168 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
169
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700170 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
171
Mathew Inwood876a8462013-06-14 14:12:41 +0100172 /**
173 * IntentStarter uses request codes starting with this. This must be greater than all activity
174 * request codes used internally.
175 */
176 protected static final int REQUEST_LAST = 100;
177
Michael Jurka0a457bf2012-11-19 14:05:05 -0800178 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700179 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800180 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
Winson Chung2672ff92012-05-04 16:22:30 -0700182 // The Intent extra that defines whether to ignore the launch animation
183 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400184 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700185
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700186 public static final String ACTION_APPWIDGET_HOST_RESET =
187 "com.android.launcher3.intent.ACTION_APPWIDGET_HOST_RESET";
188
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 // Type: int
190 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700191 // Type: int
192 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700193 // Type: Content Values / parcelable
194 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700195 // Type: parcelable
196 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000197 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800198 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199
Adam Cohen432609a2014-03-13 17:03:22 -0700200 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800201 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
202
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700203 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700204 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
205 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700206
Adam Cohen091440a2015-03-18 14:16:05 -0700207 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700208 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700209
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700210 private boolean mIsSafeModeEnabled;
211
Joe Onorato9c1289c2009-08-17 11:03:03 -0400212 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800213 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700214 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700215 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
Winson Chunga2413752012-04-03 14:22:34 -0700217 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700218 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
219 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700220 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700221
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700222 private final BroadcastReceiver mUiBroadcastReceiver = new BroadcastReceiver() {
223
224 @Override
225 public void onReceive(Context context, Intent intent) {
226 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
227 closeSystemDialogs();
228 } else if (ACTION_APPWIDGET_HOST_RESET.equals(intent.getAction())) {
229 if (mAppWidgetHost != null) {
230 mAppWidgetHost.startListening();
231 }
232 }
233 }
234 };
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800235
Adam Cohen091440a2015-03-18 14:16:05 -0700236 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700237 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700238 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800239 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700240
241 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700242
Sunny Goyalffe83f12014-08-14 17:39:34 -0700243 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700244 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700245
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700246 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800247 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800248 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700249
Michael Jurka0280c3b2010-09-17 15:00:07 -0700250 private int[] mTmpAddItemCellCoordinates = new int[2];
251
Sunny Goyal316490e2015-06-02 09:38:28 -0700252 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800253 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700254
Michael Jurka838a4ca2011-02-07 13:33:06 -0800255 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700256 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700257
Sunny Goyal47328fd2016-05-25 18:56:41 -0700258 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700259
260 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700261 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700262 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700263
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700264 // Main container view and the model for the widget tray screen.
265 @Thunk WidgetsContainerView mWidgetsView;
266 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700267
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700269 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
270 // scroll issues (because the workspace may not have been measured yet) and extra work.
271 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
272 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273
274 private SpannableStringBuilder mDefaultKeySsb = null;
275
Adam Cohen091440a2015-03-18 14:16:05 -0700276 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400277
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800278 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279 private boolean mRestoring;
280 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700281 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282
Michael Jurka1e2f4652013-07-08 18:03:46 -0700283 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700284 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700285 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700286
Joe Onorato9c1289c2009-08-17 11:03:03 -0400287 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800288 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700289 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700290 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Adam Cohen091440a2015-03-18 14:16:05 -0700291 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800292 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700293 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800294 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400295
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700296 /** Maps launcher activity components to their list of shortcut ids. */
297 private MultiHashMap<ComponentKey, String> mDeepShortcutMap = new MultiHashMap<>();
298
Sunny Goyal639e9062015-08-19 19:17:06 -0700299 private LauncherClings mClings;
300
Adam Cohen61f560d2013-09-30 15:58:20 -0700301 private View.OnTouchListener mHapticFeedbackTouchListener;
302
Adam Cohended9f8d2010-11-03 13:25:16 -0700303 // Related to the auto-advancing of widgets
304 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700305 private static final int ADVANCE_INTERVAL = 20000;
306 private static final int ADVANCE_STAGGER = 250;
307
308 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700309 private long mAutoAdvanceSentTime;
310 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700311 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700312
Winson Chung400438b2011-01-16 17:53:48 -0800313 // Determines how long to wait after a rotation before restoring the screen orientation to
314 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700315 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800316
Adam Cohen091440a2015-03-18 14:16:05 -0700317 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700318
Adam Cohen1462de32012-07-24 22:34:36 -0700319 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700320 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700321
Winson Chung46353de2012-02-16 14:05:10 -0800322 // We only want to get the SharedPreferences once since it does an FS stat each time we get
323 // it from the context.
324 private SharedPreferences mSharedPrefs;
325
Winson Chungf0c6ae02012-03-21 16:10:31 -0700326 // Holds the page that we need to animate to, and the icon views that we need to animate up
327 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700328 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700329 private Bitmap mFolderIconBitmap;
330 private Canvas mFolderIconCanvas;
331 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700332
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700333 private DeviceProfile mDeviceProfile;
334
Adam Cohen4b66bf32015-09-18 12:15:19 -0700335 private boolean mMoveToDefaultScreenFromNewIntent;
336
Winson Chung13eb5272015-05-11 16:30:13 -0700337 // This is set to the view that launched the activity that navigated the user away from
338 // launcher. Since there is no callback for when the activity has finished launching, enable
339 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800340 private BubbleTextView mWaitingForResume;
341
Adam Cohen59400422014-03-05 18:07:04 -0800342 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
343 new HashMap<String, CustomAppWidget>();
344
Adam Cohen59400422014-03-05 18:07:04 -0800345 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700346 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
347 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800348 }
349 }
350
Adam Cohen091440a2015-03-18 14:16:05 -0700351 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700352 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700353 if (mWorkspace != null) {
354 mWorkspace.buildPageHardwareLayers();
355 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700356 }
357 };
358
Adam Cohendb364c32014-05-20 14:23:40 -0700359 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800360
Adam Cohen091440a2015-03-18 14:16:05 -0700361 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800362 int requestCode;
363 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700364 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700365 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800366 int cellX;
367 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700368 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800369 }
370
Hyunyoung Songaa953652016-04-19 18:30:24 -0700371 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800372
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700373 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700374 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400375
376 @Thunk void setOrientation() {
377 if (mRotationEnabled) {
378 unlockScreenOrientation(true);
379 } else {
380 setRequestedOrientation(
381 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700382 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400383 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700384
Sunny Goyal745bad92016-05-02 10:54:12 -0700385 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700386
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 @Override
388 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700389 if (DEBUG_STRICT_MODE) {
390 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
391 .detectDiskReads()
392 .detectDiskWrites()
393 .detectNetwork() // or .detectAll() for all detectable problems
394 .penaltyLog()
395 .build());
396 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
397 .detectLeakedSqlLiteObjects()
398 .detectLeakedClosableObjects()
399 .penaltyLog()
400 .penaltyDeath()
401 .build());
402 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700403 if (LauncherAppState.PROFILE_STARTUP) {
404 Trace.beginSection("Launcher-onCreate");
405 }
Winson Chunga2413752012-04-03 14:22:34 -0700406
Adam Cohen9211d422014-10-07 18:14:53 -0700407 if (mLauncherCallbacks != null) {
408 mLauncherCallbacks.preOnCreate();
409 }
410
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400412
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400413 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700414
Adam Cohen2e6da152015-05-06 11:42:25 -0700415 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700416 mDeviceProfile = getResources().getConfiguration().orientation
417 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700418 app.getInvariantDeviceProfile().landscapeProfile
419 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700420
Sunny Goyalf7258242015-10-19 16:59:07 -0700421 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700422 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800423 mModel = app.setLauncher(this);
424 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700425 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700426
Joe Onorato41a12d22009-10-31 18:30:00 -0400427 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700428 mAllAppsController = new AllAppsTransitionController(this);
429 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700430
Sunny Goyalffe83f12014-08-14 17:39:34 -0700431 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700432
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700433 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
434 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700435
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700436 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
437 // this also ensures that any synchronous binding below doesn't re-trigger another
438 // LauncherModel load.
439 mPaused = false;
440
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700442
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700444 mDeviceProfile.layout(this);
Tony Wickham827cef22016-03-17 15:39:39 -0700445 mExtractedColors = new ExtractedColors();
446 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447
Tony Wickhame2217252016-03-22 16:34:23 -0700448 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
449 .addAccessibilityStateChangeListener(this);
450
Joe Onorato7c312c12009-08-13 21:36:53 -0700451 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700452
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 mSavedState = savedInstanceState;
454 restoreState(mSavedState);
455
Sunny Goyale26d1002016-06-20 14:52:14 -0700456 if (LauncherAppState.PROFILE_STARTUP) {
457 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458 }
459
460 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700461 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700462 // If the user leaves launcher, then we should just load items asynchronously when
463 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700464 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700465 } else {
466 // We only load the page synchronously if the user rotates (or triggers a
467 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700468 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700469 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470 }
471
472 // For handling default keys
473 mDefaultKeySsb = new SpannableStringBuilder();
474 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800475
476 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700477 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
478 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800479
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800480 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700481 // In case we are on a device with locked rotation, we should look at preferences to check
482 // if the user has specifically allowed rotation.
483 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700484 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700485 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
486 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700487 }
488
489 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
490 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400491 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700492
Adam Cohen9211d422014-10-07 18:14:53 -0700493 if (mLauncherCallbacks != null) {
494 mLauncherCallbacks.onCreate(savedInstanceState);
495 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700496
497 if (shouldShowIntroScreen()) {
498 showIntroScreen();
499 } else {
500 showFirstRunActivity();
501 showFirstRunClings();
502 }
Adam Cohen9211d422014-10-07 18:14:53 -0700503 }
504
Sunny Goyal7779d622015-06-11 16:18:39 -0700505 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700506 public void onExtractedColorsChanged() {
507 loadExtractedColorsAndColorItems();
508 }
509
510 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700511 // TODO: do this in pre-N as well, once the extraction part is complete.
512 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700513 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700514 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700515 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham827cef22016-03-17 15:39:39 -0700516 }
517 }
518
Adam Cohen9211d422014-10-07 18:14:53 -0700519 private LauncherCallbacks mLauncherCallbacks;
520
521 public void onPostCreate(Bundle savedInstanceState) {
522 super.onPostCreate(savedInstanceState);
523 if (mLauncherCallbacks != null) {
524 mLauncherCallbacks.onPostCreate(savedInstanceState);
525 }
526 }
527
Sunny Goyal32554d12015-12-03 15:31:25 -0800528 /**
529 * Call this after onCreate to set or clear overlay.
530 */
531 public void setLauncherOverlay(LauncherOverlay overlay) {
532 if (overlay != null) {
533 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
534 }
535 mWorkspace.setLauncherOverlay(overlay);
536 }
537
Adam Cohen9211d422014-10-07 18:14:53 -0700538 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
539 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700540 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700541 private boolean mWorkspaceImportanceStored = false;
542 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700543 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800544 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700545 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
546
547 @Override
548 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700549 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700550 return;
551 }
552 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700553 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700554 if (mWorkspace != null) {
555 mWorkspaceImportanceForAccessibility =
556 mWorkspace.getImportantForAccessibility();
557 mWorkspace.setImportantForAccessibility(
558 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
559 mWorkspaceImportanceStored = true;
560 }
561 if (mHotseat != null) {
562 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
563 mHotseat.setImportantForAccessibility(
564 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
565 mHotseatImportanceStored = true;
566 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700567 }
568
569 @Override
570 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700571 if (mWorkspaceImportanceStored && mWorkspace != null) {
572 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
573 }
574 if (mHotseatImportanceStored && mHotseat != null) {
575 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
576 }
577 mWorkspaceImportanceStored = false;
578 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700579 }
580 });
Adam Cohen9211d422014-10-07 18:14:53 -0700581 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700582 }
583
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700584 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700585 public void onLauncherProviderChange() {
586 if (mLauncherCallbacks != null) {
587 mLauncherCallbacks.onLauncherProviderChange();
588 }
589 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700590
Adam Cohen9211d422014-10-07 18:14:53 -0700591 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700592 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700593 if (mLauncherCallbacks != null) {
594 return mLauncherCallbacks.hasCustomContentToLeft();
595 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700596 return false;
597 }
598
Dave Hawkeya8881582013-09-17 15:55:33 -0600599 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500600 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600601 * {@link #addToCustomContentPage}. This will only be invoked if
602 * {@link #hasCustomContentToLeft()} is {@code true}.
603 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500604 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700605 if (mLauncherCallbacks != null) {
606 mLauncherCallbacks.populateCustomContentContainer();
607 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600608 }
609
610 /**
611 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
612 * ensure the custom content page is added or removed if necessary.
613 */
614 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600615 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600616 // Not bound yet, wait for bindScreens to be called.
617 return;
618 }
619
620 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
621 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500622 mWorkspace.createCustomContentContainer();
623 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600624 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
625 mWorkspace.removeCustomContentPage();
626 }
627 }
628
Hyunyoung Songaa953652016-04-19 18:30:24 -0700629 public UserEventDispatcher getUserEventDispatcher() {
630 if (mLauncherCallbacks != null) {
631 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
632 if (dispatcher != null) {
633 return dispatcher;
634 }
635 }
636
Sunny Goyal64976d52016-06-20 14:56:28 -0700637 // Logger object is a singleton and does not have to be coupled with the foreground
638 // activity. Since most user event logging is done on the UI, the object is retrieved
639 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700640 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700641 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700642 }
643 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800644 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100645
Hyunyoung Song3f471442015-04-08 19:01:34 -0700646 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700647 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
648 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700649 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700650 }
651
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000652 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700653 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
654 // This cast is safe as long as the id < 0x00FFFFFF
655 // Since we jail all the dynamically generated views, there should be no clashes
656 // with any other views.
657 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000658 }
659
660 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700661 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
662 * a configuration step, this allows the proper animations to run after other transitions.
663 */
Adam Cohendb364c32014-05-20 14:23:40 -0700664 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700665 long screenId = args.screenId;
666 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
667 // When the screen id represents an actual screen (as opposed to a rank) we make sure
668 // that the drop page actually exists.
669 screenId = ensurePendingDropLayoutExists(args.screenId);
670 }
Adam Cohendb364c32014-05-20 14:23:40 -0700671
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800672 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800673 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700674 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700675 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700676 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800677 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700678 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700679 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700680 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyald478c832016-04-01 12:04:16 -0700681 completeRestoreAppWidget(args.appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700682 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700683 case REQUEST_BIND_PENDING_APPWIDGET: {
684 int widgetId = args.appWidgetId;
685 LauncherAppWidgetInfo info =
686 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
687 if (info != null) {
688 // Since the view was just bound, also launch the configure activity if needed
689 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
690 .getLauncherAppWidgetInfo(widgetId);
691 if (provider != null && provider.configure != null) {
692 startRestoredWidgetReconfigActivity(provider, info);
693 }
694 }
695 break;
696 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800697 }
Michael Jurka27614d22012-04-02 06:40:22 -0700698 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
699 // if you turned the screen off and then back while in All Apps, Launcher would not
700 // return to the workspace. Clearing mAddInfo.container here fixes this issue
701 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700702 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800703 }
704
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800705 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700706 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700707 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700708 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700709 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800710 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700711
Adam Cohenad4e15c2013-10-17 16:21:35 -0700712 Runnable exitSpringLoaded = new Runnable() {
713 @Override
714 public void run() {
715 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
716 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
717 }
718 };
719
Michael Jurka8b805b12012-04-18 14:23:14 -0700720 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700721 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700722 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700723 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
724 if (resultCode == RESULT_CANCELED) {
725 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700726 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700727 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700728 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800729 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700730 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700731 }
732 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200733 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
734 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700735 // User could have free-scrolled between pages before picking a wallpaper; make sure
736 // we move to the closest one now.
737 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700738 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200739 }
740 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700741 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200742
Adam Cohened66b2b2012-01-23 17:28:51 -0800743 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700744 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700745
Adam Cohendb364c32014-05-20 14:23:40 -0700746 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800747 // We have special handling for widgets
748 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800749 final int appWidgetId;
750 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
751 : -1;
752 if (widgetId < 0) {
753 appWidgetId = pendingAddWidgetId;
754 } else {
755 appWidgetId = widgetId;
756 }
757
Adam Cohenad4e15c2013-10-17 16:21:35 -0700758 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800759 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700760 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
761 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700762 result = RESULT_CANCELED;
763 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700764 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700765 @Override
766 public void run() {
767 exitSpringLoadedDragModeDelayed(false, 0, null);
768 }
769 };
Adam Cohendb364c32014-05-20 14:23:40 -0700770 if (workspaceLocked) {
771 // No need to remove the empty screen if we're mid-binding, as the
772 // the bind will not add the empty screen.
773 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
774 } else {
775 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
776 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
777 }
Winson Chung5aaab772012-04-27 15:24:02 -0700778 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700779 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700780 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
781 // When the screen id represents an actual screen (as opposed to a rank)
782 // we make sure that the drop page actually exists.
783 mPendingAddInfo.screenId =
784 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
785 }
Adam Cohendb364c32014-05-20 14:23:40 -0700786 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
787
788 dropLayout.setDropPending(true);
789 final Runnable onComplete = new Runnable() {
790 @Override
791 public void run() {
792 completeTwoStageWidgetDrop(resultCode, appWidgetId);
793 dropLayout.setDropPending(false);
794 }
795 };
796 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
797 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
798 } else {
799 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
800 mPendingAddInfo);
801 sPendingAddItem = args;
802 }
Winson Chung5aaab772012-04-27 15:24:02 -0700803 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800804 return;
805 }
806
Sunny Goyald478c832016-04-01 12:04:16 -0700807 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
808 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700809 if (resultCode == RESULT_OK) {
810 // Update the widget view.
811 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
812 pendingAddWidgetId, mPendingAddInfo);
813 if (workspaceLocked) {
814 sPendingAddItem = args;
815 } else {
816 completeAdd(args);
817 }
818 }
819 // Leave the widget in the pending state if the user canceled the configure.
820 return;
821 }
822
Sunny Goyalaad90582015-07-09 14:22:50 -0700823 if (requestCode == REQUEST_CREATE_SHORTCUT) {
824 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
825 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
826 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
827 mPendingAddInfo);
828 if (isWorkspaceLocked()) {
829 sPendingAddItem = args;
830 } else {
831 completeAdd(args);
832 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
833 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
834 }
835 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700836 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
837 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800840 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800841
842 }
843
844 @Override
845 protected void onActivityResult(
846 final int requestCode, final int resultCode, final Intent data) {
847 handleActivityResult(requestCode, resultCode, data);
848 if (mLauncherCallbacks != null) {
849 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
850 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800851 }
852
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600853 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700854 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600855 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700856 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
857 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
858 View v = null;
859 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
860 if (layout != null) {
861 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
862 }
863 Intent intent = sPendingAddItem.intent;
864 sPendingAddItem = null;
865 if (grantResults.length > 0
866 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700867 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700868 } else {
869 // TODO: Show a snack bar with link to settings
870 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
871 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
872 }
873 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600874 if (mLauncherCallbacks != null) {
875 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
876 grantResults);
877 }
878 }
879
Adam Cohendb364c32014-05-20 14:23:40 -0700880 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
881 appWidgetId, ItemInfo info) {
882 PendingAddArguments args = new PendingAddArguments();
883 args.requestCode = requestCode;
884 args.intent = data;
885 args.container = info.container;
886 args.screenId = info.screenId;
887 args.cellX = info.cellX;
888 args.cellY = info.cellY;
889 args.appWidgetId = appWidgetId;
890 return args;
891 }
892
893 /**
894 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
895 *
896 * @param screenId the screen id to check
897 * @return the new screen, or screenId if it exists
898 */
899 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800900 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700901 if (dropLayout == null) {
902 // it's possible that the add screen was removed because it was
903 // empty and a re-bind occurred
904 mWorkspace.addExtraEmptyScreen();
905 return mWorkspace.commitExtraEmptyScreen();
906 } else {
907 return screenId;
908 }
909 }
910
Adam Cohen091440a2015-03-18 14:16:05 -0700911 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800912 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800913 Runnable onCompleteRunnable = null;
914 int animationType = 0;
915
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700916 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800917 if (resultCode == RESULT_OK) {
918 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
919 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700920 mPendingAddWidgetInfo);
921 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800922 onCompleteRunnable = new Runnable() {
923 @Override
924 public void run() {
925 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700926 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700927 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
928 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800929 }
930 };
931 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800932 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800933 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800934 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700935 if (mDragLayer.getAnimatedView() != null) {
936 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
937 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
938 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700939 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700940 // The animated view may be null in the case of a rotation during widget configuration
941 onCompleteRunnable.run();
942 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 }
944
945 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700946 protected void onStop() {
947 super.onStop();
948 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700949
950 if (mLauncherCallbacks != null) {
951 mLauncherCallbacks.onStop();
952 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700953 }
954
955 @Override
956 protected void onStart() {
957 super.onStart();
958 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700959
960 if (mLauncherCallbacks != null) {
961 mLauncherCallbacks.onStart();
962 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700963 }
964
965 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200967 long startTime = 0;
968 if (DEBUG_RESUME_TIME) {
969 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400970 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200971 }
Adam Cohen9211d422014-10-07 18:14:53 -0700972
973 if (mLauncherCallbacks != null) {
974 mLauncherCallbacks.preOnResume();
975 }
976
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700978 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700979
Winson Chung4a2afa32012-07-19 14:53:05 -0700980 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700981 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700982 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800983 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700984 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700985 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700986 // view after launching an app, as they may be depending on the UI to be static to
987 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700988 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700989 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800990 } else if (mOnResumeState == State.WIDGETS) {
991 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700992 }
993 mOnResumeState = State.NONE;
994
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700995 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700996 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
997 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700998
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800999 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001000 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001001 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001002 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001003 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001004 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001006 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001007 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1008 // execute them here
1009 long startTimeCallbacks = 0;
1010 if (DEBUG_RESUME_TIME) {
1011 startTimeCallbacks = System.currentTimeMillis();
1012 }
1013
Michael Jurka1e2f4652013-07-08 18:03:46 -07001014 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1015 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001016 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001017 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001018 if (DEBUG_RESUME_TIME) {
1019 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1020 (System.currentTimeMillis() - startTimeCallbacks));
1021 }
Michael Jurka447bf842013-05-15 14:52:15 +02001022 }
Michael Jurka54554252013-08-01 12:52:23 +02001023 if (mOnResumeCallbacks.size() > 0) {
1024 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1025 mOnResumeCallbacks.get(i).run();
1026 }
1027 mOnResumeCallbacks.clear();
1028 }
Winson Chunge4e50662012-01-23 14:45:13 -08001029
1030 // Reset the pressed state of icons that were locked in the press state while activities
1031 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001032 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001033 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001034 mWaitingForResume.setStayPressed(false);
1035 }
Winson Chung82963d52013-10-09 11:20:57 -07001036
Adam Cohen06dff352012-06-01 17:17:08 -07001037 // It is possible that widgets can receive updates while launcher is not in the foreground.
1038 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1039 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1040 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001041 if (!isWorkspaceLoading()) {
1042 getWorkspace().reinflateWidgetsIfNecessary();
1043 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001044
Michael Jurka447bf842013-05-15 14:52:15 +02001045 if (DEBUG_RESUME_TIME) {
1046 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1047 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001048
Adam Cohen4b66bf32015-09-18 12:15:19 -07001049 // We want to suppress callbacks about CustomContent being shown if we have just received
1050 // onNewIntent while the user was present within launcher. In that case, we post a call
1051 // to move the user to the main screen (which will occur after onResume). We don't want to
1052 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1053 // suppress here.
1054 if (mWorkspace.getCustomContentCallbacks() != null
1055 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001056 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001057 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001058 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1059 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001060 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001061 }
1062 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001063 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001064 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001065 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001066
Sunny Goyal756adbc2015-04-16 15:20:51 -07001067 if (!isWorkspaceLoading()) {
1068 // Process any items that were added while Launcher was away.
1069 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1070 }
Adam Cohen9211d422014-10-07 18:14:53 -07001071
1072 if (mLauncherCallbacks != null) {
1073 mLauncherCallbacks.onResume();
1074 }
Adam Cohen06dff352012-06-01 17:17:08 -07001075 }
1076
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001078 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001079 // Ensure that items added to Launcher are queued until Launcher returns
1080 InstallShortcutReceiver.enableInstallQueue();
1081
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001082 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001083 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001084 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001085 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001086
1087 // We call onHide() aggressively. The custom content callbacks should be able to
1088 // debounce excess onHide calls.
1089 if (mWorkspace.getCustomContentCallbacks() != null) {
1090 mWorkspace.getCustomContentCallbacks().onHide();
1091 }
Romain Guycbb89e42009-06-08 15:52:54 -07001092
Adam Cohen9211d422014-10-07 18:14:53 -07001093 if (mLauncherCallbacks != null) {
1094 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001095 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001096 }
1097
1098 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001099 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1100 // by a onResume or by scrolling otherwise.
1101 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001102
1103 // Custom content is completely hidden
1104 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001105
1106 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1107 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001108
1109 // Indicates whether the user is allowed to scroll away from the custom content.
1110 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001111 }
1112
Adam Cohenc2d6e892014-10-16 09:49:24 -07001113 public interface LauncherOverlay {
1114
1115 /**
1116 * Touch interaction leading to overscroll has begun
1117 */
1118 public void onScrollInteractionBegin();
1119
1120 /**
1121 * Touch interaction related to overscroll has ended
1122 */
1123 public void onScrollInteractionEnd();
1124
1125 /**
1126 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1127 * screen (or in the case of RTL, the rightmost screen).
1128 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001129 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001130
1131 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001132 * Called when the launcher is ready to use the overlay
1133 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001134 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001135 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001136 }
1137
Jun Mukai8af0cd82015-05-12 12:32:12 -07001138 public interface LauncherSearchCallbacks {
1139 /**
1140 * Called when the search overlay is shown.
1141 */
1142 public void onSearchOverlayOpened();
1143
1144 /**
1145 * Called when the search overlay is dismissed.
1146 */
1147 public void onSearchOverlayClosed();
1148 }
1149
Adam Cohenc2d6e892014-10-16 09:49:24 -07001150 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001151 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001152 }
1153
1154 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1155
Sunny Goyalc86df472016-02-25 09:19:38 -08001156 public void onScrollChanged(float progress) {
1157 if (mWorkspace != null) {
1158 mWorkspace.onOverlayScrollChanged(progress);
1159 }
1160 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001161 }
1162
Jorim Jaggid017f882014-01-14 17:08:48 -08001163 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001164 if (mLauncherCallbacks != null) {
1165 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001166 } else {
1167 // On devices with a locked orientation, we will at least have the allow rotation
1168 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001169 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001170 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001171 }
1172
Adam Cohen9211d422014-10-07 18:14:53 -07001173 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001174 CustomContentCallbacks callbacks, String description) {
1175 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001176 }
1177
Adam Cohenedb40762013-07-18 16:45:45 -07001178 // The custom content needs to offset its content to account for the QSB
1179 public int getTopOffsetForCustomContent() {
1180 return mWorkspace.getPaddingTop();
1181 }
1182
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001183 @Override
1184 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001185 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001186 if (mModel.isCurrentCallbacks(this)) {
1187 mModel.stopLoader();
1188 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001189 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1190
Romain Guy13c2e7b2010-03-10 19:45:00 -08001191 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001192 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001193
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001194 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001195 @Override
1196 public void onWindowFocusChanged(boolean hasFocus) {
1197 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001198 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001199
1200 if (mLauncherCallbacks != null) {
1201 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1202 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001203 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001204
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 private boolean acceptFilter() {
1206 final InputMethodManager inputManager = (InputMethodManager)
1207 getSystemService(Context.INPUT_METHOD_SERVICE);
1208 return !inputManager.isFullscreenMode();
1209 }
1210
1211 @Override
1212 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001213 final int uniChar = event.getUnicodeChar();
1214 final boolean handled = super.onKeyDown(keyCode, event);
1215 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1216 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1218 keyCode, event);
1219 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001220 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001221 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001222 // showSearchDialog()
1223 // If there are multiple keystrokes before the search dialog takes focus,
1224 // onSearchRequested() will be called for every keystroke,
1225 // but it is idempotent, so it's fine.
1226 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 }
1228 }
1229
Joe Onorato8a9625e2010-01-28 15:55:35 -08001230 // Eat the long press event so the keyboard doesn't come up.
1231 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1232 return true;
1233 }
1234
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 return handled;
1236 }
1237
Winson46163472015-09-22 17:31:56 -07001238 @Override
1239 public boolean onKeyUp(int keyCode, KeyEvent event) {
1240 if (keyCode == KeyEvent.KEYCODE_MENU) {
1241 // Ignore the menu key if we are currently dragging or are on the custom content screen
1242 if (!isOnCustomContent() && !mDragController.isDragging()) {
1243 // Close any open folders
1244 closeFolder();
1245
1246 // Stop resizing any widgets
1247 mWorkspace.exitWidgetResizeMode();
1248
1249 // Show the overview mode if we are on the workspace
1250 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1251 !mWorkspace.isSwitchingState()) {
1252 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001253 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001254 }
1255 }
1256 return true;
1257 }
1258 return super.onKeyUp(keyCode, event);
1259 }
1260
Karl Rosaen138a0412009-04-23 19:00:21 -07001261 private String getTypedText() {
1262 return mDefaultKeySsb.toString();
1263 }
1264
1265 private void clearTypedText() {
1266 mDefaultKeySsb.clear();
1267 mDefaultKeySsb.clearSpans();
1268 Selection.setSelection(mDefaultKeySsb, 0);
1269 }
1270
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001272 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1273 * State
1274 */
1275 private static State intToState(int stateOrdinal) {
1276 State state = State.WORKSPACE;
1277 final State[] stateValues = State.values();
1278 for (int i = 0; i < stateValues.length; i++) {
1279 if (stateValues[i].ordinal() == stateOrdinal) {
1280 state = stateValues[i];
1281 break;
1282 }
1283 }
1284 return state;
1285 }
1286
1287 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 * Restores the previous state, if it exists.
1289 *
1290 * @param savedState The previous state.
1291 */
1292 private void restoreState(Bundle savedState) {
1293 if (savedState == null) {
1294 return;
1295 }
1296
Michael Jurka883f55b2010-10-21 15:47:14 -07001297 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001298 if (state == State.APPS || state == State.WIDGETS) {
1299 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001300 }
1301
Adam Cohen21cd0022013-10-09 18:57:02 -07001302 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1303 PagedView.INVALID_RESTORE_PAGE);
1304 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001305 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 }
1307
Sunny Goyal756cd262015-08-20 12:33:21 -07001308 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1309 if (itemValues != null) {
1310 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001311 AppWidgetProviderInfo info = savedState.getParcelable(
1312 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001313 mPendingAddWidgetInfo = info == null ?
1314 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1315
Adam Cohen4637b5a2013-11-04 18:21:24 -08001316 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001317 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 mRestoring = true;
1319 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001320 }
1321
1322 /**
1323 * Finds all the views we need and configure them properly.
1324 */
1325 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001326 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001327 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001328 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001329 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001330 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001331
Sunny Goyal784f9c32016-03-23 16:56:54 -07001332 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1333 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1334 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001335 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336
Winson Chung4c98d922011-05-31 16:50:48 -07001337 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001338
1339 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001340
Winson Chung3d503fb2011-07-13 17:25:49 -07001341 // Setup the hotseat
1342 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1343 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001344 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001345 }
1346
Winsone9f27272015-10-13 10:47:51 -07001347 // Setup the overview panel
1348 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001349
Winson Chung4c98d922011-05-31 16:50:48 -07001350 // Setup the workspace
1351 mWorkspace.setHapticFeedbackEnabled(false);
1352 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001353 mWorkspace.setup(mDragController);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001354 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001355 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001356
Tony Wickham34d2c912015-09-11 09:27:58 -07001357 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001358 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001359
Winson Chungef7f8742015-06-04 17:18:17 -07001360 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001361 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001362 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001363 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1364 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1365 } else {
1366 mAppsView.setSearchBarController(new DefaultAppSearchController());
1367 }
Craig Mautner360310b2012-10-26 15:13:08 -07001368
Winson Chung3d503fb2011-07-13 17:25:49 -07001369 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001370 mDragController.setDragScoller(mWorkspace);
1371 mDragController.setScrollView(mDragLayer);
1372 mDragController.setMoveTarget(mWorkspace);
1373 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001374 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001375
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001376 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1377
Sunny Goyal322d5562015-06-25 19:35:49 -07001378 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1379 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001380 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 }
1382
Winsone9f27272015-10-13 10:47:51 -07001383 private void setupOverviewPanel() {
1384 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1385
1386 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1387 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1388 @Override
1389 public boolean onLongClick(View v) {
1390 return v.performClick();
1391 }
1392 };
1393
1394 // Bind wallpaper button actions
1395 View wallpaperButton = findViewById(R.id.wallpaper_button);
1396 wallpaperButton.setOnClickListener(new OnClickListener() {
1397 @Override
1398 public void onClick(View view) {
1399 if (!mWorkspace.isSwitchingState()) {
1400 onClickWallpaperPicker(view);
1401 }
1402 }
1403 });
1404 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1405 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1406
1407 // Bind widget button actions
1408 mWidgetsButton = findViewById(R.id.widget_button);
1409 mWidgetsButton.setOnClickListener(new OnClickListener() {
1410 @Override
1411 public void onClick(View view) {
1412 if (!mWorkspace.isSwitchingState()) {
1413 onClickAddWidgetButton(view);
1414 }
1415 }
1416 });
1417 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1418 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1419
1420 // Bind settings actions
1421 View settingsButton = findViewById(R.id.settings_button);
1422 boolean hasSettings = hasSettings();
1423 if (hasSettings) {
1424 settingsButton.setOnClickListener(new OnClickListener() {
1425 @Override
1426 public void onClick(View view) {
1427 if (!mWorkspace.isSwitchingState()) {
1428 onClickSettingsButton(view);
1429 }
1430 }
1431 });
1432 settingsButton.setOnLongClickListener(performClickOnLongClick);
1433 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1434 } else {
1435 settingsButton.setVisibility(View.GONE);
1436 }
1437
1438 mOverviewPanel.setAlpha(0f);
1439 }
1440
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001442 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001443 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001444 */
1445 public void setAllAppsButton(View allAppsButton) {
1446 mAllAppsButton = allAppsButton;
1447 }
1448
Sunny Goyalbb011da2016-06-15 15:42:29 -07001449 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001450 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001451 }
1452
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001453 public View getWidgetsButton() {
1454 return mWidgetsButton;
1455 }
1456
Anjali Koppal5ad44842014-03-10 20:34:39 -07001457 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 * Creates a view representing a shortcut.
1459 *
1460 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001462 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001463 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 }
1465
1466 /**
1467 * Creates a view representing a shortcut inflated from the specified resource.
1468 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 * @param parent The group the shortcut belongs to.
1470 * @param info The data structure describing the shortcut.
1471 *
1472 * @return A View inflated from layoutResId.
1473 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001474 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001475 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001476 parent, false);
1477 favorite.applyFromShortcutInfo(info, mIconCache);
1478 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001480 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 return favorite;
1482 }
1483
1484 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 * Add a shortcut to the workspace.
1486 *
1487 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001489 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001490 int cellY) {
1491 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001492 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001493
Sunny Goyal5c97f512015-05-19 16:03:28 -07001494 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001495 if (info == null) {
1496 return;
1497 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001498 final View view = createShortcut(info);
1499
Sunny Goyal5c97f512015-05-19 16:03:28 -07001500 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001501 // First we check if we already know the exact location where we want to add this item.
1502 if (cellX >= 0 && cellY >= 0) {
1503 cellXY[0] = cellX;
1504 cellXY[1] = cellY;
1505 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001506
1507 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001508 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001509 true, null,null)) {
1510 return;
1511 }
1512 DragObject dragObject = new DragObject();
1513 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001514 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1515 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001516 return;
1517 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001518 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001519 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001520 }
1521
1522 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001523 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001524 return;
1525 }
1526
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001527 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528
1529 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001530 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001531 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 }
1533 }
1534
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001536 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001538 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 */
Adam Cohen091440a2015-03-18 14:16:05 -07001540 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001541 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1542
1543 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001544 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001545 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001546 }
Romain Guycbb89e42009-06-08 15:52:54 -07001547
Adam Cohen59400422014-03-05 18:07:04 -08001548 if (appWidgetInfo.isCustomWidget) {
1549 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001550 }
1551
Adam Cohen59400422014-03-05 18:07:04 -08001552 LauncherAppWidgetInfo launcherInfo;
1553 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1554 launcherInfo.spanX = info.spanX;
1555 launcherInfo.spanY = info.spanY;
1556 launcherInfo.minSpanX = info.minSpanX;
1557 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001558 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001559
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001561 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562
1563 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001564 if (hostView == null) {
1565 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001566 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001567 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001568 hostView.setVisibility(View.VISIBLE);
1569 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001571 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001572 }
Romain Guycbb89e42009-06-08 15:52:54 -07001573
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001574 private void addAppWidgetToWorkspace(
1575 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001576 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001577 hostView.setTag(item);
1578 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001579
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001580 hostView.setFocusable(true);
1581 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001582
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001583 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001584 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1585
1586 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001587 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001588 }
1589 }
1590
Adam Cohended9f8d2010-11-03 13:25:16 -07001591 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1592 @Override
1593 public void onReceive(Context context, Intent intent) {
1594 final String action = intent.getAction();
1595 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1596 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001597 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001598 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001599
Winson Chungc100e8e2011-08-09 16:02:43 -07001600 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001601 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001602 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001603 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001604 if (!showWorkspace(false)) {
1605 // If we are already on the workspace, then manually reset all apps
1606 mAppsView.reset();
1607 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001608 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001609 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1610 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001611 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001612 }
1613 }
1614 };
1615
1616 @Override
1617 public void onAttachedToWindow() {
1618 super.onAttachedToWindow();
1619
1620 // Listen for broadcasts related to user-presence
1621 final IntentFilter filter = new IntentFilter();
1622 filter.addAction(Intent.ACTION_SCREEN_OFF);
1623 filter.addAction(Intent.ACTION_USER_PRESENT);
1624 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001625 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001626 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001627 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001628
1629 if (mLauncherCallbacks != null) {
1630 mLauncherCallbacks.onAttachedToWindow();
1631 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001632 }
1633
1634 @Override
1635 public void onDetachedFromWindow() {
1636 super.onDetachedFromWindow();
1637 mVisible = false;
1638
Adam Cohend113e0c2010-11-11 10:48:05 -08001639 if (mAttached) {
1640 unregisterReceiver(mReceiver);
1641 mAttached = false;
1642 }
Winson Chungb745afb2015-03-02 11:51:23 -08001643 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001644
1645 if (mLauncherCallbacks != null) {
1646 mLauncherCallbacks.onDetachedFromWindow();
1647 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001648 }
1649
1650 public void onWindowVisibilityChanged(int visibility) {
1651 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001652 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001653 // The following code used to be in onResume, but it turns out onResume is called when
1654 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1655 // is a more appropriate event to handle
1656 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001657 if (!mWorkspaceLoading) {
1658 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001659 // We want to let Launcher draw itself at least once before we force it to build
1660 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001661 // apps is nice and speedy.
1662 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001663 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001664 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001665 if (mStarted) return;
1666 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001667 // We delay the layer building a bit in order to give
1668 // other message processing a time to run. In particular
1669 // this avoids a delay in hiding the IME if it was
1670 // currently shown, because doing that may involve
1671 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001672 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001673 final ViewTreeObserver.OnDrawListener listener = this;
1674 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001675 public void run() {
1676 if (mWorkspace != null &&
1677 mWorkspace.getViewTreeObserver() != null) {
1678 mWorkspace.getViewTreeObserver().
1679 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001680 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001681 }
1682 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001683 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001684 }
1685 });
1686 }
1687 clearTypedText();
1688 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001689 }
1690
Adam Cohen091440a2015-03-18 14:16:05 -07001691 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001692 mHandler.removeMessages(ADVANCE_MSG);
1693 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1694 mHandler.sendMessageDelayed(msg, delay);
1695 mAutoAdvanceSentTime = System.currentTimeMillis();
1696 }
1697
Adam Cohen091440a2015-03-18 14:16:05 -07001698 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001699 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1700 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1701 mAutoAdvanceRunning = autoAdvanceRunning;
1702 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001703 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001704 sendAdvanceMessage(delay);
1705 } else {
1706 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001707 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001708 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1709 }
1710 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001711 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001712 }
1713 }
1714 }
1715
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001716 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1717
Adam Cohended9f8d2010-11-03 13:25:16 -07001718 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001719 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001720 if (msg.what == ADVANCE_MSG) {
1721 int i = 0;
1722 for (View key: mWidgetsToAdvance.keySet()) {
1723 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001724 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001725 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001726 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001727 public void run() {
1728 ((Advanceable) v).advance();
1729 }
1730 }, delay);
1731 }
1732 i++;
1733 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001734 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001735 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001736 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001737 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001738 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001739
Winsonc0b52fe2015-09-09 16:38:15 -07001740 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001741 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001742 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1743 if (v instanceof Advanceable) {
1744 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001745 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001746 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001747 }
1748 }
1749
Winsonc0b52fe2015-09-09 16:38:15 -07001750 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001751 if (mWidgetsToAdvance.containsKey(hostView)) {
1752 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001753 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001754 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001755 }
1756
Hyunyoung Song3f471442015-04-08 19:01:34 -07001757 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001758 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1759 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001760 }
1761
Winson Chunga6945242014-01-08 14:04:34 -08001762 public DragLayer getDragLayer() {
1763 return mDragLayer;
1764 }
1765
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001766 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001767 return mAppsView;
1768 }
1769
Hyunyoung Song3f471442015-04-08 19:01:34 -07001770 public WidgetsContainerView getWidgetsView() {
1771 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001772 }
1773
Winson Chunga6945242014-01-08 14:04:34 -08001774 public Workspace getWorkspace() {
1775 return mWorkspace;
1776 }
1777
1778 public Hotseat getHotseat() {
1779 return mHotseat;
1780 }
1781
Jorim Jaggid017f882014-01-14 17:08:48 -08001782 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001783 return mOverviewPanel;
1784 }
1785
Sunny Goyal47328fd2016-05-25 18:56:41 -07001786 public DropTargetBar getDropTargetBar() {
1787 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001788 }
1789
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001790 public LauncherAppWidgetHost getAppWidgetHost() {
1791 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 }
Romain Guycbb89e42009-06-08 15:52:54 -07001793
Winson Chunga9abd0e2010-10-27 17:18:37 -07001794 public LauncherModel getModel() {
1795 return mModel;
1796 }
1797
Adam Cohen79d90c52016-04-22 13:29:20 -07001798 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001799 return mSharedPrefs;
1800 }
1801
Adam Cohen2e6da152015-05-06 11:42:25 -07001802 public DeviceProfile getDeviceProfile() {
1803 return mDeviceProfile;
1804 }
1805
Bjorn Bringertc459e522013-06-07 19:36:01 +01001806 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001807 getWindow().closeAllPanels();
1808
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001809 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001810 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001811 }
1812
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001813 @Override
1814 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001815 long startTime = 0;
1816 if (DEBUG_RESUME_TIME) {
1817 startTime = System.currentTimeMillis();
1818 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001819 super.onNewIntent(intent);
1820
1821 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001822 Folder openFolder = mWorkspace.getOpenFolder();
1823 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1824 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1825 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1826 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1827 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001828 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001829 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001830
Adam Cohen6fecd412013-10-02 17:41:50 -07001831 if (mWorkspace == null) {
1832 // Can be cases where mWorkspace is null, this prevents a NPE
1833 return;
1834 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001835 // In all these cases, only animate if we're already on home
1836 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001837
Sunny Goyal935fca12015-10-13 10:19:01 -07001838 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001839 exitSpringLoadedDragMode();
1840
1841 // If we are already on home, then just animate back to the workspace,
1842 // otherwise, just wait until onResume to set the state back to Workspace
1843 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001844 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001845 } else {
1846 mOnResumeState = State.WORKSPACE;
1847 }
1848
1849 final View v = getWindow().peekDecorView();
1850 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001851 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001852 INPUT_METHOD_SERVICE);
1853 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1854 }
1855
Winson Chungb745afb2015-03-02 11:51:23 -08001856 // Reset the apps view
1857 if (!alreadyOnHome && mAppsView != null) {
1858 mAppsView.scrollToTop();
1859 }
1860
Hyunyoung Song3f471442015-04-08 19:01:34 -07001861 // Reset the widgets view
1862 if (!alreadyOnHome && mWidgetsView != null) {
1863 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001864 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001865
Adam Cohen9211d422014-10-07 18:14:53 -07001866 if (mLauncherCallbacks != null) {
1867 mLauncherCallbacks.onHomeIntent();
1868 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869 }
Adam Cohened307df2013-10-02 09:37:31 -07001870
Adam Cohen9211d422014-10-07 18:14:53 -07001871 if (mLauncherCallbacks != null) {
1872 mLauncherCallbacks.onNewIntent(intent);
1873 }
Winson15f8b172015-08-19 11:02:39 -07001874
1875 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1876 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1877 // animation.
1878 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001879 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1880 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001881 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1882 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001883
1884 // We use this flag to suppress noisy callbacks above custom content state
1885 // from onResume.
1886 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001887 mWorkspace.post(new Runnable() {
1888 @Override
1889 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001890 if (mWorkspace != null) {
1891 mWorkspace.moveToDefaultScreen(true);
1892 }
Winson15f8b172015-08-19 11:02:39 -07001893 }
1894 });
1895 }
1896 }
1897
1898 if (DEBUG_RESUME_TIME) {
1899 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1900 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001901 }
1902
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001904 public void onRestoreInstanceState(Bundle state) {
1905 super.onRestoreInstanceState(state);
1906 for (int page: mSynchronouslyBoundPages) {
1907 mWorkspace.restoreInstanceStateForChild(page);
1908 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 }
1910
1911 @Override
1912 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001913 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001914 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1915 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001916
Adam Cohen21cd0022013-10-09 18:57:02 -07001917 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001918 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919
Michael Jurka883f55b2010-10-21 15:47:14 -07001920 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001921 // We close any open folder since it will not be re-opened, and we need to make sure
1922 // this state is reflected.
1923 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1924 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925
Adam Cohendcd297f2013-06-18 13:13:40 -07001926 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001927 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001928 ContentValues itemValues = new ContentValues();
1929 mPendingAddInfo.writeToValues(itemValues);
1930 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001931 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001932 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933 }
1934
Adam Cohen9211d422014-10-07 18:14:53 -07001935 if (mLauncherCallbacks != null) {
1936 mLauncherCallbacks.onSaveInstanceState(outState);
1937 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 }
1939
1940 @Override
1941 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001943
Winson Chunge7a03942011-08-05 15:05:12 -07001944 // Remove all pending runnables
1945 mHandler.removeMessages(ADVANCE_MSG);
1946 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001947 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001948 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001949
Winson Chungcd2b0142011-06-08 16:02:26 -07001950 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001951 // It's possible to receive onDestroy after a new Launcher activity has
1952 // been created. In this case, don't interfere with the new Launcher.
1953 if (mModel.isCurrentCallbacks(this)) {
1954 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001955 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001956 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001957
Sunny Goyal745bad92016-05-02 10:54:12 -07001958 if (mRotationPrefChangeHandler != null) {
1959 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1960 }
1961
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001963 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001965 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001967 mAppWidgetHost = null;
1968
1969 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001970
1971 TextKeyListener.getInstance().release();
1972
Tony Wickhame2217252016-03-22 16:34:23 -07001973 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1974 .removeAccessibilityStateChangeListener(this);
1975
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001976 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001977
Michael Jurka2ecf9952012-06-18 12:52:28 -07001978 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001979
1980 if (mLauncherCallbacks != null) {
1981 mLauncherCallbacks.onDestroy();
1982 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 }
1984
Sunny Goyalae502842016-06-17 08:43:56 -07001985 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1986 return mAccessibilityDelegate;
1987 }
1988
Adam Cohena9cf38f2011-05-02 15:36:58 -07001989 public DragController getDragController() {
1990 return mDragController;
1991 }
1992
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 @Override
1994 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07001995 onStartForResult(requestCode);
1996 super.startActivityForResult(intent, requestCode);
1997 }
1998
1999 @Override
2000 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2001 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2002 onStartForResult(requestCode);
2003 try {
2004 super.startIntentSenderForResult(intent, requestCode,
2005 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2006 } catch (IntentSender.SendIntentException e) {
2007 throw new ActivityNotFoundException();
2008 }
2009 }
2010
2011 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002012 if (requestCode >= 0) {
2013 setWaitingForResult(true);
2014 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 }
2016
Winson Chung70d72102011-08-12 11:24:35 -07002017 /**
2018 * Indicates that we want global search for this activity by setting the globalSearch
2019 * argument for {@link #startSearch} to true.
2020 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002022 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002024
Karl Rosaen138a0412009-04-23 19:00:21 -07002025 if (initialQuery == null) {
2026 // Use any text typed in the launcher as the initial query
2027 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002028 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 if (appSearchData == null) {
2030 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002031 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002033
2034 // TODO send proper bounds.
2035 Rect sourceBounds = null;
Romain Guycbb89e42009-06-08 15:52:54 -07002036
Ian Parkinson047036e2014-09-01 15:40:53 +01002037 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002038 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002039 if (clearTextImmediately) {
2040 clearTypedText();
2041 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002042
2043 // We need to show the workspace after starting the search
2044 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002045 }
2046
Ian Parkinson047036e2014-09-01 15:40:53 +01002047 /**
2048 * Start a text search.
2049 *
2050 * @return {@code true} if the search will start immediately, so any further keypresses
2051 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2052 * to buffer keypresses.
2053 */
2054 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002055 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002056 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2057 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2058 sourceBounds);
2059 }
2060
Michael Jurkaa33411c2012-06-14 16:18:21 -07002061 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002062 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002063 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002064 }
2065
2066 /**
2067 * Starts the global search activity. This code is a copied from SearchManager
2068 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002069 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002070 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002071 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002072 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2073 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2074 if (globalSearchActivity == null) {
2075 Log.w(TAG, "No global search activity found.");
2076 return;
2077 }
2078 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2079 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2080 intent.setComponent(globalSearchActivity);
2081 // Make sure that we have a Bundle to put source in
2082 if (appSearchData == null) {
2083 appSearchData = new Bundle();
2084 } else {
2085 appSearchData = new Bundle(appSearchData);
2086 }
Adam Cohen9211d422014-10-07 18:14:53 -07002087 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002088 if (!appSearchData.containsKey("source")) {
2089 appSearchData.putString("source", getPackageName());
2090 }
2091 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2092 if (!TextUtils.isEmpty(initialQuery)) {
2093 intent.putExtra(SearchManager.QUERY, initialQuery);
2094 }
2095 if (selectInitialQuery) {
2096 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2097 }
2098 intent.setSourceBounds(sourceBounds);
2099 try {
2100 startActivity(intent);
2101 } catch (ActivityNotFoundException ex) {
2102 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2103 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002104 }
2105
Yura4f93ec62014-02-04 14:15:21 +00002106 public boolean isOnCustomContent() {
2107 return mWorkspace.isOnOrMovingToCustomContent();
2108 }
2109
Winson Chung70d72102011-08-12 11:24:35 -07002110 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002111 public boolean onPrepareOptionsMenu(Menu menu) {
2112 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002113 if (mLauncherCallbacks != null) {
2114 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2115 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002116 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002117 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002118
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002119 @Override
2120 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002121 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002122 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002123 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002124 }
2125
Joe Onorato9c1289c2009-08-17 11:03:03 -04002126 public boolean isWorkspaceLocked() {
2127 return mWorkspaceLoading || mWaitingForResult;
2128 }
2129
Adam Cohen517a7f52014-03-01 12:12:59 -08002130 public boolean isWorkspaceLoading() {
2131 return mWorkspaceLoading;
2132 }
2133
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002134 private void setWorkspaceLoading(boolean value) {
2135 boolean isLocked = isWorkspaceLocked();
2136 mWorkspaceLoading = value;
2137 if (isLocked != isWorkspaceLocked()) {
2138 onWorkspaceLockedChanged();
2139 }
2140 }
2141
2142 private void setWaitingForResult(boolean value) {
2143 boolean isLocked = isWorkspaceLocked();
2144 mWaitingForResult = value;
2145 if (isLocked != isWorkspaceLocked()) {
2146 onWorkspaceLockedChanged();
2147 }
2148 }
2149
Adam Cohen9211d422014-10-07 18:14:53 -07002150 protected void onWorkspaceLockedChanged() {
2151 if (mLauncherCallbacks != null) {
2152 mLauncherCallbacks.onWorkspaceLockedChanged();
2153 }
2154 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002155
Michael Jurka0280c3b2010-09-17 15:00:07 -07002156 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002157 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002158 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002159 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2160 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002161 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002162 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002163
Tony Wickhama0628cc2015-10-14 15:23:04 -07002164 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002165 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002166 if (LOGD) {
2167 Log.d(TAG, "Adding widget from drop");
2168 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002169 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2170 }
2171
Sunny Goyale6b63a32015-01-16 16:14:29 -08002172 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002173 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2174 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002175 if (appWidgetInfo.configure != null) {
2176 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002177 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002178
Bjorn Bringert7984c942009-12-09 15:38:25 +00002179 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002180 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2181 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2182
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002184 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002185 Runnable onComplete = new Runnable() {
2186 @Override
2187 public void run() {
2188 // Exit spring loaded mode if necessary after adding the widget
2189 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2190 null);
2191 }
2192 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002193 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002194 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002195 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002196 }
2197 }
Romain Guycbb89e42009-06-08 15:52:54 -07002198
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002199 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002200 // Close any folders that may be open.
2201 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002202 mWorkspace.moveToCustomContentScreen(animate);
2203 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002204
2205 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2206 int[] cell, int spanX, int spanY) {
2207 switch (info.itemType) {
2208 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2209 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2210 int span[] = new int[2];
2211 span[0] = spanX;
2212 span[1] = spanY;
2213 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2214 container, screenId, cell, span);
2215 break;
2216 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2217 processShortcutFromDrop(info.componentName, container, screenId, cell);
2218 break;
2219 default:
2220 throw new IllegalStateException("Unknown item type: " + info.itemType);
2221 }
2222 }
2223
Adam Cohenfbba09b2011-07-18 21:43:05 -07002224 /**
2225 * Process a shortcut drop.
2226 *
2227 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002228 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002229 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002230 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002231 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2232 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002233 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002234 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002235 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002236
2237 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002238 mPendingAddInfo.cellX = cell[0];
2239 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002240 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002241
2242 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2243 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002244 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002245 }
2246
Adam Cohenfbba09b2011-07-18 21:43:05 -07002247 /**
2248 * Process a widget drop.
2249 *
2250 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002251 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002252 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002253 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002254 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2255 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002256 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002257 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002258 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002259 mPendingAddInfo.minSpanX = info.minSpanX;
2260 mPendingAddInfo.minSpanY = info.minSpanY;
2261
Adam Cohenfbba09b2011-07-18 21:43:05 -07002262 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002263 mPendingAddInfo.cellX = cell[0];
2264 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002265 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002266 if (span != null) {
2267 mPendingAddInfo.spanX = span[0];
2268 mPendingAddInfo.spanY = span[1];
2269 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002270
Adam Cohened66b2b2012-01-23 17:28:51 -08002271 AppWidgetHostView hostView = info.boundWidget;
2272 int appWidgetId;
2273 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002274 // In the case where we've prebound the widget, we remove it from the DragLayer
2275 if (LOGD) {
2276 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2277 }
2278 getDragLayer().removeView(hostView);
2279
Adam Cohened66b2b2012-01-23 17:28:51 -08002280 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002281 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002282
2283 // Clear the boundWidget so that it doesn't get destroyed.
2284 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002285 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002286 // In this case, we either need to start an activity to get permission to bind
2287 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002288 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002289 Bundle options = info.bindOptions;
2290
Sunny Goyalffe83f12014-08-14 17:39:34 -07002291 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2292 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002293 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002294 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002295 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002296 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002297 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2298 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2299 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002300 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2301 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002302 // TODO: we need to make sure that this accounts for the options bundle.
2303 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002304 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2305 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002306 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002307 }
2308
Adam Cohendcd297f2013-06-18 13:13:40 -07002309 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002310 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002311 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002312 folderInfo.title = getText(R.string.folder_name);
2313
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002314 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002315 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2316 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002317
2318 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002319 FolderIcon newFolder =
2320 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002321 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002322 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002323 // Force measure the new folder icon
2324 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2325 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002326 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002327 }
Romain Guycbb89e42009-06-08 15:52:54 -07002328
Winsonc0b52fe2015-09-09 16:38:15 -07002329 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002330 * Unbinds the view for the specified item, and removes the item and all its children.
2331 *
2332 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002333 * @param itemInfo the {@link ItemInfo} for this view.
2334 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002335 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002336 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002337 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002338 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002339 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2340 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002341 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002342 } else {
2343 mWorkspace.removeWorkspaceItem(v);
2344 }
Winsonc0b52fe2015-09-09 16:38:15 -07002345 if (deleteFromDb) {
2346 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2347 }
2348 } else if (itemInfo instanceof FolderInfo) {
2349 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002350 if (v instanceof FolderIcon) {
2351 ((FolderIcon) v).removeListeners();
2352 }
Winsonc0b52fe2015-09-09 16:38:15 -07002353 mWorkspace.removeWorkspaceItem(v);
2354 if (deleteFromDb) {
2355 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2356 }
2357 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2358 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002359 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002360 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002361 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002362 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002363 }
2364 } else {
2365 return false;
2366 }
2367 return true;
2368 }
2369
2370 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002371 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002372 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002373 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002374 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002375 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002376 // Deleting an app widget ID is a void call but writes to disk before returning
2377 // to the caller...
2378 new AsyncTask<Void, Void, Void>() {
2379 public Void doInBackground(Void ... args) {
2380 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2381 return null;
2382 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002383 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002384 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002385 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002386 }
2387
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002388 @Override
2389 public boolean dispatchKeyEvent(KeyEvent event) {
2390 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2391 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002392 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002393 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002394 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002395 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002396 dumpState();
2397 return true;
2398 }
2399 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002400 }
2401 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2402 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002403 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002404 return true;
2405 }
2406 }
2407
2408 return super.dispatchKeyEvent(event);
2409 }
2410
Joe Onorato88ec0992009-11-19 13:16:06 -08002411 @Override
2412 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002413 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2414 return;
2415 }
2416
Sunny Goyal45478022015-06-08 16:52:41 -07002417 if (mDragController.isDragging()) {
2418 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002419 return;
2420 }
2421
Winson Chungb745afb2015-03-02 11:51:23 -08002422 if (isAppsViewVisible()) {
2423 showWorkspace(true);
2424 } else if (isWidgetsViewVisible()) {
2425 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002426 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002427 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002428 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002429 Folder openFolder = mWorkspace.getOpenFolder();
2430 if (openFolder.isEditingName()) {
2431 openFolder.dismissEditingName();
2432 } else {
2433 closeFolder();
2434 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002435 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002436 mWorkspace.exitWidgetResizeMode();
2437
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002438 // Back button is a no-op here, but give at least some feedback for the button press
2439 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002440 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002441 }
2442
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002443 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002444 * Launches the intent referred by the clicked shortcut.
2445 *
2446 * @param v The view representing the clicked shortcut.
2447 */
2448 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002449 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2450 // view has detached (it's possible for this to happen if the view is removed mid touch).
2451 if (v.getWindowToken() == null) {
2452 return;
2453 }
2454
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002455 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002456 return;
2457 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002458
Adam Cohen1697b792013-09-17 19:08:21 -07002459 if (v instanceof Workspace) {
2460 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002461 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002462 }
2463 return;
2464 }
2465
2466 if (v instanceof CellLayout) {
2467 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002468 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2469 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002470 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002471 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002472 }
2473
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002474 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002475 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002476 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002477 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002478 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002479 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002480 }
Sunny Goyalbb011da2016-06-15 15:42:29 -07002481 } else if (v instanceof PageIndicator || (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002482 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002483 } else if (tag instanceof AppInfo) {
2484 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002485 } else if (tag instanceof LauncherAppWidgetInfo) {
2486 if (v instanceof PendingAppWidgetHostView) {
2487 onClickPendingWidget((PendingAppWidgetHostView) v);
2488 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002489 }
2490 }
2491
Sunny Goyal70660032015-05-14 00:07:08 -07002492 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002493 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002494 return false;
2495 }
2496
Michael Jurkaaf442092010-06-10 17:01:57 -07002497 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002498 * Event handler for the app widget view which has not fully restored.
2499 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002500 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002501 if (mIsSafeModeEnabled) {
2502 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2503 return;
2504 }
2505
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002506 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002507 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002508 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2509 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2510 // This should not happen, as we make sure that an Id is allocated during bind.
2511 return;
2512 }
2513 LauncherAppWidgetProviderInfo appWidgetInfo =
2514 mAppWidgetManager.findProvider(info.providerName, info.user);
2515 if (appWidgetInfo != null) {
2516 mPendingAddWidgetId = info.appWidgetId;
2517 mPendingAddInfo.copyFrom(info);
2518 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002519
Sunny Goyald478c832016-04-01 12:04:16 -07002520 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2521 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingAddWidgetId);
2522 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
2523 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2524 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2525 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2526 }
2527 } else {
2528 LauncherAppWidgetProviderInfo appWidgetInfo =
2529 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2530 if (appWidgetInfo != null) {
2531 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2532 }
Sunny Goyalff572272014-07-23 13:58:07 -07002533 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002534 } else if (info.installProgress < 0) {
2535 // The install has not been queued
2536 final String packageName = info.providerName.getPackageName();
2537 showBrokenAppInstallDialog(packageName,
2538 new DialogInterface.OnClickListener() {
2539 public void onClick(DialogInterface dialog, int id) {
2540 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2541 }
2542 });
2543 } else {
2544 // Download has started.
2545 final String packageName = info.providerName.getPackageName();
2546 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002547 }
2548 }
2549
Sunny Goyald478c832016-04-01 12:04:16 -07002550 private void startRestoredWidgetReconfigActivity(
2551 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
2552 mPendingAddWidgetInfo = provider;
2553 mPendingAddInfo.copyFrom(info);
2554 mPendingAddWidgetId = info.appWidgetId;
2555 mAppWidgetManager.startConfigActivity(provider,
2556 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2557 }
2558
Sunny Goyalff572272014-07-23 13:58:07 -07002559 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002560 * Event handler for the "grid" button that appears on the home screen, which
2561 * enters all apps mode.
2562 *
2563 * @param v The view that was clicked.
2564 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002565 protected void onClickAllAppsButton(View v) {
2566 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002567 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002568 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002569 true /* updatePredictedApps */, false /* focusSearchBar */);
2570 }
2571 }
2572
2573 protected void onLongClickAllAppsButton(View v) {
2574 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2575 if (!isAppsViewVisible()) {
2576 showAppsView(true /* animated */, false /* resetListToTop */,
2577 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002578 }
2579 }
2580
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002581 private void showBrokenAppInstallDialog(final String packageName,
2582 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002583 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002584 .setTitle(R.string.abandoned_promises_title)
2585 .setMessage(R.string.abandoned_promise_explanation)
2586 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2587 .setNeutralButton(R.string.abandoned_clean_this,
2588 new DialogInterface.OnClickListener() {
2589 public void onClick(DialogInterface dialog, int id) {
2590 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2591 mWorkspace.removeAbandonedPromise(packageName, user);
2592 }
2593 })
2594 .create().show();
2595 return;
2596 }
2597
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002598 /**
2599 * Event handler for an app shortcut click.
2600 *
2601 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2602 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002603 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002604 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2605 Object tag = v.getTag();
2606 if (!(tag instanceof ShortcutInfo)) {
2607 throw new IllegalArgumentException("Input must be a Shortcut");
2608 }
2609
2610 // Open shortcut
2611 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002612
2613 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002614 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2615 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002616 // Launch activity anyway, framework will tell the user why the app is suspended.
2617 } else {
2618 int error = R.string.activity_not_available;
2619 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2620 error = R.string.safemode_shortcut_error;
2621 }
2622 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2623 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002624 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002625 }
2626
Chris Wren40c5ed32014-06-24 18:24:23 -04002627 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002628 if ((v instanceof BubbleTextView)
2629 && shortcut.isPromise()
2630 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002631 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002632 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002633 new DialogInterface.OnClickListener() {
2634 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002635 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002636 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002637 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002638 return;
2639 }
2640
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002641 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002642 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002643 }
2644
Sunny Goyala7ce1662016-05-31 15:01:35 -07002645 private void startAppShortcutOrInfoActivity(View v) {
2646 ItemInfo item = (ItemInfo) v.getTag();
2647 Intent intent = item.getIntent();
2648 if (intent == null) {
2649 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002650 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002651 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002652 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002653
2654 if (success && v instanceof BubbleTextView) {
2655 mWaitingForResume = (BubbleTextView) v;
2656 mWaitingForResume.setStayPressed(true);
2657 }
2658 }
2659
2660 /**
2661 * Event handler for a folder icon click.
2662 *
2663 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2664 */
2665 protected void onClickFolderIcon(View v) {
2666 if (LOGD) Log.d(TAG, "onClickFolder");
2667 if (!(v instanceof FolderIcon)){
2668 throw new IllegalArgumentException("Input must be a FolderIcon");
2669 }
2670
2671 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002672 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002673 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002674 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002675 }
Michael Jurka5130e402011-10-13 04:55:35 -07002676 }
2677
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002678 /**
2679 * Event handler for the (Add) Widgets button that appears after a long press
2680 * on the home screen.
2681 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002682 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002683 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002684 if (mIsSafeModeEnabled) {
2685 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2686 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002687 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002688 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002689 }
2690
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002691 /**
2692 * Event handler for the wallpaper picker button that appears after a long press
2693 * on the home screen.
2694 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002695 public void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002696 if (!Utilities.isWallapaperAllowed(this)) {
2697 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2698 return;
2699 }
2700
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002701 String pickerPackage = getString(R.string.wallpaper_picker_package);
2702 if (TextUtils.isEmpty(pickerPackage)) {
2703 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2704 }
2705
Tony Wickham785f7a52015-08-31 17:28:32 -07002706 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2707 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002708 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER)
2709 .setPackage(pickerPackage)
2710 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset),
2711 REQUEST_PICK_WALLPAPER);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002712 }
2713
2714 /**
2715 * Event handler for a click on the settings button that appears after a long press
2716 * on the home screen.
2717 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002718 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002719 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002720 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2721 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002722 }
2723
Adam Cohen61f560d2013-09-30 15:58:20 -07002724 public View.OnTouchListener getHapticFeedbackTouchListener() {
2725 if (mHapticFeedbackTouchListener == null) {
2726 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002727 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002728 @Override
2729 public boolean onTouch(View v, MotionEvent event) {
2730 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2731 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2732 }
2733 return false;
2734 }
2735 };
2736 }
2737 return mHapticFeedbackTouchListener;
2738 }
2739
Tony Wickhame2217252016-03-22 16:34:23 -07002740 @Override
2741 public void onAccessibilityStateChanged(boolean enabled) {
2742 mDragLayer.onAccessibilityStateChanged(enabled);
2743 }
2744
Adam Cohen9211d422014-10-07 18:14:53 -07002745 public void onDragStarted(View view) {
2746 if (isOnCustomContent()) {
2747 // Custom content screen doesn't participate in drag and drop. If on custom
2748 // content screen, move to default.
2749 moveWorkspaceToDefaultScreen();
2750 }
Adam Cohen9211d422014-10-07 18:14:53 -07002751 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002752
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002753 /**
2754 * Called when the user stops interacting with the launcher.
2755 * This implies that the user is now on the homescreen and is not doing housekeeping.
2756 */
Adam Cohen9211d422014-10-07 18:14:53 -07002757 protected void onInteractionEnd() {
2758 if (mLauncherCallbacks != null) {
2759 mLauncherCallbacks.onInteractionEnd();
2760 }
2761 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002762
2763 /**
2764 * Called when the user starts interacting with the launcher.
2765 * The possible interactions are:
2766 * - open all apps
2767 * - reorder an app shortcut, or a widget
2768 * - open the overview mode.
2769 * This is a good time to stop doing things that only make sense
2770 * when the user is on the homescreen and not doing housekeeping.
2771 */
Adam Cohen9211d422014-10-07 18:14:53 -07002772 protected void onInteractionBegin() {
2773 if (mLauncherCallbacks != null) {
2774 mLauncherCallbacks.onInteractionBegin();
2775 }
2776 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002777
Winson Chungcd99cd32015-04-29 11:03:24 -07002778 /** Updates the interaction state. */
2779 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002780 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002781 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002782 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2783 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002784 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002785 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002786 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002787 onInteractionEnd();
2788 }
2789 }
2790
Sunny Goyala7ce1662016-05-31 15:01:35 -07002791 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002792 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002793 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2794 try {
2795 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2796 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2797 // is enabled by default on NYC.
2798 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2799 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002800
2801 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2802 String id = ((ShortcutInfo) info).getDeepShortcutId();
2803 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002804 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2805 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002806 } else {
2807 // Could be launching some bookkeeping activity
2808 startActivity(intent, optsBundle);
2809 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002810 } finally {
2811 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002812 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002813 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002814 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2815 // corresponding permission. Show the appropriate permission prompt if that
2816 // is the case.
2817 if (intent.getComponent() == null
2818 && Intent.ACTION_CALL.equals(intent.getAction())
2819 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2820 PackageManager.PERMISSION_GRANTED) {
2821 // TODO: Rename sPendingAddItem to a generic name.
2822 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2823 0, info);
2824 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2825 REQUEST_PERMISSION_CALL_PHONE);
2826 } else {
2827 // No idea why this was thrown.
2828 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002829 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002830 }
2831 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002832
Sunny Goyala7ce1662016-05-31 15:01:35 -07002833 private Bundle getActivityLaunchOptions(View v) {
2834 if (Utilities.ATLEAST_MARSHMALLOW) {
2835 int left = 0, top = 0;
2836 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2837 if (v instanceof TextView) {
2838 // Launch from center of icon, not entire view
2839 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2840 if (icon != null) {
2841 Rect bounds = icon.getBounds();
2842 left = (width - bounds.width()) / 2;
2843 top = v.getPaddingTop();
2844 width = bounds.width();
2845 height = bounds.height();
2846 }
2847 }
2848 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2849 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2850 // On L devices, we use the device default slide-up transition.
2851 // On L MR1 devices, we use a custom version of the slide-up transition which
2852 // doesn't have the delay present in the device default.
2853 return ActivityOptions.makeCustomAnimation(
2854 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2855 }
2856 return null;
2857 }
2858
2859 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002860 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2861 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2862 return false;
2863 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002864 // Only launch using the new animation if the shortcut has not opted out (this is a
2865 // private contract between launcher and may be ignored in the future).
2866 boolean useLaunchAnimation = (v != null) &&
2867 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2868 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2869
2870 UserHandleCompat user = null;
2871 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2872 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2873 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002874 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002875
2876 // Prepare intent
2877 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2878 if (v != null) {
2879 int[] pos = new int[2];
2880 v.getLocationOnScreen(pos);
2881 intent.setSourceBounds(
2882 new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2883 }
2884 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002885 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2886 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
2887 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002888 // Shortcuts need some special checks due to legacy reasons.
2889 startShortcutIntentSafely(intent, optsBundle, item);
2890 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2891 // Could be launching some bookkeeping activity
2892 startActivity(intent, optsBundle);
2893 } else {
2894 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2895 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2896 }
2897 return true;
2898 } catch (ActivityNotFoundException|SecurityException e) {
2899 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2900 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2901 }
2902 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002903 }
2904
Adam Cohen268c4752012-06-06 17:47:33 -07002905 /**
2906 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2907 * in the DragLayer in the exact absolute location of the original FolderIcon.
2908 */
2909 private void copyFolderIconToImage(FolderIcon fi) {
2910 final int width = fi.getMeasuredWidth();
2911 final int height = fi.getMeasuredHeight();
2912
2913 // Lazy load ImageView, Bitmap and Canvas
2914 if (mFolderIconImageView == null) {
2915 mFolderIconImageView = new ImageView(this);
2916 }
2917 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2918 mFolderIconBitmap.getHeight() != height) {
2919 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2920 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2921 }
2922
2923 DragLayer.LayoutParams lp;
2924 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2925 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2926 } else {
2927 lp = new DragLayer.LayoutParams(width, height);
2928 }
2929
Adam Cohen307fe232012-08-16 17:55:58 -07002930 // The layout from which the folder is being opened may be scaled, adjust the starting
2931 // view size by this scale factor.
2932 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002933 lp.customPosition = true;
2934 lp.x = mRectForFolderAnimation.left;
2935 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002936 lp.width = (int) (scale * width);
2937 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002938
2939 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2940 fi.draw(mFolderIconCanvas);
2941 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002942 if (fi.getFolder() != null) {
2943 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2944 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002945 }
Adam Cohen268c4752012-06-06 17:47:33 -07002946 // Just in case this image view is still in the drag layer from a previous animation,
2947 // we remove it and re-add it.
2948 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2949 mDragLayer.removeView(mFolderIconImageView);
2950 }
2951 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002952 if (fi.getFolder() != null) {
2953 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002954 }
Adam Cohen268c4752012-06-06 17:47:33 -07002955 }
2956
Adam Cohen37b2a492016-04-06 18:36:06 -07002957 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002958 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002959 FolderInfo info = (FolderInfo) fi.getTag();
2960 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2961 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002962 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2963 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002964 }
2965
Adam Cohen268c4752012-06-06 17:47:33 -07002966 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2967 copyFolderIconToImage(fi);
2968 fi.setVisibility(View.INVISIBLE);
2969
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002970 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2971 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002972 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002973 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2974 }
2975 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002976 oa.start();
2977 }
2978
Sunny Goyal935fca12015-10-13 10:19:01 -07002979 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002980 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002981 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002982
Adam Cohen268c4752012-06-06 17:47:33 -07002983 // We remove and re-draw the FolderIcon in-case it has changed
2984 mDragLayer.removeView(mFolderIconImageView);
2985 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08002986
2987 if (cl != null) {
2988 cl.clearFolderLeaveBehind();
2989 }
2990
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002991 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002992 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002993 oa.addListener(new AnimatorListenerAdapter() {
2994 @Override
2995 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002996 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07002997 // Remove the ImageView copy of the FolderIcon and make the original visible.
2998 mDragLayer.removeView(mFolderIconImageView);
2999 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003000 }
3001 }
3002 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003003 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003004 if (!animate) {
3005 oa.end();
3006 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003007 }
3008
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003009 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003010 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003011 * is animated relative to the specified View. If the View is null, no animation
3012 * is played.
3013 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003014 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003015 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003016 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003017
Adam Cohenfb91f302012-06-11 15:45:18 -07003018 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003019 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3020 if (openFolder != null && openFolder != folder) {
3021 // Close any open folder before opening a folder.
3022 closeFolder();
3023 }
3024
Adam Cohena9cf38f2011-05-02 15:36:58 -07003025 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003026
Adam Cohena9cf38f2011-05-02 15:36:58 -07003027 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003028
Sunny Goyalf4066152015-04-15 09:42:19 -07003029 // While the folder is open, the position of the icon cannot change.
3030 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3031
Adam Cohen4554ee12011-08-03 16:13:21 -07003032 // Just verify that the folder hasn't already been added to the DragLayer.
3033 // There was a one-off crash where the folder had a parent already.
3034 if (folder.getParent() == null) {
3035 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003036 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003037 } else {
3038 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3039 folder.getParent() + ").");
3040 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003041 folder.animateOpen();
3042
3043 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003044
3045 // Notify the accessibility manager that this folder "window" has appeared and occluded
3046 // the workspace items
3047 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3048 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003049 }
3050
3051 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003052 closeFolder(true);
3053 }
3054
3055 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003056 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003057 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003058 if (folder.isEditingName()) {
3059 folder.dismissEditingName();
3060 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003061 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003062 }
3063 }
3064
Sunny Goyal935fca12015-10-13 10:19:01 -07003065 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003066 animate &= !Utilities.isPowerSaverOn(this);
3067
Adam Cohen4554ee12011-08-03 16:13:21 -07003068 folder.getInfo().opened = false;
3069
3070 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3071 if (parent != null) {
3072 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003073 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003074 if (fi != null) {
3075 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3076 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003077 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003078 if (animate) {
3079 folder.animateClosed();
3080 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003081 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003082 }
Winson Chung83ca4802013-04-12 15:10:52 -07003083
Sunny Goyal935fca12015-10-13 10:19:01 -07003084 // Notify the accessibility manager that this folder "window" has disappeared and no
3085 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003086 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003087 }
3088
Tony Wickhamdadb3042016-02-24 11:07:00 -08003089 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003090 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003091 if (!isDraggingEnabled()) return false;
3092 if (isWorkspaceLocked()) return false;
3093 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003094
Sunny Goyalbb011da2016-06-15 15:42:29 -07003095 if (v == mAllAppsButton && mAllAppsButton != null) {
Winson Chung76648c52015-07-10 14:33:23 -07003096 onLongClickAllAppsButton(v);
3097 return true;
3098 }
3099
Adam Cohen1697b792013-09-17 19:08:21 -07003100 if (v instanceof Workspace) {
3101 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003102 if (!mWorkspace.isTouchActive()) {
3103 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003104 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3105 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3106 return true;
3107 } else {
3108 return false;
3109 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003110 } else {
3111 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003112 }
Adam Cohen1697b792013-09-17 19:08:21 -07003113 }
3114
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003115 CellLayout.CellInfo longClickCellInfo = null;
3116 View itemUnderLongClick = null;
3117 if (v.getTag() instanceof ItemInfo) {
3118 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003119 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003120 itemUnderLongClick = longClickCellInfo.cell;
3121 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003122 }
3123
Winson Chung3d503fb2011-07-13 17:25:49 -07003124 // The hotseat touch handling does not go through Workspace, and we always allow long press
3125 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003126 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003127 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003128 // User long pressed on empty space
3129 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3130 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003131 if (mWorkspace.isInOverviewMode()) {
3132 mWorkspace.startReordering(v);
3133 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003134 showOverviewMode(true);
Hyunyoung Song645764e2016-06-06 14:19:02 -07003135 mHotseat.requestDisallowInterceptTouchEvent(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003136 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003137 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003138 final boolean isAllAppsButton =
3139 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3140 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3141 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003142 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003143 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003144 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003145 }
3146 }
3147 }
3148 return true;
3149 }
3150
Winson Chung3d503fb2011-07-13 17:25:49 -07003151 boolean isHotseatLayout(View layout) {
3152 return mHotseat != null && layout != null &&
3153 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3154 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003155
Winson Chung3d503fb2011-07-13 17:25:49 -07003156 /**
3157 * Returns the CellLayout of the specified container at the specified screen.
3158 */
Sunny Goyal92820592015-03-02 11:31:35 -08003159 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003160 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3161 if (mHotseat != null) {
3162 return mHotseat.getLayout();
3163 } else {
3164 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003165 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003166 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003167 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003168 }
3169 }
3170
Winson Chungb745afb2015-03-02 11:51:23 -08003171 /**
3172 * For overridden classes.
3173 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003174 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003175 return isAppsViewVisible();
3176 }
3177
3178 public boolean isAppsViewVisible() {
3179 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3180 }
3181
3182 public boolean isWidgetsViewVisible() {
3183 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003184 }
3185
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003186 private void setWorkspaceBackground(int background) {
3187 switch (background) {
3188 case WORKSPACE_BACKGROUND_TRANSPARENT:
3189 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3190 break;
3191 case WORKSPACE_BACKGROUND_BLACK:
3192 getWindow().setBackgroundDrawable(null);
3193 break;
3194 default:
3195 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3196 }
Craig Mautner360310b2012-10-26 15:13:08 -07003197 }
3198
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003199 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003200 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3201 int curflags = getWindow().getAttributes().flags
3202 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3203 if (wpflags != curflags) {
3204 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3205 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003206 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003207 }
3208
Michael Jurkae326f182011-11-21 14:05:46 -08003209 @Override
3210 public void onTrimMemory(int level) {
3211 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003212 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3213 // The widget preview db can result in holding onto over
3214 // 3MB of memory for caching which isn't necessary.
3215 SQLiteDatabase.releaseMemory();
3216
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003217 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003218 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003219 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003220 if (mLauncherCallbacks != null) {
3221 mLauncherCallbacks.onTrimMemory(level);
3222 }
Michael Jurkae326f182011-11-21 14:05:46 -08003223 }
3224
Winson5c6bdbb2015-09-03 11:36:19 -07003225 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003226 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003227 }
3228
Winson5c6bdbb2015-09-03 11:36:19 -07003229 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003230 boolean changed = mState != State.WORKSPACE ||
3231 mWorkspace.getState() != Workspace.State.NORMAL;
3232 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003233 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003234 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003235 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003236
Michael Jurkab3e22d92011-10-31 15:58:33 -07003237 // Set focus to the AppsCustomize button
3238 if (mAllAppsButton != null) {
3239 mAllAppsButton.requestFocus();
3240 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003241 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003242
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003243 // Change the state *after* we've called all the transition code
3244 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003245
Winson Chung5fb63472011-02-02 17:03:37 -08003246 // Resume the auto-advance of widgets
3247 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003248 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003249
Winson Chung0f785722015-04-08 10:27:49 -07003250 if (changed) {
3251 // Send an accessibility event to announce the context change
3252 getWindow().getDecorView()
3253 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003254 }
Winson5c6bdbb2015-09-03 11:36:19 -07003255 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003256 }
3257
Winsone9f27272015-10-13 10:47:51 -07003258 /**
3259 * Shows the overview button.
3260 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07003261 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003262 showOverviewMode(animated, false);
3263 }
3264
3265 /**
3266 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3267 * onto one of the overview panel buttons.
3268 */
3269 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3270 Runnable postAnimRunnable = null;
3271 if (requestButtonFocus) {
3272 postAnimRunnable = new Runnable() {
3273 @Override
3274 public void run() {
3275 // Hitting the menu button when in touch mode does not trigger touch mode to
3276 // be disabled, so if requested, force focus on one of the overview panel
3277 // buttons.
3278 mOverviewPanel.requestFocusFromTouch();
3279 }
3280 };
3281 }
Adam Cohened307df2013-10-02 09:37:31 -07003282 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003283 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003284 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003285 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003286 }
3287
Winson Chungb745afb2015-03-02 11:51:23 -08003288 /**
3289 * Shows the apps view.
3290 */
Hyunyoung Song645764e2016-06-06 14:19:02 -07003291 public void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003292 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003293 if (resetListToTop) {
3294 mAppsView.scrollToTop();
3295 }
Winson Chung4ac30062015-05-08 17:34:17 -07003296 if (updatePredictedApps) {
3297 tryAndUpdatePredictedApps();
3298 }
Winson Chung76648c52015-07-10 14:33:23 -07003299 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003300 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003301
Winson Chungb745afb2015-03-02 11:51:23 -08003302 /**
3303 * Shows the widgets view.
3304 */
3305 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003306 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003307 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003308 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003309 }
Winson Chung76648c52015-07-10 14:33:23 -07003310 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003311
3312 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003313 @Override
3314 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003315 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003316 }
3317 });
Winson Chungb745afb2015-03-02 11:51:23 -08003318 }
3319
3320 /**
3321 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003322 *
3323 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003324 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003325 // TODO: calling method should use the return value so that when {@code false} is returned
3326 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003327 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003328 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3329 mState != State.WIDGETS_SPRING_LOADED) {
3330 return false;
3331 }
3332 if (toState != State.APPS && toState != State.WIDGETS) {
3333 return false;
3334 }
Winson Chungb745afb2015-03-02 11:51:23 -08003335
3336 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003337 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3338 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003339 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003340 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003341 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003342
Michael Jurkab3e22d92011-10-31 15:58:33 -07003343 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003344 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003345
3346 // Pause the auto-advance of widgets until we are out of AllApps
3347 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003348 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003349 closeFolder();
3350
3351 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003352 getWindow().getDecorView()
3353 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003354 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003355 }
3356
Winson Chungcd99cd32015-04-29 11:03:24 -07003357 /**
3358 * Updates the workspace and interaction state on state change, and return the animation to this
3359 * new state.
3360 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003361 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003362 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003363 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003364 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003365 updateInteraction(fromState, toState);
3366 return anim;
3367 }
3368
Jun Mukaif0033da2015-08-04 18:34:30 -07003369 public void onLauncherClingShown() {
3370 // When a launcher cling appears, it should cover the underlying layers, so their focus
3371 // should be blocked.
3372 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3373 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3374 }
3375 }
3376
3377 public void onLauncherClingDismissed() {
3378 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3379 }
3380
Hyunyoung Song3f471442015-04-08 19:01:34 -07003381 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003382 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003383 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003384 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003385 }
Winson Chungb745afb2015-03-02 11:51:23 -08003386
Winson Chung006ee262015-08-03 14:40:11 -07003387 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003388 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003389 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003390
3391 if (isAppsViewVisible()) {
3392 mState = State.APPS_SPRING_LOADED;
3393 } else if (isWidgetsViewVisible()) {
3394 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003395 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003396 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003397 } else {
3398 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003399 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003400 }
Adam Cohen7777d962011-08-18 18:58:38 -07003401
Hyunyoung Song3f471442015-04-08 19:01:34 -07003402 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003403 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003404 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003405
Winson Chunge7a03942011-08-05 15:05:12 -07003406 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003407 @Override
3408 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003409 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003410 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3411 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003412 // Before we show workspace, hide all apps again because
3413 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3414 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003415 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003416 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003417 } else {
3418 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003419 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003420 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003421 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003422 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003423
Tony Wickhame0c33232016-02-08 11:37:04 -08003424 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003425 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3426 || mState == State.WIDGETS_SPRING_LOADED;
3427 }
3428
Michael Jurkad3ef3062010-11-23 16:23:58 -08003429 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003430 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003431 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003432 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003433 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003434 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003435 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3436 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003437 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003438 }
3439
Winson Chung4ac30062015-05-08 17:34:17 -07003440 /**
3441 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3442 * resumed.
3443 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003444 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003445 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003446 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003447 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003448 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003449 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003450 }
3451 }
3452 }
3453
Michael Jurkab3e22d92011-10-31 15:58:33 -07003454 void lockAllApps() {
3455 // TODO
3456 }
3457
3458 void unlockAllApps() {
3459 // TODO
3460 }
3461
Winsonf768d932015-09-25 16:12:35 -07003462 public boolean launcherCallbacksProvidesSearch() {
3463 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3464 }
3465
Michael Jurkad7c28052012-04-27 15:43:36 -07003466 @Override
3467 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003468 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003469 final List<CharSequence> text = event.getText();
3470 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003471 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003472 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003473 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003474 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003475 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003476 } else if (mWorkspace != null) {
3477 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003478 } else {
3479 text.add(getString(R.string.all_apps_home_button_label));
3480 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003481 return result;
3482 }
3483
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003484 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003485 * If the activity is currently paused, signal that we need to run the passed Runnable
3486 * in onResume.
3487 *
3488 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003489 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3490 * wrong when we're not running, and if the activity comes back to what the configuration was
3491 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003492 *
3493 * Implementation of the method from LauncherModel.Callbacks.
3494 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003495 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003496 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003497 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003498 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003499 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003500 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003501 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003502 }
3503 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003504 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003505 return true;
3506 } else {
3507 return false;
3508 }
3509 }
3510
Michael Jurkac402cd92013-05-20 15:49:32 +02003511 private boolean waitUntilResume(Runnable run) {
3512 return waitUntilResume(run, false);
3513 }
3514
Michael Jurka1e2f4652013-07-08 18:03:46 -07003515 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003516 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003517 }
3518
Michael Jurka7607c2f2013-04-03 14:33:19 -07003519 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003520 * If the activity is currently paused, signal that we need to re-run the loader
3521 * in onResume.
3522 *
3523 * This needs to be called from incoming places where resources might have been loaded
3524 * while we are paused. That is becaues the Configuration might be wrong
3525 * when we're not running, and if it comes back to what it was when we
3526 * were paused, we are not restarted.
3527 *
3528 * Implementation of the method from LauncherModel.Callbacks.
3529 *
3530 * @return true if we are currently paused. The caller might be able to
3531 * skip some work in that case since we will come back again.
3532 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003533 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003534 public boolean setLoadOnResume() {
3535 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003536 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003537 mOnResumeNeedsLoad = true;
3538 return true;
3539 } else {
3540 return false;
3541 }
3542 }
3543
3544 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003545 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003546 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003547 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003548 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003549 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003550 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003551 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003552 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003553 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003554 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003555
Joe Onorato9c1289c2009-08-17 11:03:03 -04003556 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003557 * Clear any pending bind callbacks. This is called when is loader is planning to
3558 * perform a full rebind from scratch.
3559 */
3560 @Override
3561 public void clearPendingBinds() {
3562 mBindOnResumeCallbacks.clear();
3563 if (mPendingExecutor != null) {
3564 mPendingExecutor.markCompleted();
3565 mPendingExecutor = null;
3566 }
3567 }
3568
3569 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003570 * Refreshes the shortcuts shown on the workspace.
3571 *
3572 * Implementation of the method from LauncherModel.Callbacks.
3573 */
3574 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003575 if (LauncherAppState.PROFILE_STARTUP) {
3576 Trace.beginSection("Starting page bind");
3577 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003578 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003579
Winson Chungd64d1762013-08-20 14:37:16 -07003580 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003581 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003582 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003583
Michael Jurka05bf644e2011-11-30 20:28:53 -08003584 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003585 if (mHotseat != null) {
3586 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003587 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003588 if (LauncherAppState.PROFILE_STARTUP) {
3589 Trace.endSection();
3590 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003591 }
3592
Adam Cohendcd297f2013-06-18 13:13:40 -07003593 @Override
3594 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003595 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003596 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3597 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003598 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3599 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3600 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003601 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3602 // If there are no screens, we need to have an empty screen
3603 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003604 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003605 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003606
3607 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003608 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003609 if (hasCustomContentToLeft()) {
3610 mWorkspace.createCustomContentContainer();
3611 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003612 }
Winson Chung64359a52013-07-08 17:17:08 -07003613 }
3614
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003615 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003616 int count = orderedScreenIds.size();
3617 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003618 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003619 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003620 // No need to bind the first screen, as its always bound.
3621 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3622 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003623 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003624 }
3625
Winson Chungd64d1762013-08-20 14:37:16 -07003626 public void bindAppsAdded(final ArrayList<Long> newScreens,
3627 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003628 final ArrayList<ItemInfo> addAnimated,
3629 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003630 Runnable r = new Runnable() {
3631 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003632 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003633 }
3634 };
3635 if (waitUntilResume(r)) {
3636 return;
3637 }
3638
Winson Chungd64d1762013-08-20 14:37:16 -07003639 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003640 if (newScreens != null) {
3641 bindAddScreens(newScreens);
3642 }
Winson Chungd64d1762013-08-20 14:37:16 -07003643
3644 // We add the items without animation on non-visible pages, and with
3645 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003646 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003647 bindItems(addNotAnimated, 0,
3648 addNotAnimated.size(), false);
3649 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003650 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003651 bindItems(addAnimated, 0,
3652 addAnimated.size(), true);
3653 }
Winson Chungc58497e2013-09-03 17:48:37 -07003654
Winson Chung87412982013-10-03 18:34:14 -07003655 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003656 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003657
Winson Chungb745afb2015-03-02 11:51:23 -08003658 if (addedApps != null && mAppsView != null) {
3659 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003660 }
Winson Chungd64d1762013-08-20 14:37:16 -07003661 }
3662
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003663 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003664 * Bind the items start-end from the list.
3665 *
3666 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003667 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003668 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003669 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3670 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003671 Runnable r = new Runnable() {
3672 public void run() {
3673 bindItems(shortcuts, start, end, forceAnimateIcons);
3674 }
3675 };
3676 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003677 return;
3678 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003679
Winson Chungf0c6ae02012-03-21 16:10:31 -07003680 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003681 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3682 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003683 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003684 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003685 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003686 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003687 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003688
3689 // Short circuit if we are loading dock items for a configuration which has no dock
3690 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3691 mHotseat == null) {
3692 continue;
3693 }
3694
Sunny Goyal639e9062015-08-19 19:17:06 -07003695 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003696 switch (item.itemType) {
3697 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3698 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003699 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003700 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003701 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003702 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003703 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003704 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003705 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003706 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003707 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003708 default:
3709 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003710 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003711
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003712 /*
3713 * Remove colliding items.
3714 */
3715 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3716 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3717 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3718 View v = cl.getChildAt(item.cellX, item.cellY);
3719 Object tag = v.getTag();
3720 String desc = "Collision while binding workspace item: " + item
3721 + ". Collides with " + tag;
3722 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3723 throw (new RuntimeException(desc));
3724 } else {
3725 Log.d(TAG, desc);
3726 LauncherModel.deleteItemFromDatabase(this, item);
3727 continue;
3728 }
3729 }
3730 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003731 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3732 item.cellY, 1, 1);
3733 if (animateIcons) {
3734 // Animate all the applications up now
3735 view.setAlpha(0f);
3736 view.setScaleX(0f);
3737 view.setScaleY(0f);
3738 bounceAnims.add(createNewAppBounceAnimation(view, i));
3739 newShortcutsScreenId = item.screenId;
3740 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003741 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003742
Winson Chung997a9232013-07-24 15:33:46 -07003743 if (animateIcons) {
3744 // Animate to the correct page
3745 if (newShortcutsScreenId > -1) {
3746 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003747 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003748 final Runnable startBounceAnimRunnable = new Runnable() {
3749 public void run() {
3750 anim.playTogether(bounceAnims);
3751 anim.start();
3752 }
3753 };
Winson Chung997a9232013-07-24 15:33:46 -07003754 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003755 // We post the animation slightly delayed to prevent slowdowns
3756 // when we are loading right after we return to launcher.
3757 mWorkspace.postDelayed(new Runnable() {
3758 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003759 if (mWorkspace != null) {
3760 mWorkspace.snapToPage(newScreenIndex);
3761 mWorkspace.postDelayed(startBounceAnimRunnable,
3762 NEW_APPS_ANIMATION_DELAY);
3763 }
Winson Chung94d67682013-09-25 16:29:40 -07003764 }
3765 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003766 } else {
3767 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003768 }
3769 }
Winson Chung64359a52013-07-08 17:17:08 -07003770 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003771 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003772 }
3773
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003774 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3775 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3776 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003777 view.updateAppWidget(null);
3778 view.setOnClickListener(this);
3779 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003780 mWorkspace.requestLayout();
3781 }
3782
Joe Onorato9c1289c2009-08-17 11:03:03 -04003783 /**
3784 * Add the views for a widget to the workspace.
3785 *
3786 * Implementation of the method from LauncherModel.Callbacks.
3787 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003788 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003789 Runnable r = new Runnable() {
3790 public void run() {
3791 bindAppWidget(item);
3792 }
3793 };
3794 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003795 return;
3796 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003797
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003798 if (mIsSafeModeEnabled) {
3799 bindSafeModeWidget(item);
3800 return;
3801 }
3802
Daniel Sandler843e8602010-06-07 14:59:01 -04003803 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3804 if (DEBUG_WIDGETS) {
3805 Log.d(TAG, "bindAppWidget: " + item);
3806 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003807
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003808 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003809
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003810 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3811 // If the provider is not ready, bind as a pending widget.
3812 appWidgetInfo = null;
3813 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3814 // The widget id is not valid. Try to find the widget based on the provider info.
3815 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3816 } else {
3817 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3818 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003819
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003820 // If the provider is ready, but the width is not yet restored, try to restore it.
3821 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3822 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003823 if (appWidgetInfo == null) {
3824 if (DEBUG_WIDGETS) {
3825 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3826 + " belongs to component " + item.providerName
3827 + ", as the povider is null");
3828 }
3829 LauncherModel.deleteItemFromDatabase(this, item);
3830 return;
3831 }
Sunny Goyalff572272014-07-23 13:58:07 -07003832
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003833 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003834 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003835 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3836 // Id has not been allocated yet. Allocate a new id.
3837 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3838 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003839
Sunny Goyald478c832016-04-01 12:04:16 -07003840 // Also try to bind the widget. If the bind fails, the user will be shown
3841 // a click to setup UI, which will ask for the bind permission.
3842 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3843 pendingInfo.spanX = item.spanX;
3844 pendingInfo.spanY = item.spanY;
3845 pendingInfo.minSpanX = item.minSpanX;
3846 pendingInfo.minSpanY = item.minSpanY;
3847 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
3848 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3849 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003850
Sunny Goyald478c832016-04-01 12:04:16 -07003851 // Bind succeeded
3852 if (success) {
3853 // If the widget has a configure activity, it is still needs to set it up,
3854 // otherwise the widget is ready to go.
3855 item.restoreStatus = (appWidgetInfo.configure == null)
3856 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3857 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003858 }
Sunny Goyald478c832016-04-01 12:04:16 -07003859
3860 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003861 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003862 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003863 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003864 // The widget was marked as UI not ready, but there is no configure activity to
3865 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003866 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3867 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003868 }
Sunny Goyalff572272014-07-23 13:58:07 -07003869 }
3870
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003871 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003872 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003873 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3874 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003875 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003876
Sunny Goyal56c73602015-09-25 12:55:01 -07003877 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003878 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003879 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003880 deleteWidgetInfo(item);
3881 return;
3882 }
3883
Sunny Goyal233ee962015-08-03 13:05:01 -07003884 item.minSpanX = appWidgetInfo.minSpanX;
3885 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003886 addAppWidgetToWorkspace(
3887 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3888 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003889 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003890 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003891 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003892 view.updateAppWidget(null);
3893 view.setOnClickListener(this);
3894 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003895 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003896 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003897
Daniel Sandler843e8602010-06-07 14:59:01 -04003898 if (DEBUG_WIDGETS) {
3899 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3900 + (SystemClock.uptimeMillis()-start) + "ms");
3901 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003902 }
3903
Sunny Goyalff572272014-07-23 13:58:07 -07003904 /**
3905 * Restores a pending widget.
3906 *
3907 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003908 */
Sunny Goyald478c832016-04-01 12:04:16 -07003909 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003910 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3911 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3912 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003913 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003914 }
3915
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003916 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003917 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003918
3919 mWorkspace.reinflateWidgetsIfNecessary();
3920 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003921 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003922 }
3923
Adam Cohen1462de32012-07-24 22:34:36 -07003924 public void onPageBoundSynchronously(int page) {
3925 mSynchronouslyBoundPages.add(page);
3926 }
3927
Sunny Goyal527c7d32015-08-28 15:19:36 -07003928 @Override
3929 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3930 if (mPendingExecutor != null) {
3931 mPendingExecutor.markCompleted();
3932 }
3933 mPendingExecutor = executor;
3934 executor.attachTo(this);
3935 }
3936
3937 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3938 if (mPendingExecutor == executor) {
3939 mPendingExecutor = null;
3940 }
3941 }
3942
Joe Onorato9c1289c2009-08-17 11:03:03 -04003943 /**
3944 * Callback saying that there aren't any more items to bind.
3945 *
3946 * Implementation of the method from LauncherModel.Callbacks.
3947 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003948 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003949 Runnable r = new Runnable() {
3950 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003951 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003952 }
3953 };
3954 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003955 return;
3956 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003957 if (LauncherAppState.PROFILE_STARTUP) {
3958 Trace.beginSection("Page bind completed");
3959 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003960 if (mSavedState != null) {
3961 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003962 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003963 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003964
Joe Onorato9c1289c2009-08-17 11:03:03 -04003965 mSavedState = null;
3966 }
3967
Adam Cohen1462de32012-07-24 22:34:36 -07003968 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003969
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003970 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003971
3972 // If we received the result of any pending adds while the loader was running (e.g. the
3973 // widget configuration forced an orientation change), process them now.
3974 if (sPendingAddItem != null) {
3975 final long screenId = completeAdd(sPendingAddItem);
3976
3977 // TODO: this moves the user to the page where the pending item was added. Ideally,
3978 // the screen would be guaranteed to exist after bind, and the page would be set through
3979 // the workspace restore process.
3980 mWorkspace.post(new Runnable() {
3981 @Override
3982 public void run() {
3983 mWorkspace.snapToScreenId(screenId);
3984 }
3985 });
3986 sPendingAddItem = null;
3987 }
3988
Sunny Goyal756adbc2015-04-16 15:20:51 -07003989 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003990
3991 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003992 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003993 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003994 if (LauncherAppState.PROFILE_STARTUP) {
3995 Trace.endSection();
3996 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003997 }
3998
Winson Chunga2413752012-04-03 14:22:34 -07003999 private boolean canRunNewAppsAnimation() {
4000 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004001 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4002 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004003 }
4004
Winson Chungc9168342013-06-26 14:54:55 -07004005 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004006 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004007 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4008 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004009 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004010 return bounceAnim;
4011 }
4012
Adam Cohen27772732013-11-11 14:00:56 +00004013 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004014 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004015 }
4016
Tony Wickham55616cd2015-09-23 14:55:17 -07004017 public int getSearchBarHeight() {
4018 if (mLauncherCallbacks != null) {
4019 return mLauncherCallbacks.getSearchBarHeight();
4020 }
4021 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4022 }
4023
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004024 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004025 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4026 * multiple calls to bind the same list.)
4027 */
4028 @Thunk ArrayList<AppInfo> mTmpAppsList;
4029 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4030 public void run() {
4031 bindAllApplications(mTmpAppsList);
4032 mTmpAppsList = null;
4033 }
4034 };
4035
4036 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004037 * Add the icons for all apps.
4038 *
4039 * Implementation of the method from LauncherModel.Callbacks.
4040 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004041 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004042 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4043 mTmpAppsList = apps;
4044 return;
4045 }
4046
Winson Chungb745afb2015-03-02 11:51:23 -08004047 if (mAppsView != null) {
4048 mAppsView.setApps(apps);
4049 }
Adam Cohen9211d422014-10-07 18:14:53 -07004050 if (mLauncherCallbacks != null) {
4051 mLauncherCallbacks.bindAllApplications(apps);
4052 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004053 }
4054
4055 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004056 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
4057 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
4058 */
4059 @Override
4060 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
4061 mDeepShortcutMap = deepShortcutMapCopy;
4062 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
4063 }
4064
4065 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004066 * A package was updated.
4067 *
4068 * Implementation of the method from LauncherModel.Callbacks.
4069 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004070 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004071 Runnable r = new Runnable() {
4072 public void run() {
4073 bindAppsUpdated(apps);
4074 }
4075 };
4076 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004077 return;
4078 }
4079
Winson Chungb745afb2015-03-02 11:51:23 -08004080 if (mAppsView != null) {
4081 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004082 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004083 }
4084
Sunny Goyal4390ace2014-10-13 11:33:11 -07004085 @Override
4086 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4087 Runnable r = new Runnable() {
4088 public void run() {
4089 bindWidgetsRestored(widgets);
4090 }
4091 };
4092 if (waitUntilResume(r)) {
4093 return;
4094 }
4095 mWorkspace.widgetsRestored(widgets);
4096 }
4097
Joe Onorato9c1289c2009-08-17 11:03:03 -04004098 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004099 * Some shortcuts were updated in the background.
4100 *
4101 * Implementation of the method from LauncherModel.Callbacks.
4102 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004103 @Override
4104 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4105 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004106 Runnable r = new Runnable() {
4107 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004108 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004109 }
4110 };
4111 if (waitUntilResume(r)) {
4112 return;
4113 }
4114
Sunny Goyal4390ace2014-10-13 11:33:11 -07004115 if (!updated.isEmpty()) {
4116 mWorkspace.updateShortcuts(updated);
4117 }
4118
4119 if (!removed.isEmpty()) {
4120 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4121 for (ShortcutInfo si : removed) {
4122 removedComponents.add(si.getTargetComponent());
4123 }
4124 mWorkspace.removeItemsByComponentName(removedComponents, user);
4125 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004126 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004127 }
4128 }
4129
4130 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004131 * Update the state of a package, typically related to install state.
4132 *
4133 * Implementation of the method from LauncherModel.Callbacks.
4134 */
Sunny Goyale755d462014-07-22 13:48:29 -07004135 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004136 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4137 Runnable r = new Runnable() {
4138 public void run() {
4139 bindRestoreItemsChange(updates);
4140 }
4141 };
4142 if (waitUntilResume(r)) {
4143 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004144 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004145
Sunny Goyal756adbc2015-04-16 15:20:51 -07004146 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004147 }
4148
4149 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004150 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004151 * in addition to specific applications to remove, the reason being that
4152 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004153 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004154 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004155 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004156 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004157 public void bindWorkspaceComponentsRemoved(
4158 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4159 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004160 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004161 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004162 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004163 }
Winson Chungd64d1762013-08-20 14:37:16 -07004164 };
4165 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004166 return;
4167 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004168 if (!packageNames.isEmpty()) {
4169 mWorkspace.removeItemsByPackageName(packageNames, user);
4170 }
4171 if (!components.isEmpty()) {
4172 mWorkspace.removeItemsByComponentName(components, user);
4173 }
4174 // Notify the drag controller
4175 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004176
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004177 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004178
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004179 @Override
4180 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4181 Runnable r = new Runnable() {
4182 public void run() {
4183 bindAppInfosRemoved(appInfos);
4184 }
4185 };
4186 if (waitUntilResume(r)) {
4187 return;
Joe Onorato36115782010-06-17 13:28:48 -04004188 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004189
Winson Chungdf95eb12013-10-16 14:57:07 -07004190 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004191 if (mAppsView != null) {
4192 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004193 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004194 }
Joe Onoratobe386092009-11-17 17:32:16 -08004195
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004196 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004197 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004198 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004199 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004200 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004201
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004202 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004203 public void bindWidgetsModel(WidgetsModel model) {
4204 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004205 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004206 return;
4207 }
4208
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004209 if (mWidgetsView != null && model != null) {
4210 mWidgetsView.addWidgets(model);
4211 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004212 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004213 }
4214
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004215 @Override
4216 public void notifyWidgetProvidersChanged() {
4217 if (mWorkspace.getState().shouldUpdateWidget) {
4218 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4219 }
4220 }
4221
Winson Chung400438b2011-01-16 17:53:48 -08004222 private int mapConfigurationOriActivityInfoOri(int configOri) {
4223 final Display d = getWindowManager().getDefaultDisplay();
4224 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4225 switch (d.getRotation()) {
4226 case Surface.ROTATION_0:
4227 case Surface.ROTATION_180:
4228 // We are currently in the same basic orientation as the natural orientation
4229 naturalOri = configOri;
4230 break;
4231 case Surface.ROTATION_90:
4232 case Surface.ROTATION_270:
4233 // We are currently in the other basic orientation to the natural orientation
4234 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4235 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4236 break;
4237 }
4238
4239 int[] oriMap = {
4240 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4241 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4242 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4243 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4244 };
4245 // Since the map starts at portrait, we need to offset if this device's natural orientation
4246 // is landscape.
4247 int indexOffset = 0;
4248 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4249 indexOffset = 1;
4250 }
4251 return oriMap[(d.getRotation() + indexOffset) % 4];
4252 }
Adam Cohen446e9402011-09-15 18:21:21 -07004253
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004254 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004255 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004256 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004257 if (Utilities.ATLEAST_JB_MR2) {
4258 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4259 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004260 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4261 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004262 }
Winson Chung4b919f82012-05-01 10:44:08 -07004263 }
4264 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004265
Winson Chung4b919f82012-05-01 10:44:08 -07004266 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004267 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004268 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004269 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004270 } else {
4271 mHandler.postDelayed(new Runnable() {
4272 public void run() {
4273 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4274 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004275 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004276 }
Winson Chung4b919f82012-05-01 10:44:08 -07004277 }
Winson Chung400438b2011-01-16 17:53:48 -08004278 }
4279
Adam Cohenea90f832014-05-21 15:03:34 -07004280 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004281 * To be overridden by subclasses to indicate that there is an activity to launch
4282 * before showing the standard launcher experience.
4283 */
4284 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004285 if (mLauncherCallbacks != null) {
4286 return mLauncherCallbacks.hasFirstRunActivity();
4287 }
Adam Cohen432609a2014-03-13 17:03:22 -07004288 return false;
4289 }
4290
4291 /**
4292 * To be overridden by subclasses to launch any first run activity
4293 */
4294 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004295 if (mLauncherCallbacks != null) {
4296 return mLauncherCallbacks.getFirstRunActivity();
4297 }
Adam Cohen432609a2014-03-13 17:03:22 -07004298 return null;
4299 }
4300
Winson Chunga6945242014-01-08 14:04:34 -08004301 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004302 return !ActivityManager.isRunningInTestHarness() &&
4303 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004304 }
4305
Adam Cohen4a9423d2014-03-28 14:22:31 -07004306 protected boolean hasRunFirstRunActivity() {
4307 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4308 }
4309
Adam Cohen432609a2014-03-13 17:03:22 -07004310 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004311 if (shouldRunFirstRunActivity() &&
4312 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004313 Intent firstRunIntent = getFirstRunActivity();
4314 if (firstRunIntent != null) {
4315 startActivity(firstRunIntent);
4316 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004317 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004318 }
4319 }
Adam Cohen432609a2014-03-13 17:03:22 -07004320 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004321 }
4322
4323 private void markFirstRunActivityShown() {
4324 SharedPreferences.Editor editor = mSharedPrefs.edit();
4325 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4326 editor.apply();
4327 }
4328
Adam Cohen432609a2014-03-13 17:03:22 -07004329 /**
4330 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4331 * screen that must be displayed and dismissed.
4332 */
4333 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004334 if (mLauncherCallbacks != null) {
4335 return mLauncherCallbacks.hasDismissableIntroScreen();
4336 }
Adam Cohen432609a2014-03-13 17:03:22 -07004337 return false;
4338 }
4339
4340 /**
4341 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4342 */
4343 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004344 if (mLauncherCallbacks != null) {
4345 return mLauncherCallbacks.getIntroScreen();
4346 }
Adam Cohen432609a2014-03-13 17:03:22 -07004347 return null;
4348 }
4349
4350 /**
4351 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4352 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4353 */
4354 private boolean shouldShowIntroScreen() {
4355 return hasDismissableIntroScreen() &&
4356 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4357 }
4358
4359 protected void showIntroScreen() {
4360 View introScreen = getIntroScreen();
4361 changeWallpaperVisiblity(false);
4362 if (introScreen != null) {
4363 mDragLayer.showOverlayView(introScreen);
4364 }
4365 }
4366
4367 public void dismissIntroScreen() {
4368 markIntroScreenDismissed();
4369 if (showFirstRunActivity()) {
4370 // We delay hiding the intro view until the first run activity is showing. This
4371 // avoids a blip.
4372 mWorkspace.postDelayed(new Runnable() {
4373 @Override
4374 public void run() {
4375 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004376 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004377 }
4378 }, ACTIVITY_START_DELAY);
4379 } else {
4380 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004381 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004382 }
4383 changeWallpaperVisiblity(true);
4384 }
4385
4386 private void markIntroScreenDismissed() {
4387 SharedPreferences.Editor editor = mSharedPrefs.edit();
4388 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4389 editor.apply();
4390 }
4391
Adam Cohen091440a2015-03-18 14:16:05 -07004392 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004393 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4394 // on the device, then we always show the first run cling experience (or if there is no
4395 // launcher2). Otherwise, we prompt the user upon started for migration
4396 LauncherClings launcherClings = new LauncherClings(this);
4397 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004398 mClings = launcherClings;
Sunny Goyalded0fdb2016-05-23 15:55:41 -07004399 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004400 }
4401 }
4402
Winson Chung5ffd51d2015-06-25 11:36:50 -07004403 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004404 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004405 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004406 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004407 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004408 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004409 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4410 if (userHandle != null) {
4411 user = UserHandleCompat.fromUser(userHandle);
4412 }
4413 }
4414 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004415 }
4416
4417 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004418 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004419 if (user == null) {
4420 user = UserHandleCompat.myUserHandle();
4421 }
4422
4423 // Called from search suggestion, add the profile extra to the intent to ensure that we
4424 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004425 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004426 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004427 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004428 return null;
4429 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004430 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004431 }
4432
Winson Chung5ffd51d2015-06-25 11:36:50 -07004433 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004434 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4435 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004436 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004437 UserHandleCompat.myUserHandle());
4438 }
4439
Winson Chung5ffd51d2015-06-25 11:36:50 -07004440 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004441 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4442 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004443 mWorkspace.onExternalDragStartedWithItem(dragView);
4444 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004445 }
4446
Winson Chung5ffd51d2015-06-25 11:36:50 -07004447 protected void moveWorkspaceToDefaultScreen() {
4448 mWorkspace.moveToDefaultScreen(false);
4449 }
4450
Winson Chung80baf5a2010-08-09 16:03:15 -07004451 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004452 * Returns a FastBitmapDrawable with the icon, accurately sized.
4453 */
4454 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4455 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4456 d.setFilterBitmap(true);
4457 resizeIconDrawable(d);
4458 return d;
4459 }
4460
4461 /**
4462 * Resizes an icon drawable to the correct icon size.
4463 */
Winson5fbe0742015-09-30 15:33:00 -07004464 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004465 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004466 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004467 }
4468
4469 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004470 * Prints out out state for debugging.
4471 */
4472 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004473 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004474 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004475 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4476 Log.d(TAG, "mRestoring=" + mRestoring);
4477 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004478 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004479 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004480
Daniel Sandler325dc232013-06-05 22:57:57 -04004481 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004482 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004483
4484 @Override
4485 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4486 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004487 // Dump workspace
4488 writer.println(prefix + "Workspace Items");
4489 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4490 writer.println(prefix + " Homescreen " + i);
4491
4492 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4493 for (int j = 0; j < layout.getChildCount(); j++) {
4494 Object tag = layout.getChildAt(j).getTag();
4495 if (tag != null) {
4496 writer.println(prefix + " " + tag.toString());
4497 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004498 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004499 }
Sunny Goyala1365452015-10-01 15:46:24 -07004500
4501 writer.println(prefix + " Hotseat");
4502 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4503 for (int j = 0; j < layout.getChildCount(); j++) {
4504 Object tag = layout.getChildAt(j).getTag();
4505 if (tag != null) {
4506 writer.println(prefix + " " + tag.toString());
4507 }
4508 }
4509
Sunny Goyal713edfc2016-05-06 09:58:34 -07004510 try {
4511 FileLog.flushAll(writer);
4512 } catch (Exception e) {
4513 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004514 }
4515
Adam Cohen9211d422014-10-07 18:14:53 -07004516 if (mLauncherCallbacks != null) {
4517 mLauncherCallbacks.dump(prefix, fd, writer, args);
4518 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004519 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004520
Adam Cohen59400422014-03-05 18:07:04 -08004521 public static CustomAppWidget getCustomAppWidget(String name) {
4522 return sCustomAppWidgets.get(name);
4523 }
4524
4525 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4526 return sCustomAppWidgets;
4527 }
4528
Sunny Goyal29538332016-02-18 09:10:19 -08004529 public static List<View> getFolderContents(View icon) {
4530 if (icon instanceof FolderIcon) {
4531 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4532 } else {
4533 return Collections.EMPTY_LIST;
4534 }
4535 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004536
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004537 public static Launcher getLauncher(Context context) {
4538 if (context instanceof Launcher) {
4539 return (Launcher) context;
4540 }
4541 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4542 }
4543
Sunny Goyal745bad92016-05-02 10:54:12 -07004544 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4545
4546 @Override
4547 public void onSharedPreferenceChanged(
4548 SharedPreferences sharedPreferences, String key) {
4549 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4550 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4551 if (!waitUntilResume(this, true)) {
4552 run();
4553 }
4554 }
4555 }
4556
4557 @Override
4558 public void run() {
4559 setOrientation();
4560 }
4561 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004562}