blob: 156c1b0b013aa0b805a4818c98addc860d539c58 [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;
Chris Wren40c5ed32014-06-24 18:24:23 -040041import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070044import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070046import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070050import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070053import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070056import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020059import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020061import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080062import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070063import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070064import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070066import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.HapticFeedbackConstants;
74import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.MotionEvent;
77import android.view.Surface;
78import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070079import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080080import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070081import android.view.ViewGroup;
82import android.view.ViewTreeObserver;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080084import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070085import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070086import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080090import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070092
Sunny Goyal651077b2014-06-30 14:15:31 -070093import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070094import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyalae502842016-06-17 08:43:56 -070095import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070096import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070097import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080098import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -070099import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100100import com.android.launcher3.compat.LauncherActivityInfoCompat;
101import com.android.launcher3.compat.LauncherAppsCompat;
102import com.android.launcher3.compat.UserHandleCompat;
103import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800104import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700105import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700106import com.android.launcher3.dragndrop.DragController;
107import com.android.launcher3.dragndrop.DragLayer;
108import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -0700109import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000110import com.android.launcher3.folder.Folder;
111import com.android.launcher3.folder.FolderIcon;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700112import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700113import com.android.launcher3.logging.FileLog;
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800114import com.android.launcher3.logging.LoggerUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700115import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700116import com.android.launcher3.model.WidgetsModel;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700117import com.android.launcher3.pageindicators.PageIndicator;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800118import com.android.launcher3.userevent.nano.LauncherLogProto;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700119import com.android.launcher3.util.ComponentKey;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700120import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal322d5562015-06-25 19:35:49 -0700121import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700122import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700123import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700124import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700125import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700126import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700127
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700128import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700129import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700130import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700131import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800132import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700133import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700134import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700135import java.util.List;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800136import java.util.Locale;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700137
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138/**
139 * Default launcher application.
140 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100141public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700142 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
143 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700144 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700145 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700146 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Daniel Sandlerf061f822013-06-27 13:59:36 -0400148 static final boolean PROFILE_STARTUP = false;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700149 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700150 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400151 static final boolean DEBUG_RESUME_TIME = false;
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700152 static final boolean DEBUG_LOGGING = 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 }
400
Adam Cohen9211d422014-10-07 18:14:53 -0700401 if (mLauncherCallbacks != null) {
402 mLauncherCallbacks.preOnCreate();
403 }
404
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800405 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400406
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400407 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700408
Adam Cohen2e6da152015-05-06 11:42:25 -0700409 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700410 mDeviceProfile = getResources().getConfiguration().orientation
411 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700412 app.getInvariantDeviceProfile().landscapeProfile
413 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700414
Sunny Goyalf7258242015-10-19 16:59:07 -0700415 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700416 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800417 mModel = app.setLauncher(this);
418 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700419 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700420
Joe Onorato41a12d22009-10-31 18:30:00 -0400421 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700422 mAllAppsController = new AllAppsTransitionController(this);
423 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700424
Sunny Goyalffe83f12014-08-14 17:39:34 -0700425 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700426
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700427 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
428 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700429
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700430 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
431 // this also ensures that any synchronous binding below doesn't re-trigger another
432 // LauncherModel load.
433 mPaused = false;
434
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800435 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200436 android.os.Debug.startMethodTracing(
437 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 }
439
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700441
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700443 mDeviceProfile.layout(this);
Tony Wickham827cef22016-03-17 15:39:39 -0700444 mExtractedColors = new ExtractedColors();
445 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446
Tony Wickhame2217252016-03-22 16:34:23 -0700447 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
448 .addAccessibilityStateChangeListener(this);
449
Joe Onorato7c312c12009-08-13 21:36:53 -0700450 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700451
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 mSavedState = savedInstanceState;
453 restoreState(mSavedState);
454
455 if (PROFILE_STARTUP) {
456 android.os.Debug.stopMethodTracing();
457 }
458
459 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700460 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700461 // If the user leaves launcher, then we should just load items asynchronously when
462 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700463 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700464 } else {
465 // We only load the page synchronously if the user rotates (or triggers a
466 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700467 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700468 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 }
470
471 // For handling default keys
472 mDefaultKeySsb = new SpannableStringBuilder();
473 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800474
475 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700476 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
477 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800478
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800479 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700480 // In case we are on a device with locked rotation, we should look at preferences to check
481 // if the user has specifically allowed rotation.
482 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700483 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700484 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
485 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700486 }
487
488 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
489 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400490 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700491
Adam Cohen9211d422014-10-07 18:14:53 -0700492 if (mLauncherCallbacks != null) {
493 mLauncherCallbacks.onCreate(savedInstanceState);
494 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700495
496 if (shouldShowIntroScreen()) {
497 showIntroScreen();
498 } else {
499 showFirstRunActivity();
500 showFirstRunClings();
501 }
Adam Cohen9211d422014-10-07 18:14:53 -0700502 }
503
Sunny Goyal7779d622015-06-11 16:18:39 -0700504 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700505 public void onExtractedColorsChanged() {
506 loadExtractedColorsAndColorItems();
507 }
508
509 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700510 // TODO: do this in pre-N as well, once the extraction part is complete.
511 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700512 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700513 mHotseat.updateColor(mExtractedColors, !mPaused);
Tony Wickham770217c2016-05-18 15:16:40 -0700514 if (mPageIndicator != null) {
515 mPageIndicator.updateColor(mExtractedColors);
516 }
Tony Wickham827cef22016-03-17 15:39:39 -0700517 }
518 }
519
Adam Cohen9211d422014-10-07 18:14:53 -0700520 private LauncherCallbacks mLauncherCallbacks;
521
522 public void onPostCreate(Bundle savedInstanceState) {
523 super.onPostCreate(savedInstanceState);
524 if (mLauncherCallbacks != null) {
525 mLauncherCallbacks.onPostCreate(savedInstanceState);
526 }
527 }
528
Sunny Goyal32554d12015-12-03 15:31:25 -0800529 /**
530 * Call this after onCreate to set or clear overlay.
531 */
532 public void setLauncherOverlay(LauncherOverlay overlay) {
533 if (overlay != null) {
534 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
535 }
536 mWorkspace.setLauncherOverlay(overlay);
537 }
538
Adam Cohen9211d422014-10-07 18:14:53 -0700539 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
540 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700541 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700542 private boolean mWorkspaceImportanceStored = false;
543 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700544 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800545 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700546 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
547
548 @Override
549 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700550 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700551 return;
552 }
553 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700554 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700555 if (mWorkspace != null) {
556 mWorkspaceImportanceForAccessibility =
557 mWorkspace.getImportantForAccessibility();
558 mWorkspace.setImportantForAccessibility(
559 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
560 mWorkspaceImportanceStored = true;
561 }
562 if (mHotseat != null) {
563 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
564 mHotseat.setImportantForAccessibility(
565 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
566 mHotseatImportanceStored = true;
567 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700568 }
569
570 @Override
571 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700572 if (mWorkspaceImportanceStored && mWorkspace != null) {
573 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
574 }
575 if (mHotseatImportanceStored && mHotseat != null) {
576 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
577 }
578 mWorkspaceImportanceStored = false;
579 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700580 }
581 });
Adam Cohen9211d422014-10-07 18:14:53 -0700582 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700583 }
584
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700585 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700586 public void onLauncherProviderChange() {
587 if (mLauncherCallbacks != null) {
588 mLauncherCallbacks.onLauncherProviderChange();
589 }
590 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700591
Adam Cohen9211d422014-10-07 18:14:53 -0700592 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700593 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700594 if (mLauncherCallbacks != null) {
595 return mLauncherCallbacks.hasCustomContentToLeft();
596 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700597 return false;
598 }
599
Dave Hawkeya8881582013-09-17 15:55:33 -0600600 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500601 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600602 * {@link #addToCustomContentPage}. This will only be invoked if
603 * {@link #hasCustomContentToLeft()} is {@code true}.
604 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500605 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700606 if (mLauncherCallbacks != null) {
607 mLauncherCallbacks.populateCustomContentContainer();
608 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600609 }
610
611 /**
612 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
613 * ensure the custom content page is added or removed if necessary.
614 */
615 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600616 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600617 // Not bound yet, wait for bindScreens to be called.
618 return;
619 }
620
621 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
622 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500623 mWorkspace.createCustomContentContainer();
624 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600625 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
626 mWorkspace.removeCustomContentPage();
627 }
628 }
629
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800630 /**
631 * Logger object is a singleton and does not have to be coupled with the foreground activity.
632 * Since most user event logging is done on the UI, the object is retrieved from the
633 * callback for convenience.
634 */
Hyunyoung Songaa953652016-04-19 18:30:24 -0700635 private UserEventDispatcher createUserEventDispatcher() {
636 return new UserEventDispatcher() {
637 @Override
638 public void dispatchUserEvent(LauncherLogProto.LauncherEvent ev, Intent intent) {
639 if (!DEBUG_LOGGING) {
640 return;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800641 }
Hyunyoung Songaa953652016-04-19 18:30:24 -0700642 Log.d("UserEvent", String.format(Locale.US,
643 "action:%s\nchild:%s\nparent:%s\nelapsed container %d ms session %d ms",
644 LoggerUtils.getActionStr(ev.action),
645 LoggerUtils.getTargetStr(ev.srcTarget[0]),
646 LoggerUtils.getTargetStr(ev.srcTarget[1]),
647 ev.elapsedContainerMillis,
648 ev.elapsedSessionMillis));
649 }
650 };
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800651 }
652
Hyunyoung Songaa953652016-04-19 18:30:24 -0700653 public UserEventDispatcher getUserEventDispatcher() {
654 if (mLauncherCallbacks != null) {
655 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
656 if (dispatcher != null) {
657 return dispatcher;
658 }
659 }
660
661 if (mUserEventDispatcher == null) {
662 mUserEventDispatcher = createUserEventDispatcher();
663 }
664 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800665 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100666
Hyunyoung Song3f471442015-04-08 19:01:34 -0700667 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700668 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
669 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700670 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700671 }
672
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000673 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700674 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
675 // This cast is safe as long as the id < 0x00FFFFFF
676 // Since we jail all the dynamically generated views, there should be no clashes
677 // with any other views.
678 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000679 }
680
681 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700682 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
683 * a configuration step, this allows the proper animations to run after other transitions.
684 */
Adam Cohendb364c32014-05-20 14:23:40 -0700685 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700686 long screenId = args.screenId;
687 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
688 // When the screen id represents an actual screen (as opposed to a rank) we make sure
689 // that the drop page actually exists.
690 screenId = ensurePendingDropLayoutExists(args.screenId);
691 }
Adam Cohendb364c32014-05-20 14:23:40 -0700692
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800693 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800694 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700695 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700696 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700697 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800698 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700699 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700700 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700701 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyald478c832016-04-01 12:04:16 -0700702 completeRestoreAppWidget(args.appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700703 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700704 case REQUEST_BIND_PENDING_APPWIDGET: {
705 int widgetId = args.appWidgetId;
706 LauncherAppWidgetInfo info =
707 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
708 if (info != null) {
709 // Since the view was just bound, also launch the configure activity if needed
710 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
711 .getLauncherAppWidgetInfo(widgetId);
712 if (provider != null && provider.configure != null) {
713 startRestoredWidgetReconfigActivity(provider, info);
714 }
715 }
716 break;
717 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800718 }
Michael Jurka27614d22012-04-02 06:40:22 -0700719 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
720 // if you turned the screen off and then back while in All Apps, Launcher would not
721 // return to the workspace. Clearing mAddInfo.container here fixes this issue
722 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700723 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800724 }
725
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800726 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700727 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700728 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700729 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700730 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800731 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700732
Adam Cohenad4e15c2013-10-17 16:21:35 -0700733 Runnable exitSpringLoaded = new Runnable() {
734 @Override
735 public void run() {
736 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
737 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
738 }
739 };
740
Michael Jurka8b805b12012-04-18 14:23:14 -0700741 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700742 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700743 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700744 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
745 if (resultCode == RESULT_CANCELED) {
746 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700747 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700748 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700749 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800750 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700751 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700752 }
753 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200754 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
755 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700756 // User could have free-scrolled between pages before picking a wallpaper; make sure
757 // we move to the closest one now.
758 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700759 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200760 }
761 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700762 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200763
Adam Cohened66b2b2012-01-23 17:28:51 -0800764 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700765 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700766
Adam Cohendb364c32014-05-20 14:23:40 -0700767 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800768 // We have special handling for widgets
769 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800770 final int appWidgetId;
771 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
772 : -1;
773 if (widgetId < 0) {
774 appWidgetId = pendingAddWidgetId;
775 } else {
776 appWidgetId = widgetId;
777 }
778
Adam Cohenad4e15c2013-10-17 16:21:35 -0700779 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800780 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700781 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
782 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700783 result = RESULT_CANCELED;
784 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700785 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700786 @Override
787 public void run() {
788 exitSpringLoadedDragModeDelayed(false, 0, null);
789 }
790 };
Adam Cohendb364c32014-05-20 14:23:40 -0700791 if (workspaceLocked) {
792 // No need to remove the empty screen if we're mid-binding, as the
793 // the bind will not add the empty screen.
794 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
795 } else {
796 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
797 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
798 }
Winson Chung5aaab772012-04-27 15:24:02 -0700799 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700800 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700801 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
802 // When the screen id represents an actual screen (as opposed to a rank)
803 // we make sure that the drop page actually exists.
804 mPendingAddInfo.screenId =
805 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
806 }
Adam Cohendb364c32014-05-20 14:23:40 -0700807 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
808
809 dropLayout.setDropPending(true);
810 final Runnable onComplete = new Runnable() {
811 @Override
812 public void run() {
813 completeTwoStageWidgetDrop(resultCode, appWidgetId);
814 dropLayout.setDropPending(false);
815 }
816 };
817 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
818 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
819 } else {
820 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
821 mPendingAddInfo);
822 sPendingAddItem = args;
823 }
Winson Chung5aaab772012-04-27 15:24:02 -0700824 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800825 return;
826 }
827
Sunny Goyald478c832016-04-01 12:04:16 -0700828 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
829 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700830 if (resultCode == RESULT_OK) {
831 // Update the widget view.
832 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
833 pendingAddWidgetId, mPendingAddInfo);
834 if (workspaceLocked) {
835 sPendingAddItem = args;
836 } else {
837 completeAdd(args);
838 }
839 }
840 // Leave the widget in the pending state if the user canceled the configure.
841 return;
842 }
843
Sunny Goyalaad90582015-07-09 14:22:50 -0700844 if (requestCode == REQUEST_CREATE_SHORTCUT) {
845 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
846 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
847 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
848 mPendingAddInfo);
849 if (isWorkspaceLocked()) {
850 sPendingAddItem = args;
851 } else {
852 completeAdd(args);
853 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
854 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
855 }
856 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700857 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
858 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800861 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800862
863 }
864
865 @Override
866 protected void onActivityResult(
867 final int requestCode, final int resultCode, final Intent data) {
868 handleActivityResult(requestCode, resultCode, data);
869 if (mLauncherCallbacks != null) {
870 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
871 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800872 }
873
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600874 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700875 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600876 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700877 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
878 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
879 View v = null;
880 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
881 if (layout != null) {
882 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
883 }
884 Intent intent = sPendingAddItem.intent;
885 sPendingAddItem = null;
886 if (grantResults.length > 0
887 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700888 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700889 } else {
890 // TODO: Show a snack bar with link to settings
891 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
892 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
893 }
894 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600895 if (mLauncherCallbacks != null) {
896 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
897 grantResults);
898 }
899 }
900
Adam Cohendb364c32014-05-20 14:23:40 -0700901 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
902 appWidgetId, ItemInfo info) {
903 PendingAddArguments args = new PendingAddArguments();
904 args.requestCode = requestCode;
905 args.intent = data;
906 args.container = info.container;
907 args.screenId = info.screenId;
908 args.cellX = info.cellX;
909 args.cellY = info.cellY;
910 args.appWidgetId = appWidgetId;
911 return args;
912 }
913
914 /**
915 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
916 *
917 * @param screenId the screen id to check
918 * @return the new screen, or screenId if it exists
919 */
920 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800921 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700922 if (dropLayout == null) {
923 // it's possible that the add screen was removed because it was
924 // empty and a re-bind occurred
925 mWorkspace.addExtraEmptyScreen();
926 return mWorkspace.commitExtraEmptyScreen();
927 } else {
928 return screenId;
929 }
930 }
931
Adam Cohen091440a2015-03-18 14:16:05 -0700932 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800933 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800934 Runnable onCompleteRunnable = null;
935 int animationType = 0;
936
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700937 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800938 if (resultCode == RESULT_OK) {
939 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
940 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700941 mPendingAddWidgetInfo);
942 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800943 onCompleteRunnable = new Runnable() {
944 @Override
945 public void run() {
946 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700947 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700948 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
949 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800950 }
951 };
952 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800953 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800954 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800955 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700956 if (mDragLayer.getAnimatedView() != null) {
957 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
958 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
959 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700960 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700961 // The animated view may be null in the case of a rotation during widget configuration
962 onCompleteRunnable.run();
963 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 }
965
966 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700967 protected void onStop() {
968 super.onStop();
969 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700970
971 if (mLauncherCallbacks != null) {
972 mLauncherCallbacks.onStop();
973 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700974 }
975
976 @Override
977 protected void onStart() {
978 super.onStart();
979 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700980
981 if (mLauncherCallbacks != null) {
982 mLauncherCallbacks.onStart();
983 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700984 }
985
986 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200988 long startTime = 0;
989 if (DEBUG_RESUME_TIME) {
990 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400991 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200992 }
Adam Cohen9211d422014-10-07 18:14:53 -0700993
994 if (mLauncherCallbacks != null) {
995 mLauncherCallbacks.preOnResume();
996 }
997
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700999 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001000
Winson Chung4a2afa32012-07-19 14:53:05 -07001001 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001002 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001003 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001004 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -07001005 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -07001006 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -07001007 // view after launching an app, as they may be depending on the UI to be static to
1008 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -07001009 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07001010 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08001011 } else if (mOnResumeState == State.WIDGETS) {
1012 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001013 }
1014 mOnResumeState = State.NONE;
1015
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001016 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001017 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1018 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001019
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001020 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001021 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001022 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001023 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001024 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001025 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001027 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001028 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1029 // execute them here
1030 long startTimeCallbacks = 0;
1031 if (DEBUG_RESUME_TIME) {
1032 startTimeCallbacks = System.currentTimeMillis();
1033 }
1034
Michael Jurka1e2f4652013-07-08 18:03:46 -07001035 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1036 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001037 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001038 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001039 if (DEBUG_RESUME_TIME) {
1040 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1041 (System.currentTimeMillis() - startTimeCallbacks));
1042 }
Michael Jurka447bf842013-05-15 14:52:15 +02001043 }
Michael Jurka54554252013-08-01 12:52:23 +02001044 if (mOnResumeCallbacks.size() > 0) {
1045 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1046 mOnResumeCallbacks.get(i).run();
1047 }
1048 mOnResumeCallbacks.clear();
1049 }
Winson Chunge4e50662012-01-23 14:45:13 -08001050
1051 // Reset the pressed state of icons that were locked in the press state while activities
1052 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001053 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001054 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001055 mWaitingForResume.setStayPressed(false);
1056 }
Winson Chung82963d52013-10-09 11:20:57 -07001057
Adam Cohen06dff352012-06-01 17:17:08 -07001058 // It is possible that widgets can receive updates while launcher is not in the foreground.
1059 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1060 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1061 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001062 if (!isWorkspaceLoading()) {
1063 getWorkspace().reinflateWidgetsIfNecessary();
1064 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001065
Michael Jurka447bf842013-05-15 14:52:15 +02001066 if (DEBUG_RESUME_TIME) {
1067 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1068 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001069
Adam Cohen4b66bf32015-09-18 12:15:19 -07001070 // We want to suppress callbacks about CustomContent being shown if we have just received
1071 // onNewIntent while the user was present within launcher. In that case, we post a call
1072 // to move the user to the main screen (which will occur after onResume). We don't want to
1073 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1074 // suppress here.
1075 if (mWorkspace.getCustomContentCallbacks() != null
1076 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001077 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001078 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001079 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1080 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001081 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001082 }
1083 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001084 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001085 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001086 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001087
Sunny Goyal756adbc2015-04-16 15:20:51 -07001088 if (!isWorkspaceLoading()) {
1089 // Process any items that were added while Launcher was away.
1090 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1091 }
Adam Cohen9211d422014-10-07 18:14:53 -07001092
1093 if (mLauncherCallbacks != null) {
1094 mLauncherCallbacks.onResume();
1095 }
Adam Cohen06dff352012-06-01 17:17:08 -07001096 }
1097
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001099 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001100 // Ensure that items added to Launcher are queued until Launcher returns
1101 InstallShortcutReceiver.enableInstallQueue();
1102
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001103 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001104 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001105 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001106 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001107
1108 // We call onHide() aggressively. The custom content callbacks should be able to
1109 // debounce excess onHide calls.
1110 if (mWorkspace.getCustomContentCallbacks() != null) {
1111 mWorkspace.getCustomContentCallbacks().onHide();
1112 }
Romain Guycbb89e42009-06-08 15:52:54 -07001113
Adam Cohen9211d422014-10-07 18:14:53 -07001114 if (mLauncherCallbacks != null) {
1115 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001116 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001117 }
1118
1119 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001120 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1121 // by a onResume or by scrolling otherwise.
1122 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001123
1124 // Custom content is completely hidden
1125 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001126
1127 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1128 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001129
1130 // Indicates whether the user is allowed to scroll away from the custom content.
1131 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001132 }
1133
Adam Cohenc2d6e892014-10-16 09:49:24 -07001134 public interface LauncherOverlay {
1135
1136 /**
1137 * Touch interaction leading to overscroll has begun
1138 */
1139 public void onScrollInteractionBegin();
1140
1141 /**
1142 * Touch interaction related to overscroll has ended
1143 */
1144 public void onScrollInteractionEnd();
1145
1146 /**
1147 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1148 * screen (or in the case of RTL, the rightmost screen).
1149 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001150 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001151
1152 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001153 * Called when the launcher is ready to use the overlay
1154 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001155 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001156 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001157 }
1158
Jun Mukai8af0cd82015-05-12 12:32:12 -07001159 public interface LauncherSearchCallbacks {
1160 /**
1161 * Called when the search overlay is shown.
1162 */
1163 public void onSearchOverlayOpened();
1164
1165 /**
1166 * Called when the search overlay is dismissed.
1167 */
1168 public void onSearchOverlayClosed();
1169 }
1170
Adam Cohenc2d6e892014-10-16 09:49:24 -07001171 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001172 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001173 }
1174
1175 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1176
Sunny Goyalc86df472016-02-25 09:19:38 -08001177 public void onScrollChanged(float progress) {
1178 if (mWorkspace != null) {
1179 mWorkspace.onOverlayScrollChanged(progress);
1180 }
1181 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001182 }
1183
Jorim Jaggid017f882014-01-14 17:08:48 -08001184 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001185 if (mLauncherCallbacks != null) {
1186 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001187 } else {
1188 // On devices with a locked orientation, we will at least have the allow rotation
1189 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001190 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001191 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001192 }
1193
Adam Cohen9211d422014-10-07 18:14:53 -07001194 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001195 CustomContentCallbacks callbacks, String description) {
1196 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001197 }
1198
Adam Cohenedb40762013-07-18 16:45:45 -07001199 // The custom content needs to offset its content to account for the QSB
1200 public int getTopOffsetForCustomContent() {
1201 return mWorkspace.getPaddingTop();
1202 }
1203
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001204 @Override
1205 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001206 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001207 if (mModel.isCurrentCallbacks(this)) {
1208 mModel.stopLoader();
1209 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001210 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1211
Romain Guy13c2e7b2010-03-10 19:45:00 -08001212 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001213 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001214
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001215 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001216 @Override
1217 public void onWindowFocusChanged(boolean hasFocus) {
1218 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001219 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001220
1221 if (mLauncherCallbacks != null) {
1222 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1223 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001224 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001225
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226 private boolean acceptFilter() {
1227 final InputMethodManager inputManager = (InputMethodManager)
1228 getSystemService(Context.INPUT_METHOD_SERVICE);
1229 return !inputManager.isFullscreenMode();
1230 }
1231
1232 @Override
1233 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001234 final int uniChar = event.getUnicodeChar();
1235 final boolean handled = super.onKeyDown(keyCode, event);
1236 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1237 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1239 keyCode, event);
1240 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001241 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001242 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001243 // showSearchDialog()
1244 // If there are multiple keystrokes before the search dialog takes focus,
1245 // onSearchRequested() will be called for every keystroke,
1246 // but it is idempotent, so it's fine.
1247 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 }
1249 }
1250
Joe Onorato8a9625e2010-01-28 15:55:35 -08001251 // Eat the long press event so the keyboard doesn't come up.
1252 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1253 return true;
1254 }
1255
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 return handled;
1257 }
1258
Winson46163472015-09-22 17:31:56 -07001259 @Override
1260 public boolean onKeyUp(int keyCode, KeyEvent event) {
1261 if (keyCode == KeyEvent.KEYCODE_MENU) {
1262 // Ignore the menu key if we are currently dragging or are on the custom content screen
1263 if (!isOnCustomContent() && !mDragController.isDragging()) {
1264 // Close any open folders
1265 closeFolder();
1266
1267 // Stop resizing any widgets
1268 mWorkspace.exitWidgetResizeMode();
1269
1270 // Show the overview mode if we are on the workspace
1271 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1272 !mWorkspace.isSwitchingState()) {
1273 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001274 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001275 }
1276 }
1277 return true;
1278 }
1279 return super.onKeyUp(keyCode, event);
1280 }
1281
Karl Rosaen138a0412009-04-23 19:00:21 -07001282 private String getTypedText() {
1283 return mDefaultKeySsb.toString();
1284 }
1285
1286 private void clearTypedText() {
1287 mDefaultKeySsb.clear();
1288 mDefaultKeySsb.clearSpans();
1289 Selection.setSelection(mDefaultKeySsb, 0);
1290 }
1291
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001293 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1294 * State
1295 */
1296 private static State intToState(int stateOrdinal) {
1297 State state = State.WORKSPACE;
1298 final State[] stateValues = State.values();
1299 for (int i = 0; i < stateValues.length; i++) {
1300 if (stateValues[i].ordinal() == stateOrdinal) {
1301 state = stateValues[i];
1302 break;
1303 }
1304 }
1305 return state;
1306 }
1307
1308 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 * Restores the previous state, if it exists.
1310 *
1311 * @param savedState The previous state.
1312 */
1313 private void restoreState(Bundle savedState) {
1314 if (savedState == null) {
1315 return;
1316 }
1317
Michael Jurka883f55b2010-10-21 15:47:14 -07001318 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001319 if (state == State.APPS || state == State.WIDGETS) {
1320 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001321 }
1322
Adam Cohen21cd0022013-10-09 18:57:02 -07001323 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1324 PagedView.INVALID_RESTORE_PAGE);
1325 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001326 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 }
1328
Sunny Goyal756cd262015-08-20 12:33:21 -07001329 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1330 if (itemValues != null) {
1331 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001332 AppWidgetProviderInfo info = savedState.getParcelable(
1333 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001334 mPendingAddWidgetInfo = info == null ?
1335 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1336
Adam Cohen4637b5a2013-11-04 18:21:24 -08001337 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001338 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339 mRestoring = true;
1340 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 }
1342
1343 /**
1344 * Finds all the views we need and configure them properly.
1345 */
1346 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001347 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001348 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001349 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
1350
Winson Chung4c98d922011-05-31 16:50:48 -07001351 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Hyunyoung Song7d2fc812016-06-15 12:51:30 -07001352 mPageIndicator = (PageIndicator) mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001353
Sunny Goyal784f9c32016-03-23 16:56:54 -07001354 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1355 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1356 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001357 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358
Winson Chung4c98d922011-05-31 16:50:48 -07001359 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001360
1361 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001362
Winson Chung3d503fb2011-07-13 17:25:49 -07001363 // Setup the hotseat
1364 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1365 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001366 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001367 }
1368
Winsone9f27272015-10-13 10:47:51 -07001369 // Setup the overview panel
1370 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001371
Winson Chung4c98d922011-05-31 16:50:48 -07001372 // Setup the workspace
1373 mWorkspace.setHapticFeedbackEnabled(false);
1374 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001375 mWorkspace.setup(mDragController);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001376 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001377 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001378
Tony Wickham34d2c912015-09-11 09:27:58 -07001379 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001380 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001381
Winson Chungef7f8742015-06-04 17:18:17 -07001382 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001383 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001384 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001385 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1386 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1387 } else {
1388 mAppsView.setSearchBarController(new DefaultAppSearchController());
1389 }
Craig Mautner360310b2012-10-26 15:13:08 -07001390
Winson Chung3d503fb2011-07-13 17:25:49 -07001391 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001392 mDragController.setDragScoller(mWorkspace);
1393 mDragController.setScrollView(mDragLayer);
1394 mDragController.setMoveTarget(mWorkspace);
1395 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001396 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001397
Sunny Goyal322d5562015-06-25 19:35:49 -07001398 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1399 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001400 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 }
1402
Winsone9f27272015-10-13 10:47:51 -07001403 private void setupOverviewPanel() {
1404 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1405
1406 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1407 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1408 @Override
1409 public boolean onLongClick(View v) {
1410 return v.performClick();
1411 }
1412 };
1413
1414 // Bind wallpaper button actions
1415 View wallpaperButton = findViewById(R.id.wallpaper_button);
1416 wallpaperButton.setOnClickListener(new OnClickListener() {
1417 @Override
1418 public void onClick(View view) {
1419 if (!mWorkspace.isSwitchingState()) {
1420 onClickWallpaperPicker(view);
1421 }
1422 }
1423 });
1424 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1425 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1426
1427 // Bind widget button actions
1428 mWidgetsButton = findViewById(R.id.widget_button);
1429 mWidgetsButton.setOnClickListener(new OnClickListener() {
1430 @Override
1431 public void onClick(View view) {
1432 if (!mWorkspace.isSwitchingState()) {
1433 onClickAddWidgetButton(view);
1434 }
1435 }
1436 });
1437 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1438 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1439
1440 // Bind settings actions
1441 View settingsButton = findViewById(R.id.settings_button);
1442 boolean hasSettings = hasSettings();
1443 if (hasSettings) {
1444 settingsButton.setOnClickListener(new OnClickListener() {
1445 @Override
1446 public void onClick(View view) {
1447 if (!mWorkspace.isSwitchingState()) {
1448 onClickSettingsButton(view);
1449 }
1450 }
1451 });
1452 settingsButton.setOnLongClickListener(performClickOnLongClick);
1453 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1454 } else {
1455 settingsButton.setVisibility(View.GONE);
1456 }
1457
1458 mOverviewPanel.setAlpha(0f);
1459 }
1460
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001462 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001463 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001464 */
1465 public void setAllAppsButton(View allAppsButton) {
1466 mAllAppsButton = allAppsButton;
1467 }
1468
Sunny Goyalbb011da2016-06-15 15:42:29 -07001469 public View getStartViewForAllAppsRevealAnimation() {
1470 return FeatureFlags.NO_ALL_APPS_ICON ? mPageIndicator : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001471 }
1472
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001473 public View getWidgetsButton() {
1474 return mWidgetsButton;
1475 }
1476
Anjali Koppal5ad44842014-03-10 20:34:39 -07001477 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 * Creates a view representing a shortcut.
1479 *
1480 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001482 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001483 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 }
1485
1486 /**
1487 * Creates a view representing a shortcut inflated from the specified resource.
1488 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 * @param parent The group the shortcut belongs to.
1490 * @param info The data structure describing the shortcut.
1491 *
1492 * @return A View inflated from layoutResId.
1493 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001494 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001495 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001496 parent, false);
1497 favorite.applyFromShortcutInfo(info, mIconCache);
1498 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001500 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 return favorite;
1502 }
1503
1504 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 * Add a shortcut to the workspace.
1506 *
1507 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001509 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001510 int cellY) {
1511 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001512 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001513
Sunny Goyal5c97f512015-05-19 16:03:28 -07001514 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001515 if (info == null) {
1516 return;
1517 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001518 final View view = createShortcut(info);
1519
Sunny Goyal5c97f512015-05-19 16:03:28 -07001520 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001521 // First we check if we already know the exact location where we want to add this item.
1522 if (cellX >= 0 && cellY >= 0) {
1523 cellXY[0] = cellX;
1524 cellXY[1] = cellY;
1525 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001526
1527 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001528 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001529 true, null,null)) {
1530 return;
1531 }
1532 DragObject dragObject = new DragObject();
1533 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001534 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1535 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001536 return;
1537 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001538 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001539 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001540 }
1541
1542 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001543 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001544 return;
1545 }
1546
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001547 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548
1549 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001550 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001551 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 }
1553 }
1554
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001556 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001558 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 */
Adam Cohen091440a2015-03-18 14:16:05 -07001560 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001561 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1562
1563 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001564 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001565 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001566 }
Romain Guycbb89e42009-06-08 15:52:54 -07001567
Adam Cohen59400422014-03-05 18:07:04 -08001568 if (appWidgetInfo.isCustomWidget) {
1569 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001570 }
1571
Adam Cohen59400422014-03-05 18:07:04 -08001572 LauncherAppWidgetInfo launcherInfo;
1573 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1574 launcherInfo.spanX = info.spanX;
1575 launcherInfo.spanY = info.spanY;
1576 launcherInfo.minSpanX = info.minSpanX;
1577 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001578 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001579
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001581 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582
1583 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001584 if (hostView == null) {
1585 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001586 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001587 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001588 hostView.setVisibility(View.VISIBLE);
1589 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001591 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 }
Romain Guycbb89e42009-06-08 15:52:54 -07001593
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001594 private void addAppWidgetToWorkspace(
1595 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001596 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001597 hostView.setTag(item);
1598 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001599
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001600 hostView.setFocusable(true);
1601 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001602
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001603 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001604 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1605
1606 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001607 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001608 }
1609 }
1610
Adam Cohended9f8d2010-11-03 13:25:16 -07001611 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1612 @Override
1613 public void onReceive(Context context, Intent intent) {
1614 final String action = intent.getAction();
1615 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1616 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001617 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001618 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001619
Winson Chungc100e8e2011-08-09 16:02:43 -07001620 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001621 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001622 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001623 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001624 if (!showWorkspace(false)) {
1625 // If we are already on the workspace, then manually reset all apps
1626 mAppsView.reset();
1627 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001628 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001629 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1630 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001631 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001632 }
1633 }
1634 };
1635
1636 @Override
1637 public void onAttachedToWindow() {
1638 super.onAttachedToWindow();
1639
1640 // Listen for broadcasts related to user-presence
1641 final IntentFilter filter = new IntentFilter();
1642 filter.addAction(Intent.ACTION_SCREEN_OFF);
1643 filter.addAction(Intent.ACTION_USER_PRESENT);
1644 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001645 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001646 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001647 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001648
1649 if (mLauncherCallbacks != null) {
1650 mLauncherCallbacks.onAttachedToWindow();
1651 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001652 }
1653
1654 @Override
1655 public void onDetachedFromWindow() {
1656 super.onDetachedFromWindow();
1657 mVisible = false;
1658
Adam Cohend113e0c2010-11-11 10:48:05 -08001659 if (mAttached) {
1660 unregisterReceiver(mReceiver);
1661 mAttached = false;
1662 }
Winson Chungb745afb2015-03-02 11:51:23 -08001663 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001664
1665 if (mLauncherCallbacks != null) {
1666 mLauncherCallbacks.onDetachedFromWindow();
1667 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001668 }
1669
1670 public void onWindowVisibilityChanged(int visibility) {
1671 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001672 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001673 // The following code used to be in onResume, but it turns out onResume is called when
1674 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1675 // is a more appropriate event to handle
1676 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001677 if (!mWorkspaceLoading) {
1678 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001679 // We want to let Launcher draw itself at least once before we force it to build
1680 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001681 // apps is nice and speedy.
1682 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001683 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001684 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001685 if (mStarted) return;
1686 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001687 // We delay the layer building a bit in order to give
1688 // other message processing a time to run. In particular
1689 // this avoids a delay in hiding the IME if it was
1690 // currently shown, because doing that may involve
1691 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001692 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001693 final ViewTreeObserver.OnDrawListener listener = this;
1694 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001695 public void run() {
1696 if (mWorkspace != null &&
1697 mWorkspace.getViewTreeObserver() != null) {
1698 mWorkspace.getViewTreeObserver().
1699 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001700 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001701 }
1702 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001703 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001704 }
1705 });
1706 }
1707 clearTypedText();
1708 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001709 }
1710
Adam Cohen091440a2015-03-18 14:16:05 -07001711 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001712 mHandler.removeMessages(ADVANCE_MSG);
1713 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1714 mHandler.sendMessageDelayed(msg, delay);
1715 mAutoAdvanceSentTime = System.currentTimeMillis();
1716 }
1717
Adam Cohen091440a2015-03-18 14:16:05 -07001718 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001719 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1720 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1721 mAutoAdvanceRunning = autoAdvanceRunning;
1722 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001723 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001724 sendAdvanceMessage(delay);
1725 } else {
1726 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001727 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001728 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1729 }
1730 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001731 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001732 }
1733 }
1734 }
1735
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001736 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1737
Adam Cohended9f8d2010-11-03 13:25:16 -07001738 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001739 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001740 if (msg.what == ADVANCE_MSG) {
1741 int i = 0;
1742 for (View key: mWidgetsToAdvance.keySet()) {
1743 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001744 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001745 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001746 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001747 public void run() {
1748 ((Advanceable) v).advance();
1749 }
1750 }, delay);
1751 }
1752 i++;
1753 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001754 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001755 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001756 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001757 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001758 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001759
Winsonc0b52fe2015-09-09 16:38:15 -07001760 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001761 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001762 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1763 if (v instanceof Advanceable) {
1764 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001765 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001766 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001767 }
1768 }
1769
Winsonc0b52fe2015-09-09 16:38:15 -07001770 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001771 if (mWidgetsToAdvance.containsKey(hostView)) {
1772 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001773 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001774 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001775 }
1776
Hyunyoung Song3f471442015-04-08 19:01:34 -07001777 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001778 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1779 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001780 }
1781
Winson Chunga6945242014-01-08 14:04:34 -08001782 public DragLayer getDragLayer() {
1783 return mDragLayer;
1784 }
1785
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001786 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001787 return mAppsView;
1788 }
1789
Hyunyoung Song3f471442015-04-08 19:01:34 -07001790 public WidgetsContainerView getWidgetsView() {
1791 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001792 }
1793
Winson Chunga6945242014-01-08 14:04:34 -08001794 public Workspace getWorkspace() {
1795 return mWorkspace;
1796 }
1797
1798 public Hotseat getHotseat() {
1799 return mHotseat;
1800 }
1801
Jorim Jaggid017f882014-01-14 17:08:48 -08001802 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001803 return mOverviewPanel;
1804 }
1805
Sunny Goyal47328fd2016-05-25 18:56:41 -07001806 public DropTargetBar getDropTargetBar() {
1807 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001808 }
1809
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001810 public LauncherAppWidgetHost getAppWidgetHost() {
1811 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 }
Romain Guycbb89e42009-06-08 15:52:54 -07001813
Winson Chunga9abd0e2010-10-27 17:18:37 -07001814 public LauncherModel getModel() {
1815 return mModel;
1816 }
1817
Adam Cohen79d90c52016-04-22 13:29:20 -07001818 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001819 return mSharedPrefs;
1820 }
1821
Adam Cohen2e6da152015-05-06 11:42:25 -07001822 public DeviceProfile getDeviceProfile() {
1823 return mDeviceProfile;
1824 }
1825
Bjorn Bringertc459e522013-06-07 19:36:01 +01001826 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001827 getWindow().closeAllPanels();
1828
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001829 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001830 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001831 }
1832
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833 @Override
1834 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001835 long startTime = 0;
1836 if (DEBUG_RESUME_TIME) {
1837 startTime = System.currentTimeMillis();
1838 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001839 super.onNewIntent(intent);
1840
1841 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001842 Folder openFolder = mWorkspace.getOpenFolder();
1843 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1844 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1845 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1846 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1847 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001848 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001849 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001850
Adam Cohen6fecd412013-10-02 17:41:50 -07001851 if (mWorkspace == null) {
1852 // Can be cases where mWorkspace is null, this prevents a NPE
1853 return;
1854 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001855 // In all these cases, only animate if we're already on home
1856 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001857
Sunny Goyal935fca12015-10-13 10:19:01 -07001858 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001859 exitSpringLoadedDragMode();
1860
1861 // If we are already on home, then just animate back to the workspace,
1862 // otherwise, just wait until onResume to set the state back to Workspace
1863 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001864 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001865 } else {
1866 mOnResumeState = State.WORKSPACE;
1867 }
1868
1869 final View v = getWindow().peekDecorView();
1870 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001871 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001872 INPUT_METHOD_SERVICE);
1873 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1874 }
1875
Winson Chungb745afb2015-03-02 11:51:23 -08001876 // Reset the apps view
1877 if (!alreadyOnHome && mAppsView != null) {
1878 mAppsView.scrollToTop();
1879 }
1880
Hyunyoung Song3f471442015-04-08 19:01:34 -07001881 // Reset the widgets view
1882 if (!alreadyOnHome && mWidgetsView != null) {
1883 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001884 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001885
Adam Cohen9211d422014-10-07 18:14:53 -07001886 if (mLauncherCallbacks != null) {
1887 mLauncherCallbacks.onHomeIntent();
1888 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 }
Adam Cohened307df2013-10-02 09:37:31 -07001890
Adam Cohen9211d422014-10-07 18:14:53 -07001891 if (mLauncherCallbacks != null) {
1892 mLauncherCallbacks.onNewIntent(intent);
1893 }
Winson15f8b172015-08-19 11:02:39 -07001894
1895 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1896 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1897 // animation.
1898 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001899 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1900 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001901 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1902 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001903
1904 // We use this flag to suppress noisy callbacks above custom content state
1905 // from onResume.
1906 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001907 mWorkspace.post(new Runnable() {
1908 @Override
1909 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001910 if (mWorkspace != null) {
1911 mWorkspace.moveToDefaultScreen(true);
1912 }
Winson15f8b172015-08-19 11:02:39 -07001913 }
1914 });
1915 }
1916 }
1917
1918 if (DEBUG_RESUME_TIME) {
1919 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1920 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001921 }
1922
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001924 public void onRestoreInstanceState(Bundle state) {
1925 super.onRestoreInstanceState(state);
1926 for (int page: mSynchronouslyBoundPages) {
1927 mWorkspace.restoreInstanceStateForChild(page);
1928 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 }
1930
1931 @Override
1932 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001933 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001934 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1935 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001936
Adam Cohen21cd0022013-10-09 18:57:02 -07001937 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001938 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001939
Michael Jurka883f55b2010-10-21 15:47:14 -07001940 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001941 // We close any open folder since it will not be re-opened, and we need to make sure
1942 // this state is reflected.
1943 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1944 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945
Adam Cohendcd297f2013-06-18 13:13:40 -07001946 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001947 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001948 ContentValues itemValues = new ContentValues();
1949 mPendingAddInfo.writeToValues(itemValues);
1950 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001951 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001952 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953 }
1954
Adam Cohen9211d422014-10-07 18:14:53 -07001955 if (mLauncherCallbacks != null) {
1956 mLauncherCallbacks.onSaveInstanceState(outState);
1957 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 }
1959
1960 @Override
1961 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001963
Winson Chunge7a03942011-08-05 15:05:12 -07001964 // Remove all pending runnables
1965 mHandler.removeMessages(ADVANCE_MSG);
1966 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001967 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001968 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001969
Winson Chungcd2b0142011-06-08 16:02:26 -07001970 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001971 // It's possible to receive onDestroy after a new Launcher activity has
1972 // been created. In this case, don't interfere with the new Launcher.
1973 if (mModel.isCurrentCallbacks(this)) {
1974 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001975 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001976 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001977
Sunny Goyal745bad92016-05-02 10:54:12 -07001978 if (mRotationPrefChangeHandler != null) {
1979 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1980 }
1981
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001983 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001985 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001987 mAppWidgetHost = null;
1988
1989 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990
1991 TextKeyListener.getInstance().release();
1992
Tony Wickhame2217252016-03-22 16:34:23 -07001993 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1994 .removeAccessibilityStateChangeListener(this);
1995
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001996 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001997
Michael Jurka2ecf9952012-06-18 12:52:28 -07001998 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001999
2000 if (mLauncherCallbacks != null) {
2001 mLauncherCallbacks.onDestroy();
2002 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 }
2004
Sunny Goyalae502842016-06-17 08:43:56 -07002005 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
2006 return mAccessibilityDelegate;
2007 }
2008
Adam Cohena9cf38f2011-05-02 15:36:58 -07002009 public DragController getDragController() {
2010 return mDragController;
2011 }
2012
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013 @Override
2014 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002015 onStartForResult(requestCode);
2016 super.startActivityForResult(intent, requestCode);
2017 }
2018
2019 @Override
2020 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2021 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2022 onStartForResult(requestCode);
2023 try {
2024 super.startIntentSenderForResult(intent, requestCode,
2025 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2026 } catch (IntentSender.SendIntentException e) {
2027 throw new ActivityNotFoundException();
2028 }
2029 }
2030
2031 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002032 if (requestCode >= 0) {
2033 setWaitingForResult(true);
2034 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035 }
2036
Winson Chung70d72102011-08-12 11:24:35 -07002037 /**
2038 * Indicates that we want global search for this activity by setting the globalSearch
2039 * argument for {@link #startSearch} to true.
2040 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002042 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002044
Karl Rosaen138a0412009-04-23 19:00:21 -07002045 if (initialQuery == null) {
2046 // Use any text typed in the launcher as the initial query
2047 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002048 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049 if (appSearchData == null) {
2050 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002051 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002052 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002053
2054 // TODO send proper bounds.
2055 Rect sourceBounds = null;
Romain Guycbb89e42009-06-08 15:52:54 -07002056
Ian Parkinson047036e2014-09-01 15:40:53 +01002057 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002058 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002059 if (clearTextImmediately) {
2060 clearTypedText();
2061 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002062
2063 // We need to show the workspace after starting the search
2064 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002065 }
2066
Ian Parkinson047036e2014-09-01 15:40:53 +01002067 /**
2068 * Start a text search.
2069 *
2070 * @return {@code true} if the search will start immediately, so any further keypresses
2071 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2072 * to buffer keypresses.
2073 */
2074 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002075 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002076 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2077 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2078 sourceBounds);
2079 }
2080
Michael Jurkaa33411c2012-06-14 16:18:21 -07002081 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002082 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002083 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002084 }
2085
2086 /**
2087 * Starts the global search activity. This code is a copied from SearchManager
2088 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002089 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002090 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002091 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002092 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2093 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2094 if (globalSearchActivity == null) {
2095 Log.w(TAG, "No global search activity found.");
2096 return;
2097 }
2098 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2099 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2100 intent.setComponent(globalSearchActivity);
2101 // Make sure that we have a Bundle to put source in
2102 if (appSearchData == null) {
2103 appSearchData = new Bundle();
2104 } else {
2105 appSearchData = new Bundle(appSearchData);
2106 }
Adam Cohen9211d422014-10-07 18:14:53 -07002107 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002108 if (!appSearchData.containsKey("source")) {
2109 appSearchData.putString("source", getPackageName());
2110 }
2111 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2112 if (!TextUtils.isEmpty(initialQuery)) {
2113 intent.putExtra(SearchManager.QUERY, initialQuery);
2114 }
2115 if (selectInitialQuery) {
2116 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2117 }
2118 intent.setSourceBounds(sourceBounds);
2119 try {
2120 startActivity(intent);
2121 } catch (ActivityNotFoundException ex) {
2122 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2123 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002124 }
2125
Yura4f93ec62014-02-04 14:15:21 +00002126 public boolean isOnCustomContent() {
2127 return mWorkspace.isOnOrMovingToCustomContent();
2128 }
2129
Winson Chung70d72102011-08-12 11:24:35 -07002130 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002131 public boolean onPrepareOptionsMenu(Menu menu) {
2132 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002133 if (mLauncherCallbacks != null) {
2134 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2135 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002136 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002137 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002138
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002139 @Override
2140 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002141 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002142 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002143 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002144 }
2145
Joe Onorato9c1289c2009-08-17 11:03:03 -04002146 public boolean isWorkspaceLocked() {
2147 return mWorkspaceLoading || mWaitingForResult;
2148 }
2149
Adam Cohen517a7f52014-03-01 12:12:59 -08002150 public boolean isWorkspaceLoading() {
2151 return mWorkspaceLoading;
2152 }
2153
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002154 private void setWorkspaceLoading(boolean value) {
2155 boolean isLocked = isWorkspaceLocked();
2156 mWorkspaceLoading = value;
2157 if (isLocked != isWorkspaceLocked()) {
2158 onWorkspaceLockedChanged();
2159 }
2160 }
2161
2162 private void setWaitingForResult(boolean value) {
2163 boolean isLocked = isWorkspaceLocked();
2164 mWaitingForResult = value;
2165 if (isLocked != isWorkspaceLocked()) {
2166 onWorkspaceLockedChanged();
2167 }
2168 }
2169
Adam Cohen9211d422014-10-07 18:14:53 -07002170 protected void onWorkspaceLockedChanged() {
2171 if (mLauncherCallbacks != null) {
2172 mLauncherCallbacks.onWorkspaceLockedChanged();
2173 }
2174 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002175
Michael Jurka0280c3b2010-09-17 15:00:07 -07002176 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002177 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002178 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002179 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2180 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002181 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002182 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002183
Tony Wickhama0628cc2015-10-14 15:23:04 -07002184 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002185 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002186 if (LOGD) {
2187 Log.d(TAG, "Adding widget from drop");
2188 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002189 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2190 }
2191
Sunny Goyale6b63a32015-01-16 16:14:29 -08002192 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002193 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2194 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002195 if (appWidgetInfo.configure != null) {
2196 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002197 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002198
Bjorn Bringert7984c942009-12-09 15:38:25 +00002199 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002200 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2201 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2202
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002203 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002204 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002205 Runnable onComplete = new Runnable() {
2206 @Override
2207 public void run() {
2208 // Exit spring loaded mode if necessary after adding the widget
2209 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2210 null);
2211 }
2212 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002213 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002214 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002215 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002216 }
2217 }
Romain Guycbb89e42009-06-08 15:52:54 -07002218
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002219 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002220 // Close any folders that may be open.
2221 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002222 mWorkspace.moveToCustomContentScreen(animate);
2223 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002224
2225 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2226 int[] cell, int spanX, int spanY) {
2227 switch (info.itemType) {
2228 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2229 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2230 int span[] = new int[2];
2231 span[0] = spanX;
2232 span[1] = spanY;
2233 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2234 container, screenId, cell, span);
2235 break;
2236 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2237 processShortcutFromDrop(info.componentName, container, screenId, cell);
2238 break;
2239 default:
2240 throw new IllegalStateException("Unknown item type: " + info.itemType);
2241 }
2242 }
2243
Adam Cohenfbba09b2011-07-18 21:43:05 -07002244 /**
2245 * Process a shortcut drop.
2246 *
2247 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002248 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002249 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002250 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002251 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2252 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002253 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002254 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002255 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002256
2257 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002258 mPendingAddInfo.cellX = cell[0];
2259 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002260 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002261
2262 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2263 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002264 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002265 }
2266
Adam Cohenfbba09b2011-07-18 21:43:05 -07002267 /**
2268 * Process a widget drop.
2269 *
2270 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002271 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002272 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002273 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002274 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2275 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002276 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002277 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002278 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002279 mPendingAddInfo.minSpanX = info.minSpanX;
2280 mPendingAddInfo.minSpanY = info.minSpanY;
2281
Adam Cohenfbba09b2011-07-18 21:43:05 -07002282 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002283 mPendingAddInfo.cellX = cell[0];
2284 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002285 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002286 if (span != null) {
2287 mPendingAddInfo.spanX = span[0];
2288 mPendingAddInfo.spanY = span[1];
2289 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002290
Adam Cohened66b2b2012-01-23 17:28:51 -08002291 AppWidgetHostView hostView = info.boundWidget;
2292 int appWidgetId;
2293 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002294 // In the case where we've prebound the widget, we remove it from the DragLayer
2295 if (LOGD) {
2296 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2297 }
2298 getDragLayer().removeView(hostView);
2299
Adam Cohened66b2b2012-01-23 17:28:51 -08002300 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002301 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002302
2303 // Clear the boundWidget so that it doesn't get destroyed.
2304 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002305 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002306 // In this case, we either need to start an activity to get permission to bind
2307 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002308 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002309 Bundle options = info.bindOptions;
2310
Sunny Goyalffe83f12014-08-14 17:39:34 -07002311 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2312 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002313 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002314 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002315 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002316 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002317 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2318 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2319 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002320 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2321 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002322 // TODO: we need to make sure that this accounts for the options bundle.
2323 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002324 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2325 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002326 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002327 }
2328
Adam Cohendcd297f2013-06-18 13:13:40 -07002329 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002330 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002331 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002332 folderInfo.title = getText(R.string.folder_name);
2333
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002334 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002335 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2336 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002337
2338 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002339 FolderIcon newFolder =
2340 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002341 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002342 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002343 // Force measure the new folder icon
2344 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2345 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002346 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002347 }
Romain Guycbb89e42009-06-08 15:52:54 -07002348
Winsonc0b52fe2015-09-09 16:38:15 -07002349 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002350 * Unbinds the view for the specified item, and removes the item and all its children.
2351 *
2352 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002353 * @param itemInfo the {@link ItemInfo} for this view.
2354 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002355 */
Winson2949fb52015-09-24 09:56:11 -07002356 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002357 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002358 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002359 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2360 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002361 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002362 } else {
2363 mWorkspace.removeWorkspaceItem(v);
2364 }
Winsonc0b52fe2015-09-09 16:38:15 -07002365 if (deleteFromDb) {
2366 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2367 }
2368 } else if (itemInfo instanceof FolderInfo) {
2369 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002370 if (v instanceof FolderIcon) {
2371 ((FolderIcon) v).removeListeners();
2372 }
Winsonc0b52fe2015-09-09 16:38:15 -07002373 mWorkspace.removeWorkspaceItem(v);
2374 if (deleteFromDb) {
2375 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2376 }
2377 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2378 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002379 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002380 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002381 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002382 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002383 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002384
Winsonc0b52fe2015-09-09 16:38:15 -07002385 } else {
2386 return false;
2387 }
2388 return true;
2389 }
2390
2391 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002392 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002393 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002394 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002395 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002396 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002397 // Deleting an app widget ID is a void call but writes to disk before returning
2398 // to the caller...
2399 new AsyncTask<Void, Void, Void>() {
2400 public Void doInBackground(Void ... args) {
2401 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2402 return null;
2403 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002404 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002405 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002406 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002407 }
2408
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002409 @Override
2410 public boolean dispatchKeyEvent(KeyEvent event) {
2411 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2412 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002413 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002414 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002415 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002416 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002417 dumpState();
2418 return true;
2419 }
2420 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002421 }
2422 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2423 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002424 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002425 return true;
2426 }
2427 }
2428
2429 return super.dispatchKeyEvent(event);
2430 }
2431
Joe Onorato88ec0992009-11-19 13:16:06 -08002432 @Override
2433 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002434 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2435 return;
2436 }
2437
Sunny Goyal45478022015-06-08 16:52:41 -07002438 if (mDragController.isDragging()) {
2439 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002440 return;
2441 }
2442
Winson Chungb745afb2015-03-02 11:51:23 -08002443 if (isAppsViewVisible()) {
2444 showWorkspace(true);
2445 } else if (isWidgetsViewVisible()) {
2446 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002447 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002448 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002449 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002450 Folder openFolder = mWorkspace.getOpenFolder();
2451 if (openFolder.isEditingName()) {
2452 openFolder.dismissEditingName();
2453 } else {
2454 closeFolder();
2455 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002456 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002457 mWorkspace.exitWidgetResizeMode();
2458
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002459 // Back button is a no-op here, but give at least some feedback for the button press
2460 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002461 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002462 }
2463
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002464 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002465 * Launches the intent referred by the clicked shortcut.
2466 *
2467 * @param v The view representing the clicked shortcut.
2468 */
2469 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002470 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2471 // view has detached (it's possible for this to happen if the view is removed mid touch).
2472 if (v.getWindowToken() == null) {
2473 return;
2474 }
2475
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002476 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002477 return;
2478 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002479
Adam Cohen1697b792013-09-17 19:08:21 -07002480 if (v instanceof Workspace) {
2481 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002482 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002483 }
2484 return;
2485 }
2486
2487 if (v instanceof CellLayout) {
2488 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002489 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2490 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002491 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002492 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002493 }
2494
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002495 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002496 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002497 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002498 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002499 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002500 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002501 }
Sunny Goyalbb011da2016-06-15 15:42:29 -07002502 } else if (v instanceof PageIndicator || (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002503 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002504 } else if (tag instanceof AppInfo) {
2505 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002506 } else if (tag instanceof LauncherAppWidgetInfo) {
2507 if (v instanceof PendingAppWidgetHostView) {
2508 onClickPendingWidget((PendingAppWidgetHostView) v);
2509 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002510 }
2511 }
2512
Sunny Goyal70660032015-05-14 00:07:08 -07002513 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002514 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002515 return false;
2516 }
2517
Michael Jurkaaf442092010-06-10 17:01:57 -07002518 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002519 * Event handler for the app widget view which has not fully restored.
2520 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002521 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002522 if (mIsSafeModeEnabled) {
2523 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2524 return;
2525 }
2526
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002527 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002528 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002529 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2530 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2531 // This should not happen, as we make sure that an Id is allocated during bind.
2532 return;
2533 }
2534 LauncherAppWidgetProviderInfo appWidgetInfo =
2535 mAppWidgetManager.findProvider(info.providerName, info.user);
2536 if (appWidgetInfo != null) {
2537 mPendingAddWidgetId = info.appWidgetId;
2538 mPendingAddInfo.copyFrom(info);
2539 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002540
Sunny Goyald478c832016-04-01 12:04:16 -07002541 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2542 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingAddWidgetId);
2543 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
2544 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2545 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2546 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2547 }
2548 } else {
2549 LauncherAppWidgetProviderInfo appWidgetInfo =
2550 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2551 if (appWidgetInfo != null) {
2552 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2553 }
Sunny Goyalff572272014-07-23 13:58:07 -07002554 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002555 } else if (info.installProgress < 0) {
2556 // The install has not been queued
2557 final String packageName = info.providerName.getPackageName();
2558 showBrokenAppInstallDialog(packageName,
2559 new DialogInterface.OnClickListener() {
2560 public void onClick(DialogInterface dialog, int id) {
2561 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2562 }
2563 });
2564 } else {
2565 // Download has started.
2566 final String packageName = info.providerName.getPackageName();
2567 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002568 }
2569 }
2570
Sunny Goyald478c832016-04-01 12:04:16 -07002571 private void startRestoredWidgetReconfigActivity(
2572 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
2573 mPendingAddWidgetInfo = provider;
2574 mPendingAddInfo.copyFrom(info);
2575 mPendingAddWidgetId = info.appWidgetId;
2576 mAppWidgetManager.startConfigActivity(provider,
2577 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2578 }
2579
Sunny Goyalff572272014-07-23 13:58:07 -07002580 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002581 * Event handler for the "grid" button that appears on the home screen, which
2582 * enters all apps mode.
2583 *
2584 * @param v The view that was clicked.
2585 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002586 protected void onClickAllAppsButton(View v) {
2587 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002588 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002589 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002590 true /* updatePredictedApps */, false /* focusSearchBar */);
2591 }
2592 }
2593
2594 protected void onLongClickAllAppsButton(View v) {
2595 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2596 if (!isAppsViewVisible()) {
2597 showAppsView(true /* animated */, false /* resetListToTop */,
2598 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002599 }
2600 }
2601
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002602 private void showBrokenAppInstallDialog(final String packageName,
2603 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002604 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002605 .setTitle(R.string.abandoned_promises_title)
2606 .setMessage(R.string.abandoned_promise_explanation)
2607 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2608 .setNeutralButton(R.string.abandoned_clean_this,
2609 new DialogInterface.OnClickListener() {
2610 public void onClick(DialogInterface dialog, int id) {
2611 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2612 mWorkspace.removeAbandonedPromise(packageName, user);
2613 }
2614 })
2615 .create().show();
2616 return;
2617 }
2618
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002619 /**
2620 * Event handler for an app shortcut click.
2621 *
2622 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2623 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002624 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002625 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2626 Object tag = v.getTag();
2627 if (!(tag instanceof ShortcutInfo)) {
2628 throw new IllegalArgumentException("Input must be a Shortcut");
2629 }
2630
2631 // Open shortcut
2632 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002633
2634 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002635 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2636 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002637 // Launch activity anyway, framework will tell the user why the app is suspended.
2638 } else {
2639 int error = R.string.activity_not_available;
2640 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2641 error = R.string.safemode_shortcut_error;
2642 }
2643 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2644 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002645 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002646 }
2647
Chris Wren40c5ed32014-06-24 18:24:23 -04002648 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002649 if ((v instanceof BubbleTextView)
2650 && shortcut.isPromise()
2651 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002652 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002653 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002654 new DialogInterface.OnClickListener() {
2655 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002656 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002657 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002658 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002659 return;
2660 }
2661
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002662 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002663 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002664 }
2665
Sunny Goyala7ce1662016-05-31 15:01:35 -07002666 private void startAppShortcutOrInfoActivity(View v) {
2667 ItemInfo item = (ItemInfo) v.getTag();
2668 Intent intent = item.getIntent();
2669 if (intent == null) {
2670 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002671 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002672 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002673 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002674
2675 if (success && v instanceof BubbleTextView) {
2676 mWaitingForResume = (BubbleTextView) v;
2677 mWaitingForResume.setStayPressed(true);
2678 }
2679 }
2680
2681 /**
2682 * Event handler for a folder icon click.
2683 *
2684 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2685 */
2686 protected void onClickFolderIcon(View v) {
2687 if (LOGD) Log.d(TAG, "onClickFolder");
2688 if (!(v instanceof FolderIcon)){
2689 throw new IllegalArgumentException("Input must be a FolderIcon");
2690 }
2691
2692 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002693 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002694 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002695 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002696 }
Michael Jurka5130e402011-10-13 04:55:35 -07002697 }
2698
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002699 /**
2700 * Event handler for the (Add) Widgets button that appears after a long press
2701 * on the home screen.
2702 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002703 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002704 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002705 if (mIsSafeModeEnabled) {
2706 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2707 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002708 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002709 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002710 }
2711
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002712 /**
2713 * Event handler for the wallpaper picker button that appears after a long press
2714 * on the home screen.
2715 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002716 protected void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002717 if (!Utilities.isWallapaperAllowed(this)) {
2718 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2719 return;
2720 }
2721
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002722 String pickerPackage = getString(R.string.wallpaper_picker_package);
2723 if (TextUtils.isEmpty(pickerPackage)) {
2724 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2725 }
2726
Tony Wickham785f7a52015-08-31 17:28:32 -07002727 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2728 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002729 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER)
2730 .setPackage(pickerPackage)
2731 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset),
2732 REQUEST_PICK_WALLPAPER);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002733 }
2734
2735 /**
2736 * Event handler for a click on the settings button that appears after a long press
2737 * on the home screen.
2738 */
Sunny Goyal745bad92016-05-02 10:54:12 -07002739 private void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002740 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002741 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2742 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002743 }
2744
Adam Cohen61f560d2013-09-30 15:58:20 -07002745 public View.OnTouchListener getHapticFeedbackTouchListener() {
2746 if (mHapticFeedbackTouchListener == null) {
2747 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002748 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002749 @Override
2750 public boolean onTouch(View v, MotionEvent event) {
2751 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2752 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2753 }
2754 return false;
2755 }
2756 };
2757 }
2758 return mHapticFeedbackTouchListener;
2759 }
2760
Tony Wickhame2217252016-03-22 16:34:23 -07002761 @Override
2762 public void onAccessibilityStateChanged(boolean enabled) {
2763 mDragLayer.onAccessibilityStateChanged(enabled);
2764 }
2765
Adam Cohen9211d422014-10-07 18:14:53 -07002766 public void onDragStarted(View view) {
2767 if (isOnCustomContent()) {
2768 // Custom content screen doesn't participate in drag and drop. If on custom
2769 // content screen, move to default.
2770 moveWorkspaceToDefaultScreen();
2771 }
Adam Cohen9211d422014-10-07 18:14:53 -07002772 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002773
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002774 /**
2775 * Called when the user stops interacting with the launcher.
2776 * This implies that the user is now on the homescreen and is not doing housekeeping.
2777 */
Adam Cohen9211d422014-10-07 18:14:53 -07002778 protected void onInteractionEnd() {
2779 if (mLauncherCallbacks != null) {
2780 mLauncherCallbacks.onInteractionEnd();
2781 }
2782 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002783
2784 /**
2785 * Called when the user starts interacting with the launcher.
2786 * The possible interactions are:
2787 * - open all apps
2788 * - reorder an app shortcut, or a widget
2789 * - open the overview mode.
2790 * This is a good time to stop doing things that only make sense
2791 * when the user is on the homescreen and not doing housekeeping.
2792 */
Adam Cohen9211d422014-10-07 18:14:53 -07002793 protected void onInteractionBegin() {
2794 if (mLauncherCallbacks != null) {
2795 mLauncherCallbacks.onInteractionBegin();
2796 }
2797 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002798
Winson Chungcd99cd32015-04-29 11:03:24 -07002799 /** Updates the interaction state. */
2800 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002801 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002802 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002803 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2804 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002805 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002806 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002807 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002808 onInteractionEnd();
2809 }
2810 }
2811
Sunny Goyala7ce1662016-05-31 15:01:35 -07002812 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002813 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002814 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2815 try {
2816 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2817 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2818 // is enabled by default on NYC.
2819 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2820 .penaltyLog().build());
2821 // Could be launching some bookkeeping activity
2822 startActivity(intent, optsBundle);
2823 } finally {
2824 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002825 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002826 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002827 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2828 // corresponding permission. Show the appropriate permission prompt if that
2829 // is the case.
2830 if (intent.getComponent() == null
2831 && Intent.ACTION_CALL.equals(intent.getAction())
2832 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2833 PackageManager.PERMISSION_GRANTED) {
2834 // TODO: Rename sPendingAddItem to a generic name.
2835 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2836 0, info);
2837 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2838 REQUEST_PERMISSION_CALL_PHONE);
2839 } else {
2840 // No idea why this was thrown.
2841 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002842 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002843 }
2844 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002845
Sunny Goyala7ce1662016-05-31 15:01:35 -07002846 private Bundle getActivityLaunchOptions(View v) {
2847 if (Utilities.ATLEAST_MARSHMALLOW) {
2848 int left = 0, top = 0;
2849 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2850 if (v instanceof TextView) {
2851 // Launch from center of icon, not entire view
2852 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2853 if (icon != null) {
2854 Rect bounds = icon.getBounds();
2855 left = (width - bounds.width()) / 2;
2856 top = v.getPaddingTop();
2857 width = bounds.width();
2858 height = bounds.height();
2859 }
2860 }
2861 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2862 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2863 // On L devices, we use the device default slide-up transition.
2864 // On L MR1 devices, we use a custom version of the slide-up transition which
2865 // doesn't have the delay present in the device default.
2866 return ActivityOptions.makeCustomAnimation(
2867 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2868 }
2869 return null;
2870 }
2871
2872 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002873 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2874 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2875 return false;
2876 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002877 // Only launch using the new animation if the shortcut has not opted out (this is a
2878 // private contract between launcher and may be ignored in the future).
2879 boolean useLaunchAnimation = (v != null) &&
2880 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2881 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2882
2883 UserHandleCompat user = null;
2884 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2885 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2886 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002887 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002888
2889 // Prepare intent
2890 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2891 if (v != null) {
2892 int[] pos = new int[2];
2893 v.getLocationOnScreen(pos);
2894 intent.setSourceBounds(
2895 new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2896 }
2897 try {
2898 if (Utilities.ATLEAST_MARSHMALLOW &&
2899 item != null && item.itemType == Favorites.ITEM_TYPE_SHORTCUT) {
2900 // Shortcuts need some special checks due to legacy reasons.
2901 startShortcutIntentSafely(intent, optsBundle, item);
2902 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2903 // Could be launching some bookkeeping activity
2904 startActivity(intent, optsBundle);
2905 } else {
2906 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2907 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2908 }
2909 return true;
2910 } catch (ActivityNotFoundException|SecurityException e) {
2911 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2912 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2913 }
2914 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002915 }
2916
Adam Cohen268c4752012-06-06 17:47:33 -07002917 /**
2918 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2919 * in the DragLayer in the exact absolute location of the original FolderIcon.
2920 */
2921 private void copyFolderIconToImage(FolderIcon fi) {
2922 final int width = fi.getMeasuredWidth();
2923 final int height = fi.getMeasuredHeight();
2924
2925 // Lazy load ImageView, Bitmap and Canvas
2926 if (mFolderIconImageView == null) {
2927 mFolderIconImageView = new ImageView(this);
2928 }
2929 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2930 mFolderIconBitmap.getHeight() != height) {
2931 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2932 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2933 }
2934
2935 DragLayer.LayoutParams lp;
2936 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2937 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2938 } else {
2939 lp = new DragLayer.LayoutParams(width, height);
2940 }
2941
Adam Cohen307fe232012-08-16 17:55:58 -07002942 // The layout from which the folder is being opened may be scaled, adjust the starting
2943 // view size by this scale factor.
2944 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002945 lp.customPosition = true;
2946 lp.x = mRectForFolderAnimation.left;
2947 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002948 lp.width = (int) (scale * width);
2949 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002950
2951 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2952 fi.draw(mFolderIconCanvas);
2953 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002954 if (fi.getFolder() != null) {
2955 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2956 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002957 }
Adam Cohen268c4752012-06-06 17:47:33 -07002958 // Just in case this image view is still in the drag layer from a previous animation,
2959 // we remove it and re-add it.
2960 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2961 mDragLayer.removeView(mFolderIconImageView);
2962 }
2963 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002964 if (fi.getFolder() != null) {
2965 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002966 }
Adam Cohen268c4752012-06-06 17:47:33 -07002967 }
2968
Adam Cohen37b2a492016-04-06 18:36:06 -07002969 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002970 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002971 FolderInfo info = (FolderInfo) fi.getTag();
2972 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2973 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002974 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2975 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002976 }
2977
Adam Cohen268c4752012-06-06 17:47:33 -07002978 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2979 copyFolderIconToImage(fi);
2980 fi.setVisibility(View.INVISIBLE);
2981
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002982 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2983 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002984 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002985 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2986 }
2987 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002988 oa.start();
2989 }
2990
Sunny Goyal935fca12015-10-13 10:19:01 -07002991 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002992 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002993 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002994
Adam Cohen268c4752012-06-06 17:47:33 -07002995 // We remove and re-draw the FolderIcon in-case it has changed
2996 mDragLayer.removeView(mFolderIconImageView);
2997 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08002998
2999 if (cl != null) {
3000 cl.clearFolderLeaveBehind();
3001 }
3002
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003003 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003004 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003005 oa.addListener(new AnimatorListenerAdapter() {
3006 @Override
3007 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003008 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003009 // Remove the ImageView copy of the FolderIcon and make the original visible.
3010 mDragLayer.removeView(mFolderIconImageView);
3011 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003012 }
3013 }
3014 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003015 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003016 if (!animate) {
3017 oa.end();
3018 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003019 }
3020
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003021 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003022 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003023 * is animated relative to the specified View. If the View is null, no animation
3024 * is played.
3025 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003026 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003027 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003028 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003029
Adam Cohenfb91f302012-06-11 15:45:18 -07003030 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003031 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3032 if (openFolder != null && openFolder != folder) {
3033 // Close any open folder before opening a folder.
3034 closeFolder();
3035 }
3036
Adam Cohena9cf38f2011-05-02 15:36:58 -07003037 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003038
Adam Cohena9cf38f2011-05-02 15:36:58 -07003039 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003040
Sunny Goyalf4066152015-04-15 09:42:19 -07003041 // While the folder is open, the position of the icon cannot change.
3042 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3043
Adam Cohen4554ee12011-08-03 16:13:21 -07003044 // Just verify that the folder hasn't already been added to the DragLayer.
3045 // There was a one-off crash where the folder had a parent already.
3046 if (folder.getParent() == null) {
3047 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003048 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003049 } else {
3050 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3051 folder.getParent() + ").");
3052 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003053 folder.animateOpen();
3054
3055 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003056
3057 // Notify the accessibility manager that this folder "window" has appeared and occluded
3058 // the workspace items
3059 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3060 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003061 }
3062
3063 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003064 closeFolder(true);
3065 }
3066
3067 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003068 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003069 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003070 if (folder.isEditingName()) {
3071 folder.dismissEditingName();
3072 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003073 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003074 }
3075 }
3076
Sunny Goyal935fca12015-10-13 10:19:01 -07003077 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003078 animate &= !Utilities.isPowerSaverOn(this);
3079
Adam Cohen4554ee12011-08-03 16:13:21 -07003080 folder.getInfo().opened = false;
3081
3082 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3083 if (parent != null) {
3084 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003085 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003086 if (fi != null) {
3087 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3088 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003089 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003090 if (animate) {
3091 folder.animateClosed();
3092 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003093 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003094 }
Winson Chung83ca4802013-04-12 15:10:52 -07003095
Sunny Goyal935fca12015-10-13 10:19:01 -07003096 // Notify the accessibility manager that this folder "window" has disappeared and no
3097 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003098 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003099 }
3100
Tony Wickhamdadb3042016-02-24 11:07:00 -08003101 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003102 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003103 if (!isDraggingEnabled()) return false;
3104 if (isWorkspaceLocked()) return false;
3105 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003106
Sunny Goyalbb011da2016-06-15 15:42:29 -07003107 if (v == mAllAppsButton && mAllAppsButton != null) {
Winson Chung76648c52015-07-10 14:33:23 -07003108 onLongClickAllAppsButton(v);
3109 return true;
3110 }
3111
Adam Cohen1697b792013-09-17 19:08:21 -07003112 if (v instanceof Workspace) {
3113 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003114 if (!mWorkspace.isTouchActive()) {
3115 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003116 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3117 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3118 return true;
3119 } else {
3120 return false;
3121 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003122 } else {
3123 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003124 }
Adam Cohen1697b792013-09-17 19:08:21 -07003125 }
3126
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003127 CellLayout.CellInfo longClickCellInfo = null;
3128 View itemUnderLongClick = null;
3129 if (v.getTag() instanceof ItemInfo) {
3130 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003131 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003132 itemUnderLongClick = longClickCellInfo.cell;
3133 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003134 }
3135
Winson Chung3d503fb2011-07-13 17:25:49 -07003136 // The hotseat touch handling does not go through Workspace, and we always allow long press
3137 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003138 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003139 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003140 // User long pressed on empty space
3141 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3142 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003143 if (mWorkspace.isInOverviewMode()) {
3144 mWorkspace.startReordering(v);
3145 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003146 showOverviewMode(true);
Hyunyoung Song645764e2016-06-06 14:19:02 -07003147 mHotseat.requestDisallowInterceptTouchEvent(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003148 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003149 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003150 final boolean isAllAppsButton =
3151 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3152 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3153 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003154 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003155 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003156 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003157 }
3158 }
3159 }
3160 return true;
3161 }
3162
Winson Chung3d503fb2011-07-13 17:25:49 -07003163 boolean isHotseatLayout(View layout) {
3164 return mHotseat != null && layout != null &&
3165 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3166 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003167
Winson Chung3d503fb2011-07-13 17:25:49 -07003168 /**
3169 * Returns the CellLayout of the specified container at the specified screen.
3170 */
Sunny Goyal92820592015-03-02 11:31:35 -08003171 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003172 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3173 if (mHotseat != null) {
3174 return mHotseat.getLayout();
3175 } else {
3176 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003177 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003178 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003179 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003180 }
3181 }
3182
Winson Chungb745afb2015-03-02 11:51:23 -08003183 /**
3184 * For overridden classes.
3185 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003186 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003187 return isAppsViewVisible();
3188 }
3189
3190 public boolean isAppsViewVisible() {
3191 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3192 }
3193
3194 public boolean isWidgetsViewVisible() {
3195 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003196 }
3197
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003198 private void setWorkspaceBackground(int background) {
3199 switch (background) {
3200 case WORKSPACE_BACKGROUND_TRANSPARENT:
3201 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3202 break;
3203 case WORKSPACE_BACKGROUND_BLACK:
3204 getWindow().setBackgroundDrawable(null);
3205 break;
3206 default:
3207 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3208 }
Craig Mautner360310b2012-10-26 15:13:08 -07003209 }
3210
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003211 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003212 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3213 int curflags = getWindow().getAttributes().flags
3214 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3215 if (wpflags != curflags) {
3216 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3217 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003218 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003219 }
3220
Michael Jurkae326f182011-11-21 14:05:46 -08003221 @Override
3222 public void onTrimMemory(int level) {
3223 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003224 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3225 // The widget preview db can result in holding onto over
3226 // 3MB of memory for caching which isn't necessary.
3227 SQLiteDatabase.releaseMemory();
3228
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003229 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003230 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003231 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003232 if (mLauncherCallbacks != null) {
3233 mLauncherCallbacks.onTrimMemory(level);
3234 }
Michael Jurkae326f182011-11-21 14:05:46 -08003235 }
3236
Winson5c6bdbb2015-09-03 11:36:19 -07003237 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003238 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003239 }
3240
Winson5c6bdbb2015-09-03 11:36:19 -07003241 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003242 boolean changed = mState != State.WORKSPACE ||
3243 mWorkspace.getState() != Workspace.State.NORMAL;
3244 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003245 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003246 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003247 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003248
Michael Jurkab3e22d92011-10-31 15:58:33 -07003249 // Set focus to the AppsCustomize button
3250 if (mAllAppsButton != null) {
3251 mAllAppsButton.requestFocus();
3252 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003253 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003254
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003255 // Change the state *after* we've called all the transition code
3256 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003257
Winson Chung5fb63472011-02-02 17:03:37 -08003258 // Resume the auto-advance of widgets
3259 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003260 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003261
Winson Chung0f785722015-04-08 10:27:49 -07003262 if (changed) {
3263 // Send an accessibility event to announce the context change
3264 getWindow().getDecorView()
3265 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003266 }
Winson5c6bdbb2015-09-03 11:36:19 -07003267 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003268 }
3269
Winsone9f27272015-10-13 10:47:51 -07003270 /**
3271 * Shows the overview button.
3272 */
Adam Cohened307df2013-10-02 09:37:31 -07003273 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003274 showOverviewMode(animated, false);
3275 }
3276
3277 /**
3278 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3279 * onto one of the overview panel buttons.
3280 */
3281 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3282 Runnable postAnimRunnable = null;
3283 if (requestButtonFocus) {
3284 postAnimRunnable = new Runnable() {
3285 @Override
3286 public void run() {
3287 // Hitting the menu button when in touch mode does not trigger touch mode to
3288 // be disabled, so if requested, force focus on one of the overview panel
3289 // buttons.
3290 mOverviewPanel.requestFocusFromTouch();
3291 }
3292 };
3293 }
Adam Cohened307df2013-10-02 09:37:31 -07003294 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003295 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003296 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003297 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003298 }
3299
Winson Chungb745afb2015-03-02 11:51:23 -08003300 /**
3301 * Shows the apps view.
3302 */
Hyunyoung Song645764e2016-06-06 14:19:02 -07003303 public void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003304 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003305 if (resetListToTop) {
3306 mAppsView.scrollToTop();
3307 }
Winson Chung4ac30062015-05-08 17:34:17 -07003308 if (updatePredictedApps) {
3309 tryAndUpdatePredictedApps();
3310 }
Winson Chung76648c52015-07-10 14:33:23 -07003311 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003312 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003313
Winson Chungb745afb2015-03-02 11:51:23 -08003314 /**
3315 * Shows the widgets view.
3316 */
3317 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003318 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003319 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003320 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003321 }
Winson Chung76648c52015-07-10 14:33:23 -07003322 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003323
3324 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003325 @Override
3326 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003327 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003328 }
3329 });
Winson Chungb745afb2015-03-02 11:51:23 -08003330 }
3331
3332 /**
3333 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003334 *
3335 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003336 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003337 // TODO: calling method should use the return value so that when {@code false} is returned
3338 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003339 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003340 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3341 mState != State.WIDGETS_SPRING_LOADED) {
3342 return false;
3343 }
3344 if (toState != State.APPS && toState != State.WIDGETS) {
3345 return false;
3346 }
Winson Chungb745afb2015-03-02 11:51:23 -08003347
3348 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003349 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3350 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003351 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003352 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003353 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003354
Michael Jurkab3e22d92011-10-31 15:58:33 -07003355 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003356 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003357
3358 // Pause the auto-advance of widgets until we are out of AllApps
3359 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003360 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003361 closeFolder();
3362
3363 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003364 getWindow().getDecorView()
3365 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003366 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003367 }
3368
Winson Chungcd99cd32015-04-29 11:03:24 -07003369 /**
3370 * Updates the workspace and interaction state on state change, and return the animation to this
3371 * new state.
3372 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003373 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003374 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003375 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003376 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003377 updateInteraction(fromState, toState);
3378 return anim;
3379 }
3380
Jun Mukaif0033da2015-08-04 18:34:30 -07003381 public void onLauncherClingShown() {
3382 // When a launcher cling appears, it should cover the underlying layers, so their focus
3383 // should be blocked.
3384 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3385 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3386 }
3387 }
3388
3389 public void onLauncherClingDismissed() {
3390 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3391 }
3392
Hyunyoung Song3f471442015-04-08 19:01:34 -07003393 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003394 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003395 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003396 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003397 }
Winson Chungb745afb2015-03-02 11:51:23 -08003398
Winson Chung006ee262015-08-03 14:40:11 -07003399 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003400 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003401 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003402
3403 if (isAppsViewVisible()) {
3404 mState = State.APPS_SPRING_LOADED;
3405 } else if (isWidgetsViewVisible()) {
3406 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003407 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003408 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003409 } else {
3410 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003411 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003412 }
Adam Cohen7777d962011-08-18 18:58:38 -07003413
Hyunyoung Song3f471442015-04-08 19:01:34 -07003414 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003415 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003416 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003417
Winson Chunge7a03942011-08-05 15:05:12 -07003418 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003419 @Override
3420 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003421 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003422 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3423 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003424 // Before we show workspace, hide all apps again because
3425 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3426 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003427 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003428 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003429 } else {
3430 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003431 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003432 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003433 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003434 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003435
Tony Wickhame0c33232016-02-08 11:37:04 -08003436 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003437 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3438 || mState == State.WIDGETS_SPRING_LOADED;
3439 }
3440
Michael Jurkad3ef3062010-11-23 16:23:58 -08003441 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003442 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003443 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003444 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003445 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003446 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003447 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3448 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003449 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003450 }
3451
Winson Chung4ac30062015-05-08 17:34:17 -07003452 /**
3453 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3454 * resumed.
3455 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003456 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003457 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003458 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003459 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003460 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003461 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003462 }
3463 }
3464 }
3465
Michael Jurkab3e22d92011-10-31 15:58:33 -07003466 void lockAllApps() {
3467 // TODO
3468 }
3469
3470 void unlockAllApps() {
3471 // TODO
3472 }
3473
Winsonf768d932015-09-25 16:12:35 -07003474 public boolean launcherCallbacksProvidesSearch() {
3475 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3476 }
3477
Michael Jurkad7c28052012-04-27 15:43:36 -07003478 @Override
3479 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003480 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003481 final List<CharSequence> text = event.getText();
3482 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003483 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003484 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003485 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003486 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003487 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003488 } else if (mWorkspace != null) {
3489 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003490 } else {
3491 text.add(getString(R.string.all_apps_home_button_label));
3492 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003493 return result;
3494 }
3495
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003496 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003497 * If the activity is currently paused, signal that we need to run the passed Runnable
3498 * in onResume.
3499 *
3500 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003501 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3502 * wrong when we're not running, and if the activity comes back to what the configuration was
3503 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003504 *
3505 * Implementation of the method from LauncherModel.Callbacks.
3506 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003507 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003508 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003509 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003510 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003511 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003512 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003513 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003514 }
3515 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003516 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003517 return true;
3518 } else {
3519 return false;
3520 }
3521 }
3522
Michael Jurkac402cd92013-05-20 15:49:32 +02003523 private boolean waitUntilResume(Runnable run) {
3524 return waitUntilResume(run, false);
3525 }
3526
Michael Jurka1e2f4652013-07-08 18:03:46 -07003527 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003528 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003529 }
3530
Michael Jurka7607c2f2013-04-03 14:33:19 -07003531 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003532 * If the activity is currently paused, signal that we need to re-run the loader
3533 * in onResume.
3534 *
3535 * This needs to be called from incoming places where resources might have been loaded
3536 * while we are paused. That is becaues the Configuration might be wrong
3537 * when we're not running, and if it comes back to what it was when we
3538 * were paused, we are not restarted.
3539 *
3540 * Implementation of the method from LauncherModel.Callbacks.
3541 *
3542 * @return true if we are currently paused. The caller might be able to
3543 * skip some work in that case since we will come back again.
3544 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003545 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003546 public boolean setLoadOnResume() {
3547 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003548 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003549 mOnResumeNeedsLoad = true;
3550 return true;
3551 } else {
3552 return false;
3553 }
3554 }
3555
3556 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003557 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003558 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003559 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003560 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003561 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003562 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003563 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003564 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003565 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003566 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003567
Joe Onorato9c1289c2009-08-17 11:03:03 -04003568 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003569 * Clear any pending bind callbacks. This is called when is loader is planning to
3570 * perform a full rebind from scratch.
3571 */
3572 @Override
3573 public void clearPendingBinds() {
3574 mBindOnResumeCallbacks.clear();
3575 if (mPendingExecutor != null) {
3576 mPendingExecutor.markCompleted();
3577 mPendingExecutor = null;
3578 }
3579 }
3580
3581 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003582 * Refreshes the shortcuts shown on the workspace.
3583 *
3584 * Implementation of the method from LauncherModel.Callbacks.
3585 */
3586 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003587 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003588
Winson Chungd64d1762013-08-20 14:37:16 -07003589 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003590 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003591 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003592
Michael Jurka05bf644e2011-11-30 20:28:53 -08003593 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003594 if (mHotseat != null) {
3595 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003596 }
3597 }
3598
Adam Cohendcd297f2013-06-18 13:13:40 -07003599 @Override
3600 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003601 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003602 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3603 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003604 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3605 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3606 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003607 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3608 // If there are no screens, we need to have an empty screen
3609 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003610 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003611 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003612
3613 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003614 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003615 if (hasCustomContentToLeft()) {
3616 mWorkspace.createCustomContentContainer();
3617 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003618 }
Winson Chung64359a52013-07-08 17:17:08 -07003619 }
3620
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003621 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003622 int count = orderedScreenIds.size();
3623 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003624 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003625 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003626 // No need to bind the first screen, as its always bound.
3627 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3628 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003629 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003630 }
3631
Winson Chungd64d1762013-08-20 14:37:16 -07003632 public void bindAppsAdded(final ArrayList<Long> newScreens,
3633 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003634 final ArrayList<ItemInfo> addAnimated,
3635 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003636 Runnable r = new Runnable() {
3637 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003638 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003639 }
3640 };
3641 if (waitUntilResume(r)) {
3642 return;
3643 }
3644
Winson Chungd64d1762013-08-20 14:37:16 -07003645 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003646 if (newScreens != null) {
3647 bindAddScreens(newScreens);
3648 }
Winson Chungd64d1762013-08-20 14:37:16 -07003649
3650 // We add the items without animation on non-visible pages, and with
3651 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003652 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003653 bindItems(addNotAnimated, 0,
3654 addNotAnimated.size(), false);
3655 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003656 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003657 bindItems(addAnimated, 0,
3658 addAnimated.size(), true);
3659 }
Winson Chungc58497e2013-09-03 17:48:37 -07003660
Winson Chung87412982013-10-03 18:34:14 -07003661 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003662 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003663
Winson Chungb745afb2015-03-02 11:51:23 -08003664 if (addedApps != null && mAppsView != null) {
3665 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003666 }
Winson Chungd64d1762013-08-20 14:37:16 -07003667 }
3668
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003669 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003670 * Bind the items start-end from the list.
3671 *
3672 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003673 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003674 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003675 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3676 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003677 Runnable r = new Runnable() {
3678 public void run() {
3679 bindItems(shortcuts, start, end, forceAnimateIcons);
3680 }
3681 };
3682 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003683 return;
3684 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003685
Winson Chungf0c6ae02012-03-21 16:10:31 -07003686 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003687 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3688 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003689 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003690 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003691 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003692 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003693 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003694
3695 // Short circuit if we are loading dock items for a configuration which has no dock
3696 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3697 mHotseat == null) {
3698 continue;
3699 }
3700
Sunny Goyal639e9062015-08-19 19:17:06 -07003701 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003702 switch (item.itemType) {
3703 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3704 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003705 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003706 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003707 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003708 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003709 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003710 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003711 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003712 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003713 default:
3714 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003715 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003716
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003717 /*
3718 * Remove colliding items.
3719 */
3720 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3721 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3722 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3723 View v = cl.getChildAt(item.cellX, item.cellY);
3724 Object tag = v.getTag();
3725 String desc = "Collision while binding workspace item: " + item
3726 + ". Collides with " + tag;
3727 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3728 throw (new RuntimeException(desc));
3729 } else {
3730 Log.d(TAG, desc);
3731 LauncherModel.deleteItemFromDatabase(this, item);
3732 continue;
3733 }
3734 }
3735 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003736 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3737 item.cellY, 1, 1);
3738 if (animateIcons) {
3739 // Animate all the applications up now
3740 view.setAlpha(0f);
3741 view.setScaleX(0f);
3742 view.setScaleY(0f);
3743 bounceAnims.add(createNewAppBounceAnimation(view, i));
3744 newShortcutsScreenId = item.screenId;
3745 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003746 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003747
Winson Chung997a9232013-07-24 15:33:46 -07003748 if (animateIcons) {
3749 // Animate to the correct page
3750 if (newShortcutsScreenId > -1) {
3751 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003752 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003753 final Runnable startBounceAnimRunnable = new Runnable() {
3754 public void run() {
3755 anim.playTogether(bounceAnims);
3756 anim.start();
3757 }
3758 };
Winson Chung997a9232013-07-24 15:33:46 -07003759 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003760 // We post the animation slightly delayed to prevent slowdowns
3761 // when we are loading right after we return to launcher.
3762 mWorkspace.postDelayed(new Runnable() {
3763 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003764 if (mWorkspace != null) {
3765 mWorkspace.snapToPage(newScreenIndex);
3766 mWorkspace.postDelayed(startBounceAnimRunnable,
3767 NEW_APPS_ANIMATION_DELAY);
3768 }
Winson Chung94d67682013-09-25 16:29:40 -07003769 }
3770 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003771 } else {
3772 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003773 }
3774 }
Winson Chung64359a52013-07-08 17:17:08 -07003775 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003776 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003777 }
3778
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003779 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3780 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3781 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003782 view.updateAppWidget(null);
3783 view.setOnClickListener(this);
3784 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003785 mWorkspace.requestLayout();
3786 }
3787
Joe Onorato9c1289c2009-08-17 11:03:03 -04003788 /**
3789 * Add the views for a widget to the workspace.
3790 *
3791 * Implementation of the method from LauncherModel.Callbacks.
3792 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003793 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003794 Runnable r = new Runnable() {
3795 public void run() {
3796 bindAppWidget(item);
3797 }
3798 };
3799 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003800 return;
3801 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003802
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003803 if (mIsSafeModeEnabled) {
3804 bindSafeModeWidget(item);
3805 return;
3806 }
3807
Daniel Sandler843e8602010-06-07 14:59:01 -04003808 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3809 if (DEBUG_WIDGETS) {
3810 Log.d(TAG, "bindAppWidget: " + item);
3811 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003812
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003813 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003814
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003815 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3816 // If the provider is not ready, bind as a pending widget.
3817 appWidgetInfo = null;
3818 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3819 // The widget id is not valid. Try to find the widget based on the provider info.
3820 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3821 } else {
3822 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3823 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003824
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003825 // If the provider is ready, but the width is not yet restored, try to restore it.
3826 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3827 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003828 if (appWidgetInfo == null) {
3829 if (DEBUG_WIDGETS) {
3830 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3831 + " belongs to component " + item.providerName
3832 + ", as the povider is null");
3833 }
3834 LauncherModel.deleteItemFromDatabase(this, item);
3835 return;
3836 }
Sunny Goyalff572272014-07-23 13:58:07 -07003837
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003838 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003839 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003840 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3841 // Id has not been allocated yet. Allocate a new id.
3842 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3843 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003844
Sunny Goyald478c832016-04-01 12:04:16 -07003845 // Also try to bind the widget. If the bind fails, the user will be shown
3846 // a click to setup UI, which will ask for the bind permission.
3847 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3848 pendingInfo.spanX = item.spanX;
3849 pendingInfo.spanY = item.spanY;
3850 pendingInfo.minSpanX = item.minSpanX;
3851 pendingInfo.minSpanY = item.minSpanY;
3852 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
3853 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3854 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003855
Sunny Goyald478c832016-04-01 12:04:16 -07003856 // Bind succeeded
3857 if (success) {
3858 // If the widget has a configure activity, it is still needs to set it up,
3859 // otherwise the widget is ready to go.
3860 item.restoreStatus = (appWidgetInfo.configure == null)
3861 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3862 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003863 }
Sunny Goyald478c832016-04-01 12:04:16 -07003864
3865 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003866 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003867 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003868 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003869 // The widget was marked as UI not ready, but there is no configure activity to
3870 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003871 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3872 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003873 }
Sunny Goyalff572272014-07-23 13:58:07 -07003874 }
3875
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003876 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003877 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003878 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3879 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003880 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003881
Sunny Goyal56c73602015-09-25 12:55:01 -07003882 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003883 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003884 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003885 deleteWidgetInfo(item);
3886 return;
3887 }
3888
Sunny Goyal233ee962015-08-03 13:05:01 -07003889 item.minSpanX = appWidgetInfo.minSpanX;
3890 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003891 addAppWidgetToWorkspace(
3892 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3893 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003894 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003895 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003896 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003897 view.updateAppWidget(null);
3898 view.setOnClickListener(this);
3899 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003900 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003901 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003902
Daniel Sandler843e8602010-06-07 14:59:01 -04003903 if (DEBUG_WIDGETS) {
3904 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3905 + (SystemClock.uptimeMillis()-start) + "ms");
3906 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003907 }
3908
Sunny Goyalff572272014-07-23 13:58:07 -07003909 /**
3910 * Restores a pending widget.
3911 *
3912 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003913 */
Sunny Goyald478c832016-04-01 12:04:16 -07003914 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003915 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3916 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3917 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003918 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003919 }
3920
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003921 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003922 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003923
3924 mWorkspace.reinflateWidgetsIfNecessary();
3925 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003926 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003927 }
3928
Adam Cohen1462de32012-07-24 22:34:36 -07003929 public void onPageBoundSynchronously(int page) {
3930 mSynchronouslyBoundPages.add(page);
3931 }
3932
Sunny Goyal527c7d32015-08-28 15:19:36 -07003933 @Override
3934 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3935 if (mPendingExecutor != null) {
3936 mPendingExecutor.markCompleted();
3937 }
3938 mPendingExecutor = executor;
3939 executor.attachTo(this);
3940 }
3941
3942 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3943 if (mPendingExecutor == executor) {
3944 mPendingExecutor = null;
3945 }
3946 }
3947
Joe Onorato9c1289c2009-08-17 11:03:03 -04003948 /**
3949 * Callback saying that there aren't any more items to bind.
3950 *
3951 * Implementation of the method from LauncherModel.Callbacks.
3952 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003953 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003954 Runnable r = new Runnable() {
3955 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003956 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003957 }
3958 };
3959 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003960 return;
3961 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003962 if (mSavedState != null) {
3963 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003964 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003965 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003966
Joe Onorato9c1289c2009-08-17 11:03:03 -04003967 mSavedState = null;
3968 }
3969
Adam Cohen1462de32012-07-24 22:34:36 -07003970 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003971
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003972 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003973
3974 // If we received the result of any pending adds while the loader was running (e.g. the
3975 // widget configuration forced an orientation change), process them now.
3976 if (sPendingAddItem != null) {
3977 final long screenId = completeAdd(sPendingAddItem);
3978
3979 // TODO: this moves the user to the page where the pending item was added. Ideally,
3980 // the screen would be guaranteed to exist after bind, and the page would be set through
3981 // the workspace restore process.
3982 mWorkspace.post(new Runnable() {
3983 @Override
3984 public void run() {
3985 mWorkspace.snapToScreenId(screenId);
3986 }
3987 });
3988 sPendingAddItem = null;
3989 }
3990
Sunny Goyal756adbc2015-04-16 15:20:51 -07003991 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003992
3993 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003994 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003995 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003996 }
3997
Winson Chunga2413752012-04-03 14:22:34 -07003998 private boolean canRunNewAppsAnimation() {
3999 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004000 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4001 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004002 }
4003
Winson Chungc9168342013-06-26 14:54:55 -07004004 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004005 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004006 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4007 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004008 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004009 return bounceAnim;
4010 }
4011
Adam Cohen27772732013-11-11 14:00:56 +00004012 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004013 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004014 }
4015
Tony Wickham55616cd2015-09-23 14:55:17 -07004016 public int getSearchBarHeight() {
4017 if (mLauncherCallbacks != null) {
4018 return mLauncherCallbacks.getSearchBarHeight();
4019 }
4020 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4021 }
4022
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004023 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004024 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4025 * multiple calls to bind the same list.)
4026 */
4027 @Thunk ArrayList<AppInfo> mTmpAppsList;
4028 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4029 public void run() {
4030 bindAllApplications(mTmpAppsList);
4031 mTmpAppsList = null;
4032 }
4033 };
4034
4035 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004036 * Add the icons for all apps.
4037 *
4038 * Implementation of the method from LauncherModel.Callbacks.
4039 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004040 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004041 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4042 mTmpAppsList = apps;
4043 return;
4044 }
4045
Winson Chungb745afb2015-03-02 11:51:23 -08004046 if (mAppsView != null) {
4047 mAppsView.setApps(apps);
4048 }
Adam Cohen9211d422014-10-07 18:14:53 -07004049 if (mLauncherCallbacks != null) {
4050 mLauncherCallbacks.bindAllApplications(apps);
4051 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004052 }
4053
4054 /**
4055 * A package was updated.
4056 *
4057 * Implementation of the method from LauncherModel.Callbacks.
4058 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004059 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004060 Runnable r = new Runnable() {
4061 public void run() {
4062 bindAppsUpdated(apps);
4063 }
4064 };
4065 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004066 return;
4067 }
4068
Winson Chungb745afb2015-03-02 11:51:23 -08004069 if (mAppsView != null) {
4070 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004071 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004072 }
4073
Sunny Goyal4390ace2014-10-13 11:33:11 -07004074 @Override
4075 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4076 Runnable r = new Runnable() {
4077 public void run() {
4078 bindWidgetsRestored(widgets);
4079 }
4080 };
4081 if (waitUntilResume(r)) {
4082 return;
4083 }
4084 mWorkspace.widgetsRestored(widgets);
4085 }
4086
Joe Onorato9c1289c2009-08-17 11:03:03 -04004087 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004088 * Some shortcuts were updated in the background.
4089 *
4090 * Implementation of the method from LauncherModel.Callbacks.
4091 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004092 @Override
4093 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4094 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004095 Runnable r = new Runnable() {
4096 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004097 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004098 }
4099 };
4100 if (waitUntilResume(r)) {
4101 return;
4102 }
4103
Sunny Goyal4390ace2014-10-13 11:33:11 -07004104 if (!updated.isEmpty()) {
4105 mWorkspace.updateShortcuts(updated);
4106 }
4107
4108 if (!removed.isEmpty()) {
4109 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4110 for (ShortcutInfo si : removed) {
4111 removedComponents.add(si.getTargetComponent());
4112 }
4113 mWorkspace.removeItemsByComponentName(removedComponents, user);
4114 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004115 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004116 }
4117 }
4118
4119 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004120 * Update the state of a package, typically related to install state.
4121 *
4122 * Implementation of the method from LauncherModel.Callbacks.
4123 */
Sunny Goyale755d462014-07-22 13:48:29 -07004124 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004125 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4126 Runnable r = new Runnable() {
4127 public void run() {
4128 bindRestoreItemsChange(updates);
4129 }
4130 };
4131 if (waitUntilResume(r)) {
4132 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004133 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004134
Sunny Goyal756adbc2015-04-16 15:20:51 -07004135 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004136 }
4137
4138 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004139 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004140 * in addition to specific applications to remove, the reason being that
4141 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004142 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004143 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004144 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004145 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004146 public void bindWorkspaceComponentsRemoved(
4147 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4148 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004149 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004150 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004151 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004152 }
Winson Chungd64d1762013-08-20 14:37:16 -07004153 };
4154 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004155 return;
4156 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004157 if (!packageNames.isEmpty()) {
4158 mWorkspace.removeItemsByPackageName(packageNames, user);
4159 }
4160 if (!components.isEmpty()) {
4161 mWorkspace.removeItemsByComponentName(components, user);
4162 }
4163 // Notify the drag controller
4164 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004165
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004166 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004167
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004168 @Override
4169 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4170 Runnable r = new Runnable() {
4171 public void run() {
4172 bindAppInfosRemoved(appInfos);
4173 }
4174 };
4175 if (waitUntilResume(r)) {
4176 return;
Joe Onorato36115782010-06-17 13:28:48 -04004177 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004178
Winson Chungdf95eb12013-10-16 14:57:07 -07004179 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004180 if (mAppsView != null) {
4181 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004182 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004183 }
Joe Onoratobe386092009-11-17 17:32:16 -08004184
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004185 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004186 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004187 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004188 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004189 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004190
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004191 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004192 public void bindWidgetsModel(WidgetsModel model) {
4193 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004194 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004195 return;
4196 }
4197
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004198 if (mWidgetsView != null && model != null) {
4199 mWidgetsView.addWidgets(model);
4200 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004201 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004202 }
4203
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004204 @Override
4205 public void notifyWidgetProvidersChanged() {
4206 if (mWorkspace.getState().shouldUpdateWidget) {
4207 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4208 }
4209 }
4210
Winson Chung400438b2011-01-16 17:53:48 -08004211 private int mapConfigurationOriActivityInfoOri(int configOri) {
4212 final Display d = getWindowManager().getDefaultDisplay();
4213 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4214 switch (d.getRotation()) {
4215 case Surface.ROTATION_0:
4216 case Surface.ROTATION_180:
4217 // We are currently in the same basic orientation as the natural orientation
4218 naturalOri = configOri;
4219 break;
4220 case Surface.ROTATION_90:
4221 case Surface.ROTATION_270:
4222 // We are currently in the other basic orientation to the natural orientation
4223 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4224 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4225 break;
4226 }
4227
4228 int[] oriMap = {
4229 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4230 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4231 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4232 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4233 };
4234 // Since the map starts at portrait, we need to offset if this device's natural orientation
4235 // is landscape.
4236 int indexOffset = 0;
4237 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4238 indexOffset = 1;
4239 }
4240 return oriMap[(d.getRotation() + indexOffset) % 4];
4241 }
Adam Cohen446e9402011-09-15 18:21:21 -07004242
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004243 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004244 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004245 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004246 if (Utilities.ATLEAST_JB_MR2) {
4247 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4248 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004249 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4250 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004251 }
Winson Chung4b919f82012-05-01 10:44:08 -07004252 }
4253 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004254
Winson Chung4b919f82012-05-01 10:44:08 -07004255 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004256 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004257 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004258 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004259 } else {
4260 mHandler.postDelayed(new Runnable() {
4261 public void run() {
4262 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4263 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004264 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004265 }
Winson Chung4b919f82012-05-01 10:44:08 -07004266 }
Winson Chung400438b2011-01-16 17:53:48 -08004267 }
4268
Adam Cohenea90f832014-05-21 15:03:34 -07004269 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004270 * To be overridden by subclasses to indicate that there is an activity to launch
4271 * before showing the standard launcher experience.
4272 */
4273 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004274 if (mLauncherCallbacks != null) {
4275 return mLauncherCallbacks.hasFirstRunActivity();
4276 }
Adam Cohen432609a2014-03-13 17:03:22 -07004277 return false;
4278 }
4279
4280 /**
4281 * To be overridden by subclasses to launch any first run activity
4282 */
4283 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004284 if (mLauncherCallbacks != null) {
4285 return mLauncherCallbacks.getFirstRunActivity();
4286 }
Adam Cohen432609a2014-03-13 17:03:22 -07004287 return null;
4288 }
4289
Winson Chunga6945242014-01-08 14:04:34 -08004290 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004291 return !ActivityManager.isRunningInTestHarness() &&
4292 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004293 }
4294
Adam Cohen4a9423d2014-03-28 14:22:31 -07004295 protected boolean hasRunFirstRunActivity() {
4296 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4297 }
4298
Adam Cohen432609a2014-03-13 17:03:22 -07004299 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004300 if (shouldRunFirstRunActivity() &&
4301 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004302 Intent firstRunIntent = getFirstRunActivity();
4303 if (firstRunIntent != null) {
4304 startActivity(firstRunIntent);
4305 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004306 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004307 }
4308 }
Adam Cohen432609a2014-03-13 17:03:22 -07004309 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004310 }
4311
4312 private void markFirstRunActivityShown() {
4313 SharedPreferences.Editor editor = mSharedPrefs.edit();
4314 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4315 editor.apply();
4316 }
4317
Adam Cohen432609a2014-03-13 17:03:22 -07004318 /**
4319 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4320 * screen that must be displayed and dismissed.
4321 */
4322 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004323 if (mLauncherCallbacks != null) {
4324 return mLauncherCallbacks.hasDismissableIntroScreen();
4325 }
Adam Cohen432609a2014-03-13 17:03:22 -07004326 return false;
4327 }
4328
4329 /**
4330 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4331 */
4332 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004333 if (mLauncherCallbacks != null) {
4334 return mLauncherCallbacks.getIntroScreen();
4335 }
Adam Cohen432609a2014-03-13 17:03:22 -07004336 return null;
4337 }
4338
4339 /**
4340 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4341 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4342 */
4343 private boolean shouldShowIntroScreen() {
4344 return hasDismissableIntroScreen() &&
4345 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4346 }
4347
4348 protected void showIntroScreen() {
4349 View introScreen = getIntroScreen();
4350 changeWallpaperVisiblity(false);
4351 if (introScreen != null) {
4352 mDragLayer.showOverlayView(introScreen);
4353 }
4354 }
4355
4356 public void dismissIntroScreen() {
4357 markIntroScreenDismissed();
4358 if (showFirstRunActivity()) {
4359 // We delay hiding the intro view until the first run activity is showing. This
4360 // avoids a blip.
4361 mWorkspace.postDelayed(new Runnable() {
4362 @Override
4363 public void run() {
4364 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004365 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004366 }
4367 }, ACTIVITY_START_DELAY);
4368 } else {
4369 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004370 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004371 }
4372 changeWallpaperVisiblity(true);
4373 }
4374
4375 private void markIntroScreenDismissed() {
4376 SharedPreferences.Editor editor = mSharedPrefs.edit();
4377 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4378 editor.apply();
4379 }
4380
Adam Cohen091440a2015-03-18 14:16:05 -07004381 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004382 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4383 // on the device, then we always show the first run cling experience (or if there is no
4384 // launcher2). Otherwise, we prompt the user upon started for migration
4385 LauncherClings launcherClings = new LauncherClings(this);
4386 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004387 mClings = launcherClings;
Sunny Goyalded0fdb2016-05-23 15:55:41 -07004388 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004389 }
4390 }
4391
Winson Chung5ffd51d2015-06-25 11:36:50 -07004392 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004393 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004394 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004395 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004396 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004397 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004398 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4399 if (userHandle != null) {
4400 user = UserHandleCompat.fromUser(userHandle);
4401 }
4402 }
4403 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004404 }
4405
4406 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004407 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004408 if (user == null) {
4409 user = UserHandleCompat.myUserHandle();
4410 }
4411
4412 // Called from search suggestion, add the profile extra to the intent to ensure that we
4413 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004414 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004415 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004416 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004417 return null;
4418 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004419 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004420 }
4421
Winson Chung5ffd51d2015-06-25 11:36:50 -07004422 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004423 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4424 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004425 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004426 UserHandleCompat.myUserHandle());
4427 }
4428
Winson Chung5ffd51d2015-06-25 11:36:50 -07004429 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004430 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4431 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004432 mWorkspace.onExternalDragStartedWithItem(dragView);
4433 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004434 }
4435
Winson Chung5ffd51d2015-06-25 11:36:50 -07004436 protected void moveWorkspaceToDefaultScreen() {
4437 mWorkspace.moveToDefaultScreen(false);
4438 }
4439
Winson Chung80baf5a2010-08-09 16:03:15 -07004440 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004441 * Returns a FastBitmapDrawable with the icon, accurately sized.
4442 */
4443 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4444 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4445 d.setFilterBitmap(true);
4446 resizeIconDrawable(d);
4447 return d;
4448 }
4449
4450 /**
4451 * Resizes an icon drawable to the correct icon size.
4452 */
Winson5fbe0742015-09-30 15:33:00 -07004453 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004454 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004455 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004456 }
4457
4458 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004459 * Prints out out state for debugging.
4460 */
4461 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004462 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004463 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004464 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4465 Log.d(TAG, "mRestoring=" + mRestoring);
4466 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004467 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004468 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004469
Daniel Sandler325dc232013-06-05 22:57:57 -04004470 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004471 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004472
4473 @Override
4474 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4475 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004476 // Dump workspace
4477 writer.println(prefix + "Workspace Items");
4478 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4479 writer.println(prefix + " Homescreen " + i);
4480
4481 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4482 for (int j = 0; j < layout.getChildCount(); j++) {
4483 Object tag = layout.getChildAt(j).getTag();
4484 if (tag != null) {
4485 writer.println(prefix + " " + tag.toString());
4486 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004487 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004488 }
Sunny Goyala1365452015-10-01 15:46:24 -07004489
4490 writer.println(prefix + " Hotseat");
4491 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4492 for (int j = 0; j < layout.getChildCount(); j++) {
4493 Object tag = layout.getChildAt(j).getTag();
4494 if (tag != null) {
4495 writer.println(prefix + " " + tag.toString());
4496 }
4497 }
4498
Sunny Goyal713edfc2016-05-06 09:58:34 -07004499 try {
4500 FileLog.flushAll(writer);
4501 } catch (Exception e) {
4502 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004503 }
4504
Adam Cohen9211d422014-10-07 18:14:53 -07004505 if (mLauncherCallbacks != null) {
4506 mLauncherCallbacks.dump(prefix, fd, writer, args);
4507 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004508 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004509
Adam Cohen59400422014-03-05 18:07:04 -08004510 public static CustomAppWidget getCustomAppWidget(String name) {
4511 return sCustomAppWidgets.get(name);
4512 }
4513
4514 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4515 return sCustomAppWidgets;
4516 }
4517
Sunny Goyal29538332016-02-18 09:10:19 -08004518 public static List<View> getFolderContents(View icon) {
4519 if (icon instanceof FolderIcon) {
4520 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4521 } else {
4522 return Collections.EMPTY_LIST;
4523 }
4524 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004525
4526 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4527
4528 @Override
4529 public void onSharedPreferenceChanged(
4530 SharedPreferences sharedPreferences, String key) {
4531 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4532 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4533 if (!waitUntilResume(this, true)) {
4534 run();
4535 }
4536 }
4537 }
4538
4539 @Override
4540 public void run() {
4541 setOrientation();
4542 }
4543 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004544}