blob: e1d292cbbdc3a9ac05d3867ae0b77a9ba926a383 [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;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070094import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070095import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080096import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -070097import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010098import com.android.launcher3.compat.LauncherActivityInfoCompat;
99import com.android.launcher3.compat.LauncherAppsCompat;
100import com.android.launcher3.compat.UserHandleCompat;
101import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800102import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700103import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700104import com.android.launcher3.dragndrop.DragController;
105import com.android.launcher3.dragndrop.DragLayer;
106import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -0700107import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000108import com.android.launcher3.folder.Folder;
109import com.android.launcher3.folder.FolderIcon;
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800110import com.android.launcher3.logging.LoggerUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700111import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700112import com.android.launcher3.model.WidgetsModel;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700113import com.android.launcher3.pageindicators.PageIndicatorLine;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800114import com.android.launcher3.userevent.nano.LauncherLogProto;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700115import com.android.launcher3.util.ComponentKey;
Sunny Goyal713edfc2016-05-06 09:58:34 -0700116import com.android.launcher3.logging.FileLog;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700117import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal322d5562015-06-25 19:35:49 -0700118import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700119import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700120import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700121import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700122import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700123import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700124
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700125import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700126import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700127import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700128import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800129import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700130import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700131import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700132import java.util.List;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800133import java.util.Locale;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700134
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135/**
136 * Default launcher application.
137 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100138public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700139 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
140 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700141 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700142 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700143 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Daniel Sandlerf061f822013-06-27 13:59:36 -0400145 static final boolean PROFILE_STARTUP = false;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700146 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700147 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400148 static final boolean DEBUG_RESUME_TIME = false;
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700149 static final boolean DEBUG_LOGGING = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700150
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700152 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700153 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700154 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155
Michael Jurka8b805b12012-04-18 14:23:14 -0700156 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700157 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700158 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700159
Sunny Goyal28c6b962015-10-12 11:42:05 -0700160 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
161
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700162 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
163 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
164 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
165
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700166 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
167
Mathew Inwood876a8462013-06-14 14:12:41 +0100168 /**
169 * IntentStarter uses request codes starting with this. This must be greater than all activity
170 * request codes used internally.
171 */
172 protected static final int REQUEST_LAST = 100;
173
Michael Jurka0a457bf2012-11-19 14:05:05 -0800174 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700175 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800176 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177
Winson Chung2672ff92012-05-04 16:22:30 -0700178 // The Intent extra that defines whether to ignore the launch animation
179 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400180 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700181
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700182 public static final String ACTION_APPWIDGET_HOST_RESET =
183 "com.android.launcher3.intent.ACTION_APPWIDGET_HOST_RESET";
184
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 // Type: int
186 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700187 // Type: int
188 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700189 // Type: Content Values / parcelable
190 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700191 // Type: parcelable
192 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000193 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800194 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195
Adam Cohen432609a2014-03-13 17:03:22 -0700196 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800197 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
198
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700199 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700200 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
201 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700202
Adam Cohen091440a2015-03-18 14:16:05 -0700203 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700204 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700205
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700206 private boolean mIsSafeModeEnabled;
207
Joe Onorato9c1289c2009-08-17 11:03:03 -0400208 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800209 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700210 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700211 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212
Winson Chunga2413752012-04-03 14:22:34 -0700213 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700214 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
215 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700216 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700217
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700218 private final BroadcastReceiver mUiBroadcastReceiver = new BroadcastReceiver() {
219
220 @Override
221 public void onReceive(Context context, Intent intent) {
222 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
223 closeSystemDialogs();
224 } else if (ACTION_APPWIDGET_HOST_RESET.equals(intent.getAction())) {
225 if (mAppWidgetHost != null) {
226 mAppWidgetHost.startListening();
227 }
228 }
229 }
230 };
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800231
Adam Cohen091440a2015-03-18 14:16:05 -0700232 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700233 private View mLauncherView;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700234 private PageIndicatorLine mPageIndicator;
Adam Cohen091440a2015-03-18 14:16:05 -0700235 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800236 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700237
238 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700239
Sunny Goyalffe83f12014-08-14 17:39:34 -0700240 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700241 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700242
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700243 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800244 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800245 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700246
Michael Jurka0280c3b2010-09-17 15:00:07 -0700247 private int[] mTmpAddItemCellCoordinates = new int[2];
248
Sunny Goyal316490e2015-06-02 09:38:28 -0700249 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800250 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700251
Michael Jurka838a4ca2011-02-07 13:33:06 -0800252 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700253 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700254
Sunny Goyal47328fd2016-05-25 18:56:41 -0700255 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700256
257 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700258 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700259 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700260
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700261 // Main container view and the model for the widget tray screen.
262 @Thunk WidgetsContainerView mWidgetsView;
263 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700264
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700266 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
267 // scroll issues (because the workspace may not have been measured yet) and extra work.
268 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
269 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270
271 private SpannableStringBuilder mDefaultKeySsb = null;
272
Adam Cohen091440a2015-03-18 14:16:05 -0700273 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400274
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800275 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 private boolean mRestoring;
277 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700278 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279
Michael Jurka1e2f4652013-07-08 18:03:46 -0700280 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700281 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700282 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700283
Joe Onorato9c1289c2009-08-17 11:03:03 -0400284 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800285 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700286 private ExtractedColors mExtractedColors;
Adam Cohen091440a2015-03-18 14:16:05 -0700287 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800288 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700289 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800290 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400291
Sunny Goyal639e9062015-08-19 19:17:06 -0700292 private LauncherClings mClings;
293
Adam Cohen61f560d2013-09-30 15:58:20 -0700294 private View.OnTouchListener mHapticFeedbackTouchListener;
295
Adam Cohended9f8d2010-11-03 13:25:16 -0700296 // Related to the auto-advancing of widgets
297 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700298 private static final int ADVANCE_INTERVAL = 20000;
299 private static final int ADVANCE_STAGGER = 250;
300
301 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700302 private long mAutoAdvanceSentTime;
303 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700304 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700305
Winson Chung400438b2011-01-16 17:53:48 -0800306 // Determines how long to wait after a rotation before restoring the screen orientation to
307 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700308 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800309
Adam Cohen091440a2015-03-18 14:16:05 -0700310 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700311
Adam Cohen1462de32012-07-24 22:34:36 -0700312 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700313 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700314
Winson Chung46353de2012-02-16 14:05:10 -0800315 // We only want to get the SharedPreferences once since it does an FS stat each time we get
316 // it from the context.
317 private SharedPreferences mSharedPrefs;
318
Winson Chungf0c6ae02012-03-21 16:10:31 -0700319 // Holds the page that we need to animate to, and the icon views that we need to animate up
320 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700321 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700322 private Bitmap mFolderIconBitmap;
323 private Canvas mFolderIconCanvas;
324 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700325
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700326 private DeviceProfile mDeviceProfile;
327
Adam Cohen4b66bf32015-09-18 12:15:19 -0700328 private boolean mMoveToDefaultScreenFromNewIntent;
329
Winson Chung13eb5272015-05-11 16:30:13 -0700330 // This is set to the view that launched the activity that navigated the user away from
331 // launcher. Since there is no callback for when the activity has finished launching, enable
332 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800333 private BubbleTextView mWaitingForResume;
334
Adam Cohen59400422014-03-05 18:07:04 -0800335 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
336 new HashMap<String, CustomAppWidget>();
337
Adam Cohen59400422014-03-05 18:07:04 -0800338 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700339 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
340 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800341 }
342 }
343
Adam Cohen091440a2015-03-18 14:16:05 -0700344 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700345 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700346 if (mWorkspace != null) {
347 mWorkspace.buildPageHardwareLayers();
348 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700349 }
350 };
351
Adam Cohendb364c32014-05-20 14:23:40 -0700352 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800353
Adam Cohen091440a2015-03-18 14:16:05 -0700354 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800355 int requestCode;
356 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700357 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700358 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800359 int cellX;
360 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700361 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800362 }
363
Hyunyoung Songaa953652016-04-19 18:30:24 -0700364 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800365
Adam Cohenf9c184a2016-01-15 16:47:43 -0800366 public FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700367 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400368
369 @Thunk void setOrientation() {
370 if (mRotationEnabled) {
371 unlockScreenOrientation(true);
372 } else {
373 setRequestedOrientation(
374 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700375 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400376 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700377
Sunny Goyal745bad92016-05-02 10:54:12 -0700378 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700379
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 @Override
381 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700382 if (DEBUG_STRICT_MODE) {
383 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
384 .detectDiskReads()
385 .detectDiskWrites()
386 .detectNetwork() // or .detectAll() for all detectable problems
387 .penaltyLog()
388 .build());
389 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
390 .detectLeakedSqlLiteObjects()
391 .detectLeakedClosableObjects()
392 .penaltyLog()
393 .penaltyDeath()
394 .build());
395 }
396
Adam Cohen9211d422014-10-07 18:14:53 -0700397 if (mLauncherCallbacks != null) {
398 mLauncherCallbacks.preOnCreate();
399 }
400
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400402
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400403 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700404
Adam Cohen2e6da152015-05-06 11:42:25 -0700405 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700406 mDeviceProfile = getResources().getConfiguration().orientation
407 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700408 app.getInvariantDeviceProfile().landscapeProfile
409 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700410
Sunny Goyalf7258242015-10-19 16:59:07 -0700411 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700412 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800413 mModel = app.setLauncher(this);
414 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700415
Joe Onorato41a12d22009-10-31 18:30:00 -0400416 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700417 mAllAppsController = new AllAppsTransitionController(this);
418 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700419
Sunny Goyalffe83f12014-08-14 17:39:34 -0700420 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700421
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700422 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
423 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700424
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700425 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
426 // this also ensures that any synchronous binding below doesn't re-trigger another
427 // LauncherModel load.
428 mPaused = false;
429
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800430 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200431 android.os.Debug.startMethodTracing(
432 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433 }
434
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800435 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700436
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700438 mDeviceProfile.layout(this);
Tony Wickham827cef22016-03-17 15:39:39 -0700439 mExtractedColors = new ExtractedColors();
440 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441
Tony Wickhame2217252016-03-22 16:34:23 -0700442 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
443 .addAccessibilityStateChangeListener(this);
444
Joe Onorato7c312c12009-08-13 21:36:53 -0700445 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700446
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 mSavedState = savedInstanceState;
448 restoreState(mSavedState);
449
450 if (PROFILE_STARTUP) {
451 android.os.Debug.stopMethodTracing();
452 }
453
454 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700455 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700456 // If the user leaves launcher, then we should just load items asynchronously when
457 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700458 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700459 } else {
460 // We only load the page synchronously if the user rotates (or triggers a
461 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700462 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700463 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464 }
465
466 // For handling default keys
467 mDefaultKeySsb = new SpannableStringBuilder();
468 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800469
470 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700471 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
472 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800473
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800474 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700475 // In case we are on a device with locked rotation, we should look at preferences to check
476 // if the user has specifically allowed rotation.
477 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700478 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700479 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
480 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700481 }
482
483 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
484 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400485 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700486
Adam Cohen9211d422014-10-07 18:14:53 -0700487 if (mLauncherCallbacks != null) {
488 mLauncherCallbacks.onCreate(savedInstanceState);
489 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700490
491 if (shouldShowIntroScreen()) {
492 showIntroScreen();
493 } else {
494 showFirstRunActivity();
495 showFirstRunClings();
496 }
Adam Cohen9211d422014-10-07 18:14:53 -0700497 }
498
Sunny Goyal7779d622015-06-11 16:18:39 -0700499 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700500 public void onExtractedColorsChanged() {
501 loadExtractedColorsAndColorItems();
502 }
503
504 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700505 // TODO: do this in pre-N as well, once the extraction part is complete.
506 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700507 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700508 mHotseat.updateColor(mExtractedColors, !mPaused);
Tony Wickham770217c2016-05-18 15:16:40 -0700509 if (mPageIndicator != null) {
510 mPageIndicator.updateColor(mExtractedColors);
511 }
Tony Wickham827cef22016-03-17 15:39:39 -0700512 }
513 }
514
Adam Cohen9211d422014-10-07 18:14:53 -0700515 private LauncherCallbacks mLauncherCallbacks;
516
517 public void onPostCreate(Bundle savedInstanceState) {
518 super.onPostCreate(savedInstanceState);
519 if (mLauncherCallbacks != null) {
520 mLauncherCallbacks.onPostCreate(savedInstanceState);
521 }
522 }
523
Sunny Goyal32554d12015-12-03 15:31:25 -0800524 /**
525 * Call this after onCreate to set or clear overlay.
526 */
527 public void setLauncherOverlay(LauncherOverlay overlay) {
528 if (overlay != null) {
529 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
530 }
531 mWorkspace.setLauncherOverlay(overlay);
532 }
533
Adam Cohen9211d422014-10-07 18:14:53 -0700534 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
535 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700536 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700537 private boolean mWorkspaceImportanceStored = false;
538 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700539 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800540 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700541 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
542
543 @Override
544 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700545 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700546 return;
547 }
548 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700549 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700550 if (mWorkspace != null) {
551 mWorkspaceImportanceForAccessibility =
552 mWorkspace.getImportantForAccessibility();
553 mWorkspace.setImportantForAccessibility(
554 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
555 mWorkspaceImportanceStored = true;
556 }
557 if (mHotseat != null) {
558 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
559 mHotseat.setImportantForAccessibility(
560 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
561 mHotseatImportanceStored = true;
562 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700563 }
564
565 @Override
566 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700567 if (mWorkspaceImportanceStored && mWorkspace != null) {
568 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
569 }
570 if (mHotseatImportanceStored && mHotseat != null) {
571 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
572 }
573 mWorkspaceImportanceStored = false;
574 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700575 }
576 });
Adam Cohen9211d422014-10-07 18:14:53 -0700577 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700578 }
579
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700580 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700581 public void onLauncherProviderChange() {
582 if (mLauncherCallbacks != null) {
583 mLauncherCallbacks.onLauncherProviderChange();
584 }
585 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700586
Adam Cohen9211d422014-10-07 18:14:53 -0700587 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700588 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700589 if (mLauncherCallbacks != null) {
590 return mLauncherCallbacks.hasCustomContentToLeft();
591 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700592 return false;
593 }
594
Dave Hawkeya8881582013-09-17 15:55:33 -0600595 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500596 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600597 * {@link #addToCustomContentPage}. This will only be invoked if
598 * {@link #hasCustomContentToLeft()} is {@code true}.
599 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500600 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700601 if (mLauncherCallbacks != null) {
602 mLauncherCallbacks.populateCustomContentContainer();
603 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600604 }
605
606 /**
607 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
608 * ensure the custom content page is added or removed if necessary.
609 */
610 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600611 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600612 // Not bound yet, wait for bindScreens to be called.
613 return;
614 }
615
616 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
617 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500618 mWorkspace.createCustomContentContainer();
619 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600620 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
621 mWorkspace.removeCustomContentPage();
622 }
623 }
624
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800625 /**
626 * Logger object is a singleton and does not have to be coupled with the foreground activity.
627 * Since most user event logging is done on the UI, the object is retrieved from the
628 * callback for convenience.
629 */
Hyunyoung Songaa953652016-04-19 18:30:24 -0700630 private UserEventDispatcher createUserEventDispatcher() {
631 return new UserEventDispatcher() {
632 @Override
633 public void dispatchUserEvent(LauncherLogProto.LauncherEvent ev, Intent intent) {
634 if (!DEBUG_LOGGING) {
635 return;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800636 }
Hyunyoung Songaa953652016-04-19 18:30:24 -0700637 Log.d("UserEvent", String.format(Locale.US,
638 "action:%s\nchild:%s\nparent:%s\nelapsed container %d ms session %d ms",
639 LoggerUtils.getActionStr(ev.action),
640 LoggerUtils.getTargetStr(ev.srcTarget[0]),
641 LoggerUtils.getTargetStr(ev.srcTarget[1]),
642 ev.elapsedContainerMillis,
643 ev.elapsedSessionMillis));
644 }
645 };
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800646 }
647
Hyunyoung Songaa953652016-04-19 18:30:24 -0700648 public UserEventDispatcher getUserEventDispatcher() {
649 if (mLauncherCallbacks != null) {
650 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
651 if (dispatcher != null) {
652 return dispatcher;
653 }
654 }
655
656 if (mUserEventDispatcher == null) {
657 mUserEventDispatcher = createUserEventDispatcher();
658 }
659 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800660 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100661
Hyunyoung Song3f471442015-04-08 19:01:34 -0700662 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700663 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
664 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700665 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700666 }
667
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000668 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700669 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
670 // This cast is safe as long as the id < 0x00FFFFFF
671 // Since we jail all the dynamically generated views, there should be no clashes
672 // with any other views.
673 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000674 }
675
676 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700677 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
678 * a configuration step, this allows the proper animations to run after other transitions.
679 */
Adam Cohendb364c32014-05-20 14:23:40 -0700680 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700681 long screenId = args.screenId;
682 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
683 // When the screen id represents an actual screen (as opposed to a rank) we make sure
684 // that the drop page actually exists.
685 screenId = ensurePendingDropLayoutExists(args.screenId);
686 }
Adam Cohendb364c32014-05-20 14:23:40 -0700687
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800688 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800689 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700690 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700691 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700692 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800693 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700694 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700695 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700696 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyald478c832016-04-01 12:04:16 -0700697 completeRestoreAppWidget(args.appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700698 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700699 case REQUEST_BIND_PENDING_APPWIDGET: {
700 int widgetId = args.appWidgetId;
701 LauncherAppWidgetInfo info =
702 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
703 if (info != null) {
704 // Since the view was just bound, also launch the configure activity if needed
705 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
706 .getLauncherAppWidgetInfo(widgetId);
707 if (provider != null && provider.configure != null) {
708 startRestoredWidgetReconfigActivity(provider, info);
709 }
710 }
711 break;
712 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800713 }
Michael Jurka27614d22012-04-02 06:40:22 -0700714 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
715 // if you turned the screen off and then back while in All Apps, Launcher would not
716 // return to the workspace. Clearing mAddInfo.container here fixes this issue
717 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700718 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800719 }
720
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800721 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700722 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700723 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700724 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700725 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800726 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700727
Adam Cohenad4e15c2013-10-17 16:21:35 -0700728 Runnable exitSpringLoaded = new Runnable() {
729 @Override
730 public void run() {
731 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
732 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
733 }
734 };
735
Michael Jurka8b805b12012-04-18 14:23:14 -0700736 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700737 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700738 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700739 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
740 if (resultCode == RESULT_CANCELED) {
741 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700742 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700743 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700744 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800745 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700746 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700747 }
748 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200749 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
750 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700751 // User could have free-scrolled between pages before picking a wallpaper; make sure
752 // we move to the closest one now.
753 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700754 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200755 }
756 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700757 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200758
Adam Cohened66b2b2012-01-23 17:28:51 -0800759 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700760 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700761
Adam Cohendb364c32014-05-20 14:23:40 -0700762 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800763 // We have special handling for widgets
764 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800765 final int appWidgetId;
766 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
767 : -1;
768 if (widgetId < 0) {
769 appWidgetId = pendingAddWidgetId;
770 } else {
771 appWidgetId = widgetId;
772 }
773
Adam Cohenad4e15c2013-10-17 16:21:35 -0700774 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800775 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700776 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
777 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700778 result = RESULT_CANCELED;
779 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700780 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700781 @Override
782 public void run() {
783 exitSpringLoadedDragModeDelayed(false, 0, null);
784 }
785 };
Adam Cohendb364c32014-05-20 14:23:40 -0700786 if (workspaceLocked) {
787 // No need to remove the empty screen if we're mid-binding, as the
788 // the bind will not add the empty screen.
789 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
790 } else {
791 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
792 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
793 }
Winson Chung5aaab772012-04-27 15:24:02 -0700794 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700795 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700796 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
797 // When the screen id represents an actual screen (as opposed to a rank)
798 // we make sure that the drop page actually exists.
799 mPendingAddInfo.screenId =
800 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
801 }
Adam Cohendb364c32014-05-20 14:23:40 -0700802 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
803
804 dropLayout.setDropPending(true);
805 final Runnable onComplete = new Runnable() {
806 @Override
807 public void run() {
808 completeTwoStageWidgetDrop(resultCode, appWidgetId);
809 dropLayout.setDropPending(false);
810 }
811 };
812 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
813 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
814 } else {
815 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
816 mPendingAddInfo);
817 sPendingAddItem = args;
818 }
Winson Chung5aaab772012-04-27 15:24:02 -0700819 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800820 return;
821 }
822
Sunny Goyald478c832016-04-01 12:04:16 -0700823 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
824 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700825 if (resultCode == RESULT_OK) {
826 // Update the widget view.
827 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
828 pendingAddWidgetId, mPendingAddInfo);
829 if (workspaceLocked) {
830 sPendingAddItem = args;
831 } else {
832 completeAdd(args);
833 }
834 }
835 // Leave the widget in the pending state if the user canceled the configure.
836 return;
837 }
838
Sunny Goyalaad90582015-07-09 14:22:50 -0700839 if (requestCode == REQUEST_CREATE_SHORTCUT) {
840 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
841 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
842 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
843 mPendingAddInfo);
844 if (isWorkspaceLocked()) {
845 sPendingAddItem = args;
846 } else {
847 completeAdd(args);
848 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
849 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
850 }
851 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700852 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
853 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800856 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800857
858 }
859
860 @Override
861 protected void onActivityResult(
862 final int requestCode, final int resultCode, final Intent data) {
863 handleActivityResult(requestCode, resultCode, data);
864 if (mLauncherCallbacks != null) {
865 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
866 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800867 }
868
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600869 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700870 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600871 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700872 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
873 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
874 View v = null;
875 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
876 if (layout != null) {
877 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
878 }
879 Intent intent = sPendingAddItem.intent;
880 sPendingAddItem = null;
881 if (grantResults.length > 0
882 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700883 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700884 } else {
885 // TODO: Show a snack bar with link to settings
886 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
887 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
888 }
889 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600890 if (mLauncherCallbacks != null) {
891 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
892 grantResults);
893 }
894 }
895
Adam Cohendb364c32014-05-20 14:23:40 -0700896 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
897 appWidgetId, ItemInfo info) {
898 PendingAddArguments args = new PendingAddArguments();
899 args.requestCode = requestCode;
900 args.intent = data;
901 args.container = info.container;
902 args.screenId = info.screenId;
903 args.cellX = info.cellX;
904 args.cellY = info.cellY;
905 args.appWidgetId = appWidgetId;
906 return args;
907 }
908
909 /**
910 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
911 *
912 * @param screenId the screen id to check
913 * @return the new screen, or screenId if it exists
914 */
915 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800916 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700917 if (dropLayout == null) {
918 // it's possible that the add screen was removed because it was
919 // empty and a re-bind occurred
920 mWorkspace.addExtraEmptyScreen();
921 return mWorkspace.commitExtraEmptyScreen();
922 } else {
923 return screenId;
924 }
925 }
926
Adam Cohen091440a2015-03-18 14:16:05 -0700927 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800928 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800929 Runnable onCompleteRunnable = null;
930 int animationType = 0;
931
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700932 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800933 if (resultCode == RESULT_OK) {
934 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
935 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700936 mPendingAddWidgetInfo);
937 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800938 onCompleteRunnable = new Runnable() {
939 @Override
940 public void run() {
941 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700942 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700943 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
944 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800945 }
946 };
947 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800948 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800949 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800950 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700951 if (mDragLayer.getAnimatedView() != null) {
952 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
953 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
954 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700955 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700956 // The animated view may be null in the case of a rotation during widget configuration
957 onCompleteRunnable.run();
958 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 }
960
961 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700962 protected void onStop() {
963 super.onStop();
964 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700965
966 if (mLauncherCallbacks != null) {
967 mLauncherCallbacks.onStop();
968 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700969 }
970
971 @Override
972 protected void onStart() {
973 super.onStart();
974 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700975
976 if (mLauncherCallbacks != null) {
977 mLauncherCallbacks.onStart();
978 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700979 }
980
981 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200983 long startTime = 0;
984 if (DEBUG_RESUME_TIME) {
985 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400986 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200987 }
Adam Cohen9211d422014-10-07 18:14:53 -0700988
989 if (mLauncherCallbacks != null) {
990 mLauncherCallbacks.preOnResume();
991 }
992
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700994 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700995
Winson Chung4a2afa32012-07-19 14:53:05 -0700996 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700997 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700998 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800999 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -07001000 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -07001001 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -07001002 // view after launching an app, as they may be depending on the UI to be static to
1003 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -07001004 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07001005 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08001006 } else if (mOnResumeState == State.WIDGETS) {
1007 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001008 }
1009 mOnResumeState = State.NONE;
1010
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001011 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001012 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1013 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001014
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001015 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001016 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001017 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001018 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001019 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001020 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001021 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001022 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001023 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1024 // execute them here
1025 long startTimeCallbacks = 0;
1026 if (DEBUG_RESUME_TIME) {
1027 startTimeCallbacks = System.currentTimeMillis();
1028 }
1029
Michael Jurka1e2f4652013-07-08 18:03:46 -07001030 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1031 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001032 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001033 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001034 if (DEBUG_RESUME_TIME) {
1035 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1036 (System.currentTimeMillis() - startTimeCallbacks));
1037 }
Michael Jurka447bf842013-05-15 14:52:15 +02001038 }
Michael Jurka54554252013-08-01 12:52:23 +02001039 if (mOnResumeCallbacks.size() > 0) {
1040 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1041 mOnResumeCallbacks.get(i).run();
1042 }
1043 mOnResumeCallbacks.clear();
1044 }
Winson Chunge4e50662012-01-23 14:45:13 -08001045
1046 // Reset the pressed state of icons that were locked in the press state while activities
1047 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001048 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001049 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001050 mWaitingForResume.setStayPressed(false);
1051 }
Winson Chung82963d52013-10-09 11:20:57 -07001052
Adam Cohen06dff352012-06-01 17:17:08 -07001053 // It is possible that widgets can receive updates while launcher is not in the foreground.
1054 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1055 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1056 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001057 if (!isWorkspaceLoading()) {
1058 getWorkspace().reinflateWidgetsIfNecessary();
1059 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001060
Michael Jurka447bf842013-05-15 14:52:15 +02001061 if (DEBUG_RESUME_TIME) {
1062 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1063 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001064
Adam Cohen4b66bf32015-09-18 12:15:19 -07001065 // We want to suppress callbacks about CustomContent being shown if we have just received
1066 // onNewIntent while the user was present within launcher. In that case, we post a call
1067 // to move the user to the main screen (which will occur after onResume). We don't want to
1068 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1069 // suppress here.
1070 if (mWorkspace.getCustomContentCallbacks() != null
1071 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001072 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001073 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001074 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1075 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001076 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001077 }
1078 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001079 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001080 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001081 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001082
Sunny Goyal756adbc2015-04-16 15:20:51 -07001083 if (!isWorkspaceLoading()) {
1084 // Process any items that were added while Launcher was away.
1085 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1086 }
Adam Cohen9211d422014-10-07 18:14:53 -07001087
1088 if (mLauncherCallbacks != null) {
1089 mLauncherCallbacks.onResume();
1090 }
Adam Cohen06dff352012-06-01 17:17:08 -07001091 }
1092
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001094 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001095 // Ensure that items added to Launcher are queued until Launcher returns
1096 InstallShortcutReceiver.enableInstallQueue();
1097
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001098 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001099 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001100 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001101 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001102
1103 // We call onHide() aggressively. The custom content callbacks should be able to
1104 // debounce excess onHide calls.
1105 if (mWorkspace.getCustomContentCallbacks() != null) {
1106 mWorkspace.getCustomContentCallbacks().onHide();
1107 }
Romain Guycbb89e42009-06-08 15:52:54 -07001108
Adam Cohen9211d422014-10-07 18:14:53 -07001109 if (mLauncherCallbacks != null) {
1110 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001111 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001112 }
1113
1114 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001115 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1116 // by a onResume or by scrolling otherwise.
1117 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001118
1119 // Custom content is completely hidden
1120 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001121
1122 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1123 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001124
1125 // Indicates whether the user is allowed to scroll away from the custom content.
1126 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001127 }
1128
Adam Cohenc2d6e892014-10-16 09:49:24 -07001129 public interface LauncherOverlay {
1130
1131 /**
1132 * Touch interaction leading to overscroll has begun
1133 */
1134 public void onScrollInteractionBegin();
1135
1136 /**
1137 * Touch interaction related to overscroll has ended
1138 */
1139 public void onScrollInteractionEnd();
1140
1141 /**
1142 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1143 * screen (or in the case of RTL, the rightmost screen).
1144 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001145 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001146
1147 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001148 * Called when the launcher is ready to use the overlay
1149 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001150 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001151 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001152 }
1153
Jun Mukai8af0cd82015-05-12 12:32:12 -07001154 public interface LauncherSearchCallbacks {
1155 /**
1156 * Called when the search overlay is shown.
1157 */
1158 public void onSearchOverlayOpened();
1159
1160 /**
1161 * Called when the search overlay is dismissed.
1162 */
1163 public void onSearchOverlayClosed();
1164 }
1165
Adam Cohenc2d6e892014-10-16 09:49:24 -07001166 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001167 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001168 }
1169
1170 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1171
Sunny Goyalc86df472016-02-25 09:19:38 -08001172 public void onScrollChanged(float progress) {
1173 if (mWorkspace != null) {
1174 mWorkspace.onOverlayScrollChanged(progress);
1175 }
1176 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001177 }
1178
Jorim Jaggid017f882014-01-14 17:08:48 -08001179 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001180 if (mLauncherCallbacks != null) {
1181 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001182 } else {
1183 // On devices with a locked orientation, we will at least have the allow rotation
1184 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001185 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001186 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001187 }
1188
Adam Cohen9211d422014-10-07 18:14:53 -07001189 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001190 CustomContentCallbacks callbacks, String description) {
1191 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001192 }
1193
Adam Cohenedb40762013-07-18 16:45:45 -07001194 // The custom content needs to offset its content to account for the QSB
1195 public int getTopOffsetForCustomContent() {
1196 return mWorkspace.getPaddingTop();
1197 }
1198
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001199 @Override
1200 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001201 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001202 if (mModel.isCurrentCallbacks(this)) {
1203 mModel.stopLoader();
1204 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001205 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1206
Romain Guy13c2e7b2010-03-10 19:45:00 -08001207 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001208 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001209
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001210 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001211 @Override
1212 public void onWindowFocusChanged(boolean hasFocus) {
1213 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001214 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001215
1216 if (mLauncherCallbacks != null) {
1217 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1218 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001219 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001220
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221 private boolean acceptFilter() {
1222 final InputMethodManager inputManager = (InputMethodManager)
1223 getSystemService(Context.INPUT_METHOD_SERVICE);
1224 return !inputManager.isFullscreenMode();
1225 }
1226
1227 @Override
1228 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001229 final int uniChar = event.getUnicodeChar();
1230 final boolean handled = super.onKeyDown(keyCode, event);
1231 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1232 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1234 keyCode, event);
1235 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001236 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001237 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001238 // showSearchDialog()
1239 // If there are multiple keystrokes before the search dialog takes focus,
1240 // onSearchRequested() will be called for every keystroke,
1241 // but it is idempotent, so it's fine.
1242 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 }
1244 }
1245
Joe Onorato8a9625e2010-01-28 15:55:35 -08001246 // Eat the long press event so the keyboard doesn't come up.
1247 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1248 return true;
1249 }
1250
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 return handled;
1252 }
1253
Winson46163472015-09-22 17:31:56 -07001254 @Override
1255 public boolean onKeyUp(int keyCode, KeyEvent event) {
1256 if (keyCode == KeyEvent.KEYCODE_MENU) {
1257 // Ignore the menu key if we are currently dragging or are on the custom content screen
1258 if (!isOnCustomContent() && !mDragController.isDragging()) {
1259 // Close any open folders
1260 closeFolder();
1261
1262 // Stop resizing any widgets
1263 mWorkspace.exitWidgetResizeMode();
1264
1265 // Show the overview mode if we are on the workspace
1266 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1267 !mWorkspace.isSwitchingState()) {
1268 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001269 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001270 }
1271 }
1272 return true;
1273 }
1274 return super.onKeyUp(keyCode, event);
1275 }
1276
Karl Rosaen138a0412009-04-23 19:00:21 -07001277 private String getTypedText() {
1278 return mDefaultKeySsb.toString();
1279 }
1280
1281 private void clearTypedText() {
1282 mDefaultKeySsb.clear();
1283 mDefaultKeySsb.clearSpans();
1284 Selection.setSelection(mDefaultKeySsb, 0);
1285 }
1286
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001288 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1289 * State
1290 */
1291 private static State intToState(int stateOrdinal) {
1292 State state = State.WORKSPACE;
1293 final State[] stateValues = State.values();
1294 for (int i = 0; i < stateValues.length; i++) {
1295 if (stateValues[i].ordinal() == stateOrdinal) {
1296 state = stateValues[i];
1297 break;
1298 }
1299 }
1300 return state;
1301 }
1302
1303 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 * Restores the previous state, if it exists.
1305 *
1306 * @param savedState The previous state.
1307 */
1308 private void restoreState(Bundle savedState) {
1309 if (savedState == null) {
1310 return;
1311 }
1312
Michael Jurka883f55b2010-10-21 15:47:14 -07001313 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001314 if (state == State.APPS || state == State.WIDGETS) {
1315 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001316 }
1317
Adam Cohen21cd0022013-10-09 18:57:02 -07001318 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1319 PagedView.INVALID_RESTORE_PAGE);
1320 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001321 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 }
1323
Sunny Goyal756cd262015-08-20 12:33:21 -07001324 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1325 if (itemValues != null) {
1326 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001327 AppWidgetProviderInfo info = savedState.getParcelable(
1328 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001329 mPendingAddWidgetInfo = info == null ?
1330 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1331
Adam Cohen4637b5a2013-11-04 18:21:24 -08001332 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001333 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 mRestoring = true;
1335 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 }
1337
1338 /**
1339 * Finds all the views we need and configure them properly.
1340 */
1341 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001342 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001343 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001344 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1345 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Tony Wickhamf549dab2016-05-16 09:54:06 -07001346 mPageIndicator = (PageIndicatorLine) mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001347
Sunny Goyal784f9c32016-03-23 16:56:54 -07001348 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1349 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1350 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001351 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352
Winson Chung4c98d922011-05-31 16:50:48 -07001353 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001354
1355 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001356
Winson Chung3d503fb2011-07-13 17:25:49 -07001357 // Setup the hotseat
1358 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1359 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001360 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001361 }
1362
Winsone9f27272015-10-13 10:47:51 -07001363 // Setup the overview panel
1364 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001365
Winson Chung4c98d922011-05-31 16:50:48 -07001366 // Setup the workspace
1367 mWorkspace.setHapticFeedbackEnabled(false);
1368 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001369 mWorkspace.setup(mDragController);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001370 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001371 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001372
Tony Wickham34d2c912015-09-11 09:27:58 -07001373 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001374 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001375
Winson Chungef7f8742015-06-04 17:18:17 -07001376 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001377 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001378 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001379 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1380 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1381 } else {
1382 mAppsView.setSearchBarController(new DefaultAppSearchController());
1383 }
Craig Mautner360310b2012-10-26 15:13:08 -07001384
Winson Chung3d503fb2011-07-13 17:25:49 -07001385 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001386 mDragController.setDragScoller(mWorkspace);
1387 mDragController.setScrollView(mDragLayer);
1388 mDragController.setMoveTarget(mWorkspace);
1389 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001390 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001391
Sunny Goyal322d5562015-06-25 19:35:49 -07001392 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1393 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001394 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395 }
1396
Winsone9f27272015-10-13 10:47:51 -07001397 private void setupOverviewPanel() {
1398 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1399
1400 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1401 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1402 @Override
1403 public boolean onLongClick(View v) {
1404 return v.performClick();
1405 }
1406 };
1407
1408 // Bind wallpaper button actions
1409 View wallpaperButton = findViewById(R.id.wallpaper_button);
1410 wallpaperButton.setOnClickListener(new OnClickListener() {
1411 @Override
1412 public void onClick(View view) {
1413 if (!mWorkspace.isSwitchingState()) {
1414 onClickWallpaperPicker(view);
1415 }
1416 }
1417 });
1418 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1419 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1420
1421 // Bind widget button actions
1422 mWidgetsButton = findViewById(R.id.widget_button);
1423 mWidgetsButton.setOnClickListener(new OnClickListener() {
1424 @Override
1425 public void onClick(View view) {
1426 if (!mWorkspace.isSwitchingState()) {
1427 onClickAddWidgetButton(view);
1428 }
1429 }
1430 });
1431 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1432 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1433
1434 // Bind settings actions
1435 View settingsButton = findViewById(R.id.settings_button);
1436 boolean hasSettings = hasSettings();
1437 if (hasSettings) {
1438 settingsButton.setOnClickListener(new OnClickListener() {
1439 @Override
1440 public void onClick(View view) {
1441 if (!mWorkspace.isSwitchingState()) {
1442 onClickSettingsButton(view);
1443 }
1444 }
1445 });
1446 settingsButton.setOnLongClickListener(performClickOnLongClick);
1447 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1448 } else {
1449 settingsButton.setVisibility(View.GONE);
1450 }
1451
1452 mOverviewPanel.setAlpha(0f);
1453 }
1454
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001456 * Sets the all apps button. This method is called from {@link Hotseat}.
1457 */
1458 public void setAllAppsButton(View allAppsButton) {
1459 mAllAppsButton = allAppsButton;
1460 }
1461
1462 public View getAllAppsButton() {
1463 return mAllAppsButton;
1464 }
1465
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001466 public View getWidgetsButton() {
1467 return mWidgetsButton;
1468 }
1469
Anjali Koppal5ad44842014-03-10 20:34:39 -07001470 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 * Creates a view representing a shortcut.
1472 *
1473 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001475 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001476 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 }
1478
1479 /**
1480 * Creates a view representing a shortcut inflated from the specified resource.
1481 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 * @param parent The group the shortcut belongs to.
1483 * @param info The data structure describing the shortcut.
1484 *
1485 * @return A View inflated from layoutResId.
1486 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001487 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001488 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001489 parent, false);
1490 favorite.applyFromShortcutInfo(info, mIconCache);
1491 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001493 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494 return favorite;
1495 }
1496
1497 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 * Add a shortcut to the workspace.
1499 *
1500 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001502 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001503 int cellY) {
1504 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001505 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001506
Sunny Goyal5c97f512015-05-19 16:03:28 -07001507 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001508 if (info == null) {
1509 return;
1510 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001511 final View view = createShortcut(info);
1512
Sunny Goyal5c97f512015-05-19 16:03:28 -07001513 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001514 // First we check if we already know the exact location where we want to add this item.
1515 if (cellX >= 0 && cellY >= 0) {
1516 cellXY[0] = cellX;
1517 cellXY[1] = cellY;
1518 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001519
1520 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001521 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001522 true, null,null)) {
1523 return;
1524 }
1525 DragObject dragObject = new DragObject();
1526 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001527 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1528 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001529 return;
1530 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001531 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001532 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001533 }
1534
1535 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001536 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001537 return;
1538 }
1539
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001540 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541
1542 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001543 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001544 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545 }
1546 }
1547
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001549 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001551 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 */
Adam Cohen091440a2015-03-18 14:16:05 -07001553 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001554 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1555
1556 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001557 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001558 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001559 }
Romain Guycbb89e42009-06-08 15:52:54 -07001560
Adam Cohen59400422014-03-05 18:07:04 -08001561 if (appWidgetInfo.isCustomWidget) {
1562 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001563 }
1564
Adam Cohen59400422014-03-05 18:07:04 -08001565 LauncherAppWidgetInfo launcherInfo;
1566 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1567 launcherInfo.spanX = info.spanX;
1568 launcherInfo.spanY = info.spanY;
1569 launcherInfo.minSpanX = info.minSpanX;
1570 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001571 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001572
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001574 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575
1576 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001577 if (hostView == null) {
1578 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001579 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001580 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001581 hostView.setVisibility(View.VISIBLE);
1582 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001584 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 }
Romain Guycbb89e42009-06-08 15:52:54 -07001586
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001587 private void addAppWidgetToWorkspace(
1588 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001589 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001590 hostView.setTag(item);
1591 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001592
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001593 hostView.setFocusable(true);
1594 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001595
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001596 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001597 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1598
1599 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001600 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001601 }
1602 }
1603
Adam Cohended9f8d2010-11-03 13:25:16 -07001604 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1605 @Override
1606 public void onReceive(Context context, Intent intent) {
1607 final String action = intent.getAction();
1608 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1609 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001610 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001611 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001612
Winson Chungc100e8e2011-08-09 16:02:43 -07001613 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001614 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001615 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001616 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001617 if (!showWorkspace(false)) {
1618 // If we are already on the workspace, then manually reset all apps
1619 mAppsView.reset();
1620 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001621 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001622 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1623 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001624 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001625 }
1626 }
1627 };
1628
1629 @Override
1630 public void onAttachedToWindow() {
1631 super.onAttachedToWindow();
1632
1633 // Listen for broadcasts related to user-presence
1634 final IntentFilter filter = new IntentFilter();
1635 filter.addAction(Intent.ACTION_SCREEN_OFF);
1636 filter.addAction(Intent.ACTION_USER_PRESENT);
1637 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001638 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001639 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001640 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001641
1642 if (mLauncherCallbacks != null) {
1643 mLauncherCallbacks.onAttachedToWindow();
1644 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001645 }
1646
1647 @Override
1648 public void onDetachedFromWindow() {
1649 super.onDetachedFromWindow();
1650 mVisible = false;
1651
Adam Cohend113e0c2010-11-11 10:48:05 -08001652 if (mAttached) {
1653 unregisterReceiver(mReceiver);
1654 mAttached = false;
1655 }
Winson Chungb745afb2015-03-02 11:51:23 -08001656 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001657
1658 if (mLauncherCallbacks != null) {
1659 mLauncherCallbacks.onDetachedFromWindow();
1660 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001661 }
1662
1663 public void onWindowVisibilityChanged(int visibility) {
1664 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001665 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001666 // The following code used to be in onResume, but it turns out onResume is called when
1667 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1668 // is a more appropriate event to handle
1669 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001670 if (!mWorkspaceLoading) {
1671 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001672 // We want to let Launcher draw itself at least once before we force it to build
1673 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001674 // apps is nice and speedy.
1675 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001676 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001677 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001678 if (mStarted) return;
1679 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001680 // We delay the layer building a bit in order to give
1681 // other message processing a time to run. In particular
1682 // this avoids a delay in hiding the IME if it was
1683 // currently shown, because doing that may involve
1684 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001685 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001686 final ViewTreeObserver.OnDrawListener listener = this;
1687 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001688 public void run() {
1689 if (mWorkspace != null &&
1690 mWorkspace.getViewTreeObserver() != null) {
1691 mWorkspace.getViewTreeObserver().
1692 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001693 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001694 }
1695 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001696 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001697 }
1698 });
1699 }
1700 clearTypedText();
1701 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001702 }
1703
Adam Cohen091440a2015-03-18 14:16:05 -07001704 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001705 mHandler.removeMessages(ADVANCE_MSG);
1706 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1707 mHandler.sendMessageDelayed(msg, delay);
1708 mAutoAdvanceSentTime = System.currentTimeMillis();
1709 }
1710
Adam Cohen091440a2015-03-18 14:16:05 -07001711 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001712 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1713 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1714 mAutoAdvanceRunning = autoAdvanceRunning;
1715 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001716 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001717 sendAdvanceMessage(delay);
1718 } else {
1719 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001720 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001721 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1722 }
1723 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001724 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001725 }
1726 }
1727 }
1728
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001729 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1730
Adam Cohended9f8d2010-11-03 13:25:16 -07001731 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001732 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001733 if (msg.what == ADVANCE_MSG) {
1734 int i = 0;
1735 for (View key: mWidgetsToAdvance.keySet()) {
1736 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001737 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001738 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001739 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001740 public void run() {
1741 ((Advanceable) v).advance();
1742 }
1743 }, delay);
1744 }
1745 i++;
1746 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001747 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001748 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001749 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001750 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001751 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001752
Winsonc0b52fe2015-09-09 16:38:15 -07001753 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001754 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001755 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1756 if (v instanceof Advanceable) {
1757 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001758 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001759 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001760 }
1761 }
1762
Winsonc0b52fe2015-09-09 16:38:15 -07001763 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001764 if (mWidgetsToAdvance.containsKey(hostView)) {
1765 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001766 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001767 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001768 }
1769
Hyunyoung Song3f471442015-04-08 19:01:34 -07001770 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001771 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1772 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001773 }
1774
Winson Chunga6945242014-01-08 14:04:34 -08001775 public DragLayer getDragLayer() {
1776 return mDragLayer;
1777 }
1778
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001779 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001780 return mAppsView;
1781 }
1782
Hyunyoung Song3f471442015-04-08 19:01:34 -07001783 public WidgetsContainerView getWidgetsView() {
1784 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001785 }
1786
Winson Chunga6945242014-01-08 14:04:34 -08001787 public Workspace getWorkspace() {
1788 return mWorkspace;
1789 }
1790
1791 public Hotseat getHotseat() {
1792 return mHotseat;
1793 }
1794
Jorim Jaggid017f882014-01-14 17:08:48 -08001795 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001796 return mOverviewPanel;
1797 }
1798
Sunny Goyal47328fd2016-05-25 18:56:41 -07001799 public DropTargetBar getDropTargetBar() {
1800 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001801 }
1802
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001803 public LauncherAppWidgetHost getAppWidgetHost() {
1804 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001805 }
Romain Guycbb89e42009-06-08 15:52:54 -07001806
Winson Chunga9abd0e2010-10-27 17:18:37 -07001807 public LauncherModel getModel() {
1808 return mModel;
1809 }
1810
Adam Cohen79d90c52016-04-22 13:29:20 -07001811 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001812 return mSharedPrefs;
1813 }
1814
Adam Cohen2e6da152015-05-06 11:42:25 -07001815 public DeviceProfile getDeviceProfile() {
1816 return mDeviceProfile;
1817 }
1818
Bjorn Bringertc459e522013-06-07 19:36:01 +01001819 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001820 getWindow().closeAllPanels();
1821
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001822 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001823 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001824 }
1825
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001826 @Override
1827 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001828 long startTime = 0;
1829 if (DEBUG_RESUME_TIME) {
1830 startTime = System.currentTimeMillis();
1831 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001832 super.onNewIntent(intent);
1833
1834 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001835 Folder openFolder = mWorkspace.getOpenFolder();
1836 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1837 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1838 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1839 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1840 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001841 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001842 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001843
Adam Cohen6fecd412013-10-02 17:41:50 -07001844 if (mWorkspace == null) {
1845 // Can be cases where mWorkspace is null, this prevents a NPE
1846 return;
1847 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001848 // In all these cases, only animate if we're already on home
1849 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001850
Sunny Goyal935fca12015-10-13 10:19:01 -07001851 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001852 exitSpringLoadedDragMode();
1853
1854 // If we are already on home, then just animate back to the workspace,
1855 // otherwise, just wait until onResume to set the state back to Workspace
1856 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001857 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001858 } else {
1859 mOnResumeState = State.WORKSPACE;
1860 }
1861
1862 final View v = getWindow().peekDecorView();
1863 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001864 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001865 INPUT_METHOD_SERVICE);
1866 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1867 }
1868
Winson Chungb745afb2015-03-02 11:51:23 -08001869 // Reset the apps view
1870 if (!alreadyOnHome && mAppsView != null) {
1871 mAppsView.scrollToTop();
1872 }
1873
Hyunyoung Song3f471442015-04-08 19:01:34 -07001874 // Reset the widgets view
1875 if (!alreadyOnHome && mWidgetsView != null) {
1876 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001877 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001878
Adam Cohen9211d422014-10-07 18:14:53 -07001879 if (mLauncherCallbacks != null) {
1880 mLauncherCallbacks.onHomeIntent();
1881 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882 }
Adam Cohened307df2013-10-02 09:37:31 -07001883
Adam Cohen9211d422014-10-07 18:14:53 -07001884 if (mLauncherCallbacks != null) {
1885 mLauncherCallbacks.onNewIntent(intent);
1886 }
Winson15f8b172015-08-19 11:02:39 -07001887
1888 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1889 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1890 // animation.
1891 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001892 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1893 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001894 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1895 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001896
1897 // We use this flag to suppress noisy callbacks above custom content state
1898 // from onResume.
1899 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001900 mWorkspace.post(new Runnable() {
1901 @Override
1902 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001903 if (mWorkspace != null) {
1904 mWorkspace.moveToDefaultScreen(true);
1905 }
Winson15f8b172015-08-19 11:02:39 -07001906 }
1907 });
1908 }
1909 }
1910
1911 if (DEBUG_RESUME_TIME) {
1912 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1913 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001914 }
1915
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001917 public void onRestoreInstanceState(Bundle state) {
1918 super.onRestoreInstanceState(state);
1919 for (int page: mSynchronouslyBoundPages) {
1920 mWorkspace.restoreInstanceStateForChild(page);
1921 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001922 }
1923
1924 @Override
1925 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001926 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001927 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1928 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001929
Adam Cohen21cd0022013-10-09 18:57:02 -07001930 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001931 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932
Michael Jurka883f55b2010-10-21 15:47:14 -07001933 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001934 // We close any open folder since it will not be re-opened, and we need to make sure
1935 // this state is reflected.
1936 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1937 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938
Adam Cohendcd297f2013-06-18 13:13:40 -07001939 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001940 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001941 ContentValues itemValues = new ContentValues();
1942 mPendingAddInfo.writeToValues(itemValues);
1943 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001944 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001945 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 }
1947
Adam Cohen9211d422014-10-07 18:14:53 -07001948 if (mLauncherCallbacks != null) {
1949 mLauncherCallbacks.onSaveInstanceState(outState);
1950 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951 }
1952
1953 @Override
1954 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001956
Winson Chunge7a03942011-08-05 15:05:12 -07001957 // Remove all pending runnables
1958 mHandler.removeMessages(ADVANCE_MSG);
1959 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001960 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001961 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001962
Winson Chungcd2b0142011-06-08 16:02:26 -07001963 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001964 // It's possible to receive onDestroy after a new Launcher activity has
1965 // been created. In this case, don't interfere with the new Launcher.
1966 if (mModel.isCurrentCallbacks(this)) {
1967 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001968 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001969 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001970
Sunny Goyal745bad92016-05-02 10:54:12 -07001971 if (mRotationPrefChangeHandler != null) {
1972 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1973 }
1974
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001976 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001978 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001980 mAppWidgetHost = null;
1981
1982 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983
1984 TextKeyListener.getInstance().release();
1985
Tony Wickhame2217252016-03-22 16:34:23 -07001986 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1987 .removeAccessibilityStateChangeListener(this);
1988
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001989 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001990
Michael Jurka2ecf9952012-06-18 12:52:28 -07001991 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001992
1993 if (mLauncherCallbacks != null) {
1994 mLauncherCallbacks.onDestroy();
1995 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 }
1997
Adam Cohena9cf38f2011-05-02 15:36:58 -07001998 public DragController getDragController() {
1999 return mDragController;
2000 }
2001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002 @Override
2003 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002004 onStartForResult(requestCode);
2005 super.startActivityForResult(intent, requestCode);
2006 }
2007
2008 @Override
2009 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2010 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2011 onStartForResult(requestCode);
2012 try {
2013 super.startIntentSenderForResult(intent, requestCode,
2014 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2015 } catch (IntentSender.SendIntentException e) {
2016 throw new ActivityNotFoundException();
2017 }
2018 }
2019
2020 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002021 if (requestCode >= 0) {
2022 setWaitingForResult(true);
2023 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002024 }
2025
Winson Chung70d72102011-08-12 11:24:35 -07002026 /**
2027 * Indicates that we want global search for this activity by setting the globalSearch
2028 * argument for {@link #startSearch} to true.
2029 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002030 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002031 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002033
Karl Rosaen138a0412009-04-23 19:00:21 -07002034 if (initialQuery == null) {
2035 // Use any text typed in the launcher as the initial query
2036 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002037 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 if (appSearchData == null) {
2039 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002040 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002042
2043 // TODO send proper bounds.
2044 Rect sourceBounds = null;
Romain Guycbb89e42009-06-08 15:52:54 -07002045
Ian Parkinson047036e2014-09-01 15:40:53 +01002046 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002047 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002048 if (clearTextImmediately) {
2049 clearTypedText();
2050 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002051
2052 // We need to show the workspace after starting the search
2053 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002054 }
2055
Ian Parkinson047036e2014-09-01 15:40:53 +01002056 /**
2057 * Start a text search.
2058 *
2059 * @return {@code true} if the search will start immediately, so any further keypresses
2060 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2061 * to buffer keypresses.
2062 */
2063 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002064 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002065 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2066 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2067 sourceBounds);
2068 }
2069
Michael Jurkaa33411c2012-06-14 16:18:21 -07002070 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002071 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002072 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002073 }
2074
2075 /**
2076 * Starts the global search activity. This code is a copied from SearchManager
2077 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002078 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002079 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002080 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002081 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2082 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2083 if (globalSearchActivity == null) {
2084 Log.w(TAG, "No global search activity found.");
2085 return;
2086 }
2087 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2088 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2089 intent.setComponent(globalSearchActivity);
2090 // Make sure that we have a Bundle to put source in
2091 if (appSearchData == null) {
2092 appSearchData = new Bundle();
2093 } else {
2094 appSearchData = new Bundle(appSearchData);
2095 }
Adam Cohen9211d422014-10-07 18:14:53 -07002096 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002097 if (!appSearchData.containsKey("source")) {
2098 appSearchData.putString("source", getPackageName());
2099 }
2100 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2101 if (!TextUtils.isEmpty(initialQuery)) {
2102 intent.putExtra(SearchManager.QUERY, initialQuery);
2103 }
2104 if (selectInitialQuery) {
2105 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2106 }
2107 intent.setSourceBounds(sourceBounds);
2108 try {
2109 startActivity(intent);
2110 } catch (ActivityNotFoundException ex) {
2111 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2112 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 }
2114
Yura4f93ec62014-02-04 14:15:21 +00002115 public boolean isOnCustomContent() {
2116 return mWorkspace.isOnOrMovingToCustomContent();
2117 }
2118
Winson Chung70d72102011-08-12 11:24:35 -07002119 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002120 public boolean onPrepareOptionsMenu(Menu menu) {
2121 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002122 if (mLauncherCallbacks != null) {
2123 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2124 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002125 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002126 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002127
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002128 @Override
2129 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002130 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002131 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002132 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002133 }
2134
Joe Onorato9c1289c2009-08-17 11:03:03 -04002135 public boolean isWorkspaceLocked() {
2136 return mWorkspaceLoading || mWaitingForResult;
2137 }
2138
Adam Cohen517a7f52014-03-01 12:12:59 -08002139 public boolean isWorkspaceLoading() {
2140 return mWorkspaceLoading;
2141 }
2142
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002143 private void setWorkspaceLoading(boolean value) {
2144 boolean isLocked = isWorkspaceLocked();
2145 mWorkspaceLoading = value;
2146 if (isLocked != isWorkspaceLocked()) {
2147 onWorkspaceLockedChanged();
2148 }
2149 }
2150
2151 private void setWaitingForResult(boolean value) {
2152 boolean isLocked = isWorkspaceLocked();
2153 mWaitingForResult = value;
2154 if (isLocked != isWorkspaceLocked()) {
2155 onWorkspaceLockedChanged();
2156 }
2157 }
2158
Adam Cohen9211d422014-10-07 18:14:53 -07002159 protected void onWorkspaceLockedChanged() {
2160 if (mLauncherCallbacks != null) {
2161 mLauncherCallbacks.onWorkspaceLockedChanged();
2162 }
2163 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002164
Michael Jurka0280c3b2010-09-17 15:00:07 -07002165 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002166 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002167 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002168 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2169 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002170 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002171 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002172
Tony Wickhama0628cc2015-10-14 15:23:04 -07002173 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002174 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002175 if (LOGD) {
2176 Log.d(TAG, "Adding widget from drop");
2177 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002178 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2179 }
2180
Sunny Goyale6b63a32015-01-16 16:14:29 -08002181 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002182 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2183 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002184 if (appWidgetInfo.configure != null) {
2185 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002186 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002187
Bjorn Bringert7984c942009-12-09 15:38:25 +00002188 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002189 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2190 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2191
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002192 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002193 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002194 Runnable onComplete = new Runnable() {
2195 @Override
2196 public void run() {
2197 // Exit spring loaded mode if necessary after adding the widget
2198 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2199 null);
2200 }
2201 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002202 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002203 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002204 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002205 }
2206 }
Romain Guycbb89e42009-06-08 15:52:54 -07002207
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002208 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002209 // Close any folders that may be open.
2210 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002211 mWorkspace.moveToCustomContentScreen(animate);
2212 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002213
2214 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2215 int[] cell, int spanX, int spanY) {
2216 switch (info.itemType) {
2217 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2218 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2219 int span[] = new int[2];
2220 span[0] = spanX;
2221 span[1] = spanY;
2222 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2223 container, screenId, cell, span);
2224 break;
2225 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2226 processShortcutFromDrop(info.componentName, container, screenId, cell);
2227 break;
2228 default:
2229 throw new IllegalStateException("Unknown item type: " + info.itemType);
2230 }
2231 }
2232
Adam Cohenfbba09b2011-07-18 21:43:05 -07002233 /**
2234 * Process a shortcut drop.
2235 *
2236 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002237 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002238 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002239 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002240 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2241 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002242 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002243 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002244 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002245
2246 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002247 mPendingAddInfo.cellX = cell[0];
2248 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002249 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002250
2251 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2252 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002253 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002254 }
2255
Adam Cohenfbba09b2011-07-18 21:43:05 -07002256 /**
2257 * Process a widget drop.
2258 *
2259 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002260 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002261 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002262 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002263 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2264 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002265 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002266 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002267 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002268 mPendingAddInfo.minSpanX = info.minSpanX;
2269 mPendingAddInfo.minSpanY = info.minSpanY;
2270
Adam Cohenfbba09b2011-07-18 21:43:05 -07002271 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002272 mPendingAddInfo.cellX = cell[0];
2273 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002274 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002275 if (span != null) {
2276 mPendingAddInfo.spanX = span[0];
2277 mPendingAddInfo.spanY = span[1];
2278 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002279
Adam Cohened66b2b2012-01-23 17:28:51 -08002280 AppWidgetHostView hostView = info.boundWidget;
2281 int appWidgetId;
2282 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002283 // In the case where we've prebound the widget, we remove it from the DragLayer
2284 if (LOGD) {
2285 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2286 }
2287 getDragLayer().removeView(hostView);
2288
Adam Cohened66b2b2012-01-23 17:28:51 -08002289 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002290 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002291
2292 // Clear the boundWidget so that it doesn't get destroyed.
2293 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002294 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002295 // In this case, we either need to start an activity to get permission to bind
2296 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002297 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002298 Bundle options = info.bindOptions;
2299
Sunny Goyalffe83f12014-08-14 17:39:34 -07002300 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2301 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002302 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002303 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002304 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002305 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002306 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2307 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2308 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002309 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2310 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002311 // TODO: we need to make sure that this accounts for the options bundle.
2312 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002313 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2314 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002315 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002316 }
2317
Adam Cohendcd297f2013-06-18 13:13:40 -07002318 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002319 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002320 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002321 folderInfo.title = getText(R.string.folder_name);
2322
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002323 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002324 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2325 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002326
2327 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002328 FolderIcon newFolder =
2329 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002330 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002331 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002332 // Force measure the new folder icon
2333 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2334 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002335 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002336 }
Romain Guycbb89e42009-06-08 15:52:54 -07002337
Winsonc0b52fe2015-09-09 16:38:15 -07002338 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002339 * Unbinds the view for the specified item, and removes the item and all its children.
2340 *
2341 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002342 * @param itemInfo the {@link ItemInfo} for this view.
2343 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002344 */
Winson2949fb52015-09-24 09:56:11 -07002345 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002346 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002347 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002348 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2349 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002350 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002351 } else {
2352 mWorkspace.removeWorkspaceItem(v);
2353 }
Winsonc0b52fe2015-09-09 16:38:15 -07002354 if (deleteFromDb) {
2355 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2356 }
2357 } else if (itemInfo instanceof FolderInfo) {
2358 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002359 if (v instanceof FolderIcon) {
2360 ((FolderIcon) v).removeListeners();
2361 }
Winsonc0b52fe2015-09-09 16:38:15 -07002362 mWorkspace.removeWorkspaceItem(v);
2363 if (deleteFromDb) {
2364 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2365 }
2366 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2367 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002368 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002369 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002370 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002371 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002372 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002373
Winsonc0b52fe2015-09-09 16:38:15 -07002374 } else {
2375 return false;
2376 }
2377 return true;
2378 }
2379
2380 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002381 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002382 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002383 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002384 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002385 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002386 // Deleting an app widget ID is a void call but writes to disk before returning
2387 // to the caller...
2388 new AsyncTask<Void, Void, Void>() {
2389 public Void doInBackground(Void ... args) {
2390 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2391 return null;
2392 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002393 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002394 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002395 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002396 }
2397
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002398 @Override
2399 public boolean dispatchKeyEvent(KeyEvent event) {
2400 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2401 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002402 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002403 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002404 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002405 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002406 dumpState();
2407 return true;
2408 }
2409 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002410 }
2411 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2412 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002413 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002414 return true;
2415 }
2416 }
2417
2418 return super.dispatchKeyEvent(event);
2419 }
2420
Joe Onorato88ec0992009-11-19 13:16:06 -08002421 @Override
2422 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002423 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2424 return;
2425 }
2426
Sunny Goyal45478022015-06-08 16:52:41 -07002427 if (mDragController.isDragging()) {
2428 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002429 return;
2430 }
2431
Winson Chungb745afb2015-03-02 11:51:23 -08002432 if (isAppsViewVisible()) {
2433 showWorkspace(true);
2434 } else if (isWidgetsViewVisible()) {
2435 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002436 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002437 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002438 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002439 Folder openFolder = mWorkspace.getOpenFolder();
2440 if (openFolder.isEditingName()) {
2441 openFolder.dismissEditingName();
2442 } else {
2443 closeFolder();
2444 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002445 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002446 mWorkspace.exitWidgetResizeMode();
2447
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002448 // Back button is a no-op here, but give at least some feedback for the button press
2449 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002450 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002451 }
2452
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002453 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002454 * Launches the intent referred by the clicked shortcut.
2455 *
2456 * @param v The view representing the clicked shortcut.
2457 */
2458 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002459 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2460 // view has detached (it's possible for this to happen if the view is removed mid touch).
2461 if (v.getWindowToken() == null) {
2462 return;
2463 }
2464
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002465 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002466 return;
2467 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002468
Adam Cohen1697b792013-09-17 19:08:21 -07002469 if (v instanceof Workspace) {
2470 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002471 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002472 }
2473 return;
2474 }
2475
2476 if (v instanceof CellLayout) {
2477 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002478 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2479 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002480 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002481 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002482 }
2483
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002484 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002485 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002486 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002487 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002488 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002489 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002490 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002491 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002492 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002493 } else if (tag instanceof AppInfo) {
2494 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002495 } else if (tag instanceof LauncherAppWidgetInfo) {
2496 if (v instanceof PendingAppWidgetHostView) {
2497 onClickPendingWidget((PendingAppWidgetHostView) v);
2498 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002499 }
2500 }
2501
Sunny Goyal70660032015-05-14 00:07:08 -07002502 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002503 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002504 return false;
2505 }
2506
Michael Jurkaaf442092010-06-10 17:01:57 -07002507 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002508 * Event handler for the app widget view which has not fully restored.
2509 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002510 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002511 if (mIsSafeModeEnabled) {
2512 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2513 return;
2514 }
2515
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002516 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002517 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002518 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2519 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2520 // This should not happen, as we make sure that an Id is allocated during bind.
2521 return;
2522 }
2523 LauncherAppWidgetProviderInfo appWidgetInfo =
2524 mAppWidgetManager.findProvider(info.providerName, info.user);
2525 if (appWidgetInfo != null) {
2526 mPendingAddWidgetId = info.appWidgetId;
2527 mPendingAddInfo.copyFrom(info);
2528 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002529
Sunny Goyald478c832016-04-01 12:04:16 -07002530 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2531 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingAddWidgetId);
2532 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
2533 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2534 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2535 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2536 }
2537 } else {
2538 LauncherAppWidgetProviderInfo appWidgetInfo =
2539 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2540 if (appWidgetInfo != null) {
2541 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2542 }
Sunny Goyalff572272014-07-23 13:58:07 -07002543 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002544 } else if (info.installProgress < 0) {
2545 // The install has not been queued
2546 final String packageName = info.providerName.getPackageName();
2547 showBrokenAppInstallDialog(packageName,
2548 new DialogInterface.OnClickListener() {
2549 public void onClick(DialogInterface dialog, int id) {
2550 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2551 }
2552 });
2553 } else {
2554 // Download has started.
2555 final String packageName = info.providerName.getPackageName();
2556 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002557 }
2558 }
2559
Sunny Goyald478c832016-04-01 12:04:16 -07002560 private void startRestoredWidgetReconfigActivity(
2561 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
2562 mPendingAddWidgetInfo = provider;
2563 mPendingAddInfo.copyFrom(info);
2564 mPendingAddWidgetId = info.appWidgetId;
2565 mAppWidgetManager.startConfigActivity(provider,
2566 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2567 }
2568
Sunny Goyalff572272014-07-23 13:58:07 -07002569 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002570 * Event handler for the "grid" button that appears on the home screen, which
2571 * enters all apps mode.
2572 *
2573 * @param v The view that was clicked.
2574 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002575 protected void onClickAllAppsButton(View v) {
2576 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002577 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002578 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002579 true /* updatePredictedApps */, false /* focusSearchBar */);
2580 }
2581 }
2582
2583 protected void onLongClickAllAppsButton(View v) {
2584 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2585 if (!isAppsViewVisible()) {
2586 showAppsView(true /* animated */, false /* resetListToTop */,
2587 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002588 }
2589 }
2590
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002591 private void showBrokenAppInstallDialog(final String packageName,
2592 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002593 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002594 .setTitle(R.string.abandoned_promises_title)
2595 .setMessage(R.string.abandoned_promise_explanation)
2596 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2597 .setNeutralButton(R.string.abandoned_clean_this,
2598 new DialogInterface.OnClickListener() {
2599 public void onClick(DialogInterface dialog, int id) {
2600 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2601 mWorkspace.removeAbandonedPromise(packageName, user);
2602 }
2603 })
2604 .create().show();
2605 return;
2606 }
2607
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002608 /**
2609 * Event handler for an app shortcut click.
2610 *
2611 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2612 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002613 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002614 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2615 Object tag = v.getTag();
2616 if (!(tag instanceof ShortcutInfo)) {
2617 throw new IllegalArgumentException("Input must be a Shortcut");
2618 }
2619
2620 // Open shortcut
2621 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002622
2623 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002624 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2625 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002626 // Launch activity anyway, framework will tell the user why the app is suspended.
2627 } else {
2628 int error = R.string.activity_not_available;
2629 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2630 error = R.string.safemode_shortcut_error;
2631 }
2632 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2633 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002634 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002635 }
2636
Chris Wren40c5ed32014-06-24 18:24:23 -04002637 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002638 if ((v instanceof BubbleTextView)
2639 && shortcut.isPromise()
2640 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002641 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002642 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002643 new DialogInterface.OnClickListener() {
2644 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002645 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002646 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002647 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002648 return;
2649 }
2650
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002651 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002652 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002653 }
2654
Adam Cohen091440a2015-03-18 14:16:05 -07002655 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002656 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002657 final ShortcutInfo shortcut;
2658 final Intent intent;
2659 if (tag instanceof ShortcutInfo) {
2660 shortcut = (ShortcutInfo) tag;
2661 intent = shortcut.intent;
2662 int[] pos = new int[2];
2663 v.getLocationOnScreen(pos);
2664 intent.setSourceBounds(new Rect(pos[0], pos[1],
2665 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002666
Sunny Goyal508da152014-08-14 10:53:27 -07002667 } else if (tag instanceof AppInfo) {
2668 shortcut = null;
2669 intent = ((AppInfo) tag).intent;
2670 } else {
2671 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2672 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002673
2674 boolean success = startActivitySafely(v, intent, tag);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002675 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002676
2677 if (success && v instanceof BubbleTextView) {
2678 mWaitingForResume = (BubbleTextView) v;
2679 mWaitingForResume.setStayPressed(true);
2680 }
2681 }
2682
2683 /**
2684 * Event handler for a folder icon click.
2685 *
2686 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2687 */
2688 protected void onClickFolderIcon(View v) {
2689 if (LOGD) Log.d(TAG, "onClickFolder");
2690 if (!(v instanceof FolderIcon)){
2691 throw new IllegalArgumentException("Input must be a FolderIcon");
2692 }
2693
2694 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002695 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002696 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002697 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002698 }
Michael Jurka5130e402011-10-13 04:55:35 -07002699 }
2700
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002701 /**
2702 * Event handler for the (Add) Widgets button that appears after a long press
2703 * on the home screen.
2704 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002705 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002706 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002707 if (mIsSafeModeEnabled) {
2708 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2709 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002710 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002711 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002712 }
2713
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002714 /**
2715 * Event handler for the wallpaper picker button that appears after a long press
2716 * on the home screen.
2717 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002718 protected void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002719 if (!Utilities.isWallapaperAllowed(this)) {
2720 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2721 return;
2722 }
2723
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002724 String pickerPackage = getString(R.string.wallpaper_picker_package);
2725 if (TextUtils.isEmpty(pickerPackage)) {
2726 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2727 }
2728
Tony Wickham785f7a52015-08-31 17:28:32 -07002729 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2730 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002731 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER)
2732 .setPackage(pickerPackage)
2733 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset),
2734 REQUEST_PICK_WALLPAPER);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002735 }
2736
2737 /**
2738 * Event handler for a click on the settings button that appears after a long press
2739 * on the home screen.
2740 */
Sunny Goyal745bad92016-05-02 10:54:12 -07002741 private void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002742 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002743 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2744 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002745 }
2746
Adam Cohen61f560d2013-09-30 15:58:20 -07002747 public View.OnTouchListener getHapticFeedbackTouchListener() {
2748 if (mHapticFeedbackTouchListener == null) {
2749 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002750 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002751 @Override
2752 public boolean onTouch(View v, MotionEvent event) {
2753 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2754 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2755 }
2756 return false;
2757 }
2758 };
2759 }
2760 return mHapticFeedbackTouchListener;
2761 }
2762
Tony Wickhame2217252016-03-22 16:34:23 -07002763 @Override
2764 public void onAccessibilityStateChanged(boolean enabled) {
2765 mDragLayer.onAccessibilityStateChanged(enabled);
2766 }
2767
Adam Cohen9211d422014-10-07 18:14:53 -07002768 public void onDragStarted(View view) {
2769 if (isOnCustomContent()) {
2770 // Custom content screen doesn't participate in drag and drop. If on custom
2771 // content screen, move to default.
2772 moveWorkspaceToDefaultScreen();
2773 }
Adam Cohen9211d422014-10-07 18:14:53 -07002774 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002775
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002776 /**
2777 * Called when the user stops interacting with the launcher.
2778 * This implies that the user is now on the homescreen and is not doing housekeeping.
2779 */
Adam Cohen9211d422014-10-07 18:14:53 -07002780 protected void onInteractionEnd() {
2781 if (mLauncherCallbacks != null) {
2782 mLauncherCallbacks.onInteractionEnd();
2783 }
2784 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002785
2786 /**
2787 * Called when the user starts interacting with the launcher.
2788 * The possible interactions are:
2789 * - open all apps
2790 * - reorder an app shortcut, or a widget
2791 * - open the overview mode.
2792 * This is a good time to stop doing things that only make sense
2793 * when the user is on the homescreen and not doing housekeeping.
2794 */
Adam Cohen9211d422014-10-07 18:14:53 -07002795 protected void onInteractionBegin() {
2796 if (mLauncherCallbacks != null) {
2797 mLauncherCallbacks.onInteractionBegin();
2798 }
2799 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002800
Winson Chungcd99cd32015-04-29 11:03:24 -07002801 /** Updates the interaction state. */
2802 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002803 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002804 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002805 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2806 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002807 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002808 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002809 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002810 onInteractionEnd();
2811 }
2812 }
2813
Winson Chung8f1eff72015-05-28 17:33:40 -07002814 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002815 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002816 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002817 // Only launch using the new animation if the shortcut has not opted out (this is a
2818 // private contract between launcher and may be ignored in the future).
2819 boolean useLaunchAnimation = (v != null) &&
2820 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002821 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2822 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002823
Kenny Guy1317e2d2014-05-08 18:52:50 +01002824 UserHandleCompat user = null;
2825 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2826 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2827 user = userManager.getUserForSerialNumber(serialNumber);
2828 }
2829
2830 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002831 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002832 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002833 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002834 int left = 0, top = 0;
2835 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2836 if (v instanceof TextView) {
2837 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002838 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2839 if (icon != null) {
2840 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002841 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002842 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002843 width = bounds.width();
2844 height = bounds.height();
2845 }
2846 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002847 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2848 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002849 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002850 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002851 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002852 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002853 // On L devices, we use the device default slide-up transition.
2854 // On L MR1 devices, we a custom version of the slide-up transition which
2855 // doesn't have the delay present in the device default.
2856 opts = ActivityOptions.makeCustomAnimation(this,
2857 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002858 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002859 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002860 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002861
2862 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -07002863 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2864 try {
2865 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2866 // containing file Uris would cause a crash as penaltyDeathOnFileUriExposure
2867 // is enabled by default on NYC.
2868 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2869 .penaltyLog().build());
2870 // Could be launching some bookkeeping activity
2871 startActivity(intent, optsBundle);
2872 } finally {
2873 StrictMode.setVmPolicy(oldPolicy);
2874 }
Winson Chungc7450e32012-04-17 17:34:08 -07002875 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002876 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002877 launcherApps.startActivityForProfile(intent.getComponent(), user,
2878 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002879 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002880 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002881 } catch (SecurityException e) {
Sunny Goyal28c6b962015-10-12 11:42:05 -07002882 if (Utilities.ATLEAST_MARSHMALLOW && tag instanceof ItemInfo) {
2883 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2884 // corresponding permission. Show the appropriate permission prompt if that
2885 // is the case.
2886 if (intent.getComponent() == null
2887 && Intent.ACTION_CALL.equals(intent.getAction())
2888 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2889 PackageManager.PERMISSION_GRANTED) {
2890 // TODO: Rename sPendingAddItem to a generic name.
2891 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2892 0, (ItemInfo) tag);
2893 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2894 REQUEST_PERMISSION_CALL_PHONE);
2895 return false;
2896 }
2897 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002898 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002899 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002900 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002901 "or use the exported attribute for this activity. "
2902 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002903 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002904 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002905 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002906
Winson Chungbedf9232015-07-10 12:38:30 -07002907 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002908 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002909 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2910 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2911 return false;
2912 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002913 try {
2914 success = startActivity(v, intent, tag);
2915 } catch (ActivityNotFoundException e) {
2916 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2917 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2918 }
2919 return success;
2920 }
2921
Adam Cohen268c4752012-06-06 17:47:33 -07002922 /**
2923 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2924 * in the DragLayer in the exact absolute location of the original FolderIcon.
2925 */
2926 private void copyFolderIconToImage(FolderIcon fi) {
2927 final int width = fi.getMeasuredWidth();
2928 final int height = fi.getMeasuredHeight();
2929
2930 // Lazy load ImageView, Bitmap and Canvas
2931 if (mFolderIconImageView == null) {
2932 mFolderIconImageView = new ImageView(this);
2933 }
2934 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2935 mFolderIconBitmap.getHeight() != height) {
2936 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2937 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2938 }
2939
2940 DragLayer.LayoutParams lp;
2941 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2942 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2943 } else {
2944 lp = new DragLayer.LayoutParams(width, height);
2945 }
2946
Adam Cohen307fe232012-08-16 17:55:58 -07002947 // The layout from which the folder is being opened may be scaled, adjust the starting
2948 // view size by this scale factor.
2949 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002950 lp.customPosition = true;
2951 lp.x = mRectForFolderAnimation.left;
2952 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002953 lp.width = (int) (scale * width);
2954 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002955
2956 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2957 fi.draw(mFolderIconCanvas);
2958 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002959 if (fi.getFolder() != null) {
2960 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2961 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002962 }
Adam Cohen268c4752012-06-06 17:47:33 -07002963 // Just in case this image view is still in the drag layer from a previous animation,
2964 // we remove it and re-add it.
2965 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2966 mDragLayer.removeView(mFolderIconImageView);
2967 }
2968 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002969 if (fi.getFolder() != null) {
2970 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002971 }
Adam Cohen268c4752012-06-06 17:47:33 -07002972 }
2973
Adam Cohen37b2a492016-04-06 18:36:06 -07002974 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002975 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002976 FolderInfo info = (FolderInfo) fi.getTag();
2977 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2978 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002979 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2980 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002981 }
2982
Adam Cohen268c4752012-06-06 17:47:33 -07002983 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2984 copyFolderIconToImage(fi);
2985 fi.setVisibility(View.INVISIBLE);
2986
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002987 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2988 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002989 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002990 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2991 }
2992 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002993 oa.start();
2994 }
2995
Sunny Goyal935fca12015-10-13 10:19:01 -07002996 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002997 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002998 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002999
Adam Cohen268c4752012-06-06 17:47:33 -07003000 // We remove and re-draw the FolderIcon in-case it has changed
3001 mDragLayer.removeView(mFolderIconImageView);
3002 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08003003
3004 if (cl != null) {
3005 cl.clearFolderLeaveBehind();
3006 }
3007
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003008 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003009 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003010 oa.addListener(new AnimatorListenerAdapter() {
3011 @Override
3012 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003013 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003014 // Remove the ImageView copy of the FolderIcon and make the original visible.
3015 mDragLayer.removeView(mFolderIconImageView);
3016 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003017 }
3018 }
3019 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003020 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003021 if (!animate) {
3022 oa.end();
3023 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003024 }
3025
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003026 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003027 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003028 * is animated relative to the specified View. If the View is null, no animation
3029 * is played.
3030 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003031 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003032 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003033 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003034
Adam Cohenfb91f302012-06-11 15:45:18 -07003035 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003036 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3037 if (openFolder != null && openFolder != folder) {
3038 // Close any open folder before opening a folder.
3039 closeFolder();
3040 }
3041
Adam Cohena9cf38f2011-05-02 15:36:58 -07003042 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003043
Adam Cohena9cf38f2011-05-02 15:36:58 -07003044 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003045
Sunny Goyalf4066152015-04-15 09:42:19 -07003046 // While the folder is open, the position of the icon cannot change.
3047 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3048
Adam Cohen4554ee12011-08-03 16:13:21 -07003049 // Just verify that the folder hasn't already been added to the DragLayer.
3050 // There was a one-off crash where the folder had a parent already.
3051 if (folder.getParent() == null) {
3052 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003053 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003054 } else {
3055 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3056 folder.getParent() + ").");
3057 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003058 folder.animateOpen();
3059
3060 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003061
3062 // Notify the accessibility manager that this folder "window" has appeared and occluded
3063 // the workspace items
3064 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3065 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003066 }
3067
3068 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003069 closeFolder(true);
3070 }
3071
3072 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003073 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003074 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003075 if (folder.isEditingName()) {
3076 folder.dismissEditingName();
3077 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003078 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003079 }
3080 }
3081
Sunny Goyal935fca12015-10-13 10:19:01 -07003082 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003083 animate &= !Utilities.isPowerSaverOn(this);
3084
Adam Cohen4554ee12011-08-03 16:13:21 -07003085 folder.getInfo().opened = false;
3086
3087 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3088 if (parent != null) {
3089 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003090 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003091 if (fi != null) {
3092 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3093 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003094 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003095 if (animate) {
3096 folder.animateClosed();
3097 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003098 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003099 }
Winson Chung83ca4802013-04-12 15:10:52 -07003100
Sunny Goyal935fca12015-10-13 10:19:01 -07003101 // Notify the accessibility manager that this folder "window" has disappeared and no
3102 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003103 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003104 }
3105
Tony Wickhamdadb3042016-02-24 11:07:00 -08003106 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003107 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003108 if (!isDraggingEnabled()) return false;
3109 if (isWorkspaceLocked()) return false;
3110 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003111
Winson Chung76648c52015-07-10 14:33:23 -07003112 if (v == mAllAppsButton) {
3113 onLongClickAllAppsButton(v);
3114 return true;
3115 }
3116
Adam Cohen1697b792013-09-17 19:08:21 -07003117 if (v instanceof Workspace) {
3118 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003119 if (!mWorkspace.isTouchActive()) {
3120 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003121 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3122 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3123 return true;
3124 } else {
3125 return false;
3126 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003127 } else {
3128 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003129 }
Adam Cohen1697b792013-09-17 19:08:21 -07003130 }
3131
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003132 CellLayout.CellInfo longClickCellInfo = null;
3133 View itemUnderLongClick = null;
3134 if (v.getTag() instanceof ItemInfo) {
3135 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003136 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003137 itemUnderLongClick = longClickCellInfo.cell;
3138 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003139 }
3140
Winson Chung3d503fb2011-07-13 17:25:49 -07003141 // The hotseat touch handling does not go through Workspace, and we always allow long press
3142 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003143 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003144 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003145 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003146 // User long pressed on empty space
3147 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3148 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003149 if (mWorkspace.isInOverviewMode()) {
3150 mWorkspace.startReordering(v);
3151 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003152 showOverviewMode(true);
Hyunyoung Song645764e2016-06-06 14:19:02 -07003153 mHotseat.requestDisallowInterceptTouchEvent(true);
3154
Adam Cohendedbd962013-07-11 14:21:49 -07003155 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003156 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003157 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3158 mHotseat.getOrderInHotseat(
3159 longClickCellInfo.cellX,
3160 longClickCellInfo.cellY));
3161 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003162 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003163 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003164 }
3165 }
3166 }
3167 return true;
3168 }
3169
Winson Chung3d503fb2011-07-13 17:25:49 -07003170 boolean isHotseatLayout(View layout) {
3171 return mHotseat != null && layout != null &&
3172 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3173 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003174
Winson Chung3d503fb2011-07-13 17:25:49 -07003175 /**
3176 * Returns the CellLayout of the specified container at the specified screen.
3177 */
Sunny Goyal92820592015-03-02 11:31:35 -08003178 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003179 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3180 if (mHotseat != null) {
3181 return mHotseat.getLayout();
3182 } else {
3183 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003184 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003185 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003186 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003187 }
3188 }
3189
Winson Chungb745afb2015-03-02 11:51:23 -08003190 /**
3191 * For overridden classes.
3192 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003193 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003194 return isAppsViewVisible();
3195 }
3196
3197 public boolean isAppsViewVisible() {
3198 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3199 }
3200
3201 public boolean isWidgetsViewVisible() {
3202 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003203 }
3204
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003205 private void setWorkspaceBackground(int background) {
3206 switch (background) {
3207 case WORKSPACE_BACKGROUND_TRANSPARENT:
3208 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3209 break;
3210 case WORKSPACE_BACKGROUND_BLACK:
3211 getWindow().setBackgroundDrawable(null);
3212 break;
3213 default:
3214 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3215 }
Craig Mautner360310b2012-10-26 15:13:08 -07003216 }
3217
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003218 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003219 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3220 int curflags = getWindow().getAttributes().flags
3221 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3222 if (wpflags != curflags) {
3223 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3224 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003225 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003226 }
3227
Michael Jurkae326f182011-11-21 14:05:46 -08003228 @Override
3229 public void onTrimMemory(int level) {
3230 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003231 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3232 // The widget preview db can result in holding onto over
3233 // 3MB of memory for caching which isn't necessary.
3234 SQLiteDatabase.releaseMemory();
3235
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003236 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003237 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003238 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003239 if (mLauncherCallbacks != null) {
3240 mLauncherCallbacks.onTrimMemory(level);
3241 }
Michael Jurkae326f182011-11-21 14:05:46 -08003242 }
3243
Winson5c6bdbb2015-09-03 11:36:19 -07003244 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003245 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003246 }
3247
Winson5c6bdbb2015-09-03 11:36:19 -07003248 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003249 boolean changed = mState != State.WORKSPACE ||
3250 mWorkspace.getState() != Workspace.State.NORMAL;
3251 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003252 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003253 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003254 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003255
Michael Jurkab3e22d92011-10-31 15:58:33 -07003256 // Set focus to the AppsCustomize button
3257 if (mAllAppsButton != null) {
3258 mAllAppsButton.requestFocus();
3259 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003260 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003261
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003262 // Change the state *after* we've called all the transition code
3263 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003264
Winson Chung5fb63472011-02-02 17:03:37 -08003265 // Resume the auto-advance of widgets
3266 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003267 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003268
Winson Chung0f785722015-04-08 10:27:49 -07003269 if (changed) {
3270 // Send an accessibility event to announce the context change
3271 getWindow().getDecorView()
3272 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003273 }
Winson5c6bdbb2015-09-03 11:36:19 -07003274 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003275 }
3276
Winsone9f27272015-10-13 10:47:51 -07003277 /**
3278 * Shows the overview button.
3279 */
Adam Cohened307df2013-10-02 09:37:31 -07003280 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003281 showOverviewMode(animated, false);
3282 }
3283
3284 /**
3285 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3286 * onto one of the overview panel buttons.
3287 */
3288 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3289 Runnable postAnimRunnable = null;
3290 if (requestButtonFocus) {
3291 postAnimRunnable = new Runnable() {
3292 @Override
3293 public void run() {
3294 // Hitting the menu button when in touch mode does not trigger touch mode to
3295 // be disabled, so if requested, force focus on one of the overview panel
3296 // buttons.
3297 mOverviewPanel.requestFocusFromTouch();
3298 }
3299 };
3300 }
Adam Cohened307df2013-10-02 09:37:31 -07003301 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003302 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003303 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003304 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003305 }
3306
Winson Chungb745afb2015-03-02 11:51:23 -08003307 /**
3308 * Shows the apps view.
3309 */
Hyunyoung Song645764e2016-06-06 14:19:02 -07003310 public void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003311 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003312 if (resetListToTop) {
3313 mAppsView.scrollToTop();
3314 }
Winson Chung4ac30062015-05-08 17:34:17 -07003315 if (updatePredictedApps) {
3316 tryAndUpdatePredictedApps();
3317 }
Winson Chung76648c52015-07-10 14:33:23 -07003318 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003319 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003320
Winson Chungb745afb2015-03-02 11:51:23 -08003321 /**
3322 * Shows the widgets view.
3323 */
3324 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003325 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003326 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003327 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003328 }
Winson Chung76648c52015-07-10 14:33:23 -07003329 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003330
3331 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003332 @Override
3333 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003334 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003335 }
3336 });
Winson Chungb745afb2015-03-02 11:51:23 -08003337 }
3338
3339 /**
3340 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003341 *
3342 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003343 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003344 // TODO: calling method should use the return value so that when {@code false} is returned
3345 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003346 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003347 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3348 mState != State.WIDGETS_SPRING_LOADED) {
3349 return false;
3350 }
3351 if (toState != State.APPS && toState != State.WIDGETS) {
3352 return false;
3353 }
Winson Chungb745afb2015-03-02 11:51:23 -08003354
3355 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003356 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3357 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003358 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003359 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003360 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003361
Michael Jurkab3e22d92011-10-31 15:58:33 -07003362 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003363 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003364
3365 // Pause the auto-advance of widgets until we are out of AllApps
3366 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003367 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003368 closeFolder();
3369
3370 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003371 getWindow().getDecorView()
3372 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003373 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003374 }
3375
Winson Chungcd99cd32015-04-29 11:03:24 -07003376 /**
3377 * Updates the workspace and interaction state on state change, and return the animation to this
3378 * new state.
3379 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003380 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003381 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003382 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003383 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003384 updateInteraction(fromState, toState);
3385 return anim;
3386 }
3387
Jun Mukaif0033da2015-08-04 18:34:30 -07003388 public void onLauncherClingShown() {
3389 // When a launcher cling appears, it should cover the underlying layers, so their focus
3390 // should be blocked.
3391 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3392 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3393 }
3394 }
3395
3396 public void onLauncherClingDismissed() {
3397 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3398 }
3399
Hyunyoung Song3f471442015-04-08 19:01:34 -07003400 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003401 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003402 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003403 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003404 }
Winson Chungb745afb2015-03-02 11:51:23 -08003405
Winson Chung006ee262015-08-03 14:40:11 -07003406 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003407 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003408 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003409
3410 if (isAppsViewVisible()) {
3411 mState = State.APPS_SPRING_LOADED;
3412 } else if (isWidgetsViewVisible()) {
3413 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003414 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003415 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003416 } else {
3417 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003418 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003419 }
Adam Cohen7777d962011-08-18 18:58:38 -07003420
Hyunyoung Song3f471442015-04-08 19:01:34 -07003421 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003422 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003423 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003424
Winson Chunge7a03942011-08-05 15:05:12 -07003425 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003426 @Override
3427 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003428 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003429 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3430 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003431 // Before we show workspace, hide all apps again because
3432 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3433 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003434 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003435 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003436 } else {
3437 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003438 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003439 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003440 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003441 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003442
Tony Wickhame0c33232016-02-08 11:37:04 -08003443 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003444 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3445 || mState == State.WIDGETS_SPRING_LOADED;
3446 }
3447
Michael Jurkad3ef3062010-11-23 16:23:58 -08003448 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003449 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003450 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003451 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003452 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003453 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003454 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3455 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003456 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003457 }
3458
Winson Chung4ac30062015-05-08 17:34:17 -07003459 /**
3460 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3461 * resumed.
3462 */
3463 private void tryAndUpdatePredictedApps() {
3464 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003465 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003466 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003467 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003468 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003469 }
3470 }
3471 }
3472
Michael Jurkab3e22d92011-10-31 15:58:33 -07003473 void lockAllApps() {
3474 // TODO
3475 }
3476
3477 void unlockAllApps() {
3478 // TODO
3479 }
3480
Winsonf768d932015-09-25 16:12:35 -07003481 public boolean launcherCallbacksProvidesSearch() {
3482 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3483 }
3484
Michael Jurkad7c28052012-04-27 15:43:36 -07003485 @Override
3486 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003487 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003488 final List<CharSequence> text = event.getText();
3489 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003490 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003491 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003492 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003493 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003494 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003495 } else if (mWorkspace != null) {
3496 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003497 } else {
3498 text.add(getString(R.string.all_apps_home_button_label));
3499 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003500 return result;
3501 }
3502
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003503 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003504 * If the activity is currently paused, signal that we need to run the passed Runnable
3505 * in onResume.
3506 *
3507 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003508 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3509 * wrong when we're not running, and if the activity comes back to what the configuration was
3510 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003511 *
3512 * Implementation of the method from LauncherModel.Callbacks.
3513 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003514 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003515 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003516 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003517 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003518 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003519 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003520 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003521 }
3522 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003523 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003524 return true;
3525 } else {
3526 return false;
3527 }
3528 }
3529
Michael Jurkac402cd92013-05-20 15:49:32 +02003530 private boolean waitUntilResume(Runnable run) {
3531 return waitUntilResume(run, false);
3532 }
3533
Michael Jurka1e2f4652013-07-08 18:03:46 -07003534 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003535 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003536 }
3537
Michael Jurka7607c2f2013-04-03 14:33:19 -07003538 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003539 * If the activity is currently paused, signal that we need to re-run the loader
3540 * in onResume.
3541 *
3542 * This needs to be called from incoming places where resources might have been loaded
3543 * while we are paused. That is becaues the Configuration might be wrong
3544 * when we're not running, and if it comes back to what it was when we
3545 * were paused, we are not restarted.
3546 *
3547 * Implementation of the method from LauncherModel.Callbacks.
3548 *
3549 * @return true if we are currently paused. The caller might be able to
3550 * skip some work in that case since we will come back again.
3551 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003552 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003553 public boolean setLoadOnResume() {
3554 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003555 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003556 mOnResumeNeedsLoad = true;
3557 return true;
3558 } else {
3559 return false;
3560 }
3561 }
3562
3563 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003564 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003565 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003566 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003567 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003568 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003569 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003570 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003571 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003572 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003573 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003574
Joe Onorato9c1289c2009-08-17 11:03:03 -04003575 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003576 * Clear any pending bind callbacks. This is called when is loader is planning to
3577 * perform a full rebind from scratch.
3578 */
3579 @Override
3580 public void clearPendingBinds() {
3581 mBindOnResumeCallbacks.clear();
3582 if (mPendingExecutor != null) {
3583 mPendingExecutor.markCompleted();
3584 mPendingExecutor = null;
3585 }
3586 }
3587
3588 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003589 * Refreshes the shortcuts shown on the workspace.
3590 *
3591 * Implementation of the method from LauncherModel.Callbacks.
3592 */
3593 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003594 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003595
Winson Chungd64d1762013-08-20 14:37:16 -07003596 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003597 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003598 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003599
Michael Jurka05bf644e2011-11-30 20:28:53 -08003600 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003601 if (mHotseat != null) {
3602 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003603 }
3604 }
3605
Adam Cohendcd297f2013-06-18 13:13:40 -07003606 @Override
3607 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003608 // Make sure the first screen is always at the start.
3609 if (orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
3610 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3611 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3612 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Adam Cohen5084cba2013-09-03 12:01:16 -07003613 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003614 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003615
3616 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003617 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003618 if (hasCustomContentToLeft()) {
3619 mWorkspace.createCustomContentContainer();
3620 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003621 }
Winson Chung64359a52013-07-08 17:17:08 -07003622 }
3623
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003624 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003625 int count = orderedScreenIds.size();
3626 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003627 long screenId = orderedScreenIds.get(i);
3628 if (screenId != Workspace.FIRST_SCREEN_ID) {
3629 // No need to bind the first screen, as its always bound.
3630 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3631 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003632 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003633 }
3634
Winson Chungd64d1762013-08-20 14:37:16 -07003635 public void bindAppsAdded(final ArrayList<Long> newScreens,
3636 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003637 final ArrayList<ItemInfo> addAnimated,
3638 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003639 Runnable r = new Runnable() {
3640 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003641 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003642 }
3643 };
3644 if (waitUntilResume(r)) {
3645 return;
3646 }
3647
Winson Chungd64d1762013-08-20 14:37:16 -07003648 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003649 if (newScreens != null) {
3650 bindAddScreens(newScreens);
3651 }
Winson Chungd64d1762013-08-20 14:37:16 -07003652
3653 // We add the items without animation on non-visible pages, and with
3654 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003655 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003656 bindItems(addNotAnimated, 0,
3657 addNotAnimated.size(), false);
3658 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003659 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003660 bindItems(addAnimated, 0,
3661 addAnimated.size(), true);
3662 }
Winson Chungc58497e2013-09-03 17:48:37 -07003663
Winson Chung87412982013-10-03 18:34:14 -07003664 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003665 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003666
Winson Chungb745afb2015-03-02 11:51:23 -08003667 if (addedApps != null && mAppsView != null) {
3668 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003669 }
Winson Chungd64d1762013-08-20 14:37:16 -07003670 }
3671
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003672 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003673 * Bind the items start-end from the list.
3674 *
3675 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003676 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003677 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003678 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3679 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003680 Runnable r = new Runnable() {
3681 public void run() {
3682 bindItems(shortcuts, start, end, forceAnimateIcons);
3683 }
3684 };
3685 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003686 return;
3687 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003688
Winson Chungf0c6ae02012-03-21 16:10:31 -07003689 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003690 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3691 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003692 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003693 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003694 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003695 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003696 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003697
3698 // Short circuit if we are loading dock items for a configuration which has no dock
3699 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3700 mHotseat == null) {
3701 continue;
3702 }
3703
Sunny Goyal639e9062015-08-19 19:17:06 -07003704 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003705 switch (item.itemType) {
3706 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3707 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003708 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003709 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003710 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003711 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003712 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003713 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003714 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003715 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003716 default:
3717 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003718 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003719
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003720 /*
3721 * Remove colliding items.
3722 */
3723 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3724 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3725 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3726 View v = cl.getChildAt(item.cellX, item.cellY);
3727 Object tag = v.getTag();
3728 String desc = "Collision while binding workspace item: " + item
3729 + ". Collides with " + tag;
3730 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3731 throw (new RuntimeException(desc));
3732 } else {
3733 Log.d(TAG, desc);
3734 LauncherModel.deleteItemFromDatabase(this, item);
3735 continue;
3736 }
3737 }
3738 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003739 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3740 item.cellY, 1, 1);
3741 if (animateIcons) {
3742 // Animate all the applications up now
3743 view.setAlpha(0f);
3744 view.setScaleX(0f);
3745 view.setScaleY(0f);
3746 bounceAnims.add(createNewAppBounceAnimation(view, i));
3747 newShortcutsScreenId = item.screenId;
3748 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003749 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003750
Winson Chung997a9232013-07-24 15:33:46 -07003751 if (animateIcons) {
3752 // Animate to the correct page
3753 if (newShortcutsScreenId > -1) {
3754 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003755 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003756 final Runnable startBounceAnimRunnable = new Runnable() {
3757 public void run() {
3758 anim.playTogether(bounceAnims);
3759 anim.start();
3760 }
3761 };
Winson Chung997a9232013-07-24 15:33:46 -07003762 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003763 // We post the animation slightly delayed to prevent slowdowns
3764 // when we are loading right after we return to launcher.
3765 mWorkspace.postDelayed(new Runnable() {
3766 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003767 if (mWorkspace != null) {
3768 mWorkspace.snapToPage(newScreenIndex);
3769 mWorkspace.postDelayed(startBounceAnimRunnable,
3770 NEW_APPS_ANIMATION_DELAY);
3771 }
Winson Chung94d67682013-09-25 16:29:40 -07003772 }
3773 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003774 } else {
3775 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003776 }
3777 }
Winson Chung64359a52013-07-08 17:17:08 -07003778 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003779 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003780 }
3781
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003782 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3783 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3784 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003785 view.updateAppWidget(null);
3786 view.setOnClickListener(this);
3787 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003788 mWorkspace.requestLayout();
3789 }
3790
Joe Onorato9c1289c2009-08-17 11:03:03 -04003791 /**
3792 * Add the views for a widget to the workspace.
3793 *
3794 * Implementation of the method from LauncherModel.Callbacks.
3795 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003796 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003797 Runnable r = new Runnable() {
3798 public void run() {
3799 bindAppWidget(item);
3800 }
3801 };
3802 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003803 return;
3804 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003805
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003806 if (mIsSafeModeEnabled) {
3807 bindSafeModeWidget(item);
3808 return;
3809 }
3810
Daniel Sandler843e8602010-06-07 14:59:01 -04003811 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3812 if (DEBUG_WIDGETS) {
3813 Log.d(TAG, "bindAppWidget: " + item);
3814 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003815
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003816 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003817
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003818 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3819 // If the provider is not ready, bind as a pending widget.
3820 appWidgetInfo = null;
3821 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3822 // The widget id is not valid. Try to find the widget based on the provider info.
3823 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3824 } else {
3825 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3826 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003827
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003828 // If the provider is ready, but the width is not yet restored, try to restore it.
3829 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3830 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003831 if (appWidgetInfo == null) {
3832 if (DEBUG_WIDGETS) {
3833 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3834 + " belongs to component " + item.providerName
3835 + ", as the povider is null");
3836 }
3837 LauncherModel.deleteItemFromDatabase(this, item);
3838 return;
3839 }
Sunny Goyalff572272014-07-23 13:58:07 -07003840
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003841 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003842 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003843 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3844 // Id has not been allocated yet. Allocate a new id.
3845 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3846 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003847
Sunny Goyald478c832016-04-01 12:04:16 -07003848 // Also try to bind the widget. If the bind fails, the user will be shown
3849 // a click to setup UI, which will ask for the bind permission.
3850 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3851 pendingInfo.spanX = item.spanX;
3852 pendingInfo.spanY = item.spanY;
3853 pendingInfo.minSpanX = item.minSpanX;
3854 pendingInfo.minSpanY = item.minSpanY;
3855 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
3856 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3857 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003858
Sunny Goyald478c832016-04-01 12:04:16 -07003859 // Bind succeeded
3860 if (success) {
3861 // If the widget has a configure activity, it is still needs to set it up,
3862 // otherwise the widget is ready to go.
3863 item.restoreStatus = (appWidgetInfo.configure == null)
3864 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3865 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003866 }
Sunny Goyald478c832016-04-01 12:04:16 -07003867
3868 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003869 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003870 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003871 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003872 // The widget was marked as UI not ready, but there is no configure activity to
3873 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003874 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3875 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003876 }
Sunny Goyalff572272014-07-23 13:58:07 -07003877 }
3878
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003879 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003880 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003881 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3882 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003883 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003884
Sunny Goyal56c73602015-09-25 12:55:01 -07003885 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003886 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003887 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003888 deleteWidgetInfo(item);
3889 return;
3890 }
3891
Sunny Goyal233ee962015-08-03 13:05:01 -07003892 item.minSpanX = appWidgetInfo.minSpanX;
3893 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003894 addAppWidgetToWorkspace(
3895 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3896 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003897 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003898 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003899 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003900 view.updateAppWidget(null);
3901 view.setOnClickListener(this);
3902 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003903 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003904 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003905
Daniel Sandler843e8602010-06-07 14:59:01 -04003906 if (DEBUG_WIDGETS) {
3907 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3908 + (SystemClock.uptimeMillis()-start) + "ms");
3909 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003910 }
3911
Sunny Goyalff572272014-07-23 13:58:07 -07003912 /**
3913 * Restores a pending widget.
3914 *
3915 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003916 */
Sunny Goyald478c832016-04-01 12:04:16 -07003917 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003918 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3919 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3920 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003921 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003922 }
3923
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003924 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003925 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003926
3927 mWorkspace.reinflateWidgetsIfNecessary();
3928 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003929 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003930 }
3931
Adam Cohen1462de32012-07-24 22:34:36 -07003932 public void onPageBoundSynchronously(int page) {
3933 mSynchronouslyBoundPages.add(page);
3934 }
3935
Sunny Goyal527c7d32015-08-28 15:19:36 -07003936 @Override
3937 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3938 if (mPendingExecutor != null) {
3939 mPendingExecutor.markCompleted();
3940 }
3941 mPendingExecutor = executor;
3942 executor.attachTo(this);
3943 }
3944
3945 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3946 if (mPendingExecutor == executor) {
3947 mPendingExecutor = null;
3948 }
3949 }
3950
Joe Onorato9c1289c2009-08-17 11:03:03 -04003951 /**
3952 * Callback saying that there aren't any more items to bind.
3953 *
3954 * Implementation of the method from LauncherModel.Callbacks.
3955 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003956 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003957 Runnable r = new Runnable() {
3958 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003959 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003960 }
3961 };
3962 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003963 return;
3964 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003965 if (mSavedState != null) {
3966 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003967 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003968 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003969
Joe Onorato9c1289c2009-08-17 11:03:03 -04003970 mSavedState = null;
3971 }
3972
Adam Cohen1462de32012-07-24 22:34:36 -07003973 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003974
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003975 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003976
3977 // If we received the result of any pending adds while the loader was running (e.g. the
3978 // widget configuration forced an orientation change), process them now.
3979 if (sPendingAddItem != null) {
3980 final long screenId = completeAdd(sPendingAddItem);
3981
3982 // TODO: this moves the user to the page where the pending item was added. Ideally,
3983 // the screen would be guaranteed to exist after bind, and the page would be set through
3984 // the workspace restore process.
3985 mWorkspace.post(new Runnable() {
3986 @Override
3987 public void run() {
3988 mWorkspace.snapToScreenId(screenId);
3989 }
3990 });
3991 sPendingAddItem = null;
3992 }
3993
Sunny Goyal756adbc2015-04-16 15:20:51 -07003994 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003995
3996 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003997 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003998 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003999 }
4000
Winson Chunga0b7e862013-09-05 16:03:15 -07004001 public boolean isAllAppsButtonRank(int rank) {
4002 if (mHotseat != null) {
4003 return mHotseat.isAllAppsButtonRank(rank);
4004 }
4005 return false;
4006 }
4007
Winson Chunga2413752012-04-03 14:22:34 -07004008 private boolean canRunNewAppsAnimation() {
4009 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004010 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4011 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004012 }
4013
Winson Chungc9168342013-06-26 14:54:55 -07004014 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004015 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004016 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4017 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004018 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004019 return bounceAnim;
4020 }
4021
Adam Cohen27772732013-11-11 14:00:56 +00004022 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004023 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004024 }
4025
Tony Wickham55616cd2015-09-23 14:55:17 -07004026 public int getSearchBarHeight() {
4027 if (mLauncherCallbacks != null) {
4028 return mLauncherCallbacks.getSearchBarHeight();
4029 }
4030 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4031 }
4032
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004033 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004034 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4035 * multiple calls to bind the same list.)
4036 */
4037 @Thunk ArrayList<AppInfo> mTmpAppsList;
4038 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4039 public void run() {
4040 bindAllApplications(mTmpAppsList);
4041 mTmpAppsList = null;
4042 }
4043 };
4044
4045 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004046 * Add the icons for all apps.
4047 *
4048 * Implementation of the method from LauncherModel.Callbacks.
4049 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004050 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004051 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4052 mTmpAppsList = apps;
4053 return;
4054 }
4055
Winson Chungb745afb2015-03-02 11:51:23 -08004056 if (mAppsView != null) {
4057 mAppsView.setApps(apps);
4058 }
Adam Cohen9211d422014-10-07 18:14:53 -07004059 if (mLauncherCallbacks != null) {
4060 mLauncherCallbacks.bindAllApplications(apps);
4061 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004062 }
4063
4064 /**
4065 * A package was updated.
4066 *
4067 * Implementation of the method from LauncherModel.Callbacks.
4068 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004069 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004070 Runnable r = new Runnable() {
4071 public void run() {
4072 bindAppsUpdated(apps);
4073 }
4074 };
4075 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004076 return;
4077 }
4078
Winson Chungb745afb2015-03-02 11:51:23 -08004079 if (mAppsView != null) {
4080 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004081 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004082 }
4083
Sunny Goyal4390ace2014-10-13 11:33:11 -07004084 @Override
4085 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4086 Runnable r = new Runnable() {
4087 public void run() {
4088 bindWidgetsRestored(widgets);
4089 }
4090 };
4091 if (waitUntilResume(r)) {
4092 return;
4093 }
4094 mWorkspace.widgetsRestored(widgets);
4095 }
4096
Joe Onorato9c1289c2009-08-17 11:03:03 -04004097 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004098 * Some shortcuts were updated in the background.
4099 *
4100 * Implementation of the method from LauncherModel.Callbacks.
4101 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004102 @Override
4103 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4104 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004105 Runnable r = new Runnable() {
4106 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004107 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004108 }
4109 };
4110 if (waitUntilResume(r)) {
4111 return;
4112 }
4113
Sunny Goyal4390ace2014-10-13 11:33:11 -07004114 if (!updated.isEmpty()) {
4115 mWorkspace.updateShortcuts(updated);
4116 }
4117
4118 if (!removed.isEmpty()) {
4119 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4120 for (ShortcutInfo si : removed) {
4121 removedComponents.add(si.getTargetComponent());
4122 }
4123 mWorkspace.removeItemsByComponentName(removedComponents, user);
4124 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004125 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004126 }
4127 }
4128
4129 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004130 * Update the state of a package, typically related to install state.
4131 *
4132 * Implementation of the method from LauncherModel.Callbacks.
4133 */
Sunny Goyale755d462014-07-22 13:48:29 -07004134 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004135 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4136 Runnable r = new Runnable() {
4137 public void run() {
4138 bindRestoreItemsChange(updates);
4139 }
4140 };
4141 if (waitUntilResume(r)) {
4142 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004143 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004144
Sunny Goyal756adbc2015-04-16 15:20:51 -07004145 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004146 }
4147
4148 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004149 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004150 * in addition to specific applications to remove, the reason being that
4151 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004152 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004153 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004154 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004155 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004156 public void bindWorkspaceComponentsRemoved(
4157 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4158 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004159 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004160 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004161 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004162 }
Winson Chungd64d1762013-08-20 14:37:16 -07004163 };
4164 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004165 return;
4166 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004167 if (!packageNames.isEmpty()) {
4168 mWorkspace.removeItemsByPackageName(packageNames, user);
4169 }
4170 if (!components.isEmpty()) {
4171 mWorkspace.removeItemsByComponentName(components, user);
4172 }
4173 // Notify the drag controller
4174 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004175
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004176 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004177
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004178 @Override
4179 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4180 Runnable r = new Runnable() {
4181 public void run() {
4182 bindAppInfosRemoved(appInfos);
4183 }
4184 };
4185 if (waitUntilResume(r)) {
4186 return;
Joe Onorato36115782010-06-17 13:28:48 -04004187 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004188
Winson Chungdf95eb12013-10-16 14:57:07 -07004189 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004190 if (mAppsView != null) {
4191 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004192 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004193 }
Joe Onoratobe386092009-11-17 17:32:16 -08004194
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004195 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004196 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004197 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004198 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004199 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004200
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004201 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004202 public void bindWidgetsModel(WidgetsModel model) {
4203 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004204 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004205 return;
4206 }
4207
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004208 if (mWidgetsView != null && model != null) {
4209 mWidgetsView.addWidgets(model);
4210 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004211 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004212 }
4213
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004214 @Override
4215 public void notifyWidgetProvidersChanged() {
4216 if (mWorkspace.getState().shouldUpdateWidget) {
4217 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4218 }
4219 }
4220
Winson Chung400438b2011-01-16 17:53:48 -08004221 private int mapConfigurationOriActivityInfoOri(int configOri) {
4222 final Display d = getWindowManager().getDefaultDisplay();
4223 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4224 switch (d.getRotation()) {
4225 case Surface.ROTATION_0:
4226 case Surface.ROTATION_180:
4227 // We are currently in the same basic orientation as the natural orientation
4228 naturalOri = configOri;
4229 break;
4230 case Surface.ROTATION_90:
4231 case Surface.ROTATION_270:
4232 // We are currently in the other basic orientation to the natural orientation
4233 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4234 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4235 break;
4236 }
4237
4238 int[] oriMap = {
4239 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4240 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4241 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4242 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4243 };
4244 // Since the map starts at portrait, we need to offset if this device's natural orientation
4245 // is landscape.
4246 int indexOffset = 0;
4247 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4248 indexOffset = 1;
4249 }
4250 return oriMap[(d.getRotation() + indexOffset) % 4];
4251 }
Adam Cohen446e9402011-09-15 18:21:21 -07004252
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004253 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004254 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004255 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004256 if (Utilities.ATLEAST_JB_MR2) {
4257 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4258 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004259 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4260 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004261 }
Winson Chung4b919f82012-05-01 10:44:08 -07004262 }
4263 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004264
Winson Chung4b919f82012-05-01 10:44:08 -07004265 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004266 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004267 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004268 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004269 } else {
4270 mHandler.postDelayed(new Runnable() {
4271 public void run() {
4272 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4273 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004274 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004275 }
Winson Chung4b919f82012-05-01 10:44:08 -07004276 }
Winson Chung400438b2011-01-16 17:53:48 -08004277 }
4278
Adam Cohenea90f832014-05-21 15:03:34 -07004279 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004280 * To be overridden by subclasses to indicate that there is an activity to launch
4281 * before showing the standard launcher experience.
4282 */
4283 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004284 if (mLauncherCallbacks != null) {
4285 return mLauncherCallbacks.hasFirstRunActivity();
4286 }
Adam Cohen432609a2014-03-13 17:03:22 -07004287 return false;
4288 }
4289
4290 /**
4291 * To be overridden by subclasses to launch any first run activity
4292 */
4293 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004294 if (mLauncherCallbacks != null) {
4295 return mLauncherCallbacks.getFirstRunActivity();
4296 }
Adam Cohen432609a2014-03-13 17:03:22 -07004297 return null;
4298 }
4299
Winson Chunga6945242014-01-08 14:04:34 -08004300 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004301 return !ActivityManager.isRunningInTestHarness() &&
4302 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004303 }
4304
Adam Cohen4a9423d2014-03-28 14:22:31 -07004305 protected boolean hasRunFirstRunActivity() {
4306 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4307 }
4308
Adam Cohen432609a2014-03-13 17:03:22 -07004309 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004310 if (shouldRunFirstRunActivity() &&
4311 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004312 Intent firstRunIntent = getFirstRunActivity();
4313 if (firstRunIntent != null) {
4314 startActivity(firstRunIntent);
4315 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004316 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004317 }
4318 }
Adam Cohen432609a2014-03-13 17:03:22 -07004319 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004320 }
4321
4322 private void markFirstRunActivityShown() {
4323 SharedPreferences.Editor editor = mSharedPrefs.edit();
4324 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4325 editor.apply();
4326 }
4327
Adam Cohen432609a2014-03-13 17:03:22 -07004328 /**
4329 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4330 * screen that must be displayed and dismissed.
4331 */
4332 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004333 if (mLauncherCallbacks != null) {
4334 return mLauncherCallbacks.hasDismissableIntroScreen();
4335 }
Adam Cohen432609a2014-03-13 17:03:22 -07004336 return false;
4337 }
4338
4339 /**
4340 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4341 */
4342 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004343 if (mLauncherCallbacks != null) {
4344 return mLauncherCallbacks.getIntroScreen();
4345 }
Adam Cohen432609a2014-03-13 17:03:22 -07004346 return null;
4347 }
4348
4349 /**
4350 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4351 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4352 */
4353 private boolean shouldShowIntroScreen() {
4354 return hasDismissableIntroScreen() &&
4355 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4356 }
4357
4358 protected void showIntroScreen() {
4359 View introScreen = getIntroScreen();
4360 changeWallpaperVisiblity(false);
4361 if (introScreen != null) {
4362 mDragLayer.showOverlayView(introScreen);
4363 }
4364 }
4365
4366 public void dismissIntroScreen() {
4367 markIntroScreenDismissed();
4368 if (showFirstRunActivity()) {
4369 // We delay hiding the intro view until the first run activity is showing. This
4370 // avoids a blip.
4371 mWorkspace.postDelayed(new Runnable() {
4372 @Override
4373 public void run() {
4374 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004375 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004376 }
4377 }, ACTIVITY_START_DELAY);
4378 } else {
4379 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004380 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004381 }
4382 changeWallpaperVisiblity(true);
4383 }
4384
4385 private void markIntroScreenDismissed() {
4386 SharedPreferences.Editor editor = mSharedPrefs.edit();
4387 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4388 editor.apply();
4389 }
4390
Adam Cohen091440a2015-03-18 14:16:05 -07004391 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004392 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4393 // on the device, then we always show the first run cling experience (or if there is no
4394 // launcher2). Otherwise, we prompt the user upon started for migration
4395 LauncherClings launcherClings = new LauncherClings(this);
4396 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004397 mClings = launcherClings;
Sunny Goyalded0fdb2016-05-23 15:55:41 -07004398 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004399 }
4400 }
4401
Winson Chung5ffd51d2015-06-25 11:36:50 -07004402 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004403 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004404 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004405 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004406 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004407 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004408 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4409 if (userHandle != null) {
4410 user = UserHandleCompat.fromUser(userHandle);
4411 }
4412 }
4413 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004414 }
4415
4416 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004417 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004418 if (user == null) {
4419 user = UserHandleCompat.myUserHandle();
4420 }
4421
4422 // Called from search suggestion, add the profile extra to the intent to ensure that we
4423 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004424 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004425 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004426 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004427 return null;
4428 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004429 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004430 }
4431
Winson Chung5ffd51d2015-06-25 11:36:50 -07004432 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004433 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4434 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004435 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004436 UserHandleCompat.myUserHandle());
4437 }
4438
Winson Chung5ffd51d2015-06-25 11:36:50 -07004439 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004440 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4441 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004442 mWorkspace.onExternalDragStartedWithItem(dragView);
4443 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004444 }
4445
Winson Chung5ffd51d2015-06-25 11:36:50 -07004446 protected void moveWorkspaceToDefaultScreen() {
4447 mWorkspace.moveToDefaultScreen(false);
4448 }
4449
Winson Chung80baf5a2010-08-09 16:03:15 -07004450 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004451 * Returns a FastBitmapDrawable with the icon, accurately sized.
4452 */
4453 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4454 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4455 d.setFilterBitmap(true);
4456 resizeIconDrawable(d);
4457 return d;
4458 }
4459
4460 /**
4461 * Resizes an icon drawable to the correct icon size.
4462 */
Winson5fbe0742015-09-30 15:33:00 -07004463 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004464 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004465 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004466 }
4467
4468 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004469 * Prints out out state for debugging.
4470 */
4471 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004472 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004473 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004474 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4475 Log.d(TAG, "mRestoring=" + mRestoring);
4476 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004477 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004478 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004479
Daniel Sandler325dc232013-06-05 22:57:57 -04004480 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004481 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004482
4483 @Override
4484 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4485 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004486 // Dump workspace
4487 writer.println(prefix + "Workspace Items");
4488 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4489 writer.println(prefix + " Homescreen " + i);
4490
4491 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).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 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004497 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004498 }
Sunny Goyala1365452015-10-01 15:46:24 -07004499
4500 writer.println(prefix + " Hotseat");
4501 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4502 for (int j = 0; j < layout.getChildCount(); j++) {
4503 Object tag = layout.getChildAt(j).getTag();
4504 if (tag != null) {
4505 writer.println(prefix + " " + tag.toString());
4506 }
4507 }
4508
Sunny Goyal713edfc2016-05-06 09:58:34 -07004509 try {
4510 FileLog.flushAll(writer);
4511 } catch (Exception e) {
4512 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004513 }
4514
Adam Cohen9211d422014-10-07 18:14:53 -07004515 if (mLauncherCallbacks != null) {
4516 mLauncherCallbacks.dump(prefix, fd, writer, args);
4517 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004518 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004519
Adam Cohen59400422014-03-05 18:07:04 -08004520 public static CustomAppWidget getCustomAppWidget(String name) {
4521 return sCustomAppWidgets.get(name);
4522 }
4523
4524 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4525 return sCustomAppWidgets;
4526 }
4527
Sunny Goyal29538332016-02-18 09:10:19 -08004528 public static List<View> getFolderContents(View icon) {
4529 if (icon instanceof FolderIcon) {
4530 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4531 } else {
4532 return Collections.EMPTY_LIST;
4533 }
4534 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004535
4536 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4537
4538 @Override
4539 public void onSharedPreferenceChanged(
4540 SharedPreferences sharedPreferences, String key) {
4541 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4542 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4543 if (!waitUntilResume(this, true)) {
4544 run();
4545 }
4546 }
4547 }
4548
4549 @Override
4550 public void run() {
4551 setOrientation();
4552 }
4553 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004554}