blob: ae1b6b8fa2933bf3c72f82097a330694cd0ce36f [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;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700122import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal322d5562015-06-25 19:35:49 -0700123import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700124import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700125import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700126import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700127import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700128import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700129
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700130import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700131import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700132import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700133import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800134import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700135import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700136import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700137import java.util.List;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800138import java.util.Locale;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700139
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140/**
141 * Default launcher application.
142 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100143public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700144 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
145 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700146 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700147 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700148 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700150 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700151 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400152 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700153
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700155 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700156 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700157 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158
Michael Jurka8b805b12012-04-18 14:23:14 -0700159 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700160 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700161 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700162
Sunny Goyal28c6b962015-10-12 11:42:05 -0700163 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
164
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700165 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
166 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
167 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
168
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700169 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
170
Mathew Inwood876a8462013-06-14 14:12:41 +0100171 /**
172 * IntentStarter uses request codes starting with this. This must be greater than all activity
173 * request codes used internally.
174 */
175 protected static final int REQUEST_LAST = 100;
176
Michael Jurka0a457bf2012-11-19 14:05:05 -0800177 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700178 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800179 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180
Winson Chung2672ff92012-05-04 16:22:30 -0700181 // The Intent extra that defines whether to ignore the launch animation
182 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400183 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700184
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700185 public static final String ACTION_APPWIDGET_HOST_RESET =
186 "com.android.launcher3.intent.ACTION_APPWIDGET_HOST_RESET";
187
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 // Type: int
189 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700190 // Type: int
191 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700192 // Type: Content Values / parcelable
193 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700194 // Type: parcelable
195 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000196 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800197 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198
Adam Cohen432609a2014-03-13 17:03:22 -0700199 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800200 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
201
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700202 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700203 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
204 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700205
Adam Cohen091440a2015-03-18 14:16:05 -0700206 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700207 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700208
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700209 private boolean mIsSafeModeEnabled;
210
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800212 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700213 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700214 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215
Winson Chunga2413752012-04-03 14:22:34 -0700216 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700217 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
218 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700219 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700220
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700221 private final BroadcastReceiver mUiBroadcastReceiver = new BroadcastReceiver() {
222
223 @Override
224 public void onReceive(Context context, Intent intent) {
225 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
226 closeSystemDialogs();
227 } else if (ACTION_APPWIDGET_HOST_RESET.equals(intent.getAction())) {
228 if (mAppWidgetHost != null) {
229 mAppWidgetHost.startListening();
230 }
231 }
232 }
233 };
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800234
Adam Cohen091440a2015-03-18 14:16:05 -0700235 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700236 private View mLauncherView;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700237 private PageIndicator mPageIndicator;
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
Sunny Goyal639e9062015-08-19 19:17:06 -0700296 private LauncherClings mClings;
297
Adam Cohen61f560d2013-09-30 15:58:20 -0700298 private View.OnTouchListener mHapticFeedbackTouchListener;
299
Adam Cohended9f8d2010-11-03 13:25:16 -0700300 // Related to the auto-advancing of widgets
301 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700302 private static final int ADVANCE_INTERVAL = 20000;
303 private static final int ADVANCE_STAGGER = 250;
304
305 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700306 private long mAutoAdvanceSentTime;
307 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700308 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700309
Winson Chung400438b2011-01-16 17:53:48 -0800310 // Determines how long to wait after a rotation before restoring the screen orientation to
311 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700312 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800313
Adam Cohen091440a2015-03-18 14:16:05 -0700314 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700315
Adam Cohen1462de32012-07-24 22:34:36 -0700316 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700317 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700318
Winson Chung46353de2012-02-16 14:05:10 -0800319 // We only want to get the SharedPreferences once since it does an FS stat each time we get
320 // it from the context.
321 private SharedPreferences mSharedPrefs;
322
Winson Chungf0c6ae02012-03-21 16:10:31 -0700323 // Holds the page that we need to animate to, and the icon views that we need to animate up
324 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700325 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700326 private Bitmap mFolderIconBitmap;
327 private Canvas mFolderIconCanvas;
328 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700329
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700330 private DeviceProfile mDeviceProfile;
331
Adam Cohen4b66bf32015-09-18 12:15:19 -0700332 private boolean mMoveToDefaultScreenFromNewIntent;
333
Winson Chung13eb5272015-05-11 16:30:13 -0700334 // This is set to the view that launched the activity that navigated the user away from
335 // launcher. Since there is no callback for when the activity has finished launching, enable
336 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800337 private BubbleTextView mWaitingForResume;
338
Adam Cohen59400422014-03-05 18:07:04 -0800339 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
340 new HashMap<String, CustomAppWidget>();
341
Adam Cohen59400422014-03-05 18:07:04 -0800342 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700343 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
344 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800345 }
346 }
347
Adam Cohen091440a2015-03-18 14:16:05 -0700348 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700349 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700350 if (mWorkspace != null) {
351 mWorkspace.buildPageHardwareLayers();
352 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700353 }
354 };
355
Adam Cohendb364c32014-05-20 14:23:40 -0700356 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800357
Adam Cohen091440a2015-03-18 14:16:05 -0700358 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800359 int requestCode;
360 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700361 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700362 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800363 int cellX;
364 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700365 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800366 }
367
Hyunyoung Songaa953652016-04-19 18:30:24 -0700368 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800369
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700370 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700371 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400372
373 @Thunk void setOrientation() {
374 if (mRotationEnabled) {
375 unlockScreenOrientation(true);
376 } else {
377 setRequestedOrientation(
378 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700379 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400380 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700381
Sunny Goyal745bad92016-05-02 10:54:12 -0700382 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700383
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 @Override
385 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700386 if (DEBUG_STRICT_MODE) {
387 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
388 .detectDiskReads()
389 .detectDiskWrites()
390 .detectNetwork() // or .detectAll() for all detectable problems
391 .penaltyLog()
392 .build());
393 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
394 .detectLeakedSqlLiteObjects()
395 .detectLeakedClosableObjects()
396 .penaltyLog()
397 .penaltyDeath()
398 .build());
399 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700400 if (LauncherAppState.PROFILE_STARTUP) {
401 Trace.beginSection("Launcher-onCreate");
402 }
Winson Chunga2413752012-04-03 14:22:34 -0700403
Adam Cohen9211d422014-10-07 18:14:53 -0700404 if (mLauncherCallbacks != null) {
405 mLauncherCallbacks.preOnCreate();
406 }
407
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400409
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400410 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700411
Adam Cohen2e6da152015-05-06 11:42:25 -0700412 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700413 mDeviceProfile = getResources().getConfiguration().orientation
414 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700415 app.getInvariantDeviceProfile().landscapeProfile
416 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700417
Sunny Goyalf7258242015-10-19 16:59:07 -0700418 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700419 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800420 mModel = app.setLauncher(this);
421 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700422 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700423
Joe Onorato41a12d22009-10-31 18:30:00 -0400424 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700425 mAllAppsController = new AllAppsTransitionController(this);
426 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700427
Sunny Goyalffe83f12014-08-14 17:39:34 -0700428 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700429
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700430 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
431 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700432
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700433 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
434 // this also ensures that any synchronous binding below doesn't re-trigger another
435 // LauncherModel load.
436 mPaused = false;
437
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700439
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700441 mDeviceProfile.layout(this);
Tony Wickham827cef22016-03-17 15:39:39 -0700442 mExtractedColors = new ExtractedColors();
443 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444
Tony Wickhame2217252016-03-22 16:34:23 -0700445 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
446 .addAccessibilityStateChangeListener(this);
447
Joe Onorato7c312c12009-08-13 21:36:53 -0700448 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700449
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 mSavedState = savedInstanceState;
451 restoreState(mSavedState);
452
Sunny Goyale26d1002016-06-20 14:52:14 -0700453 if (LauncherAppState.PROFILE_STARTUP) {
454 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 }
456
457 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700458 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700459 // If the user leaves launcher, then we should just load items asynchronously when
460 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700461 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700462 } else {
463 // We only load the page synchronously if the user rotates (or triggers a
464 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700465 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700466 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467 }
468
469 // For handling default keys
470 mDefaultKeySsb = new SpannableStringBuilder();
471 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800472
473 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700474 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
475 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800476
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800477 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700478 // In case we are on a device with locked rotation, we should look at preferences to check
479 // if the user has specifically allowed rotation.
480 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700481 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700482 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
483 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700484 }
485
486 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
487 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400488 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700489
Adam Cohen9211d422014-10-07 18:14:53 -0700490 if (mLauncherCallbacks != null) {
491 mLauncherCallbacks.onCreate(savedInstanceState);
492 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700493
494 if (shouldShowIntroScreen()) {
495 showIntroScreen();
496 } else {
497 showFirstRunActivity();
498 showFirstRunClings();
499 }
Adam Cohen9211d422014-10-07 18:14:53 -0700500 }
501
Sunny Goyal7779d622015-06-11 16:18:39 -0700502 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700503 public void onExtractedColorsChanged() {
504 loadExtractedColorsAndColorItems();
505 }
506
507 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700508 // TODO: do this in pre-N as well, once the extraction part is complete.
509 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700510 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700511 mHotseat.updateColor(mExtractedColors, !mPaused);
Tony Wickham770217c2016-05-18 15:16:40 -0700512 if (mPageIndicator != null) {
513 mPageIndicator.updateColor(mExtractedColors);
514 }
Tony Wickham827cef22016-03-17 15:39:39 -0700515 }
516 }
517
Adam Cohen9211d422014-10-07 18:14:53 -0700518 private LauncherCallbacks mLauncherCallbacks;
519
520 public void onPostCreate(Bundle savedInstanceState) {
521 super.onPostCreate(savedInstanceState);
522 if (mLauncherCallbacks != null) {
523 mLauncherCallbacks.onPostCreate(savedInstanceState);
524 }
525 }
526
Sunny Goyal32554d12015-12-03 15:31:25 -0800527 /**
528 * Call this after onCreate to set or clear overlay.
529 */
530 public void setLauncherOverlay(LauncherOverlay overlay) {
531 if (overlay != null) {
532 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
533 }
534 mWorkspace.setLauncherOverlay(overlay);
535 }
536
Adam Cohen9211d422014-10-07 18:14:53 -0700537 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
538 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700539 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700540 private boolean mWorkspaceImportanceStored = false;
541 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700542 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800543 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700544 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
545
546 @Override
547 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700548 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700549 return;
550 }
551 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700552 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700553 if (mWorkspace != null) {
554 mWorkspaceImportanceForAccessibility =
555 mWorkspace.getImportantForAccessibility();
556 mWorkspace.setImportantForAccessibility(
557 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
558 mWorkspaceImportanceStored = true;
559 }
560 if (mHotseat != null) {
561 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
562 mHotseat.setImportantForAccessibility(
563 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
564 mHotseatImportanceStored = true;
565 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700566 }
567
568 @Override
569 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700570 if (mWorkspaceImportanceStored && mWorkspace != null) {
571 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
572 }
573 if (mHotseatImportanceStored && mHotseat != null) {
574 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
575 }
576 mWorkspaceImportanceStored = false;
577 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700578 }
579 });
Adam Cohen9211d422014-10-07 18:14:53 -0700580 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700581 }
582
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700583 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700584 public void onLauncherProviderChange() {
585 if (mLauncherCallbacks != null) {
586 mLauncherCallbacks.onLauncherProviderChange();
587 }
588 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700589
Adam Cohen9211d422014-10-07 18:14:53 -0700590 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700591 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700592 if (mLauncherCallbacks != null) {
593 return mLauncherCallbacks.hasCustomContentToLeft();
594 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700595 return false;
596 }
597
Dave Hawkeya8881582013-09-17 15:55:33 -0600598 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500599 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600600 * {@link #addToCustomContentPage}. This will only be invoked if
601 * {@link #hasCustomContentToLeft()} is {@code true}.
602 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500603 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700604 if (mLauncherCallbacks != null) {
605 mLauncherCallbacks.populateCustomContentContainer();
606 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600607 }
608
609 /**
610 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
611 * ensure the custom content page is added or removed if necessary.
612 */
613 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600614 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600615 // Not bound yet, wait for bindScreens to be called.
616 return;
617 }
618
619 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
620 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500621 mWorkspace.createCustomContentContainer();
622 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600623 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
624 mWorkspace.removeCustomContentPage();
625 }
626 }
627
Hyunyoung Songaa953652016-04-19 18:30:24 -0700628 public UserEventDispatcher getUserEventDispatcher() {
629 if (mLauncherCallbacks != null) {
630 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
631 if (dispatcher != null) {
632 return dispatcher;
633 }
634 }
635
Sunny Goyal64976d52016-06-20 14:56:28 -0700636 // Logger object is a singleton and does not have to be coupled with the foreground
637 // activity. Since most user event logging is done on the UI, the object is retrieved
638 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700639 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700640 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700641 }
642 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800643 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100644
Hyunyoung Song3f471442015-04-08 19:01:34 -0700645 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700646 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
647 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700648 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700649 }
650
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000651 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700652 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
653 // This cast is safe as long as the id < 0x00FFFFFF
654 // Since we jail all the dynamically generated views, there should be no clashes
655 // with any other views.
656 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000657 }
658
659 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700660 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
661 * a configuration step, this allows the proper animations to run after other transitions.
662 */
Adam Cohendb364c32014-05-20 14:23:40 -0700663 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700664 long screenId = args.screenId;
665 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
666 // When the screen id represents an actual screen (as opposed to a rank) we make sure
667 // that the drop page actually exists.
668 screenId = ensurePendingDropLayoutExists(args.screenId);
669 }
Adam Cohendb364c32014-05-20 14:23:40 -0700670
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800671 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800672 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700673 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700674 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700675 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800676 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700677 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700678 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700679 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyald478c832016-04-01 12:04:16 -0700680 completeRestoreAppWidget(args.appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700681 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700682 case REQUEST_BIND_PENDING_APPWIDGET: {
683 int widgetId = args.appWidgetId;
684 LauncherAppWidgetInfo info =
685 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
686 if (info != null) {
687 // Since the view was just bound, also launch the configure activity if needed
688 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
689 .getLauncherAppWidgetInfo(widgetId);
690 if (provider != null && provider.configure != null) {
691 startRestoredWidgetReconfigActivity(provider, info);
692 }
693 }
694 break;
695 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800696 }
Michael Jurka27614d22012-04-02 06:40:22 -0700697 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
698 // if you turned the screen off and then back while in All Apps, Launcher would not
699 // return to the workspace. Clearing mAddInfo.container here fixes this issue
700 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700701 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800702 }
703
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800704 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700705 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700706 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700707 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700708 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800709 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700710
Adam Cohenad4e15c2013-10-17 16:21:35 -0700711 Runnable exitSpringLoaded = new Runnable() {
712 @Override
713 public void run() {
714 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
715 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
716 }
717 };
718
Michael Jurka8b805b12012-04-18 14:23:14 -0700719 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700720 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700721 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700722 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
723 if (resultCode == RESULT_CANCELED) {
724 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700725 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700726 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700727 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800728 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700729 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700730 }
731 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200732 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
733 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700734 // User could have free-scrolled between pages before picking a wallpaper; make sure
735 // we move to the closest one now.
736 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700737 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200738 }
739 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700740 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200741
Adam Cohened66b2b2012-01-23 17:28:51 -0800742 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700743 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700744
Adam Cohendb364c32014-05-20 14:23:40 -0700745 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800746 // We have special handling for widgets
747 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800748 final int appWidgetId;
749 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
750 : -1;
751 if (widgetId < 0) {
752 appWidgetId = pendingAddWidgetId;
753 } else {
754 appWidgetId = widgetId;
755 }
756
Adam Cohenad4e15c2013-10-17 16:21:35 -0700757 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800758 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700759 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
760 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700761 result = RESULT_CANCELED;
762 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700763 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700764 @Override
765 public void run() {
766 exitSpringLoadedDragModeDelayed(false, 0, null);
767 }
768 };
Adam Cohendb364c32014-05-20 14:23:40 -0700769 if (workspaceLocked) {
770 // No need to remove the empty screen if we're mid-binding, as the
771 // the bind will not add the empty screen.
772 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
773 } else {
774 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
775 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
776 }
Winson Chung5aaab772012-04-27 15:24:02 -0700777 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700778 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700779 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
780 // When the screen id represents an actual screen (as opposed to a rank)
781 // we make sure that the drop page actually exists.
782 mPendingAddInfo.screenId =
783 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
784 }
Adam Cohendb364c32014-05-20 14:23:40 -0700785 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
786
787 dropLayout.setDropPending(true);
788 final Runnable onComplete = new Runnable() {
789 @Override
790 public void run() {
791 completeTwoStageWidgetDrop(resultCode, appWidgetId);
792 dropLayout.setDropPending(false);
793 }
794 };
795 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
796 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
797 } else {
798 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
799 mPendingAddInfo);
800 sPendingAddItem = args;
801 }
Winson Chung5aaab772012-04-27 15:24:02 -0700802 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800803 return;
804 }
805
Sunny Goyald478c832016-04-01 12:04:16 -0700806 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
807 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700808 if (resultCode == RESULT_OK) {
809 // Update the widget view.
810 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
811 pendingAddWidgetId, mPendingAddInfo);
812 if (workspaceLocked) {
813 sPendingAddItem = args;
814 } else {
815 completeAdd(args);
816 }
817 }
818 // Leave the widget in the pending state if the user canceled the configure.
819 return;
820 }
821
Sunny Goyalaad90582015-07-09 14:22:50 -0700822 if (requestCode == REQUEST_CREATE_SHORTCUT) {
823 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
824 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
825 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
826 mPendingAddInfo);
827 if (isWorkspaceLocked()) {
828 sPendingAddItem = args;
829 } else {
830 completeAdd(args);
831 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
832 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
833 }
834 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700835 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
836 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800839 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800840
841 }
842
843 @Override
844 protected void onActivityResult(
845 final int requestCode, final int resultCode, final Intent data) {
846 handleActivityResult(requestCode, resultCode, data);
847 if (mLauncherCallbacks != null) {
848 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
849 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800850 }
851
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600852 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700853 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600854 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700855 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
856 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
857 View v = null;
858 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
859 if (layout != null) {
860 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
861 }
862 Intent intent = sPendingAddItem.intent;
863 sPendingAddItem = null;
864 if (grantResults.length > 0
865 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700866 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700867 } else {
868 // TODO: Show a snack bar with link to settings
869 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
870 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
871 }
872 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600873 if (mLauncherCallbacks != null) {
874 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
875 grantResults);
876 }
877 }
878
Adam Cohendb364c32014-05-20 14:23:40 -0700879 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
880 appWidgetId, ItemInfo info) {
881 PendingAddArguments args = new PendingAddArguments();
882 args.requestCode = requestCode;
883 args.intent = data;
884 args.container = info.container;
885 args.screenId = info.screenId;
886 args.cellX = info.cellX;
887 args.cellY = info.cellY;
888 args.appWidgetId = appWidgetId;
889 return args;
890 }
891
892 /**
893 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
894 *
895 * @param screenId the screen id to check
896 * @return the new screen, or screenId if it exists
897 */
898 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800899 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700900 if (dropLayout == null) {
901 // it's possible that the add screen was removed because it was
902 // empty and a re-bind occurred
903 mWorkspace.addExtraEmptyScreen();
904 return mWorkspace.commitExtraEmptyScreen();
905 } else {
906 return screenId;
907 }
908 }
909
Adam Cohen091440a2015-03-18 14:16:05 -0700910 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800911 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800912 Runnable onCompleteRunnable = null;
913 int animationType = 0;
914
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700915 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800916 if (resultCode == RESULT_OK) {
917 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
918 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700919 mPendingAddWidgetInfo);
920 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800921 onCompleteRunnable = new Runnable() {
922 @Override
923 public void run() {
924 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700925 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700926 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
927 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800928 }
929 };
930 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800931 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800932 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800933 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700934 if (mDragLayer.getAnimatedView() != null) {
935 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
936 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
937 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700938 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700939 // The animated view may be null in the case of a rotation during widget configuration
940 onCompleteRunnable.run();
941 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 }
943
944 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700945 protected void onStop() {
946 super.onStop();
947 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700948
949 if (mLauncherCallbacks != null) {
950 mLauncherCallbacks.onStop();
951 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700952 }
953
954 @Override
955 protected void onStart() {
956 super.onStart();
957 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700958
959 if (mLauncherCallbacks != null) {
960 mLauncherCallbacks.onStart();
961 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700962 }
963
964 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200966 long startTime = 0;
967 if (DEBUG_RESUME_TIME) {
968 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400969 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200970 }
Adam Cohen9211d422014-10-07 18:14:53 -0700971
972 if (mLauncherCallbacks != null) {
973 mLauncherCallbacks.preOnResume();
974 }
975
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700977 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700978
Winson Chung4a2afa32012-07-19 14:53:05 -0700979 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700980 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700981 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800982 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700983 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700984 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700985 // view after launching an app, as they may be depending on the UI to be static to
986 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700987 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700988 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800989 } else if (mOnResumeState == State.WIDGETS) {
990 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700991 }
992 mOnResumeState = State.NONE;
993
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700994 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700995 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
996 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700997
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800998 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700999 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001000 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001001 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001002 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001003 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001005 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001006 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1007 // execute them here
1008 long startTimeCallbacks = 0;
1009 if (DEBUG_RESUME_TIME) {
1010 startTimeCallbacks = System.currentTimeMillis();
1011 }
1012
Michael Jurka1e2f4652013-07-08 18:03:46 -07001013 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1014 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001015 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001016 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001017 if (DEBUG_RESUME_TIME) {
1018 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1019 (System.currentTimeMillis() - startTimeCallbacks));
1020 }
Michael Jurka447bf842013-05-15 14:52:15 +02001021 }
Michael Jurka54554252013-08-01 12:52:23 +02001022 if (mOnResumeCallbacks.size() > 0) {
1023 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1024 mOnResumeCallbacks.get(i).run();
1025 }
1026 mOnResumeCallbacks.clear();
1027 }
Winson Chunge4e50662012-01-23 14:45:13 -08001028
1029 // Reset the pressed state of icons that were locked in the press state while activities
1030 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001031 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001032 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001033 mWaitingForResume.setStayPressed(false);
1034 }
Winson Chung82963d52013-10-09 11:20:57 -07001035
Adam Cohen06dff352012-06-01 17:17:08 -07001036 // It is possible that widgets can receive updates while launcher is not in the foreground.
1037 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1038 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1039 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001040 if (!isWorkspaceLoading()) {
1041 getWorkspace().reinflateWidgetsIfNecessary();
1042 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001043
Michael Jurka447bf842013-05-15 14:52:15 +02001044 if (DEBUG_RESUME_TIME) {
1045 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1046 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001047
Adam Cohen4b66bf32015-09-18 12:15:19 -07001048 // We want to suppress callbacks about CustomContent being shown if we have just received
1049 // onNewIntent while the user was present within launcher. In that case, we post a call
1050 // to move the user to the main screen (which will occur after onResume). We don't want to
1051 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1052 // suppress here.
1053 if (mWorkspace.getCustomContentCallbacks() != null
1054 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001055 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001056 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001057 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1058 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001059 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001060 }
1061 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001062 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001063 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001064 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001065
Sunny Goyal756adbc2015-04-16 15:20:51 -07001066 if (!isWorkspaceLoading()) {
1067 // Process any items that were added while Launcher was away.
1068 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1069 }
Adam Cohen9211d422014-10-07 18:14:53 -07001070
1071 if (mLauncherCallbacks != null) {
1072 mLauncherCallbacks.onResume();
1073 }
Adam Cohen06dff352012-06-01 17:17:08 -07001074 }
1075
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001076 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001077 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001078 // Ensure that items added to Launcher are queued until Launcher returns
1079 InstallShortcutReceiver.enableInstallQueue();
1080
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001081 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001082 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001083 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001084 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001085
1086 // We call onHide() aggressively. The custom content callbacks should be able to
1087 // debounce excess onHide calls.
1088 if (mWorkspace.getCustomContentCallbacks() != null) {
1089 mWorkspace.getCustomContentCallbacks().onHide();
1090 }
Romain Guycbb89e42009-06-08 15:52:54 -07001091
Adam Cohen9211d422014-10-07 18:14:53 -07001092 if (mLauncherCallbacks != null) {
1093 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001094 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001095 }
1096
1097 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001098 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1099 // by a onResume or by scrolling otherwise.
1100 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001101
1102 // Custom content is completely hidden
1103 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001104
1105 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1106 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001107
1108 // Indicates whether the user is allowed to scroll away from the custom content.
1109 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001110 }
1111
Adam Cohenc2d6e892014-10-16 09:49:24 -07001112 public interface LauncherOverlay {
1113
1114 /**
1115 * Touch interaction leading to overscroll has begun
1116 */
1117 public void onScrollInteractionBegin();
1118
1119 /**
1120 * Touch interaction related to overscroll has ended
1121 */
1122 public void onScrollInteractionEnd();
1123
1124 /**
1125 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1126 * screen (or in the case of RTL, the rightmost screen).
1127 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001128 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001129
1130 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001131 * Called when the launcher is ready to use the overlay
1132 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001133 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001134 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001135 }
1136
Jun Mukai8af0cd82015-05-12 12:32:12 -07001137 public interface LauncherSearchCallbacks {
1138 /**
1139 * Called when the search overlay is shown.
1140 */
1141 public void onSearchOverlayOpened();
1142
1143 /**
1144 * Called when the search overlay is dismissed.
1145 */
1146 public void onSearchOverlayClosed();
1147 }
1148
Adam Cohenc2d6e892014-10-16 09:49:24 -07001149 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001150 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001151 }
1152
1153 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1154
Sunny Goyalc86df472016-02-25 09:19:38 -08001155 public void onScrollChanged(float progress) {
1156 if (mWorkspace != null) {
1157 mWorkspace.onOverlayScrollChanged(progress);
1158 }
1159 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001160 }
1161
Jorim Jaggid017f882014-01-14 17:08:48 -08001162 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001163 if (mLauncherCallbacks != null) {
1164 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001165 } else {
1166 // On devices with a locked orientation, we will at least have the allow rotation
1167 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001168 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001169 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001170 }
1171
Adam Cohen9211d422014-10-07 18:14:53 -07001172 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001173 CustomContentCallbacks callbacks, String description) {
1174 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001175 }
1176
Adam Cohenedb40762013-07-18 16:45:45 -07001177 // The custom content needs to offset its content to account for the QSB
1178 public int getTopOffsetForCustomContent() {
1179 return mWorkspace.getPaddingTop();
1180 }
1181
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001182 @Override
1183 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001184 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001185 if (mModel.isCurrentCallbacks(this)) {
1186 mModel.stopLoader();
1187 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001188 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1189
Romain Guy13c2e7b2010-03-10 19:45:00 -08001190 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001191 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001192
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001193 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001194 @Override
1195 public void onWindowFocusChanged(boolean hasFocus) {
1196 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001197 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001198
1199 if (mLauncherCallbacks != null) {
1200 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1201 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001202 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001203
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001204 private boolean acceptFilter() {
1205 final InputMethodManager inputManager = (InputMethodManager)
1206 getSystemService(Context.INPUT_METHOD_SERVICE);
1207 return !inputManager.isFullscreenMode();
1208 }
1209
1210 @Override
1211 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001212 final int uniChar = event.getUnicodeChar();
1213 final boolean handled = super.onKeyDown(keyCode, event);
1214 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1215 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1217 keyCode, event);
1218 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001219 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001220 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001221 // showSearchDialog()
1222 // If there are multiple keystrokes before the search dialog takes focus,
1223 // onSearchRequested() will be called for every keystroke,
1224 // but it is idempotent, so it's fine.
1225 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226 }
1227 }
1228
Joe Onorato8a9625e2010-01-28 15:55:35 -08001229 // Eat the long press event so the keyboard doesn't come up.
1230 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1231 return true;
1232 }
1233
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 return handled;
1235 }
1236
Winson46163472015-09-22 17:31:56 -07001237 @Override
1238 public boolean onKeyUp(int keyCode, KeyEvent event) {
1239 if (keyCode == KeyEvent.KEYCODE_MENU) {
1240 // Ignore the menu key if we are currently dragging or are on the custom content screen
1241 if (!isOnCustomContent() && !mDragController.isDragging()) {
1242 // Close any open folders
1243 closeFolder();
1244
1245 // Stop resizing any widgets
1246 mWorkspace.exitWidgetResizeMode();
1247
1248 // Show the overview mode if we are on the workspace
1249 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1250 !mWorkspace.isSwitchingState()) {
1251 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001252 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001253 }
1254 }
1255 return true;
1256 }
1257 return super.onKeyUp(keyCode, event);
1258 }
1259
Karl Rosaen138a0412009-04-23 19:00:21 -07001260 private String getTypedText() {
1261 return mDefaultKeySsb.toString();
1262 }
1263
1264 private void clearTypedText() {
1265 mDefaultKeySsb.clear();
1266 mDefaultKeySsb.clearSpans();
1267 Selection.setSelection(mDefaultKeySsb, 0);
1268 }
1269
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001271 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1272 * State
1273 */
1274 private static State intToState(int stateOrdinal) {
1275 State state = State.WORKSPACE;
1276 final State[] stateValues = State.values();
1277 for (int i = 0; i < stateValues.length; i++) {
1278 if (stateValues[i].ordinal() == stateOrdinal) {
1279 state = stateValues[i];
1280 break;
1281 }
1282 }
1283 return state;
1284 }
1285
1286 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 * Restores the previous state, if it exists.
1288 *
1289 * @param savedState The previous state.
1290 */
1291 private void restoreState(Bundle savedState) {
1292 if (savedState == null) {
1293 return;
1294 }
1295
Michael Jurka883f55b2010-10-21 15:47:14 -07001296 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001297 if (state == State.APPS || state == State.WIDGETS) {
1298 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001299 }
1300
Adam Cohen21cd0022013-10-09 18:57:02 -07001301 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1302 PagedView.INVALID_RESTORE_PAGE);
1303 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001304 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 }
1306
Sunny Goyal756cd262015-08-20 12:33:21 -07001307 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1308 if (itemValues != null) {
1309 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001310 AppWidgetProviderInfo info = savedState.getParcelable(
1311 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001312 mPendingAddWidgetInfo = info == null ?
1313 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1314
Adam Cohen4637b5a2013-11-04 18:21:24 -08001315 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001316 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 mRestoring = true;
1318 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 }
1320
1321 /**
1322 * Finds all the views we need and configure them properly.
1323 */
1324 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001325 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001326 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001327 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
1328
Winson Chung4c98d922011-05-31 16:50:48 -07001329 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Hyunyoung Song7d2fc812016-06-15 12:51:30 -07001330 mPageIndicator = (PageIndicator) mDragLayer.findViewById(R.id.page_indicator);
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() {
1450 return FeatureFlags.NO_ALL_APPS_ICON ? mPageIndicator : 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 */
Winson2949fb52015-09-24 09:56:11 -07002336 public boolean removeItem(View v, 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 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002364
Winsonc0b52fe2015-09-09 16:38:15 -07002365 } else {
2366 return false;
2367 }
2368 return true;
2369 }
2370
2371 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002372 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002373 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002374 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002375 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002376 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002377 // Deleting an app widget ID is a void call but writes to disk before returning
2378 // to the caller...
2379 new AsyncTask<Void, Void, Void>() {
2380 public Void doInBackground(Void ... args) {
2381 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2382 return null;
2383 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002384 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002385 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002386 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002387 }
2388
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002389 @Override
2390 public boolean dispatchKeyEvent(KeyEvent event) {
2391 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2392 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002393 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002394 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002395 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002396 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002397 dumpState();
2398 return true;
2399 }
2400 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002401 }
2402 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2403 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002404 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002405 return true;
2406 }
2407 }
2408
2409 return super.dispatchKeyEvent(event);
2410 }
2411
Joe Onorato88ec0992009-11-19 13:16:06 -08002412 @Override
2413 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002414 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2415 return;
2416 }
2417
Sunny Goyal45478022015-06-08 16:52:41 -07002418 if (mDragController.isDragging()) {
2419 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002420 return;
2421 }
2422
Winson Chungb745afb2015-03-02 11:51:23 -08002423 if (isAppsViewVisible()) {
2424 showWorkspace(true);
2425 } else if (isWidgetsViewVisible()) {
2426 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002427 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002428 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002429 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002430 Folder openFolder = mWorkspace.getOpenFolder();
2431 if (openFolder.isEditingName()) {
2432 openFolder.dismissEditingName();
2433 } else {
2434 closeFolder();
2435 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002436 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002437 mWorkspace.exitWidgetResizeMode();
2438
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002439 // Back button is a no-op here, but give at least some feedback for the button press
2440 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002441 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002442 }
2443
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002444 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002445 * Launches the intent referred by the clicked shortcut.
2446 *
2447 * @param v The view representing the clicked shortcut.
2448 */
2449 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002450 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2451 // view has detached (it's possible for this to happen if the view is removed mid touch).
2452 if (v.getWindowToken() == null) {
2453 return;
2454 }
2455
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002456 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002457 return;
2458 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002459
Adam Cohen1697b792013-09-17 19:08:21 -07002460 if (v instanceof Workspace) {
2461 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002462 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002463 }
2464 return;
2465 }
2466
2467 if (v instanceof CellLayout) {
2468 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002469 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2470 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002471 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002472 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002473 }
2474
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002475 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002476 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002477 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002478 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002479 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002480 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002481 }
Sunny Goyalbb011da2016-06-15 15:42:29 -07002482 } else if (v instanceof PageIndicator || (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002483 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002484 } else if (tag instanceof AppInfo) {
2485 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002486 } else if (tag instanceof LauncherAppWidgetInfo) {
2487 if (v instanceof PendingAppWidgetHostView) {
2488 onClickPendingWidget((PendingAppWidgetHostView) v);
2489 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002490 }
2491 }
2492
Sunny Goyal70660032015-05-14 00:07:08 -07002493 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002494 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002495 return false;
2496 }
2497
Michael Jurkaaf442092010-06-10 17:01:57 -07002498 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002499 * Event handler for the app widget view which has not fully restored.
2500 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002501 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002502 if (mIsSafeModeEnabled) {
2503 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2504 return;
2505 }
2506
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002507 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002508 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002509 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2510 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2511 // This should not happen, as we make sure that an Id is allocated during bind.
2512 return;
2513 }
2514 LauncherAppWidgetProviderInfo appWidgetInfo =
2515 mAppWidgetManager.findProvider(info.providerName, info.user);
2516 if (appWidgetInfo != null) {
2517 mPendingAddWidgetId = info.appWidgetId;
2518 mPendingAddInfo.copyFrom(info);
2519 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002520
Sunny Goyald478c832016-04-01 12:04:16 -07002521 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2522 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingAddWidgetId);
2523 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
2524 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2525 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2526 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2527 }
2528 } else {
2529 LauncherAppWidgetProviderInfo appWidgetInfo =
2530 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2531 if (appWidgetInfo != null) {
2532 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2533 }
Sunny Goyalff572272014-07-23 13:58:07 -07002534 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002535 } else if (info.installProgress < 0) {
2536 // The install has not been queued
2537 final String packageName = info.providerName.getPackageName();
2538 showBrokenAppInstallDialog(packageName,
2539 new DialogInterface.OnClickListener() {
2540 public void onClick(DialogInterface dialog, int id) {
2541 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2542 }
2543 });
2544 } else {
2545 // Download has started.
2546 final String packageName = info.providerName.getPackageName();
2547 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002548 }
2549 }
2550
Sunny Goyald478c832016-04-01 12:04:16 -07002551 private void startRestoredWidgetReconfigActivity(
2552 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
2553 mPendingAddWidgetInfo = provider;
2554 mPendingAddInfo.copyFrom(info);
2555 mPendingAddWidgetId = info.appWidgetId;
2556 mAppWidgetManager.startConfigActivity(provider,
2557 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2558 }
2559
Sunny Goyalff572272014-07-23 13:58:07 -07002560 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002561 * Event handler for the "grid" button that appears on the home screen, which
2562 * enters all apps mode.
2563 *
2564 * @param v The view that was clicked.
2565 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002566 protected void onClickAllAppsButton(View v) {
2567 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002568 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002569 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002570 true /* updatePredictedApps */, false /* focusSearchBar */);
2571 }
2572 }
2573
2574 protected void onLongClickAllAppsButton(View v) {
2575 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2576 if (!isAppsViewVisible()) {
2577 showAppsView(true /* animated */, false /* resetListToTop */,
2578 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002579 }
2580 }
2581
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002582 private void showBrokenAppInstallDialog(final String packageName,
2583 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002584 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002585 .setTitle(R.string.abandoned_promises_title)
2586 .setMessage(R.string.abandoned_promise_explanation)
2587 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2588 .setNeutralButton(R.string.abandoned_clean_this,
2589 new DialogInterface.OnClickListener() {
2590 public void onClick(DialogInterface dialog, int id) {
2591 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2592 mWorkspace.removeAbandonedPromise(packageName, user);
2593 }
2594 })
2595 .create().show();
2596 return;
2597 }
2598
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002599 /**
2600 * Event handler for an app shortcut click.
2601 *
2602 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2603 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002604 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002605 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2606 Object tag = v.getTag();
2607 if (!(tag instanceof ShortcutInfo)) {
2608 throw new IllegalArgumentException("Input must be a Shortcut");
2609 }
2610
2611 // Open shortcut
2612 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002613
2614 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002615 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2616 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002617 // Launch activity anyway, framework will tell the user why the app is suspended.
2618 } else {
2619 int error = R.string.activity_not_available;
2620 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2621 error = R.string.safemode_shortcut_error;
2622 }
2623 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2624 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002625 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002626 }
2627
Chris Wren40c5ed32014-06-24 18:24:23 -04002628 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002629 if ((v instanceof BubbleTextView)
2630 && shortcut.isPromise()
2631 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002632 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002633 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002634 new DialogInterface.OnClickListener() {
2635 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002636 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002637 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002638 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002639 return;
2640 }
2641
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002642 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002643 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002644 }
2645
Sunny Goyala7ce1662016-05-31 15:01:35 -07002646 private void startAppShortcutOrInfoActivity(View v) {
2647 ItemInfo item = (ItemInfo) v.getTag();
2648 Intent intent = item.getIntent();
2649 if (intent == null) {
2650 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002651 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002652 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002653 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002654
2655 if (success && v instanceof BubbleTextView) {
2656 mWaitingForResume = (BubbleTextView) v;
2657 mWaitingForResume.setStayPressed(true);
2658 }
2659 }
2660
2661 /**
2662 * Event handler for a folder icon click.
2663 *
2664 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2665 */
2666 protected void onClickFolderIcon(View v) {
2667 if (LOGD) Log.d(TAG, "onClickFolder");
2668 if (!(v instanceof FolderIcon)){
2669 throw new IllegalArgumentException("Input must be a FolderIcon");
2670 }
2671
2672 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002673 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002674 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002675 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002676 }
Michael Jurka5130e402011-10-13 04:55:35 -07002677 }
2678
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002679 /**
2680 * Event handler for the (Add) Widgets button that appears after a long press
2681 * on the home screen.
2682 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002683 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002684 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002685 if (mIsSafeModeEnabled) {
2686 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2687 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002688 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002689 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002690 }
2691
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002692 /**
2693 * Event handler for the wallpaper picker button that appears after a long press
2694 * on the home screen.
2695 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002696 protected void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002697 if (!Utilities.isWallapaperAllowed(this)) {
2698 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2699 return;
2700 }
2701
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002702 String pickerPackage = getString(R.string.wallpaper_picker_package);
2703 if (TextUtils.isEmpty(pickerPackage)) {
2704 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2705 }
2706
Tony Wickham785f7a52015-08-31 17:28:32 -07002707 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2708 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002709 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER)
2710 .setPackage(pickerPackage)
2711 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset),
2712 REQUEST_PICK_WALLPAPER);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002713 }
2714
2715 /**
2716 * Event handler for a click on the settings button that appears after a long press
2717 * on the home screen.
2718 */
Sunny Goyal745bad92016-05-02 10:54:12 -07002719 private void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002720 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002721 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2722 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002723 }
2724
Adam Cohen61f560d2013-09-30 15:58:20 -07002725 public View.OnTouchListener getHapticFeedbackTouchListener() {
2726 if (mHapticFeedbackTouchListener == null) {
2727 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002728 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002729 @Override
2730 public boolean onTouch(View v, MotionEvent event) {
2731 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2732 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2733 }
2734 return false;
2735 }
2736 };
2737 }
2738 return mHapticFeedbackTouchListener;
2739 }
2740
Tony Wickhame2217252016-03-22 16:34:23 -07002741 @Override
2742 public void onAccessibilityStateChanged(boolean enabled) {
2743 mDragLayer.onAccessibilityStateChanged(enabled);
2744 }
2745
Adam Cohen9211d422014-10-07 18:14:53 -07002746 public void onDragStarted(View view) {
2747 if (isOnCustomContent()) {
2748 // Custom content screen doesn't participate in drag and drop. If on custom
2749 // content screen, move to default.
2750 moveWorkspaceToDefaultScreen();
2751 }
Adam Cohen9211d422014-10-07 18:14:53 -07002752 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002753
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002754 /**
2755 * Called when the user stops interacting with the launcher.
2756 * This implies that the user is now on the homescreen and is not doing housekeeping.
2757 */
Adam Cohen9211d422014-10-07 18:14:53 -07002758 protected void onInteractionEnd() {
2759 if (mLauncherCallbacks != null) {
2760 mLauncherCallbacks.onInteractionEnd();
2761 }
2762 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002763
2764 /**
2765 * Called when the user starts interacting with the launcher.
2766 * The possible interactions are:
2767 * - open all apps
2768 * - reorder an app shortcut, or a widget
2769 * - open the overview mode.
2770 * This is a good time to stop doing things that only make sense
2771 * when the user is on the homescreen and not doing housekeeping.
2772 */
Adam Cohen9211d422014-10-07 18:14:53 -07002773 protected void onInteractionBegin() {
2774 if (mLauncherCallbacks != null) {
2775 mLauncherCallbacks.onInteractionBegin();
2776 }
2777 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002778
Winson Chungcd99cd32015-04-29 11:03:24 -07002779 /** Updates the interaction state. */
2780 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002781 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002782 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002783 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2784 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002785 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002786 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002787 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002788 onInteractionEnd();
2789 }
2790 }
2791
Sunny Goyala7ce1662016-05-31 15:01:35 -07002792 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002793 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002794 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2795 try {
2796 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2797 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2798 // is enabled by default on NYC.
2799 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2800 .penaltyLog().build());
2801 // Could be launching some bookkeeping activity
2802 startActivity(intent, optsBundle);
2803 } finally {
2804 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002805 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002806 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002807 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2808 // corresponding permission. Show the appropriate permission prompt if that
2809 // is the case.
2810 if (intent.getComponent() == null
2811 && Intent.ACTION_CALL.equals(intent.getAction())
2812 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2813 PackageManager.PERMISSION_GRANTED) {
2814 // TODO: Rename sPendingAddItem to a generic name.
2815 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2816 0, info);
2817 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2818 REQUEST_PERMISSION_CALL_PHONE);
2819 } else {
2820 // No idea why this was thrown.
2821 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002822 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002823 }
2824 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002825
Sunny Goyala7ce1662016-05-31 15:01:35 -07002826 private Bundle getActivityLaunchOptions(View v) {
2827 if (Utilities.ATLEAST_MARSHMALLOW) {
2828 int left = 0, top = 0;
2829 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2830 if (v instanceof TextView) {
2831 // Launch from center of icon, not entire view
2832 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2833 if (icon != null) {
2834 Rect bounds = icon.getBounds();
2835 left = (width - bounds.width()) / 2;
2836 top = v.getPaddingTop();
2837 width = bounds.width();
2838 height = bounds.height();
2839 }
2840 }
2841 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2842 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2843 // On L devices, we use the device default slide-up transition.
2844 // On L MR1 devices, we use a custom version of the slide-up transition which
2845 // doesn't have the delay present in the device default.
2846 return ActivityOptions.makeCustomAnimation(
2847 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2848 }
2849 return null;
2850 }
2851
2852 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002853 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2854 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2855 return false;
2856 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002857 // Only launch using the new animation if the shortcut has not opted out (this is a
2858 // private contract between launcher and may be ignored in the future).
2859 boolean useLaunchAnimation = (v != null) &&
2860 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2861 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2862
2863 UserHandleCompat user = null;
2864 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2865 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2866 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002867 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002868
2869 // Prepare intent
2870 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2871 if (v != null) {
2872 int[] pos = new int[2];
2873 v.getLocationOnScreen(pos);
2874 intent.setSourceBounds(
2875 new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2876 }
2877 try {
2878 if (Utilities.ATLEAST_MARSHMALLOW &&
2879 item != null && item.itemType == Favorites.ITEM_TYPE_SHORTCUT) {
2880 // Shortcuts need some special checks due to legacy reasons.
2881 startShortcutIntentSafely(intent, optsBundle, item);
2882 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2883 // Could be launching some bookkeeping activity
2884 startActivity(intent, optsBundle);
2885 } else {
2886 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2887 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2888 }
2889 return true;
2890 } catch (ActivityNotFoundException|SecurityException e) {
2891 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2892 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2893 }
2894 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002895 }
2896
Adam Cohen268c4752012-06-06 17:47:33 -07002897 /**
2898 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2899 * in the DragLayer in the exact absolute location of the original FolderIcon.
2900 */
2901 private void copyFolderIconToImage(FolderIcon fi) {
2902 final int width = fi.getMeasuredWidth();
2903 final int height = fi.getMeasuredHeight();
2904
2905 // Lazy load ImageView, Bitmap and Canvas
2906 if (mFolderIconImageView == null) {
2907 mFolderIconImageView = new ImageView(this);
2908 }
2909 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2910 mFolderIconBitmap.getHeight() != height) {
2911 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2912 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2913 }
2914
2915 DragLayer.LayoutParams lp;
2916 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2917 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2918 } else {
2919 lp = new DragLayer.LayoutParams(width, height);
2920 }
2921
Adam Cohen307fe232012-08-16 17:55:58 -07002922 // The layout from which the folder is being opened may be scaled, adjust the starting
2923 // view size by this scale factor.
2924 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002925 lp.customPosition = true;
2926 lp.x = mRectForFolderAnimation.left;
2927 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002928 lp.width = (int) (scale * width);
2929 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002930
2931 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2932 fi.draw(mFolderIconCanvas);
2933 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002934 if (fi.getFolder() != null) {
2935 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2936 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002937 }
Adam Cohen268c4752012-06-06 17:47:33 -07002938 // Just in case this image view is still in the drag layer from a previous animation,
2939 // we remove it and re-add it.
2940 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2941 mDragLayer.removeView(mFolderIconImageView);
2942 }
2943 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002944 if (fi.getFolder() != null) {
2945 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002946 }
Adam Cohen268c4752012-06-06 17:47:33 -07002947 }
2948
Adam Cohen37b2a492016-04-06 18:36:06 -07002949 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002950 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002951 FolderInfo info = (FolderInfo) fi.getTag();
2952 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2953 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002954 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2955 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002956 }
2957
Adam Cohen268c4752012-06-06 17:47:33 -07002958 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2959 copyFolderIconToImage(fi);
2960 fi.setVisibility(View.INVISIBLE);
2961
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002962 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2963 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002964 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002965 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2966 }
2967 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002968 oa.start();
2969 }
2970
Sunny Goyal935fca12015-10-13 10:19:01 -07002971 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002972 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002973 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002974
Adam Cohen268c4752012-06-06 17:47:33 -07002975 // We remove and re-draw the FolderIcon in-case it has changed
2976 mDragLayer.removeView(mFolderIconImageView);
2977 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08002978
2979 if (cl != null) {
2980 cl.clearFolderLeaveBehind();
2981 }
2982
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002983 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002984 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002985 oa.addListener(new AnimatorListenerAdapter() {
2986 @Override
2987 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002988 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07002989 // Remove the ImageView copy of the FolderIcon and make the original visible.
2990 mDragLayer.removeView(mFolderIconImageView);
2991 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002992 }
2993 }
2994 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002995 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07002996 if (!animate) {
2997 oa.end();
2998 }
Adam Cohen2801caf2011-05-13 20:57:39 -07002999 }
3000
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003001 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003002 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003003 * is animated relative to the specified View. If the View is null, no animation
3004 * is played.
3005 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003006 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003007 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003008 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003009
Adam Cohenfb91f302012-06-11 15:45:18 -07003010 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003011 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3012 if (openFolder != null && openFolder != folder) {
3013 // Close any open folder before opening a folder.
3014 closeFolder();
3015 }
3016
Adam Cohena9cf38f2011-05-02 15:36:58 -07003017 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003018
Adam Cohena9cf38f2011-05-02 15:36:58 -07003019 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003020
Sunny Goyalf4066152015-04-15 09:42:19 -07003021 // While the folder is open, the position of the icon cannot change.
3022 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3023
Adam Cohen4554ee12011-08-03 16:13:21 -07003024 // Just verify that the folder hasn't already been added to the DragLayer.
3025 // There was a one-off crash where the folder had a parent already.
3026 if (folder.getParent() == null) {
3027 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003028 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003029 } else {
3030 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3031 folder.getParent() + ").");
3032 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003033 folder.animateOpen();
3034
3035 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003036
3037 // Notify the accessibility manager that this folder "window" has appeared and occluded
3038 // the workspace items
3039 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3040 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003041 }
3042
3043 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003044 closeFolder(true);
3045 }
3046
3047 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003048 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003049 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003050 if (folder.isEditingName()) {
3051 folder.dismissEditingName();
3052 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003053 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003054 }
3055 }
3056
Sunny Goyal935fca12015-10-13 10:19:01 -07003057 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003058 animate &= !Utilities.isPowerSaverOn(this);
3059
Adam Cohen4554ee12011-08-03 16:13:21 -07003060 folder.getInfo().opened = false;
3061
3062 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3063 if (parent != null) {
3064 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003065 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003066 if (fi != null) {
3067 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3068 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003069 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003070 if (animate) {
3071 folder.animateClosed();
3072 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003073 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003074 }
Winson Chung83ca4802013-04-12 15:10:52 -07003075
Sunny Goyal935fca12015-10-13 10:19:01 -07003076 // Notify the accessibility manager that this folder "window" has disappeared and no
3077 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003078 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003079 }
3080
Tony Wickhamdadb3042016-02-24 11:07:00 -08003081 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003082 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003083 if (!isDraggingEnabled()) return false;
3084 if (isWorkspaceLocked()) return false;
3085 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003086
Sunny Goyalbb011da2016-06-15 15:42:29 -07003087 if (v == mAllAppsButton && mAllAppsButton != null) {
Winson Chung76648c52015-07-10 14:33:23 -07003088 onLongClickAllAppsButton(v);
3089 return true;
3090 }
3091
Adam Cohen1697b792013-09-17 19:08:21 -07003092 if (v instanceof Workspace) {
3093 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003094 if (!mWorkspace.isTouchActive()) {
3095 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003096 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3097 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3098 return true;
3099 } else {
3100 return false;
3101 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003102 } else {
3103 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003104 }
Adam Cohen1697b792013-09-17 19:08:21 -07003105 }
3106
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003107 CellLayout.CellInfo longClickCellInfo = null;
3108 View itemUnderLongClick = null;
3109 if (v.getTag() instanceof ItemInfo) {
3110 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003111 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003112 itemUnderLongClick = longClickCellInfo.cell;
3113 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003114 }
3115
Winson Chung3d503fb2011-07-13 17:25:49 -07003116 // The hotseat touch handling does not go through Workspace, and we always allow long press
3117 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003118 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003119 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003120 // User long pressed on empty space
3121 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3122 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003123 if (mWorkspace.isInOverviewMode()) {
3124 mWorkspace.startReordering(v);
3125 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003126 showOverviewMode(true);
Hyunyoung Song645764e2016-06-06 14:19:02 -07003127 mHotseat.requestDisallowInterceptTouchEvent(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003128 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003129 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003130 final boolean isAllAppsButton =
3131 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3132 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3133 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003134 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003135 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003136 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003137 }
3138 }
3139 }
3140 return true;
3141 }
3142
Winson Chung3d503fb2011-07-13 17:25:49 -07003143 boolean isHotseatLayout(View layout) {
3144 return mHotseat != null && layout != null &&
3145 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3146 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003147
Winson Chung3d503fb2011-07-13 17:25:49 -07003148 /**
3149 * Returns the CellLayout of the specified container at the specified screen.
3150 */
Sunny Goyal92820592015-03-02 11:31:35 -08003151 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003152 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3153 if (mHotseat != null) {
3154 return mHotseat.getLayout();
3155 } else {
3156 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003157 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003158 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003159 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003160 }
3161 }
3162
Winson Chungb745afb2015-03-02 11:51:23 -08003163 /**
3164 * For overridden classes.
3165 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003166 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003167 return isAppsViewVisible();
3168 }
3169
3170 public boolean isAppsViewVisible() {
3171 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3172 }
3173
3174 public boolean isWidgetsViewVisible() {
3175 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003176 }
3177
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003178 private void setWorkspaceBackground(int background) {
3179 switch (background) {
3180 case WORKSPACE_BACKGROUND_TRANSPARENT:
3181 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3182 break;
3183 case WORKSPACE_BACKGROUND_BLACK:
3184 getWindow().setBackgroundDrawable(null);
3185 break;
3186 default:
3187 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3188 }
Craig Mautner360310b2012-10-26 15:13:08 -07003189 }
3190
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003191 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003192 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3193 int curflags = getWindow().getAttributes().flags
3194 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3195 if (wpflags != curflags) {
3196 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3197 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003198 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003199 }
3200
Michael Jurkae326f182011-11-21 14:05:46 -08003201 @Override
3202 public void onTrimMemory(int level) {
3203 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003204 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3205 // The widget preview db can result in holding onto over
3206 // 3MB of memory for caching which isn't necessary.
3207 SQLiteDatabase.releaseMemory();
3208
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003209 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003210 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003211 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003212 if (mLauncherCallbacks != null) {
3213 mLauncherCallbacks.onTrimMemory(level);
3214 }
Michael Jurkae326f182011-11-21 14:05:46 -08003215 }
3216
Winson5c6bdbb2015-09-03 11:36:19 -07003217 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003218 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003219 }
3220
Winson5c6bdbb2015-09-03 11:36:19 -07003221 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003222 boolean changed = mState != State.WORKSPACE ||
3223 mWorkspace.getState() != Workspace.State.NORMAL;
3224 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003225 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003226 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003227 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003228
Michael Jurkab3e22d92011-10-31 15:58:33 -07003229 // Set focus to the AppsCustomize button
3230 if (mAllAppsButton != null) {
3231 mAllAppsButton.requestFocus();
3232 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003233 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003234
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003235 // Change the state *after* we've called all the transition code
3236 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003237
Winson Chung5fb63472011-02-02 17:03:37 -08003238 // Resume the auto-advance of widgets
3239 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003240 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003241
Winson Chung0f785722015-04-08 10:27:49 -07003242 if (changed) {
3243 // Send an accessibility event to announce the context change
3244 getWindow().getDecorView()
3245 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003246 }
Winson5c6bdbb2015-09-03 11:36:19 -07003247 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003248 }
3249
Winsone9f27272015-10-13 10:47:51 -07003250 /**
3251 * Shows the overview button.
3252 */
Adam Cohened307df2013-10-02 09:37:31 -07003253 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003254 showOverviewMode(animated, false);
3255 }
3256
3257 /**
3258 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3259 * onto one of the overview panel buttons.
3260 */
3261 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3262 Runnable postAnimRunnable = null;
3263 if (requestButtonFocus) {
3264 postAnimRunnable = new Runnable() {
3265 @Override
3266 public void run() {
3267 // Hitting the menu button when in touch mode does not trigger touch mode to
3268 // be disabled, so if requested, force focus on one of the overview panel
3269 // buttons.
3270 mOverviewPanel.requestFocusFromTouch();
3271 }
3272 };
3273 }
Adam Cohened307df2013-10-02 09:37:31 -07003274 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003275 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003276 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003277 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003278 }
3279
Winson Chungb745afb2015-03-02 11:51:23 -08003280 /**
3281 * Shows the apps view.
3282 */
Hyunyoung Song645764e2016-06-06 14:19:02 -07003283 public void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003284 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003285 if (resetListToTop) {
3286 mAppsView.scrollToTop();
3287 }
Winson Chung4ac30062015-05-08 17:34:17 -07003288 if (updatePredictedApps) {
3289 tryAndUpdatePredictedApps();
3290 }
Winson Chung76648c52015-07-10 14:33:23 -07003291 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003292 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003293
Winson Chungb745afb2015-03-02 11:51:23 -08003294 /**
3295 * Shows the widgets view.
3296 */
3297 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003298 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003299 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003300 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003301 }
Winson Chung76648c52015-07-10 14:33:23 -07003302 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003303
3304 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003305 @Override
3306 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003307 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003308 }
3309 });
Winson Chungb745afb2015-03-02 11:51:23 -08003310 }
3311
3312 /**
3313 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003314 *
3315 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003316 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003317 // TODO: calling method should use the return value so that when {@code false} is returned
3318 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003319 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003320 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3321 mState != State.WIDGETS_SPRING_LOADED) {
3322 return false;
3323 }
3324 if (toState != State.APPS && toState != State.WIDGETS) {
3325 return false;
3326 }
Winson Chungb745afb2015-03-02 11:51:23 -08003327
3328 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003329 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3330 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003331 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003332 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003333 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003334
Michael Jurkab3e22d92011-10-31 15:58:33 -07003335 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003336 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003337
3338 // Pause the auto-advance of widgets until we are out of AllApps
3339 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003340 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003341 closeFolder();
3342
3343 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003344 getWindow().getDecorView()
3345 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003346 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003347 }
3348
Winson Chungcd99cd32015-04-29 11:03:24 -07003349 /**
3350 * Updates the workspace and interaction state on state change, and return the animation to this
3351 * new state.
3352 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003353 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003354 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003355 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003356 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003357 updateInteraction(fromState, toState);
3358 return anim;
3359 }
3360
Jun Mukaif0033da2015-08-04 18:34:30 -07003361 public void onLauncherClingShown() {
3362 // When a launcher cling appears, it should cover the underlying layers, so their focus
3363 // should be blocked.
3364 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3365 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3366 }
3367 }
3368
3369 public void onLauncherClingDismissed() {
3370 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3371 }
3372
Hyunyoung Song3f471442015-04-08 19:01:34 -07003373 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003374 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003375 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003376 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003377 }
Winson Chungb745afb2015-03-02 11:51:23 -08003378
Winson Chung006ee262015-08-03 14:40:11 -07003379 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003380 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003381 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003382
3383 if (isAppsViewVisible()) {
3384 mState = State.APPS_SPRING_LOADED;
3385 } else if (isWidgetsViewVisible()) {
3386 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003387 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003388 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003389 } else {
3390 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003391 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003392 }
Adam Cohen7777d962011-08-18 18:58:38 -07003393
Hyunyoung Song3f471442015-04-08 19:01:34 -07003394 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003395 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003396 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003397
Winson Chunge7a03942011-08-05 15:05:12 -07003398 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003399 @Override
3400 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003401 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003402 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3403 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003404 // Before we show workspace, hide all apps again because
3405 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3406 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003407 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003408 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003409 } else {
3410 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003411 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003412 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003413 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003414 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003415
Tony Wickhame0c33232016-02-08 11:37:04 -08003416 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003417 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3418 || mState == State.WIDGETS_SPRING_LOADED;
3419 }
3420
Michael Jurkad3ef3062010-11-23 16:23:58 -08003421 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003422 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003423 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003424 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003425 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003426 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003427 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3428 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003429 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003430 }
3431
Winson Chung4ac30062015-05-08 17:34:17 -07003432 /**
3433 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3434 * resumed.
3435 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003436 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003437 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003438 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003439 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003440 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003441 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003442 }
3443 }
3444 }
3445
Michael Jurkab3e22d92011-10-31 15:58:33 -07003446 void lockAllApps() {
3447 // TODO
3448 }
3449
3450 void unlockAllApps() {
3451 // TODO
3452 }
3453
Winsonf768d932015-09-25 16:12:35 -07003454 public boolean launcherCallbacksProvidesSearch() {
3455 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3456 }
3457
Michael Jurkad7c28052012-04-27 15:43:36 -07003458 @Override
3459 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003460 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003461 final List<CharSequence> text = event.getText();
3462 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003463 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003464 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003465 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003466 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003467 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003468 } else if (mWorkspace != null) {
3469 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003470 } else {
3471 text.add(getString(R.string.all_apps_home_button_label));
3472 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003473 return result;
3474 }
3475
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003476 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003477 * If the activity is currently paused, signal that we need to run the passed Runnable
3478 * in onResume.
3479 *
3480 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003481 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3482 * wrong when we're not running, and if the activity comes back to what the configuration was
3483 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003484 *
3485 * Implementation of the method from LauncherModel.Callbacks.
3486 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003487 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003488 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003489 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003490 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003491 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003492 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003493 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003494 }
3495 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003496 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003497 return true;
3498 } else {
3499 return false;
3500 }
3501 }
3502
Michael Jurkac402cd92013-05-20 15:49:32 +02003503 private boolean waitUntilResume(Runnable run) {
3504 return waitUntilResume(run, false);
3505 }
3506
Michael Jurka1e2f4652013-07-08 18:03:46 -07003507 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003508 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003509 }
3510
Michael Jurka7607c2f2013-04-03 14:33:19 -07003511 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003512 * If the activity is currently paused, signal that we need to re-run the loader
3513 * in onResume.
3514 *
3515 * This needs to be called from incoming places where resources might have been loaded
3516 * while we are paused. That is becaues the Configuration might be wrong
3517 * when we're not running, and if it comes back to what it was when we
3518 * were paused, we are not restarted.
3519 *
3520 * Implementation of the method from LauncherModel.Callbacks.
3521 *
3522 * @return true if we are currently paused. The caller might be able to
3523 * skip some work in that case since we will come back again.
3524 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003525 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003526 public boolean setLoadOnResume() {
3527 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003528 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003529 mOnResumeNeedsLoad = true;
3530 return true;
3531 } else {
3532 return false;
3533 }
3534 }
3535
3536 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003537 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003538 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003539 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003540 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003541 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003542 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003543 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003544 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003545 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003546 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003547
Joe Onorato9c1289c2009-08-17 11:03:03 -04003548 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003549 * Clear any pending bind callbacks. This is called when is loader is planning to
3550 * perform a full rebind from scratch.
3551 */
3552 @Override
3553 public void clearPendingBinds() {
3554 mBindOnResumeCallbacks.clear();
3555 if (mPendingExecutor != null) {
3556 mPendingExecutor.markCompleted();
3557 mPendingExecutor = null;
3558 }
3559 }
3560
3561 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003562 * Refreshes the shortcuts shown on the workspace.
3563 *
3564 * Implementation of the method from LauncherModel.Callbacks.
3565 */
3566 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003567 if (LauncherAppState.PROFILE_STARTUP) {
3568 Trace.beginSection("Starting page bind");
3569 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003570 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003571
Winson Chungd64d1762013-08-20 14:37:16 -07003572 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003573 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003574 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003575
Michael Jurka05bf644e2011-11-30 20:28:53 -08003576 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003577 if (mHotseat != null) {
3578 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003579 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003580 if (LauncherAppState.PROFILE_STARTUP) {
3581 Trace.endSection();
3582 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003583 }
3584
Adam Cohendcd297f2013-06-18 13:13:40 -07003585 @Override
3586 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003587 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003588 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3589 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003590 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3591 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3592 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003593 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3594 // If there are no screens, we need to have an empty screen
3595 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003596 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003597 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003598
3599 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003600 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003601 if (hasCustomContentToLeft()) {
3602 mWorkspace.createCustomContentContainer();
3603 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003604 }
Winson Chung64359a52013-07-08 17:17:08 -07003605 }
3606
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003607 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003608 int count = orderedScreenIds.size();
3609 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003610 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003611 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003612 // No need to bind the first screen, as its always bound.
3613 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3614 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003615 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003616 }
3617
Winson Chungd64d1762013-08-20 14:37:16 -07003618 public void bindAppsAdded(final ArrayList<Long> newScreens,
3619 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003620 final ArrayList<ItemInfo> addAnimated,
3621 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003622 Runnable r = new Runnable() {
3623 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003624 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003625 }
3626 };
3627 if (waitUntilResume(r)) {
3628 return;
3629 }
3630
Winson Chungd64d1762013-08-20 14:37:16 -07003631 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003632 if (newScreens != null) {
3633 bindAddScreens(newScreens);
3634 }
Winson Chungd64d1762013-08-20 14:37:16 -07003635
3636 // We add the items without animation on non-visible pages, and with
3637 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003638 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003639 bindItems(addNotAnimated, 0,
3640 addNotAnimated.size(), false);
3641 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003642 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003643 bindItems(addAnimated, 0,
3644 addAnimated.size(), true);
3645 }
Winson Chungc58497e2013-09-03 17:48:37 -07003646
Winson Chung87412982013-10-03 18:34:14 -07003647 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003648 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003649
Winson Chungb745afb2015-03-02 11:51:23 -08003650 if (addedApps != null && mAppsView != null) {
3651 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003652 }
Winson Chungd64d1762013-08-20 14:37:16 -07003653 }
3654
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003655 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003656 * Bind the items start-end from the list.
3657 *
3658 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003659 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003660 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003661 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3662 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003663 Runnable r = new Runnable() {
3664 public void run() {
3665 bindItems(shortcuts, start, end, forceAnimateIcons);
3666 }
3667 };
3668 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003669 return;
3670 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003671
Winson Chungf0c6ae02012-03-21 16:10:31 -07003672 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003673 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3674 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003675 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003676 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003677 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003678 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003679 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003680
3681 // Short circuit if we are loading dock items for a configuration which has no dock
3682 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3683 mHotseat == null) {
3684 continue;
3685 }
3686
Sunny Goyal639e9062015-08-19 19:17:06 -07003687 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003688 switch (item.itemType) {
3689 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3690 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003691 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003692 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003693 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003694 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003695 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003696 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003697 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003698 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003699 default:
3700 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003701 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003702
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003703 /*
3704 * Remove colliding items.
3705 */
3706 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3707 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3708 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3709 View v = cl.getChildAt(item.cellX, item.cellY);
3710 Object tag = v.getTag();
3711 String desc = "Collision while binding workspace item: " + item
3712 + ". Collides with " + tag;
3713 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3714 throw (new RuntimeException(desc));
3715 } else {
3716 Log.d(TAG, desc);
3717 LauncherModel.deleteItemFromDatabase(this, item);
3718 continue;
3719 }
3720 }
3721 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003722 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3723 item.cellY, 1, 1);
3724 if (animateIcons) {
3725 // Animate all the applications up now
3726 view.setAlpha(0f);
3727 view.setScaleX(0f);
3728 view.setScaleY(0f);
3729 bounceAnims.add(createNewAppBounceAnimation(view, i));
3730 newShortcutsScreenId = item.screenId;
3731 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003732 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003733
Winson Chung997a9232013-07-24 15:33:46 -07003734 if (animateIcons) {
3735 // Animate to the correct page
3736 if (newShortcutsScreenId > -1) {
3737 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003738 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003739 final Runnable startBounceAnimRunnable = new Runnable() {
3740 public void run() {
3741 anim.playTogether(bounceAnims);
3742 anim.start();
3743 }
3744 };
Winson Chung997a9232013-07-24 15:33:46 -07003745 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003746 // We post the animation slightly delayed to prevent slowdowns
3747 // when we are loading right after we return to launcher.
3748 mWorkspace.postDelayed(new Runnable() {
3749 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003750 if (mWorkspace != null) {
3751 mWorkspace.snapToPage(newScreenIndex);
3752 mWorkspace.postDelayed(startBounceAnimRunnable,
3753 NEW_APPS_ANIMATION_DELAY);
3754 }
Winson Chung94d67682013-09-25 16:29:40 -07003755 }
3756 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003757 } else {
3758 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003759 }
3760 }
Winson Chung64359a52013-07-08 17:17:08 -07003761 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003762 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003763 }
3764
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003765 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3766 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3767 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003768 view.updateAppWidget(null);
3769 view.setOnClickListener(this);
3770 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003771 mWorkspace.requestLayout();
3772 }
3773
Joe Onorato9c1289c2009-08-17 11:03:03 -04003774 /**
3775 * Add the views for a widget to the workspace.
3776 *
3777 * Implementation of the method from LauncherModel.Callbacks.
3778 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003779 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003780 Runnable r = new Runnable() {
3781 public void run() {
3782 bindAppWidget(item);
3783 }
3784 };
3785 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003786 return;
3787 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003788
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003789 if (mIsSafeModeEnabled) {
3790 bindSafeModeWidget(item);
3791 return;
3792 }
3793
Daniel Sandler843e8602010-06-07 14:59:01 -04003794 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3795 if (DEBUG_WIDGETS) {
3796 Log.d(TAG, "bindAppWidget: " + item);
3797 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003798
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003799 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003800
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003801 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3802 // If the provider is not ready, bind as a pending widget.
3803 appWidgetInfo = null;
3804 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3805 // The widget id is not valid. Try to find the widget based on the provider info.
3806 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3807 } else {
3808 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3809 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003810
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003811 // If the provider is ready, but the width is not yet restored, try to restore it.
3812 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3813 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003814 if (appWidgetInfo == null) {
3815 if (DEBUG_WIDGETS) {
3816 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3817 + " belongs to component " + item.providerName
3818 + ", as the povider is null");
3819 }
3820 LauncherModel.deleteItemFromDatabase(this, item);
3821 return;
3822 }
Sunny Goyalff572272014-07-23 13:58:07 -07003823
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003824 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003825 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003826 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3827 // Id has not been allocated yet. Allocate a new id.
3828 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3829 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003830
Sunny Goyald478c832016-04-01 12:04:16 -07003831 // Also try to bind the widget. If the bind fails, the user will be shown
3832 // a click to setup UI, which will ask for the bind permission.
3833 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3834 pendingInfo.spanX = item.spanX;
3835 pendingInfo.spanY = item.spanY;
3836 pendingInfo.minSpanX = item.minSpanX;
3837 pendingInfo.minSpanY = item.minSpanY;
3838 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
3839 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3840 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003841
Sunny Goyald478c832016-04-01 12:04:16 -07003842 // Bind succeeded
3843 if (success) {
3844 // If the widget has a configure activity, it is still needs to set it up,
3845 // otherwise the widget is ready to go.
3846 item.restoreStatus = (appWidgetInfo.configure == null)
3847 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3848 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003849 }
Sunny Goyald478c832016-04-01 12:04:16 -07003850
3851 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003852 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003853 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003854 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003855 // The widget was marked as UI not ready, but there is no configure activity to
3856 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003857 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3858 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003859 }
Sunny Goyalff572272014-07-23 13:58:07 -07003860 }
3861
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003862 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003863 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003864 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3865 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003866 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003867
Sunny Goyal56c73602015-09-25 12:55:01 -07003868 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003869 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003870 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003871 deleteWidgetInfo(item);
3872 return;
3873 }
3874
Sunny Goyal233ee962015-08-03 13:05:01 -07003875 item.minSpanX = appWidgetInfo.minSpanX;
3876 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003877 addAppWidgetToWorkspace(
3878 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3879 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003880 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003881 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003882 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003883 view.updateAppWidget(null);
3884 view.setOnClickListener(this);
3885 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003886 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003887 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003888
Daniel Sandler843e8602010-06-07 14:59:01 -04003889 if (DEBUG_WIDGETS) {
3890 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3891 + (SystemClock.uptimeMillis()-start) + "ms");
3892 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003893 }
3894
Sunny Goyalff572272014-07-23 13:58:07 -07003895 /**
3896 * Restores a pending widget.
3897 *
3898 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003899 */
Sunny Goyald478c832016-04-01 12:04:16 -07003900 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003901 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3902 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3903 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003904 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003905 }
3906
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003907 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003908 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003909
3910 mWorkspace.reinflateWidgetsIfNecessary();
3911 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003912 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003913 }
3914
Adam Cohen1462de32012-07-24 22:34:36 -07003915 public void onPageBoundSynchronously(int page) {
3916 mSynchronouslyBoundPages.add(page);
3917 }
3918
Sunny Goyal527c7d32015-08-28 15:19:36 -07003919 @Override
3920 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3921 if (mPendingExecutor != null) {
3922 mPendingExecutor.markCompleted();
3923 }
3924 mPendingExecutor = executor;
3925 executor.attachTo(this);
3926 }
3927
3928 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3929 if (mPendingExecutor == executor) {
3930 mPendingExecutor = null;
3931 }
3932 }
3933
Joe Onorato9c1289c2009-08-17 11:03:03 -04003934 /**
3935 * Callback saying that there aren't any more items to bind.
3936 *
3937 * Implementation of the method from LauncherModel.Callbacks.
3938 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003939 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003940 Runnable r = new Runnable() {
3941 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003942 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003943 }
3944 };
3945 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003946 return;
3947 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003948 if (LauncherAppState.PROFILE_STARTUP) {
3949 Trace.beginSection("Page bind completed");
3950 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003951 if (mSavedState != null) {
3952 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003953 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003954 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003955
Joe Onorato9c1289c2009-08-17 11:03:03 -04003956 mSavedState = null;
3957 }
3958
Adam Cohen1462de32012-07-24 22:34:36 -07003959 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003960
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003961 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003962
3963 // If we received the result of any pending adds while the loader was running (e.g. the
3964 // widget configuration forced an orientation change), process them now.
3965 if (sPendingAddItem != null) {
3966 final long screenId = completeAdd(sPendingAddItem);
3967
3968 // TODO: this moves the user to the page where the pending item was added. Ideally,
3969 // the screen would be guaranteed to exist after bind, and the page would be set through
3970 // the workspace restore process.
3971 mWorkspace.post(new Runnable() {
3972 @Override
3973 public void run() {
3974 mWorkspace.snapToScreenId(screenId);
3975 }
3976 });
3977 sPendingAddItem = null;
3978 }
3979
Sunny Goyal756adbc2015-04-16 15:20:51 -07003980 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003981
3982 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003983 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003984 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003985 if (LauncherAppState.PROFILE_STARTUP) {
3986 Trace.endSection();
3987 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003988 }
3989
Winson Chunga2413752012-04-03 14:22:34 -07003990 private boolean canRunNewAppsAnimation() {
3991 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07003992 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
3993 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07003994 }
3995
Winson Chungc9168342013-06-26 14:54:55 -07003996 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003997 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003998 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3999 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004000 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004001 return bounceAnim;
4002 }
4003
Adam Cohen27772732013-11-11 14:00:56 +00004004 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004005 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004006 }
4007
Tony Wickham55616cd2015-09-23 14:55:17 -07004008 public int getSearchBarHeight() {
4009 if (mLauncherCallbacks != null) {
4010 return mLauncherCallbacks.getSearchBarHeight();
4011 }
4012 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4013 }
4014
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004015 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004016 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4017 * multiple calls to bind the same list.)
4018 */
4019 @Thunk ArrayList<AppInfo> mTmpAppsList;
4020 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4021 public void run() {
4022 bindAllApplications(mTmpAppsList);
4023 mTmpAppsList = null;
4024 }
4025 };
4026
4027 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004028 * Add the icons for all apps.
4029 *
4030 * Implementation of the method from LauncherModel.Callbacks.
4031 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004032 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004033 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4034 mTmpAppsList = apps;
4035 return;
4036 }
4037
Winson Chungb745afb2015-03-02 11:51:23 -08004038 if (mAppsView != null) {
4039 mAppsView.setApps(apps);
4040 }
Adam Cohen9211d422014-10-07 18:14:53 -07004041 if (mLauncherCallbacks != null) {
4042 mLauncherCallbacks.bindAllApplications(apps);
4043 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004044 }
4045
4046 /**
4047 * A package was updated.
4048 *
4049 * Implementation of the method from LauncherModel.Callbacks.
4050 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004051 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004052 Runnable r = new Runnable() {
4053 public void run() {
4054 bindAppsUpdated(apps);
4055 }
4056 };
4057 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004058 return;
4059 }
4060
Winson Chungb745afb2015-03-02 11:51:23 -08004061 if (mAppsView != null) {
4062 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004063 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004064 }
4065
Sunny Goyal4390ace2014-10-13 11:33:11 -07004066 @Override
4067 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4068 Runnable r = new Runnable() {
4069 public void run() {
4070 bindWidgetsRestored(widgets);
4071 }
4072 };
4073 if (waitUntilResume(r)) {
4074 return;
4075 }
4076 mWorkspace.widgetsRestored(widgets);
4077 }
4078
Joe Onorato9c1289c2009-08-17 11:03:03 -04004079 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004080 * Some shortcuts were updated in the background.
4081 *
4082 * Implementation of the method from LauncherModel.Callbacks.
4083 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004084 @Override
4085 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4086 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004087 Runnable r = new Runnable() {
4088 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004089 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004090 }
4091 };
4092 if (waitUntilResume(r)) {
4093 return;
4094 }
4095
Sunny Goyal4390ace2014-10-13 11:33:11 -07004096 if (!updated.isEmpty()) {
4097 mWorkspace.updateShortcuts(updated);
4098 }
4099
4100 if (!removed.isEmpty()) {
4101 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4102 for (ShortcutInfo si : removed) {
4103 removedComponents.add(si.getTargetComponent());
4104 }
4105 mWorkspace.removeItemsByComponentName(removedComponents, user);
4106 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004107 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004108 }
4109 }
4110
4111 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004112 * Update the state of a package, typically related to install state.
4113 *
4114 * Implementation of the method from LauncherModel.Callbacks.
4115 */
Sunny Goyale755d462014-07-22 13:48:29 -07004116 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004117 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4118 Runnable r = new Runnable() {
4119 public void run() {
4120 bindRestoreItemsChange(updates);
4121 }
4122 };
4123 if (waitUntilResume(r)) {
4124 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004125 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004126
Sunny Goyal756adbc2015-04-16 15:20:51 -07004127 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004128 }
4129
4130 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004131 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004132 * in addition to specific applications to remove, the reason being that
4133 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004134 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004135 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004136 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004137 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004138 public void bindWorkspaceComponentsRemoved(
4139 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4140 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004141 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004142 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004143 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004144 }
Winson Chungd64d1762013-08-20 14:37:16 -07004145 };
4146 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004147 return;
4148 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004149 if (!packageNames.isEmpty()) {
4150 mWorkspace.removeItemsByPackageName(packageNames, user);
4151 }
4152 if (!components.isEmpty()) {
4153 mWorkspace.removeItemsByComponentName(components, user);
4154 }
4155 // Notify the drag controller
4156 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004157
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004158 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004159
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004160 @Override
4161 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4162 Runnable r = new Runnable() {
4163 public void run() {
4164 bindAppInfosRemoved(appInfos);
4165 }
4166 };
4167 if (waitUntilResume(r)) {
4168 return;
Joe Onorato36115782010-06-17 13:28:48 -04004169 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004170
Winson Chungdf95eb12013-10-16 14:57:07 -07004171 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004172 if (mAppsView != null) {
4173 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004174 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004175 }
Joe Onoratobe386092009-11-17 17:32:16 -08004176
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004177 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004178 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004179 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004180 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004181 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004182
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004183 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004184 public void bindWidgetsModel(WidgetsModel model) {
4185 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004186 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004187 return;
4188 }
4189
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004190 if (mWidgetsView != null && model != null) {
4191 mWidgetsView.addWidgets(model);
4192 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004193 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004194 }
4195
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004196 @Override
4197 public void notifyWidgetProvidersChanged() {
4198 if (mWorkspace.getState().shouldUpdateWidget) {
4199 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4200 }
4201 }
4202
Winson Chung400438b2011-01-16 17:53:48 -08004203 private int mapConfigurationOriActivityInfoOri(int configOri) {
4204 final Display d = getWindowManager().getDefaultDisplay();
4205 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4206 switch (d.getRotation()) {
4207 case Surface.ROTATION_0:
4208 case Surface.ROTATION_180:
4209 // We are currently in the same basic orientation as the natural orientation
4210 naturalOri = configOri;
4211 break;
4212 case Surface.ROTATION_90:
4213 case Surface.ROTATION_270:
4214 // We are currently in the other basic orientation to the natural orientation
4215 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4216 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4217 break;
4218 }
4219
4220 int[] oriMap = {
4221 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4222 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4223 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4224 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4225 };
4226 // Since the map starts at portrait, we need to offset if this device's natural orientation
4227 // is landscape.
4228 int indexOffset = 0;
4229 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4230 indexOffset = 1;
4231 }
4232 return oriMap[(d.getRotation() + indexOffset) % 4];
4233 }
Adam Cohen446e9402011-09-15 18:21:21 -07004234
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004235 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004236 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004237 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004238 if (Utilities.ATLEAST_JB_MR2) {
4239 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4240 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004241 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4242 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004243 }
Winson Chung4b919f82012-05-01 10:44:08 -07004244 }
4245 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004246
Winson Chung4b919f82012-05-01 10:44:08 -07004247 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004248 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004249 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004250 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004251 } else {
4252 mHandler.postDelayed(new Runnable() {
4253 public void run() {
4254 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4255 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004256 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004257 }
Winson Chung4b919f82012-05-01 10:44:08 -07004258 }
Winson Chung400438b2011-01-16 17:53:48 -08004259 }
4260
Adam Cohenea90f832014-05-21 15:03:34 -07004261 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004262 * To be overridden by subclasses to indicate that there is an activity to launch
4263 * before showing the standard launcher experience.
4264 */
4265 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004266 if (mLauncherCallbacks != null) {
4267 return mLauncherCallbacks.hasFirstRunActivity();
4268 }
Adam Cohen432609a2014-03-13 17:03:22 -07004269 return false;
4270 }
4271
4272 /**
4273 * To be overridden by subclasses to launch any first run activity
4274 */
4275 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004276 if (mLauncherCallbacks != null) {
4277 return mLauncherCallbacks.getFirstRunActivity();
4278 }
Adam Cohen432609a2014-03-13 17:03:22 -07004279 return null;
4280 }
4281
Winson Chunga6945242014-01-08 14:04:34 -08004282 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004283 return !ActivityManager.isRunningInTestHarness() &&
4284 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004285 }
4286
Adam Cohen4a9423d2014-03-28 14:22:31 -07004287 protected boolean hasRunFirstRunActivity() {
4288 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4289 }
4290
Adam Cohen432609a2014-03-13 17:03:22 -07004291 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004292 if (shouldRunFirstRunActivity() &&
4293 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004294 Intent firstRunIntent = getFirstRunActivity();
4295 if (firstRunIntent != null) {
4296 startActivity(firstRunIntent);
4297 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004298 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004299 }
4300 }
Adam Cohen432609a2014-03-13 17:03:22 -07004301 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004302 }
4303
4304 private void markFirstRunActivityShown() {
4305 SharedPreferences.Editor editor = mSharedPrefs.edit();
4306 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4307 editor.apply();
4308 }
4309
Adam Cohen432609a2014-03-13 17:03:22 -07004310 /**
4311 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4312 * screen that must be displayed and dismissed.
4313 */
4314 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004315 if (mLauncherCallbacks != null) {
4316 return mLauncherCallbacks.hasDismissableIntroScreen();
4317 }
Adam Cohen432609a2014-03-13 17:03:22 -07004318 return false;
4319 }
4320
4321 /**
4322 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4323 */
4324 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004325 if (mLauncherCallbacks != null) {
4326 return mLauncherCallbacks.getIntroScreen();
4327 }
Adam Cohen432609a2014-03-13 17:03:22 -07004328 return null;
4329 }
4330
4331 /**
4332 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4333 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4334 */
4335 private boolean shouldShowIntroScreen() {
4336 return hasDismissableIntroScreen() &&
4337 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4338 }
4339
4340 protected void showIntroScreen() {
4341 View introScreen = getIntroScreen();
4342 changeWallpaperVisiblity(false);
4343 if (introScreen != null) {
4344 mDragLayer.showOverlayView(introScreen);
4345 }
4346 }
4347
4348 public void dismissIntroScreen() {
4349 markIntroScreenDismissed();
4350 if (showFirstRunActivity()) {
4351 // We delay hiding the intro view until the first run activity is showing. This
4352 // avoids a blip.
4353 mWorkspace.postDelayed(new Runnable() {
4354 @Override
4355 public void run() {
4356 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004357 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004358 }
4359 }, ACTIVITY_START_DELAY);
4360 } else {
4361 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004362 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004363 }
4364 changeWallpaperVisiblity(true);
4365 }
4366
4367 private void markIntroScreenDismissed() {
4368 SharedPreferences.Editor editor = mSharedPrefs.edit();
4369 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4370 editor.apply();
4371 }
4372
Adam Cohen091440a2015-03-18 14:16:05 -07004373 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004374 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4375 // on the device, then we always show the first run cling experience (or if there is no
4376 // launcher2). Otherwise, we prompt the user upon started for migration
4377 LauncherClings launcherClings = new LauncherClings(this);
4378 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004379 mClings = launcherClings;
Sunny Goyalded0fdb2016-05-23 15:55:41 -07004380 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004381 }
4382 }
4383
Winson Chung5ffd51d2015-06-25 11:36:50 -07004384 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004385 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004386 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004387 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004388 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004389 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004390 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4391 if (userHandle != null) {
4392 user = UserHandleCompat.fromUser(userHandle);
4393 }
4394 }
4395 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004396 }
4397
4398 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004399 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004400 if (user == null) {
4401 user = UserHandleCompat.myUserHandle();
4402 }
4403
4404 // Called from search suggestion, add the profile extra to the intent to ensure that we
4405 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004406 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004407 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004408 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004409 return null;
4410 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004411 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004412 }
4413
Winson Chung5ffd51d2015-06-25 11:36:50 -07004414 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004415 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4416 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004417 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004418 UserHandleCompat.myUserHandle());
4419 }
4420
Winson Chung5ffd51d2015-06-25 11:36:50 -07004421 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004422 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4423 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004424 mWorkspace.onExternalDragStartedWithItem(dragView);
4425 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004426 }
4427
Winson Chung5ffd51d2015-06-25 11:36:50 -07004428 protected void moveWorkspaceToDefaultScreen() {
4429 mWorkspace.moveToDefaultScreen(false);
4430 }
4431
Winson Chung80baf5a2010-08-09 16:03:15 -07004432 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004433 * Returns a FastBitmapDrawable with the icon, accurately sized.
4434 */
4435 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4436 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4437 d.setFilterBitmap(true);
4438 resizeIconDrawable(d);
4439 return d;
4440 }
4441
4442 /**
4443 * Resizes an icon drawable to the correct icon size.
4444 */
Winson5fbe0742015-09-30 15:33:00 -07004445 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004446 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004447 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004448 }
4449
4450 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004451 * Prints out out state for debugging.
4452 */
4453 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004454 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004455 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004456 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4457 Log.d(TAG, "mRestoring=" + mRestoring);
4458 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004459 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004460 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004461
Daniel Sandler325dc232013-06-05 22:57:57 -04004462 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004463 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004464
4465 @Override
4466 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4467 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004468 // Dump workspace
4469 writer.println(prefix + "Workspace Items");
4470 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4471 writer.println(prefix + " Homescreen " + i);
4472
4473 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4474 for (int j = 0; j < layout.getChildCount(); j++) {
4475 Object tag = layout.getChildAt(j).getTag();
4476 if (tag != null) {
4477 writer.println(prefix + " " + tag.toString());
4478 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004479 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004480 }
Sunny Goyala1365452015-10-01 15:46:24 -07004481
4482 writer.println(prefix + " Hotseat");
4483 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4484 for (int j = 0; j < layout.getChildCount(); j++) {
4485 Object tag = layout.getChildAt(j).getTag();
4486 if (tag != null) {
4487 writer.println(prefix + " " + tag.toString());
4488 }
4489 }
4490
Sunny Goyal713edfc2016-05-06 09:58:34 -07004491 try {
4492 FileLog.flushAll(writer);
4493 } catch (Exception e) {
4494 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004495 }
4496
Adam Cohen9211d422014-10-07 18:14:53 -07004497 if (mLauncherCallbacks != null) {
4498 mLauncherCallbacks.dump(prefix, fd, writer, args);
4499 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004500 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004501
Adam Cohen59400422014-03-05 18:07:04 -08004502 public static CustomAppWidget getCustomAppWidget(String name) {
4503 return sCustomAppWidgets.get(name);
4504 }
4505
4506 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4507 return sCustomAppWidgets;
4508 }
4509
Sunny Goyal29538332016-02-18 09:10:19 -08004510 public static List<View> getFolderContents(View icon) {
4511 if (icon instanceof FolderIcon) {
4512 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4513 } else {
4514 return Collections.EMPTY_LIST;
4515 }
4516 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004517
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004518 public static Launcher getLauncher(Context context) {
4519 if (context instanceof Launcher) {
4520 return (Launcher) context;
4521 }
4522 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4523 }
4524
Sunny Goyal745bad92016-05-02 10:54:12 -07004525 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4526
4527 @Override
4528 public void onSharedPreferenceChanged(
4529 SharedPreferences sharedPreferences, String key) {
4530 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4531 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4532 if (!waitUntilResume(this, true)) {
4533 run();
4534 }
4535 }
4536 }
4537
4538 @Override
4539 public void run() {
4540 setOrientation();
4541 }
4542 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004543}