blob: 1c94950c63aadc083c8703be27858486228a07ca [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 Goyal3333b0c2016-05-09 20:43:21 -0700110import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800111import com.android.launcher3.logging.LoggerUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700112import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700113import com.android.launcher3.model.WidgetsModel;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700114import com.android.launcher3.pageindicators.PageIndicatorLine;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800115import com.android.launcher3.userevent.nano.LauncherLogProto;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700116import com.android.launcher3.util.ComponentKey;
Sunny Goyal713edfc2016-05-06 09:58:34 -0700117import com.android.launcher3.logging.FileLog;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700118import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal322d5562015-06-25 19:35:49 -0700119import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700120import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700121import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700122import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700123import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700124import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700125
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700126import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700127import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700128import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700129import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800130import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700131import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700132import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700133import java.util.List;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800134import java.util.Locale;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700135
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136/**
137 * Default launcher application.
138 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100139public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700140 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
141 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700142 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700143 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700144 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
Daniel Sandlerf061f822013-06-27 13:59:36 -0400146 static final boolean PROFILE_STARTUP = false;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700147 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700148 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400149 static final boolean DEBUG_RESUME_TIME = false;
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700150 static final boolean DEBUG_LOGGING = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700151
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700153 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700154 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700155 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
Michael Jurka8b805b12012-04-18 14:23:14 -0700157 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700158 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700159 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700160
Sunny Goyal28c6b962015-10-12 11:42:05 -0700161 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
162
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700163 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
164 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
165 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
166
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700167 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
168
Mathew Inwood876a8462013-06-14 14:12:41 +0100169 /**
170 * IntentStarter uses request codes starting with this. This must be greater than all activity
171 * request codes used internally.
172 */
173 protected static final int REQUEST_LAST = 100;
174
Michael Jurka0a457bf2012-11-19 14:05:05 -0800175 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700176 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800177 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
Winson Chung2672ff92012-05-04 16:22:30 -0700179 // The Intent extra that defines whether to ignore the launch animation
180 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400181 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700182
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700183 public static final String ACTION_APPWIDGET_HOST_RESET =
184 "com.android.launcher3.intent.ACTION_APPWIDGET_HOST_RESET";
185
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 // Type: int
187 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700188 // Type: int
189 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700190 // Type: Content Values / parcelable
191 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700192 // Type: parcelable
193 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000194 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800195 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
Adam Cohen432609a2014-03-13 17:03:22 -0700197 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800198 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
199
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700200 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700201 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
202 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700203
Adam Cohen091440a2015-03-18 14:16:05 -0700204 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700205 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700206
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700207 private boolean mIsSafeModeEnabled;
208
Joe Onorato9c1289c2009-08-17 11:03:03 -0400209 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800210 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700211 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700212 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213
Winson Chunga2413752012-04-03 14:22:34 -0700214 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700215 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
216 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700217 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700218
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700219 private final BroadcastReceiver mUiBroadcastReceiver = new BroadcastReceiver() {
220
221 @Override
222 public void onReceive(Context context, Intent intent) {
223 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
224 closeSystemDialogs();
225 } else if (ACTION_APPWIDGET_HOST_RESET.equals(intent.getAction())) {
226 if (mAppWidgetHost != null) {
227 mAppWidgetHost.startListening();
228 }
229 }
230 }
231 };
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800232
Adam Cohen091440a2015-03-18 14:16:05 -0700233 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700234 private View mLauncherView;
Tony Wickhamf549dab2016-05-16 09:54:06 -0700235 private PageIndicatorLine mPageIndicator;
Adam Cohen091440a2015-03-18 14:16:05 -0700236 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800237 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700238
239 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700240
Sunny Goyalffe83f12014-08-14 17:39:34 -0700241 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700242 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700243
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700244 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800245 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800246 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700247
Michael Jurka0280c3b2010-09-17 15:00:07 -0700248 private int[] mTmpAddItemCellCoordinates = new int[2];
249
Sunny Goyal316490e2015-06-02 09:38:28 -0700250 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800251 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700252
Michael Jurka838a4ca2011-02-07 13:33:06 -0800253 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700254 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700255
Sunny Goyal47328fd2016-05-25 18:56:41 -0700256 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700257
258 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700259 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700260 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700261
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700262 // Main container view and the model for the widget tray screen.
263 @Thunk WidgetsContainerView mWidgetsView;
264 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700265
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700267 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
268 // scroll issues (because the workspace may not have been measured yet) and extra work.
269 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
270 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271
272 private SpannableStringBuilder mDefaultKeySsb = null;
273
Adam Cohen091440a2015-03-18 14:16:05 -0700274 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400275
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800276 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277 private boolean mRestoring;
278 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700279 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280
Michael Jurka1e2f4652013-07-08 18:03:46 -0700281 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700282 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700283 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700284
Joe Onorato9c1289c2009-08-17 11:03:03 -0400285 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800286 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700287 private ExtractedColors mExtractedColors;
Adam Cohen091440a2015-03-18 14:16:05 -0700288 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800289 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700290 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800291 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400292
Sunny Goyal639e9062015-08-19 19:17:06 -0700293 private LauncherClings mClings;
294
Adam Cohen61f560d2013-09-30 15:58:20 -0700295 private View.OnTouchListener mHapticFeedbackTouchListener;
296
Adam Cohended9f8d2010-11-03 13:25:16 -0700297 // Related to the auto-advancing of widgets
298 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700299 private static final int ADVANCE_INTERVAL = 20000;
300 private static final int ADVANCE_STAGGER = 250;
301
302 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700303 private long mAutoAdvanceSentTime;
304 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700305 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700306
Winson Chung400438b2011-01-16 17:53:48 -0800307 // Determines how long to wait after a rotation before restoring the screen orientation to
308 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700309 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800310
Adam Cohen091440a2015-03-18 14:16:05 -0700311 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700312
Adam Cohen1462de32012-07-24 22:34:36 -0700313 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700314 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700315
Winson Chung46353de2012-02-16 14:05:10 -0800316 // We only want to get the SharedPreferences once since it does an FS stat each time we get
317 // it from the context.
318 private SharedPreferences mSharedPrefs;
319
Winson Chungf0c6ae02012-03-21 16:10:31 -0700320 // Holds the page that we need to animate to, and the icon views that we need to animate up
321 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700322 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700323 private Bitmap mFolderIconBitmap;
324 private Canvas mFolderIconCanvas;
325 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700326
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700327 private DeviceProfile mDeviceProfile;
328
Adam Cohen4b66bf32015-09-18 12:15:19 -0700329 private boolean mMoveToDefaultScreenFromNewIntent;
330
Winson Chung13eb5272015-05-11 16:30:13 -0700331 // This is set to the view that launched the activity that navigated the user away from
332 // launcher. Since there is no callback for when the activity has finished launching, enable
333 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800334 private BubbleTextView mWaitingForResume;
335
Adam Cohen59400422014-03-05 18:07:04 -0800336 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
337 new HashMap<String, CustomAppWidget>();
338
Adam Cohen59400422014-03-05 18:07:04 -0800339 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700340 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
341 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800342 }
343 }
344
Adam Cohen091440a2015-03-18 14:16:05 -0700345 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700346 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700347 if (mWorkspace != null) {
348 mWorkspace.buildPageHardwareLayers();
349 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700350 }
351 };
352
Adam Cohendb364c32014-05-20 14:23:40 -0700353 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800354
Adam Cohen091440a2015-03-18 14:16:05 -0700355 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800356 int requestCode;
357 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700358 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700359 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800360 int cellX;
361 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700362 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800363 }
364
Hyunyoung Songaa953652016-04-19 18:30:24 -0700365 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800366
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700367 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700368 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400369
370 @Thunk void setOrientation() {
371 if (mRotationEnabled) {
372 unlockScreenOrientation(true);
373 } else {
374 setRequestedOrientation(
375 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700376 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400377 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700378
Sunny Goyal745bad92016-05-02 10:54:12 -0700379 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700380
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 @Override
382 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700383 if (DEBUG_STRICT_MODE) {
384 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
385 .detectDiskReads()
386 .detectDiskWrites()
387 .detectNetwork() // or .detectAll() for all detectable problems
388 .penaltyLog()
389 .build());
390 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
391 .detectLeakedSqlLiteObjects()
392 .detectLeakedClosableObjects()
393 .penaltyLog()
394 .penaltyDeath()
395 .build());
396 }
397
Adam Cohen9211d422014-10-07 18:14:53 -0700398 if (mLauncherCallbacks != null) {
399 mLauncherCallbacks.preOnCreate();
400 }
401
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400403
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400404 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700405
Adam Cohen2e6da152015-05-06 11:42:25 -0700406 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700407 mDeviceProfile = getResources().getConfiguration().orientation
408 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700409 app.getInvariantDeviceProfile().landscapeProfile
410 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700411
Sunny Goyalf7258242015-10-19 16:59:07 -0700412 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700413 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800414 mModel = app.setLauncher(this);
415 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700416
Joe Onorato41a12d22009-10-31 18:30:00 -0400417 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700418 mAllAppsController = new AllAppsTransitionController(this);
419 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700420
Sunny Goyalffe83f12014-08-14 17:39:34 -0700421 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700422
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700423 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
424 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700425
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700426 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
427 // this also ensures that any synchronous binding below doesn't re-trigger another
428 // LauncherModel load.
429 mPaused = false;
430
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200432 android.os.Debug.startMethodTracing(
433 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800434 }
435
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700437
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700439 mDeviceProfile.layout(this);
Tony Wickham827cef22016-03-17 15:39:39 -0700440 mExtractedColors = new ExtractedColors();
441 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442
Tony Wickhame2217252016-03-22 16:34:23 -0700443 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
444 .addAccessibilityStateChangeListener(this);
445
Joe Onorato7c312c12009-08-13 21:36:53 -0700446 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700447
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 mSavedState = savedInstanceState;
449 restoreState(mSavedState);
450
451 if (PROFILE_STARTUP) {
452 android.os.Debug.stopMethodTracing();
453 }
454
455 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700456 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700457 // If the user leaves launcher, then we should just load items asynchronously when
458 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700459 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700460 } else {
461 // We only load the page synchronously if the user rotates (or triggers a
462 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700463 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700464 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800465 }
466
467 // For handling default keys
468 mDefaultKeySsb = new SpannableStringBuilder();
469 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800470
471 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700472 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
473 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800474
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800475 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700476 // In case we are on a device with locked rotation, we should look at preferences to check
477 // if the user has specifically allowed rotation.
478 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700479 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700480 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
481 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700482 }
483
484 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
485 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400486 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700487
Adam Cohen9211d422014-10-07 18:14:53 -0700488 if (mLauncherCallbacks != null) {
489 mLauncherCallbacks.onCreate(savedInstanceState);
490 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700491
492 if (shouldShowIntroScreen()) {
493 showIntroScreen();
494 } else {
495 showFirstRunActivity();
496 showFirstRunClings();
497 }
Adam Cohen9211d422014-10-07 18:14:53 -0700498 }
499
Sunny Goyal7779d622015-06-11 16:18:39 -0700500 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700501 public void onExtractedColorsChanged() {
502 loadExtractedColorsAndColorItems();
503 }
504
505 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700506 // TODO: do this in pre-N as well, once the extraction part is complete.
507 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700508 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700509 mHotseat.updateColor(mExtractedColors, !mPaused);
Tony Wickham770217c2016-05-18 15:16:40 -0700510 if (mPageIndicator != null) {
511 mPageIndicator.updateColor(mExtractedColors);
512 }
Tony Wickham827cef22016-03-17 15:39:39 -0700513 }
514 }
515
Adam Cohen9211d422014-10-07 18:14:53 -0700516 private LauncherCallbacks mLauncherCallbacks;
517
518 public void onPostCreate(Bundle savedInstanceState) {
519 super.onPostCreate(savedInstanceState);
520 if (mLauncherCallbacks != null) {
521 mLauncherCallbacks.onPostCreate(savedInstanceState);
522 }
523 }
524
Sunny Goyal32554d12015-12-03 15:31:25 -0800525 /**
526 * Call this after onCreate to set or clear overlay.
527 */
528 public void setLauncherOverlay(LauncherOverlay overlay) {
529 if (overlay != null) {
530 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
531 }
532 mWorkspace.setLauncherOverlay(overlay);
533 }
534
Adam Cohen9211d422014-10-07 18:14:53 -0700535 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
536 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700537 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700538 private boolean mWorkspaceImportanceStored = false;
539 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700540 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800541 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700542 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
543
544 @Override
545 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700546 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700547 return;
548 }
549 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700550 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700551 if (mWorkspace != null) {
552 mWorkspaceImportanceForAccessibility =
553 mWorkspace.getImportantForAccessibility();
554 mWorkspace.setImportantForAccessibility(
555 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
556 mWorkspaceImportanceStored = true;
557 }
558 if (mHotseat != null) {
559 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
560 mHotseat.setImportantForAccessibility(
561 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
562 mHotseatImportanceStored = true;
563 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700564 }
565
566 @Override
567 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700568 if (mWorkspaceImportanceStored && mWorkspace != null) {
569 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
570 }
571 if (mHotseatImportanceStored && mHotseat != null) {
572 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
573 }
574 mWorkspaceImportanceStored = false;
575 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700576 }
577 });
Adam Cohen9211d422014-10-07 18:14:53 -0700578 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700579 }
580
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700581 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700582 public void onLauncherProviderChange() {
583 if (mLauncherCallbacks != null) {
584 mLauncherCallbacks.onLauncherProviderChange();
585 }
586 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700587
Adam Cohen9211d422014-10-07 18:14:53 -0700588 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700589 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700590 if (mLauncherCallbacks != null) {
591 return mLauncherCallbacks.hasCustomContentToLeft();
592 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700593 return false;
594 }
595
Dave Hawkeya8881582013-09-17 15:55:33 -0600596 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500597 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600598 * {@link #addToCustomContentPage}. This will only be invoked if
599 * {@link #hasCustomContentToLeft()} is {@code true}.
600 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500601 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700602 if (mLauncherCallbacks != null) {
603 mLauncherCallbacks.populateCustomContentContainer();
604 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600605 }
606
607 /**
608 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
609 * ensure the custom content page is added or removed if necessary.
610 */
611 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600612 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600613 // Not bound yet, wait for bindScreens to be called.
614 return;
615 }
616
617 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
618 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500619 mWorkspace.createCustomContentContainer();
620 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600621 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
622 mWorkspace.removeCustomContentPage();
623 }
624 }
625
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800626 /**
627 * Logger object is a singleton and does not have to be coupled with the foreground activity.
628 * Since most user event logging is done on the UI, the object is retrieved from the
629 * callback for convenience.
630 */
Hyunyoung Songaa953652016-04-19 18:30:24 -0700631 private UserEventDispatcher createUserEventDispatcher() {
632 return new UserEventDispatcher() {
633 @Override
634 public void dispatchUserEvent(LauncherLogProto.LauncherEvent ev, Intent intent) {
635 if (!DEBUG_LOGGING) {
636 return;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800637 }
Hyunyoung Songaa953652016-04-19 18:30:24 -0700638 Log.d("UserEvent", String.format(Locale.US,
639 "action:%s\nchild:%s\nparent:%s\nelapsed container %d ms session %d ms",
640 LoggerUtils.getActionStr(ev.action),
641 LoggerUtils.getTargetStr(ev.srcTarget[0]),
642 LoggerUtils.getTargetStr(ev.srcTarget[1]),
643 ev.elapsedContainerMillis,
644 ev.elapsedSessionMillis));
645 }
646 };
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800647 }
648
Hyunyoung Songaa953652016-04-19 18:30:24 -0700649 public UserEventDispatcher getUserEventDispatcher() {
650 if (mLauncherCallbacks != null) {
651 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
652 if (dispatcher != null) {
653 return dispatcher;
654 }
655 }
656
657 if (mUserEventDispatcher == null) {
658 mUserEventDispatcher = createUserEventDispatcher();
659 }
660 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800661 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100662
Hyunyoung Song3f471442015-04-08 19:01:34 -0700663 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700664 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
665 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700666 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700667 }
668
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000669 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700670 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
671 // This cast is safe as long as the id < 0x00FFFFFF
672 // Since we jail all the dynamically generated views, there should be no clashes
673 // with any other views.
674 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000675 }
676
677 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700678 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
679 * a configuration step, this allows the proper animations to run after other transitions.
680 */
Adam Cohendb364c32014-05-20 14:23:40 -0700681 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700682 long screenId = args.screenId;
683 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
684 // When the screen id represents an actual screen (as opposed to a rank) we make sure
685 // that the drop page actually exists.
686 screenId = ensurePendingDropLayoutExists(args.screenId);
687 }
Adam Cohendb364c32014-05-20 14:23:40 -0700688
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800689 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800690 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700691 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700692 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700693 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800694 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700695 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700696 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700697 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyald478c832016-04-01 12:04:16 -0700698 completeRestoreAppWidget(args.appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700699 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700700 case REQUEST_BIND_PENDING_APPWIDGET: {
701 int widgetId = args.appWidgetId;
702 LauncherAppWidgetInfo info =
703 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
704 if (info != null) {
705 // Since the view was just bound, also launch the configure activity if needed
706 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
707 .getLauncherAppWidgetInfo(widgetId);
708 if (provider != null && provider.configure != null) {
709 startRestoredWidgetReconfigActivity(provider, info);
710 }
711 }
712 break;
713 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800714 }
Michael Jurka27614d22012-04-02 06:40:22 -0700715 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
716 // if you turned the screen off and then back while in All Apps, Launcher would not
717 // return to the workspace. Clearing mAddInfo.container here fixes this issue
718 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700719 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800720 }
721
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800722 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700723 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700724 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700725 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700726 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800727 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700728
Adam Cohenad4e15c2013-10-17 16:21:35 -0700729 Runnable exitSpringLoaded = new Runnable() {
730 @Override
731 public void run() {
732 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
733 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
734 }
735 };
736
Michael Jurka8b805b12012-04-18 14:23:14 -0700737 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700738 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700739 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700740 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
741 if (resultCode == RESULT_CANCELED) {
742 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700743 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700744 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700745 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800746 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700747 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700748 }
749 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200750 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
751 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700752 // User could have free-scrolled between pages before picking a wallpaper; make sure
753 // we move to the closest one now.
754 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700755 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200756 }
757 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700758 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200759
Adam Cohened66b2b2012-01-23 17:28:51 -0800760 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700761 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700762
Adam Cohendb364c32014-05-20 14:23:40 -0700763 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800764 // We have special handling for widgets
765 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800766 final int appWidgetId;
767 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
768 : -1;
769 if (widgetId < 0) {
770 appWidgetId = pendingAddWidgetId;
771 } else {
772 appWidgetId = widgetId;
773 }
774
Adam Cohenad4e15c2013-10-17 16:21:35 -0700775 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800776 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700777 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
778 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700779 result = RESULT_CANCELED;
780 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700781 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700782 @Override
783 public void run() {
784 exitSpringLoadedDragModeDelayed(false, 0, null);
785 }
786 };
Adam Cohendb364c32014-05-20 14:23:40 -0700787 if (workspaceLocked) {
788 // No need to remove the empty screen if we're mid-binding, as the
789 // the bind will not add the empty screen.
790 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
791 } else {
792 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
793 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
794 }
Winson Chung5aaab772012-04-27 15:24:02 -0700795 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700796 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700797 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
798 // When the screen id represents an actual screen (as opposed to a rank)
799 // we make sure that the drop page actually exists.
800 mPendingAddInfo.screenId =
801 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
802 }
Adam Cohendb364c32014-05-20 14:23:40 -0700803 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
804
805 dropLayout.setDropPending(true);
806 final Runnable onComplete = new Runnable() {
807 @Override
808 public void run() {
809 completeTwoStageWidgetDrop(resultCode, appWidgetId);
810 dropLayout.setDropPending(false);
811 }
812 };
813 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
814 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
815 } else {
816 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
817 mPendingAddInfo);
818 sPendingAddItem = args;
819 }
Winson Chung5aaab772012-04-27 15:24:02 -0700820 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800821 return;
822 }
823
Sunny Goyald478c832016-04-01 12:04:16 -0700824 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
825 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700826 if (resultCode == RESULT_OK) {
827 // Update the widget view.
828 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
829 pendingAddWidgetId, mPendingAddInfo);
830 if (workspaceLocked) {
831 sPendingAddItem = args;
832 } else {
833 completeAdd(args);
834 }
835 }
836 // Leave the widget in the pending state if the user canceled the configure.
837 return;
838 }
839
Sunny Goyalaad90582015-07-09 14:22:50 -0700840 if (requestCode == REQUEST_CREATE_SHORTCUT) {
841 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
842 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
843 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
844 mPendingAddInfo);
845 if (isWorkspaceLocked()) {
846 sPendingAddItem = args;
847 } else {
848 completeAdd(args);
849 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
850 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
851 }
852 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700853 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
854 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800857 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800858
859 }
860
861 @Override
862 protected void onActivityResult(
863 final int requestCode, final int resultCode, final Intent data) {
864 handleActivityResult(requestCode, resultCode, data);
865 if (mLauncherCallbacks != null) {
866 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
867 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800868 }
869
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600870 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700871 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600872 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700873 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
874 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
875 View v = null;
876 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
877 if (layout != null) {
878 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
879 }
880 Intent intent = sPendingAddItem.intent;
881 sPendingAddItem = null;
882 if (grantResults.length > 0
883 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700884 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700885 } else {
886 // TODO: Show a snack bar with link to settings
887 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
888 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
889 }
890 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600891 if (mLauncherCallbacks != null) {
892 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
893 grantResults);
894 }
895 }
896
Adam Cohendb364c32014-05-20 14:23:40 -0700897 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
898 appWidgetId, ItemInfo info) {
899 PendingAddArguments args = new PendingAddArguments();
900 args.requestCode = requestCode;
901 args.intent = data;
902 args.container = info.container;
903 args.screenId = info.screenId;
904 args.cellX = info.cellX;
905 args.cellY = info.cellY;
906 args.appWidgetId = appWidgetId;
907 return args;
908 }
909
910 /**
911 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
912 *
913 * @param screenId the screen id to check
914 * @return the new screen, or screenId if it exists
915 */
916 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800917 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700918 if (dropLayout == null) {
919 // it's possible that the add screen was removed because it was
920 // empty and a re-bind occurred
921 mWorkspace.addExtraEmptyScreen();
922 return mWorkspace.commitExtraEmptyScreen();
923 } else {
924 return screenId;
925 }
926 }
927
Adam Cohen091440a2015-03-18 14:16:05 -0700928 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800929 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800930 Runnable onCompleteRunnable = null;
931 int animationType = 0;
932
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700933 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800934 if (resultCode == RESULT_OK) {
935 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
936 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700937 mPendingAddWidgetInfo);
938 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800939 onCompleteRunnable = new Runnable() {
940 @Override
941 public void run() {
942 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700943 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700944 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
945 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800946 }
947 };
948 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800949 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800950 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800951 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700952 if (mDragLayer.getAnimatedView() != null) {
953 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
954 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
955 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700956 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700957 // The animated view may be null in the case of a rotation during widget configuration
958 onCompleteRunnable.run();
959 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 }
961
962 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700963 protected void onStop() {
964 super.onStop();
965 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700966
967 if (mLauncherCallbacks != null) {
968 mLauncherCallbacks.onStop();
969 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700970 }
971
972 @Override
973 protected void onStart() {
974 super.onStart();
975 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700976
977 if (mLauncherCallbacks != null) {
978 mLauncherCallbacks.onStart();
979 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700980 }
981
982 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200984 long startTime = 0;
985 if (DEBUG_RESUME_TIME) {
986 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400987 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200988 }
Adam Cohen9211d422014-10-07 18:14:53 -0700989
990 if (mLauncherCallbacks != null) {
991 mLauncherCallbacks.preOnResume();
992 }
993
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700995 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700996
Winson Chung4a2afa32012-07-19 14:53:05 -0700997 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700998 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700999 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001000 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -07001001 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -07001002 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -07001003 // view after launching an app, as they may be depending on the UI to be static to
1004 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -07001005 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07001006 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08001007 } else if (mOnResumeState == State.WIDGETS) {
1008 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001009 }
1010 mOnResumeState = State.NONE;
1011
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001012 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001013 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1014 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001015
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001016 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001017 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001018 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001019 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001020 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001021 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001023 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001024 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1025 // execute them here
1026 long startTimeCallbacks = 0;
1027 if (DEBUG_RESUME_TIME) {
1028 startTimeCallbacks = System.currentTimeMillis();
1029 }
1030
Michael Jurka1e2f4652013-07-08 18:03:46 -07001031 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1032 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001033 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001034 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001035 if (DEBUG_RESUME_TIME) {
1036 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1037 (System.currentTimeMillis() - startTimeCallbacks));
1038 }
Michael Jurka447bf842013-05-15 14:52:15 +02001039 }
Michael Jurka54554252013-08-01 12:52:23 +02001040 if (mOnResumeCallbacks.size() > 0) {
1041 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1042 mOnResumeCallbacks.get(i).run();
1043 }
1044 mOnResumeCallbacks.clear();
1045 }
Winson Chunge4e50662012-01-23 14:45:13 -08001046
1047 // Reset the pressed state of icons that were locked in the press state while activities
1048 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001049 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001050 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001051 mWaitingForResume.setStayPressed(false);
1052 }
Winson Chung82963d52013-10-09 11:20:57 -07001053
Adam Cohen06dff352012-06-01 17:17:08 -07001054 // It is possible that widgets can receive updates while launcher is not in the foreground.
1055 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1056 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1057 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001058 if (!isWorkspaceLoading()) {
1059 getWorkspace().reinflateWidgetsIfNecessary();
1060 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001061
Michael Jurka447bf842013-05-15 14:52:15 +02001062 if (DEBUG_RESUME_TIME) {
1063 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1064 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001065
Adam Cohen4b66bf32015-09-18 12:15:19 -07001066 // We want to suppress callbacks about CustomContent being shown if we have just received
1067 // onNewIntent while the user was present within launcher. In that case, we post a call
1068 // to move the user to the main screen (which will occur after onResume). We don't want to
1069 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1070 // suppress here.
1071 if (mWorkspace.getCustomContentCallbacks() != null
1072 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001073 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001074 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001075 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1076 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001077 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001078 }
1079 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001080 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001081 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001082 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001083
Sunny Goyal756adbc2015-04-16 15:20:51 -07001084 if (!isWorkspaceLoading()) {
1085 // Process any items that were added while Launcher was away.
1086 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1087 }
Adam Cohen9211d422014-10-07 18:14:53 -07001088
1089 if (mLauncherCallbacks != null) {
1090 mLauncherCallbacks.onResume();
1091 }
Adam Cohen06dff352012-06-01 17:17:08 -07001092 }
1093
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001095 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001096 // Ensure that items added to Launcher are queued until Launcher returns
1097 InstallShortcutReceiver.enableInstallQueue();
1098
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001099 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001100 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001101 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001102 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001103
1104 // We call onHide() aggressively. The custom content callbacks should be able to
1105 // debounce excess onHide calls.
1106 if (mWorkspace.getCustomContentCallbacks() != null) {
1107 mWorkspace.getCustomContentCallbacks().onHide();
1108 }
Romain Guycbb89e42009-06-08 15:52:54 -07001109
Adam Cohen9211d422014-10-07 18:14:53 -07001110 if (mLauncherCallbacks != null) {
1111 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001112 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001113 }
1114
1115 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001116 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1117 // by a onResume or by scrolling otherwise.
1118 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001119
1120 // Custom content is completely hidden
1121 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001122
1123 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1124 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001125
1126 // Indicates whether the user is allowed to scroll away from the custom content.
1127 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001128 }
1129
Adam Cohenc2d6e892014-10-16 09:49:24 -07001130 public interface LauncherOverlay {
1131
1132 /**
1133 * Touch interaction leading to overscroll has begun
1134 */
1135 public void onScrollInteractionBegin();
1136
1137 /**
1138 * Touch interaction related to overscroll has ended
1139 */
1140 public void onScrollInteractionEnd();
1141
1142 /**
1143 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1144 * screen (or in the case of RTL, the rightmost screen).
1145 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001146 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001147
1148 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001149 * Called when the launcher is ready to use the overlay
1150 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001151 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001152 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001153 }
1154
Jun Mukai8af0cd82015-05-12 12:32:12 -07001155 public interface LauncherSearchCallbacks {
1156 /**
1157 * Called when the search overlay is shown.
1158 */
1159 public void onSearchOverlayOpened();
1160
1161 /**
1162 * Called when the search overlay is dismissed.
1163 */
1164 public void onSearchOverlayClosed();
1165 }
1166
Adam Cohenc2d6e892014-10-16 09:49:24 -07001167 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001168 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001169 }
1170
1171 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1172
Sunny Goyalc86df472016-02-25 09:19:38 -08001173 public void onScrollChanged(float progress) {
1174 if (mWorkspace != null) {
1175 mWorkspace.onOverlayScrollChanged(progress);
1176 }
1177 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001178 }
1179
Jorim Jaggid017f882014-01-14 17:08:48 -08001180 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001181 if (mLauncherCallbacks != null) {
1182 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001183 } else {
1184 // On devices with a locked orientation, we will at least have the allow rotation
1185 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001186 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001187 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001188 }
1189
Adam Cohen9211d422014-10-07 18:14:53 -07001190 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001191 CustomContentCallbacks callbacks, String description) {
1192 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001193 }
1194
Adam Cohenedb40762013-07-18 16:45:45 -07001195 // The custom content needs to offset its content to account for the QSB
1196 public int getTopOffsetForCustomContent() {
1197 return mWorkspace.getPaddingTop();
1198 }
1199
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001200 @Override
1201 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001202 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001203 if (mModel.isCurrentCallbacks(this)) {
1204 mModel.stopLoader();
1205 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001206 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1207
Romain Guy13c2e7b2010-03-10 19:45:00 -08001208 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001209 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001210
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001211 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001212 @Override
1213 public void onWindowFocusChanged(boolean hasFocus) {
1214 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001215 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001216
1217 if (mLauncherCallbacks != null) {
1218 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1219 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001220 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001221
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 private boolean acceptFilter() {
1223 final InputMethodManager inputManager = (InputMethodManager)
1224 getSystemService(Context.INPUT_METHOD_SERVICE);
1225 return !inputManager.isFullscreenMode();
1226 }
1227
1228 @Override
1229 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001230 final int uniChar = event.getUnicodeChar();
1231 final boolean handled = super.onKeyDown(keyCode, event);
1232 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1233 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1235 keyCode, event);
1236 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001237 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001238 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001239 // showSearchDialog()
1240 // If there are multiple keystrokes before the search dialog takes focus,
1241 // onSearchRequested() will be called for every keystroke,
1242 // but it is idempotent, so it's fine.
1243 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 }
1245 }
1246
Joe Onorato8a9625e2010-01-28 15:55:35 -08001247 // Eat the long press event so the keyboard doesn't come up.
1248 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1249 return true;
1250 }
1251
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 return handled;
1253 }
1254
Winson46163472015-09-22 17:31:56 -07001255 @Override
1256 public boolean onKeyUp(int keyCode, KeyEvent event) {
1257 if (keyCode == KeyEvent.KEYCODE_MENU) {
1258 // Ignore the menu key if we are currently dragging or are on the custom content screen
1259 if (!isOnCustomContent() && !mDragController.isDragging()) {
1260 // Close any open folders
1261 closeFolder();
1262
1263 // Stop resizing any widgets
1264 mWorkspace.exitWidgetResizeMode();
1265
1266 // Show the overview mode if we are on the workspace
1267 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1268 !mWorkspace.isSwitchingState()) {
1269 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001270 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001271 }
1272 }
1273 return true;
1274 }
1275 return super.onKeyUp(keyCode, event);
1276 }
1277
Karl Rosaen138a0412009-04-23 19:00:21 -07001278 private String getTypedText() {
1279 return mDefaultKeySsb.toString();
1280 }
1281
1282 private void clearTypedText() {
1283 mDefaultKeySsb.clear();
1284 mDefaultKeySsb.clearSpans();
1285 Selection.setSelection(mDefaultKeySsb, 0);
1286 }
1287
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001289 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1290 * State
1291 */
1292 private static State intToState(int stateOrdinal) {
1293 State state = State.WORKSPACE;
1294 final State[] stateValues = State.values();
1295 for (int i = 0; i < stateValues.length; i++) {
1296 if (stateValues[i].ordinal() == stateOrdinal) {
1297 state = stateValues[i];
1298 break;
1299 }
1300 }
1301 return state;
1302 }
1303
1304 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 * Restores the previous state, if it exists.
1306 *
1307 * @param savedState The previous state.
1308 */
1309 private void restoreState(Bundle savedState) {
1310 if (savedState == null) {
1311 return;
1312 }
1313
Michael Jurka883f55b2010-10-21 15:47:14 -07001314 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001315 if (state == State.APPS || state == State.WIDGETS) {
1316 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001317 }
1318
Adam Cohen21cd0022013-10-09 18:57:02 -07001319 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1320 PagedView.INVALID_RESTORE_PAGE);
1321 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001322 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 }
1324
Sunny Goyal756cd262015-08-20 12:33:21 -07001325 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1326 if (itemValues != null) {
1327 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001328 AppWidgetProviderInfo info = savedState.getParcelable(
1329 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001330 mPendingAddWidgetInfo = info == null ?
1331 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1332
Adam Cohen4637b5a2013-11-04 18:21:24 -08001333 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001334 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 mRestoring = true;
1336 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 }
1338
1339 /**
1340 * Finds all the views we need and configure them properly.
1341 */
1342 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001343 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001344 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001345 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
1346
Winson Chung4c98d922011-05-31 16:50:48 -07001347 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Tony Wickhamf549dab2016-05-16 09:54:06 -07001348 mPageIndicator = (PageIndicatorLine) mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001349
Sunny Goyal784f9c32016-03-23 16:56:54 -07001350 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1351 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1352 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001353 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001354
Winson Chung4c98d922011-05-31 16:50:48 -07001355 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001356
1357 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001358
Winson Chung3d503fb2011-07-13 17:25:49 -07001359 // Setup the hotseat
1360 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1361 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001362 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001363 }
1364
Winsone9f27272015-10-13 10:47:51 -07001365 // Setup the overview panel
1366 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001367
Winson Chung4c98d922011-05-31 16:50:48 -07001368 // Setup the workspace
1369 mWorkspace.setHapticFeedbackEnabled(false);
1370 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001371 mWorkspace.setup(mDragController);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001372 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001373 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001374
Tony Wickham34d2c912015-09-11 09:27:58 -07001375 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001376 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001377
Winson Chungef7f8742015-06-04 17:18:17 -07001378 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001379 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001380 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001381 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1382 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1383 } else {
1384 mAppsView.setSearchBarController(new DefaultAppSearchController());
1385 }
Craig Mautner360310b2012-10-26 15:13:08 -07001386
Winson Chung3d503fb2011-07-13 17:25:49 -07001387 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001388 mDragController.setDragScoller(mWorkspace);
1389 mDragController.setScrollView(mDragLayer);
1390 mDragController.setMoveTarget(mWorkspace);
1391 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001392 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001393
Sunny Goyal322d5562015-06-25 19:35:49 -07001394 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1395 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001396 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 }
1398
Winsone9f27272015-10-13 10:47:51 -07001399 private void setupOverviewPanel() {
1400 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1401
1402 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1403 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1404 @Override
1405 public boolean onLongClick(View v) {
1406 return v.performClick();
1407 }
1408 };
1409
1410 // Bind wallpaper button actions
1411 View wallpaperButton = findViewById(R.id.wallpaper_button);
1412 wallpaperButton.setOnClickListener(new OnClickListener() {
1413 @Override
1414 public void onClick(View view) {
1415 if (!mWorkspace.isSwitchingState()) {
1416 onClickWallpaperPicker(view);
1417 }
1418 }
1419 });
1420 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1421 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1422
1423 // Bind widget button actions
1424 mWidgetsButton = findViewById(R.id.widget_button);
1425 mWidgetsButton.setOnClickListener(new OnClickListener() {
1426 @Override
1427 public void onClick(View view) {
1428 if (!mWorkspace.isSwitchingState()) {
1429 onClickAddWidgetButton(view);
1430 }
1431 }
1432 });
1433 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1434 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1435
1436 // Bind settings actions
1437 View settingsButton = findViewById(R.id.settings_button);
1438 boolean hasSettings = hasSettings();
1439 if (hasSettings) {
1440 settingsButton.setOnClickListener(new OnClickListener() {
1441 @Override
1442 public void onClick(View view) {
1443 if (!mWorkspace.isSwitchingState()) {
1444 onClickSettingsButton(view);
1445 }
1446 }
1447 });
1448 settingsButton.setOnLongClickListener(performClickOnLongClick);
1449 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1450 } else {
1451 settingsButton.setVisibility(View.GONE);
1452 }
1453
1454 mOverviewPanel.setAlpha(0f);
1455 }
1456
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001458 * Sets the all apps button. This method is called from {@link Hotseat}.
1459 */
1460 public void setAllAppsButton(View allAppsButton) {
1461 mAllAppsButton = allAppsButton;
1462 }
1463
1464 public View getAllAppsButton() {
1465 return mAllAppsButton;
1466 }
1467
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001468 public View getWidgetsButton() {
1469 return mWidgetsButton;
1470 }
1471
Anjali Koppal5ad44842014-03-10 20:34:39 -07001472 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 * Creates a view representing a shortcut.
1474 *
1475 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001477 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001478 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 }
1480
1481 /**
1482 * Creates a view representing a shortcut inflated from the specified resource.
1483 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 * @param parent The group the shortcut belongs to.
1485 * @param info The data structure describing the shortcut.
1486 *
1487 * @return A View inflated from layoutResId.
1488 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001489 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001490 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001491 parent, false);
1492 favorite.applyFromShortcutInfo(info, mIconCache);
1493 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001495 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 return favorite;
1497 }
1498
1499 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 * Add a shortcut to the workspace.
1501 *
1502 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001504 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001505 int cellY) {
1506 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001507 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001508
Sunny Goyal5c97f512015-05-19 16:03:28 -07001509 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001510 if (info == null) {
1511 return;
1512 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001513 final View view = createShortcut(info);
1514
Sunny Goyal5c97f512015-05-19 16:03:28 -07001515 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001516 // First we check if we already know the exact location where we want to add this item.
1517 if (cellX >= 0 && cellY >= 0) {
1518 cellXY[0] = cellX;
1519 cellXY[1] = cellY;
1520 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001521
1522 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001523 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001524 true, null,null)) {
1525 return;
1526 }
1527 DragObject dragObject = new DragObject();
1528 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001529 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1530 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001531 return;
1532 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001533 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001534 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001535 }
1536
1537 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001538 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001539 return;
1540 }
1541
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001542 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543
1544 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001545 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001546 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 }
1548 }
1549
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001551 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001553 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 */
Adam Cohen091440a2015-03-18 14:16:05 -07001555 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001556 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1557
1558 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001559 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001560 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001561 }
Romain Guycbb89e42009-06-08 15:52:54 -07001562
Adam Cohen59400422014-03-05 18:07:04 -08001563 if (appWidgetInfo.isCustomWidget) {
1564 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001565 }
1566
Adam Cohen59400422014-03-05 18:07:04 -08001567 LauncherAppWidgetInfo launcherInfo;
1568 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1569 launcherInfo.spanX = info.spanX;
1570 launcherInfo.spanY = info.spanY;
1571 launcherInfo.minSpanX = info.minSpanX;
1572 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001573 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001574
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001576 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577
1578 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001579 if (hostView == null) {
1580 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001581 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001582 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001583 hostView.setVisibility(View.VISIBLE);
1584 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001586 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 }
Romain Guycbb89e42009-06-08 15:52:54 -07001588
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001589 private void addAppWidgetToWorkspace(
1590 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001591 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001592 hostView.setTag(item);
1593 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001594
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001595 hostView.setFocusable(true);
1596 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001597
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001598 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001599 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1600
1601 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001602 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001603 }
1604 }
1605
Adam Cohended9f8d2010-11-03 13:25:16 -07001606 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1607 @Override
1608 public void onReceive(Context context, Intent intent) {
1609 final String action = intent.getAction();
1610 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1611 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001612 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001613 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001614
Winson Chungc100e8e2011-08-09 16:02:43 -07001615 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001616 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001617 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001618 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001619 if (!showWorkspace(false)) {
1620 // If we are already on the workspace, then manually reset all apps
1621 mAppsView.reset();
1622 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001623 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001624 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1625 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001626 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001627 }
1628 }
1629 };
1630
1631 @Override
1632 public void onAttachedToWindow() {
1633 super.onAttachedToWindow();
1634
1635 // Listen for broadcasts related to user-presence
1636 final IntentFilter filter = new IntentFilter();
1637 filter.addAction(Intent.ACTION_SCREEN_OFF);
1638 filter.addAction(Intent.ACTION_USER_PRESENT);
1639 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001640 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001641 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001642 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001643
1644 if (mLauncherCallbacks != null) {
1645 mLauncherCallbacks.onAttachedToWindow();
1646 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001647 }
1648
1649 @Override
1650 public void onDetachedFromWindow() {
1651 super.onDetachedFromWindow();
1652 mVisible = false;
1653
Adam Cohend113e0c2010-11-11 10:48:05 -08001654 if (mAttached) {
1655 unregisterReceiver(mReceiver);
1656 mAttached = false;
1657 }
Winson Chungb745afb2015-03-02 11:51:23 -08001658 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001659
1660 if (mLauncherCallbacks != null) {
1661 mLauncherCallbacks.onDetachedFromWindow();
1662 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001663 }
1664
1665 public void onWindowVisibilityChanged(int visibility) {
1666 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001667 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001668 // The following code used to be in onResume, but it turns out onResume is called when
1669 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1670 // is a more appropriate event to handle
1671 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001672 if (!mWorkspaceLoading) {
1673 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001674 // We want to let Launcher draw itself at least once before we force it to build
1675 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001676 // apps is nice and speedy.
1677 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001678 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001679 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001680 if (mStarted) return;
1681 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001682 // We delay the layer building a bit in order to give
1683 // other message processing a time to run. In particular
1684 // this avoids a delay in hiding the IME if it was
1685 // currently shown, because doing that may involve
1686 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001687 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001688 final ViewTreeObserver.OnDrawListener listener = this;
1689 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001690 public void run() {
1691 if (mWorkspace != null &&
1692 mWorkspace.getViewTreeObserver() != null) {
1693 mWorkspace.getViewTreeObserver().
1694 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001695 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001696 }
1697 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001698 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001699 }
1700 });
1701 }
1702 clearTypedText();
1703 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001704 }
1705
Adam Cohen091440a2015-03-18 14:16:05 -07001706 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001707 mHandler.removeMessages(ADVANCE_MSG);
1708 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1709 mHandler.sendMessageDelayed(msg, delay);
1710 mAutoAdvanceSentTime = System.currentTimeMillis();
1711 }
1712
Adam Cohen091440a2015-03-18 14:16:05 -07001713 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001714 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1715 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1716 mAutoAdvanceRunning = autoAdvanceRunning;
1717 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001718 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001719 sendAdvanceMessage(delay);
1720 } else {
1721 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001722 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001723 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1724 }
1725 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001726 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001727 }
1728 }
1729 }
1730
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001731 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1732
Adam Cohended9f8d2010-11-03 13:25:16 -07001733 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001734 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001735 if (msg.what == ADVANCE_MSG) {
1736 int i = 0;
1737 for (View key: mWidgetsToAdvance.keySet()) {
1738 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001739 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001740 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001741 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001742 public void run() {
1743 ((Advanceable) v).advance();
1744 }
1745 }, delay);
1746 }
1747 i++;
1748 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001749 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001750 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001751 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001752 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001753 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001754
Winsonc0b52fe2015-09-09 16:38:15 -07001755 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001756 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001757 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1758 if (v instanceof Advanceable) {
1759 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001760 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001761 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001762 }
1763 }
1764
Winsonc0b52fe2015-09-09 16:38:15 -07001765 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001766 if (mWidgetsToAdvance.containsKey(hostView)) {
1767 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001768 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001769 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001770 }
1771
Hyunyoung Song3f471442015-04-08 19:01:34 -07001772 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001773 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1774 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001775 }
1776
Winson Chunga6945242014-01-08 14:04:34 -08001777 public DragLayer getDragLayer() {
1778 return mDragLayer;
1779 }
1780
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001781 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001782 return mAppsView;
1783 }
1784
Hyunyoung Song3f471442015-04-08 19:01:34 -07001785 public WidgetsContainerView getWidgetsView() {
1786 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001787 }
1788
Winson Chunga6945242014-01-08 14:04:34 -08001789 public Workspace getWorkspace() {
1790 return mWorkspace;
1791 }
1792
1793 public Hotseat getHotseat() {
1794 return mHotseat;
1795 }
1796
Jorim Jaggid017f882014-01-14 17:08:48 -08001797 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001798 return mOverviewPanel;
1799 }
1800
Sunny Goyal47328fd2016-05-25 18:56:41 -07001801 public DropTargetBar getDropTargetBar() {
1802 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001803 }
1804
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001805 public LauncherAppWidgetHost getAppWidgetHost() {
1806 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807 }
Romain Guycbb89e42009-06-08 15:52:54 -07001808
Winson Chunga9abd0e2010-10-27 17:18:37 -07001809 public LauncherModel getModel() {
1810 return mModel;
1811 }
1812
Adam Cohen79d90c52016-04-22 13:29:20 -07001813 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001814 return mSharedPrefs;
1815 }
1816
Adam Cohen2e6da152015-05-06 11:42:25 -07001817 public DeviceProfile getDeviceProfile() {
1818 return mDeviceProfile;
1819 }
1820
Bjorn Bringertc459e522013-06-07 19:36:01 +01001821 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001822 getWindow().closeAllPanels();
1823
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001824 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001825 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001826 }
1827
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001828 @Override
1829 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001830 long startTime = 0;
1831 if (DEBUG_RESUME_TIME) {
1832 startTime = System.currentTimeMillis();
1833 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834 super.onNewIntent(intent);
1835
1836 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001837 Folder openFolder = mWorkspace.getOpenFolder();
1838 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1839 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1840 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1841 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1842 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001843 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001844 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001845
Adam Cohen6fecd412013-10-02 17:41:50 -07001846 if (mWorkspace == null) {
1847 // Can be cases where mWorkspace is null, this prevents a NPE
1848 return;
1849 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001850 // In all these cases, only animate if we're already on home
1851 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001852
Sunny Goyal935fca12015-10-13 10:19:01 -07001853 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001854 exitSpringLoadedDragMode();
1855
1856 // If we are already on home, then just animate back to the workspace,
1857 // otherwise, just wait until onResume to set the state back to Workspace
1858 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001859 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001860 } else {
1861 mOnResumeState = State.WORKSPACE;
1862 }
1863
1864 final View v = getWindow().peekDecorView();
1865 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001866 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001867 INPUT_METHOD_SERVICE);
1868 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1869 }
1870
Winson Chungb745afb2015-03-02 11:51:23 -08001871 // Reset the apps view
1872 if (!alreadyOnHome && mAppsView != null) {
1873 mAppsView.scrollToTop();
1874 }
1875
Hyunyoung Song3f471442015-04-08 19:01:34 -07001876 // Reset the widgets view
1877 if (!alreadyOnHome && mWidgetsView != null) {
1878 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001879 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001880
Adam Cohen9211d422014-10-07 18:14:53 -07001881 if (mLauncherCallbacks != null) {
1882 mLauncherCallbacks.onHomeIntent();
1883 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001884 }
Adam Cohened307df2013-10-02 09:37:31 -07001885
Adam Cohen9211d422014-10-07 18:14:53 -07001886 if (mLauncherCallbacks != null) {
1887 mLauncherCallbacks.onNewIntent(intent);
1888 }
Winson15f8b172015-08-19 11:02:39 -07001889
1890 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1891 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1892 // animation.
1893 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001894 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1895 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001896 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1897 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001898
1899 // We use this flag to suppress noisy callbacks above custom content state
1900 // from onResume.
1901 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001902 mWorkspace.post(new Runnable() {
1903 @Override
1904 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001905 if (mWorkspace != null) {
1906 mWorkspace.moveToDefaultScreen(true);
1907 }
Winson15f8b172015-08-19 11:02:39 -07001908 }
1909 });
1910 }
1911 }
1912
1913 if (DEBUG_RESUME_TIME) {
1914 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1915 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001916 }
1917
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001919 public void onRestoreInstanceState(Bundle state) {
1920 super.onRestoreInstanceState(state);
1921 for (int page: mSynchronouslyBoundPages) {
1922 mWorkspace.restoreInstanceStateForChild(page);
1923 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924 }
1925
1926 @Override
1927 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001928 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001929 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1930 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001931
Adam Cohen21cd0022013-10-09 18:57:02 -07001932 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001933 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934
Michael Jurka883f55b2010-10-21 15:47:14 -07001935 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001936 // We close any open folder since it will not be re-opened, and we need to make sure
1937 // this state is reflected.
1938 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1939 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940
Adam Cohendcd297f2013-06-18 13:13:40 -07001941 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001942 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001943 ContentValues itemValues = new ContentValues();
1944 mPendingAddInfo.writeToValues(itemValues);
1945 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001946 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001947 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 }
1949
Adam Cohen9211d422014-10-07 18:14:53 -07001950 if (mLauncherCallbacks != null) {
1951 mLauncherCallbacks.onSaveInstanceState(outState);
1952 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953 }
1954
1955 @Override
1956 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001958
Winson Chunge7a03942011-08-05 15:05:12 -07001959 // Remove all pending runnables
1960 mHandler.removeMessages(ADVANCE_MSG);
1961 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001962 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001963 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001964
Winson Chungcd2b0142011-06-08 16:02:26 -07001965 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001966 // It's possible to receive onDestroy after a new Launcher activity has
1967 // been created. In this case, don't interfere with the new Launcher.
1968 if (mModel.isCurrentCallbacks(this)) {
1969 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001970 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001971 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001972
Sunny Goyal745bad92016-05-02 10:54:12 -07001973 if (mRotationPrefChangeHandler != null) {
1974 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1975 }
1976
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001978 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001980 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001982 mAppWidgetHost = null;
1983
1984 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985
1986 TextKeyListener.getInstance().release();
1987
Tony Wickhame2217252016-03-22 16:34:23 -07001988 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1989 .removeAccessibilityStateChangeListener(this);
1990
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001991 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001992
Michael Jurka2ecf9952012-06-18 12:52:28 -07001993 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001994
1995 if (mLauncherCallbacks != null) {
1996 mLauncherCallbacks.onDestroy();
1997 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 }
1999
Adam Cohena9cf38f2011-05-02 15:36:58 -07002000 public DragController getDragController() {
2001 return mDragController;
2002 }
2003
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002004 @Override
2005 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002006 onStartForResult(requestCode);
2007 super.startActivityForResult(intent, requestCode);
2008 }
2009
2010 @Override
2011 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2012 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2013 onStartForResult(requestCode);
2014 try {
2015 super.startIntentSenderForResult(intent, requestCode,
2016 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2017 } catch (IntentSender.SendIntentException e) {
2018 throw new ActivityNotFoundException();
2019 }
2020 }
2021
2022 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002023 if (requestCode >= 0) {
2024 setWaitingForResult(true);
2025 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 }
2027
Winson Chung70d72102011-08-12 11:24:35 -07002028 /**
2029 * Indicates that we want global search for this activity by setting the globalSearch
2030 * argument for {@link #startSearch} to true.
2031 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002033 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002035
Karl Rosaen138a0412009-04-23 19:00:21 -07002036 if (initialQuery == null) {
2037 // Use any text typed in the launcher as the initial query
2038 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002039 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 if (appSearchData == null) {
2041 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002042 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002044
2045 // TODO send proper bounds.
2046 Rect sourceBounds = null;
Romain Guycbb89e42009-06-08 15:52:54 -07002047
Ian Parkinson047036e2014-09-01 15:40:53 +01002048 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002049 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002050 if (clearTextImmediately) {
2051 clearTypedText();
2052 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002053
2054 // We need to show the workspace after starting the search
2055 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002056 }
2057
Ian Parkinson047036e2014-09-01 15:40:53 +01002058 /**
2059 * Start a text search.
2060 *
2061 * @return {@code true} if the search will start immediately, so any further keypresses
2062 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2063 * to buffer keypresses.
2064 */
2065 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002066 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002067 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2068 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2069 sourceBounds);
2070 }
2071
Michael Jurkaa33411c2012-06-14 16:18:21 -07002072 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002073 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002074 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002075 }
2076
2077 /**
2078 * Starts the global search activity. This code is a copied from SearchManager
2079 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002080 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002081 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002082 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002083 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2084 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2085 if (globalSearchActivity == null) {
2086 Log.w(TAG, "No global search activity found.");
2087 return;
2088 }
2089 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2090 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2091 intent.setComponent(globalSearchActivity);
2092 // Make sure that we have a Bundle to put source in
2093 if (appSearchData == null) {
2094 appSearchData = new Bundle();
2095 } else {
2096 appSearchData = new Bundle(appSearchData);
2097 }
Adam Cohen9211d422014-10-07 18:14:53 -07002098 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002099 if (!appSearchData.containsKey("source")) {
2100 appSearchData.putString("source", getPackageName());
2101 }
2102 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2103 if (!TextUtils.isEmpty(initialQuery)) {
2104 intent.putExtra(SearchManager.QUERY, initialQuery);
2105 }
2106 if (selectInitialQuery) {
2107 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2108 }
2109 intent.setSourceBounds(sourceBounds);
2110 try {
2111 startActivity(intent);
2112 } catch (ActivityNotFoundException ex) {
2113 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2114 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115 }
2116
Yura4f93ec62014-02-04 14:15:21 +00002117 public boolean isOnCustomContent() {
2118 return mWorkspace.isOnOrMovingToCustomContent();
2119 }
2120
Winson Chung70d72102011-08-12 11:24:35 -07002121 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002122 public boolean onPrepareOptionsMenu(Menu menu) {
2123 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002124 if (mLauncherCallbacks != null) {
2125 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2126 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002127 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002128 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002129
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002130 @Override
2131 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002132 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002133 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002134 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002135 }
2136
Joe Onorato9c1289c2009-08-17 11:03:03 -04002137 public boolean isWorkspaceLocked() {
2138 return mWorkspaceLoading || mWaitingForResult;
2139 }
2140
Adam Cohen517a7f52014-03-01 12:12:59 -08002141 public boolean isWorkspaceLoading() {
2142 return mWorkspaceLoading;
2143 }
2144
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002145 private void setWorkspaceLoading(boolean value) {
2146 boolean isLocked = isWorkspaceLocked();
2147 mWorkspaceLoading = value;
2148 if (isLocked != isWorkspaceLocked()) {
2149 onWorkspaceLockedChanged();
2150 }
2151 }
2152
2153 private void setWaitingForResult(boolean value) {
2154 boolean isLocked = isWorkspaceLocked();
2155 mWaitingForResult = value;
2156 if (isLocked != isWorkspaceLocked()) {
2157 onWorkspaceLockedChanged();
2158 }
2159 }
2160
Adam Cohen9211d422014-10-07 18:14:53 -07002161 protected void onWorkspaceLockedChanged() {
2162 if (mLauncherCallbacks != null) {
2163 mLauncherCallbacks.onWorkspaceLockedChanged();
2164 }
2165 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002166
Michael Jurka0280c3b2010-09-17 15:00:07 -07002167 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002168 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002169 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002170 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2171 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002172 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002173 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002174
Tony Wickhama0628cc2015-10-14 15:23:04 -07002175 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002176 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002177 if (LOGD) {
2178 Log.d(TAG, "Adding widget from drop");
2179 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002180 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2181 }
2182
Sunny Goyale6b63a32015-01-16 16:14:29 -08002183 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002184 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2185 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002186 if (appWidgetInfo.configure != null) {
2187 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002188 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002189
Bjorn Bringert7984c942009-12-09 15:38:25 +00002190 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002191 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2192 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2193
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002194 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002195 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002196 Runnable onComplete = new Runnable() {
2197 @Override
2198 public void run() {
2199 // Exit spring loaded mode if necessary after adding the widget
2200 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2201 null);
2202 }
2203 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002204 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002205 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002206 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002207 }
2208 }
Romain Guycbb89e42009-06-08 15:52:54 -07002209
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002210 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002211 // Close any folders that may be open.
2212 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002213 mWorkspace.moveToCustomContentScreen(animate);
2214 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002215
2216 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2217 int[] cell, int spanX, int spanY) {
2218 switch (info.itemType) {
2219 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2220 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2221 int span[] = new int[2];
2222 span[0] = spanX;
2223 span[1] = spanY;
2224 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2225 container, screenId, cell, span);
2226 break;
2227 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2228 processShortcutFromDrop(info.componentName, container, screenId, cell);
2229 break;
2230 default:
2231 throw new IllegalStateException("Unknown item type: " + info.itemType);
2232 }
2233 }
2234
Adam Cohenfbba09b2011-07-18 21:43:05 -07002235 /**
2236 * Process a shortcut drop.
2237 *
2238 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002239 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002240 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002241 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002242 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2243 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002244 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002245 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002246 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002247
2248 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002249 mPendingAddInfo.cellX = cell[0];
2250 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002251 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002252
2253 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2254 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002255 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002256 }
2257
Adam Cohenfbba09b2011-07-18 21:43:05 -07002258 /**
2259 * Process a widget drop.
2260 *
2261 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002262 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002263 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002264 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002265 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2266 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002267 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002268 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002269 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002270 mPendingAddInfo.minSpanX = info.minSpanX;
2271 mPendingAddInfo.minSpanY = info.minSpanY;
2272
Adam Cohenfbba09b2011-07-18 21:43:05 -07002273 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002274 mPendingAddInfo.cellX = cell[0];
2275 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002276 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002277 if (span != null) {
2278 mPendingAddInfo.spanX = span[0];
2279 mPendingAddInfo.spanY = span[1];
2280 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002281
Adam Cohened66b2b2012-01-23 17:28:51 -08002282 AppWidgetHostView hostView = info.boundWidget;
2283 int appWidgetId;
2284 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002285 // In the case where we've prebound the widget, we remove it from the DragLayer
2286 if (LOGD) {
2287 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2288 }
2289 getDragLayer().removeView(hostView);
2290
Adam Cohened66b2b2012-01-23 17:28:51 -08002291 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002292 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002293
2294 // Clear the boundWidget so that it doesn't get destroyed.
2295 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002296 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002297 // In this case, we either need to start an activity to get permission to bind
2298 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002299 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002300 Bundle options = info.bindOptions;
2301
Sunny Goyalffe83f12014-08-14 17:39:34 -07002302 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2303 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002304 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002305 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002306 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002307 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002308 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2309 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2310 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002311 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2312 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002313 // TODO: we need to make sure that this accounts for the options bundle.
2314 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002315 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2316 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002317 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002318 }
2319
Adam Cohendcd297f2013-06-18 13:13:40 -07002320 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002321 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002322 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002323 folderInfo.title = getText(R.string.folder_name);
2324
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002325 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002326 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2327 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002328
2329 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002330 FolderIcon newFolder =
2331 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002332 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002333 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002334 // Force measure the new folder icon
2335 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2336 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002337 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002338 }
Romain Guycbb89e42009-06-08 15:52:54 -07002339
Winsonc0b52fe2015-09-09 16:38:15 -07002340 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002341 * Unbinds the view for the specified item, and removes the item and all its children.
2342 *
2343 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002344 * @param itemInfo the {@link ItemInfo} for this view.
2345 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002346 */
Winson2949fb52015-09-24 09:56:11 -07002347 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002348 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002349 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002350 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2351 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002352 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002353 } else {
2354 mWorkspace.removeWorkspaceItem(v);
2355 }
Winsonc0b52fe2015-09-09 16:38:15 -07002356 if (deleteFromDb) {
2357 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2358 }
2359 } else if (itemInfo instanceof FolderInfo) {
2360 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002361 if (v instanceof FolderIcon) {
2362 ((FolderIcon) v).removeListeners();
2363 }
Winsonc0b52fe2015-09-09 16:38:15 -07002364 mWorkspace.removeWorkspaceItem(v);
2365 if (deleteFromDb) {
2366 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2367 }
2368 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2369 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002370 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002371 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002372 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002373 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002374 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002375
Winsonc0b52fe2015-09-09 16:38:15 -07002376 } else {
2377 return false;
2378 }
2379 return true;
2380 }
2381
2382 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002383 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002384 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002385 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002386 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002387 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002388 // Deleting an app widget ID is a void call but writes to disk before returning
2389 // to the caller...
2390 new AsyncTask<Void, Void, Void>() {
2391 public Void doInBackground(Void ... args) {
2392 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2393 return null;
2394 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002395 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002396 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002397 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002398 }
2399
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002400 @Override
2401 public boolean dispatchKeyEvent(KeyEvent event) {
2402 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2403 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002404 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002405 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002406 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002407 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002408 dumpState();
2409 return true;
2410 }
2411 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002412 }
2413 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2414 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002415 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002416 return true;
2417 }
2418 }
2419
2420 return super.dispatchKeyEvent(event);
2421 }
2422
Joe Onorato88ec0992009-11-19 13:16:06 -08002423 @Override
2424 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002425 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2426 return;
2427 }
2428
Sunny Goyal45478022015-06-08 16:52:41 -07002429 if (mDragController.isDragging()) {
2430 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002431 return;
2432 }
2433
Winson Chungb745afb2015-03-02 11:51:23 -08002434 if (isAppsViewVisible()) {
2435 showWorkspace(true);
2436 } else if (isWidgetsViewVisible()) {
2437 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002438 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002439 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002440 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002441 Folder openFolder = mWorkspace.getOpenFolder();
2442 if (openFolder.isEditingName()) {
2443 openFolder.dismissEditingName();
2444 } else {
2445 closeFolder();
2446 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002447 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002448 mWorkspace.exitWidgetResizeMode();
2449
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002450 // Back button is a no-op here, but give at least some feedback for the button press
2451 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002452 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002453 }
2454
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002455 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002456 * Launches the intent referred by the clicked shortcut.
2457 *
2458 * @param v The view representing the clicked shortcut.
2459 */
2460 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002461 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2462 // view has detached (it's possible for this to happen if the view is removed mid touch).
2463 if (v.getWindowToken() == null) {
2464 return;
2465 }
2466
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002467 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002468 return;
2469 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002470
Adam Cohen1697b792013-09-17 19:08:21 -07002471 if (v instanceof Workspace) {
2472 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002473 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002474 }
2475 return;
2476 }
2477
2478 if (v instanceof CellLayout) {
2479 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002480 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2481 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002482 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002483 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002484 }
2485
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002486 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002487 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002488 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002489 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002490 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002491 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002492 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002493 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002494 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002495 } else if (tag instanceof AppInfo) {
2496 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002497 } else if (tag instanceof LauncherAppWidgetInfo) {
2498 if (v instanceof PendingAppWidgetHostView) {
2499 onClickPendingWidget((PendingAppWidgetHostView) v);
2500 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002501 }
2502 }
2503
Sunny Goyal70660032015-05-14 00:07:08 -07002504 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002505 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002506 return false;
2507 }
2508
Michael Jurkaaf442092010-06-10 17:01:57 -07002509 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002510 * Event handler for the app widget view which has not fully restored.
2511 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002512 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002513 if (mIsSafeModeEnabled) {
2514 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2515 return;
2516 }
2517
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002518 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002519 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002520 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2521 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2522 // This should not happen, as we make sure that an Id is allocated during bind.
2523 return;
2524 }
2525 LauncherAppWidgetProviderInfo appWidgetInfo =
2526 mAppWidgetManager.findProvider(info.providerName, info.user);
2527 if (appWidgetInfo != null) {
2528 mPendingAddWidgetId = info.appWidgetId;
2529 mPendingAddInfo.copyFrom(info);
2530 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002531
Sunny Goyald478c832016-04-01 12:04:16 -07002532 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2533 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingAddWidgetId);
2534 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
2535 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2536 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2537 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2538 }
2539 } else {
2540 LauncherAppWidgetProviderInfo appWidgetInfo =
2541 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2542 if (appWidgetInfo != null) {
2543 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2544 }
Sunny Goyalff572272014-07-23 13:58:07 -07002545 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002546 } else if (info.installProgress < 0) {
2547 // The install has not been queued
2548 final String packageName = info.providerName.getPackageName();
2549 showBrokenAppInstallDialog(packageName,
2550 new DialogInterface.OnClickListener() {
2551 public void onClick(DialogInterface dialog, int id) {
2552 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2553 }
2554 });
2555 } else {
2556 // Download has started.
2557 final String packageName = info.providerName.getPackageName();
2558 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002559 }
2560 }
2561
Sunny Goyald478c832016-04-01 12:04:16 -07002562 private void startRestoredWidgetReconfigActivity(
2563 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
2564 mPendingAddWidgetInfo = provider;
2565 mPendingAddInfo.copyFrom(info);
2566 mPendingAddWidgetId = info.appWidgetId;
2567 mAppWidgetManager.startConfigActivity(provider,
2568 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2569 }
2570
Sunny Goyalff572272014-07-23 13:58:07 -07002571 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002572 * Event handler for the "grid" button that appears on the home screen, which
2573 * enters all apps mode.
2574 *
2575 * @param v The view that was clicked.
2576 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002577 protected void onClickAllAppsButton(View v) {
2578 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002579 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002580 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002581 true /* updatePredictedApps */, false /* focusSearchBar */);
2582 }
2583 }
2584
2585 protected void onLongClickAllAppsButton(View v) {
2586 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2587 if (!isAppsViewVisible()) {
2588 showAppsView(true /* animated */, false /* resetListToTop */,
2589 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002590 }
2591 }
2592
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002593 private void showBrokenAppInstallDialog(final String packageName,
2594 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002595 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002596 .setTitle(R.string.abandoned_promises_title)
2597 .setMessage(R.string.abandoned_promise_explanation)
2598 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2599 .setNeutralButton(R.string.abandoned_clean_this,
2600 new DialogInterface.OnClickListener() {
2601 public void onClick(DialogInterface dialog, int id) {
2602 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2603 mWorkspace.removeAbandonedPromise(packageName, user);
2604 }
2605 })
2606 .create().show();
2607 return;
2608 }
2609
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002610 /**
2611 * Event handler for an app shortcut click.
2612 *
2613 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2614 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002615 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002616 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2617 Object tag = v.getTag();
2618 if (!(tag instanceof ShortcutInfo)) {
2619 throw new IllegalArgumentException("Input must be a Shortcut");
2620 }
2621
2622 // Open shortcut
2623 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002624
2625 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002626 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2627 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002628 // Launch activity anyway, framework will tell the user why the app is suspended.
2629 } else {
2630 int error = R.string.activity_not_available;
2631 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2632 error = R.string.safemode_shortcut_error;
2633 }
2634 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2635 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002636 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002637 }
2638
Chris Wren40c5ed32014-06-24 18:24:23 -04002639 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002640 if ((v instanceof BubbleTextView)
2641 && shortcut.isPromise()
2642 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002643 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002644 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002645 new DialogInterface.OnClickListener() {
2646 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002647 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002648 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002649 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002650 return;
2651 }
2652
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002653 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002654 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002655 }
2656
Adam Cohen091440a2015-03-18 14:16:05 -07002657 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002658 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002659 final ShortcutInfo shortcut;
2660 final Intent intent;
2661 if (tag instanceof ShortcutInfo) {
2662 shortcut = (ShortcutInfo) tag;
2663 intent = shortcut.intent;
2664 int[] pos = new int[2];
2665 v.getLocationOnScreen(pos);
2666 intent.setSourceBounds(new Rect(pos[0], pos[1],
2667 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002668
Sunny Goyal508da152014-08-14 10:53:27 -07002669 } else if (tag instanceof AppInfo) {
2670 shortcut = null;
2671 intent = ((AppInfo) tag).intent;
2672 } else {
2673 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2674 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002675
2676 boolean success = startActivitySafely(v, intent, tag);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002677 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002678
2679 if (success && v instanceof BubbleTextView) {
2680 mWaitingForResume = (BubbleTextView) v;
2681 mWaitingForResume.setStayPressed(true);
2682 }
2683 }
2684
2685 /**
2686 * Event handler for a folder icon click.
2687 *
2688 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2689 */
2690 protected void onClickFolderIcon(View v) {
2691 if (LOGD) Log.d(TAG, "onClickFolder");
2692 if (!(v instanceof FolderIcon)){
2693 throw new IllegalArgumentException("Input must be a FolderIcon");
2694 }
2695
2696 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002697 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002698 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002699 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002700 }
Michael Jurka5130e402011-10-13 04:55:35 -07002701 }
2702
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002703 /**
2704 * Event handler for the (Add) Widgets button that appears after a long press
2705 * on the home screen.
2706 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002707 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002708 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002709 if (mIsSafeModeEnabled) {
2710 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2711 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002712 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002713 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002714 }
2715
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002716 /**
2717 * Event handler for the wallpaper picker button that appears after a long press
2718 * on the home screen.
2719 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002720 protected void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002721 if (!Utilities.isWallapaperAllowed(this)) {
2722 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2723 return;
2724 }
2725
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002726 String pickerPackage = getString(R.string.wallpaper_picker_package);
2727 if (TextUtils.isEmpty(pickerPackage)) {
2728 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2729 }
2730
Tony Wickham785f7a52015-08-31 17:28:32 -07002731 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2732 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002733 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER)
2734 .setPackage(pickerPackage)
2735 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset),
2736 REQUEST_PICK_WALLPAPER);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002737 }
2738
2739 /**
2740 * Event handler for a click on the settings button that appears after a long press
2741 * on the home screen.
2742 */
Sunny Goyal745bad92016-05-02 10:54:12 -07002743 private void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002744 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002745 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2746 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002747 }
2748
Adam Cohen61f560d2013-09-30 15:58:20 -07002749 public View.OnTouchListener getHapticFeedbackTouchListener() {
2750 if (mHapticFeedbackTouchListener == null) {
2751 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002752 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002753 @Override
2754 public boolean onTouch(View v, MotionEvent event) {
2755 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2756 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2757 }
2758 return false;
2759 }
2760 };
2761 }
2762 return mHapticFeedbackTouchListener;
2763 }
2764
Tony Wickhame2217252016-03-22 16:34:23 -07002765 @Override
2766 public void onAccessibilityStateChanged(boolean enabled) {
2767 mDragLayer.onAccessibilityStateChanged(enabled);
2768 }
2769
Adam Cohen9211d422014-10-07 18:14:53 -07002770 public void onDragStarted(View view) {
2771 if (isOnCustomContent()) {
2772 // Custom content screen doesn't participate in drag and drop. If on custom
2773 // content screen, move to default.
2774 moveWorkspaceToDefaultScreen();
2775 }
Adam Cohen9211d422014-10-07 18:14:53 -07002776 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002777
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002778 /**
2779 * Called when the user stops interacting with the launcher.
2780 * This implies that the user is now on the homescreen and is not doing housekeeping.
2781 */
Adam Cohen9211d422014-10-07 18:14:53 -07002782 protected void onInteractionEnd() {
2783 if (mLauncherCallbacks != null) {
2784 mLauncherCallbacks.onInteractionEnd();
2785 }
2786 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002787
2788 /**
2789 * Called when the user starts interacting with the launcher.
2790 * The possible interactions are:
2791 * - open all apps
2792 * - reorder an app shortcut, or a widget
2793 * - open the overview mode.
2794 * This is a good time to stop doing things that only make sense
2795 * when the user is on the homescreen and not doing housekeeping.
2796 */
Adam Cohen9211d422014-10-07 18:14:53 -07002797 protected void onInteractionBegin() {
2798 if (mLauncherCallbacks != null) {
2799 mLauncherCallbacks.onInteractionBegin();
2800 }
2801 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002802
Winson Chungcd99cd32015-04-29 11:03:24 -07002803 /** Updates the interaction state. */
2804 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002805 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002806 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002807 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2808 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002809 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002810 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002811 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002812 onInteractionEnd();
2813 }
2814 }
2815
Winson Chung8f1eff72015-05-28 17:33:40 -07002816 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002817 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002818 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002819 // Only launch using the new animation if the shortcut has not opted out (this is a
2820 // private contract between launcher and may be ignored in the future).
2821 boolean useLaunchAnimation = (v != null) &&
2822 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002823 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2824 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002825
Kenny Guy1317e2d2014-05-08 18:52:50 +01002826 UserHandleCompat user = null;
2827 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2828 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2829 user = userManager.getUserForSerialNumber(serialNumber);
2830 }
2831
2832 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002833 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002834 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002835 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002836 int left = 0, top = 0;
2837 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2838 if (v instanceof TextView) {
2839 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002840 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2841 if (icon != null) {
2842 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002843 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002844 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002845 width = bounds.width();
2846 height = bounds.height();
2847 }
2848 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002849 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2850 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002851 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002852 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002853 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002854 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002855 // On L devices, we use the device default slide-up transition.
2856 // On L MR1 devices, we a custom version of the slide-up transition which
2857 // doesn't have the delay present in the device default.
2858 opts = ActivityOptions.makeCustomAnimation(this,
2859 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002860 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002861 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002862 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002863
2864 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -07002865 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2866 try {
2867 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2868 // containing file Uris would cause a crash as penaltyDeathOnFileUriExposure
2869 // is enabled by default on NYC.
2870 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2871 .penaltyLog().build());
2872 // Could be launching some bookkeeping activity
2873 startActivity(intent, optsBundle);
2874 } finally {
2875 StrictMode.setVmPolicy(oldPolicy);
2876 }
Winson Chungc7450e32012-04-17 17:34:08 -07002877 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002878 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002879 launcherApps.startActivityForProfile(intent.getComponent(), user,
2880 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002881 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002882 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002883 } catch (SecurityException e) {
Sunny Goyal28c6b962015-10-12 11:42:05 -07002884 if (Utilities.ATLEAST_MARSHMALLOW && tag instanceof ItemInfo) {
2885 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2886 // corresponding permission. Show the appropriate permission prompt if that
2887 // is the case.
2888 if (intent.getComponent() == null
2889 && Intent.ACTION_CALL.equals(intent.getAction())
2890 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2891 PackageManager.PERMISSION_GRANTED) {
2892 // TODO: Rename sPendingAddItem to a generic name.
2893 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2894 0, (ItemInfo) tag);
2895 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2896 REQUEST_PERMISSION_CALL_PHONE);
2897 return false;
2898 }
2899 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002900 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002901 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002902 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002903 "or use the exported attribute for this activity. "
2904 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002905 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002906 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002907 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002908
Winson Chungbedf9232015-07-10 12:38:30 -07002909 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002910 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002911 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2912 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2913 return false;
2914 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002915 try {
2916 success = startActivity(v, intent, tag);
2917 } catch (ActivityNotFoundException e) {
2918 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2919 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2920 }
2921 return success;
2922 }
2923
Adam Cohen268c4752012-06-06 17:47:33 -07002924 /**
2925 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2926 * in the DragLayer in the exact absolute location of the original FolderIcon.
2927 */
2928 private void copyFolderIconToImage(FolderIcon fi) {
2929 final int width = fi.getMeasuredWidth();
2930 final int height = fi.getMeasuredHeight();
2931
2932 // Lazy load ImageView, Bitmap and Canvas
2933 if (mFolderIconImageView == null) {
2934 mFolderIconImageView = new ImageView(this);
2935 }
2936 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2937 mFolderIconBitmap.getHeight() != height) {
2938 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2939 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2940 }
2941
2942 DragLayer.LayoutParams lp;
2943 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2944 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2945 } else {
2946 lp = new DragLayer.LayoutParams(width, height);
2947 }
2948
Adam Cohen307fe232012-08-16 17:55:58 -07002949 // The layout from which the folder is being opened may be scaled, adjust the starting
2950 // view size by this scale factor.
2951 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002952 lp.customPosition = true;
2953 lp.x = mRectForFolderAnimation.left;
2954 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002955 lp.width = (int) (scale * width);
2956 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002957
2958 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2959 fi.draw(mFolderIconCanvas);
2960 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002961 if (fi.getFolder() != null) {
2962 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2963 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002964 }
Adam Cohen268c4752012-06-06 17:47:33 -07002965 // Just in case this image view is still in the drag layer from a previous animation,
2966 // we remove it and re-add it.
2967 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2968 mDragLayer.removeView(mFolderIconImageView);
2969 }
2970 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002971 if (fi.getFolder() != null) {
2972 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002973 }
Adam Cohen268c4752012-06-06 17:47:33 -07002974 }
2975
Adam Cohen37b2a492016-04-06 18:36:06 -07002976 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002977 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002978 FolderInfo info = (FolderInfo) fi.getTag();
2979 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2980 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002981 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2982 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002983 }
2984
Adam Cohen268c4752012-06-06 17:47:33 -07002985 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2986 copyFolderIconToImage(fi);
2987 fi.setVisibility(View.INVISIBLE);
2988
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002989 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2990 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002991 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002992 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2993 }
2994 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002995 oa.start();
2996 }
2997
Sunny Goyal935fca12015-10-13 10:19:01 -07002998 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002999 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003000 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003001
Adam Cohen268c4752012-06-06 17:47:33 -07003002 // We remove and re-draw the FolderIcon in-case it has changed
3003 mDragLayer.removeView(mFolderIconImageView);
3004 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08003005
3006 if (cl != null) {
3007 cl.clearFolderLeaveBehind();
3008 }
3009
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003010 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003011 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003012 oa.addListener(new AnimatorListenerAdapter() {
3013 @Override
3014 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003015 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003016 // Remove the ImageView copy of the FolderIcon and make the original visible.
3017 mDragLayer.removeView(mFolderIconImageView);
3018 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003019 }
3020 }
3021 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003022 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003023 if (!animate) {
3024 oa.end();
3025 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003026 }
3027
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003028 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003029 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003030 * is animated relative to the specified View. If the View is null, no animation
3031 * is played.
3032 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003033 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003034 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003035 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003036
Adam Cohenfb91f302012-06-11 15:45:18 -07003037 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003038 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3039 if (openFolder != null && openFolder != folder) {
3040 // Close any open folder before opening a folder.
3041 closeFolder();
3042 }
3043
Adam Cohena9cf38f2011-05-02 15:36:58 -07003044 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003045
Adam Cohena9cf38f2011-05-02 15:36:58 -07003046 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003047
Sunny Goyalf4066152015-04-15 09:42:19 -07003048 // While the folder is open, the position of the icon cannot change.
3049 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3050
Adam Cohen4554ee12011-08-03 16:13:21 -07003051 // Just verify that the folder hasn't already been added to the DragLayer.
3052 // There was a one-off crash where the folder had a parent already.
3053 if (folder.getParent() == null) {
3054 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003055 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003056 } else {
3057 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3058 folder.getParent() + ").");
3059 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003060 folder.animateOpen();
3061
3062 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003063
3064 // Notify the accessibility manager that this folder "window" has appeared and occluded
3065 // the workspace items
3066 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3067 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003068 }
3069
3070 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003071 closeFolder(true);
3072 }
3073
3074 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003075 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003076 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003077 if (folder.isEditingName()) {
3078 folder.dismissEditingName();
3079 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003080 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003081 }
3082 }
3083
Sunny Goyal935fca12015-10-13 10:19:01 -07003084 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003085 animate &= !Utilities.isPowerSaverOn(this);
3086
Adam Cohen4554ee12011-08-03 16:13:21 -07003087 folder.getInfo().opened = false;
3088
3089 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3090 if (parent != null) {
3091 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003092 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003093 if (fi != null) {
3094 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3095 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003096 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003097 if (animate) {
3098 folder.animateClosed();
3099 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003100 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003101 }
Winson Chung83ca4802013-04-12 15:10:52 -07003102
Sunny Goyal935fca12015-10-13 10:19:01 -07003103 // Notify the accessibility manager that this folder "window" has disappeared and no
3104 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003105 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003106 }
3107
Tony Wickhamdadb3042016-02-24 11:07:00 -08003108 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003109 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003110 if (!isDraggingEnabled()) return false;
3111 if (isWorkspaceLocked()) return false;
3112 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003113
Winson Chung76648c52015-07-10 14:33:23 -07003114 if (v == mAllAppsButton) {
3115 onLongClickAllAppsButton(v);
3116 return true;
3117 }
3118
Adam Cohen1697b792013-09-17 19:08:21 -07003119 if (v instanceof Workspace) {
3120 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003121 if (!mWorkspace.isTouchActive()) {
3122 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003123 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3124 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3125 return true;
3126 } else {
3127 return false;
3128 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003129 } else {
3130 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003131 }
Adam Cohen1697b792013-09-17 19:08:21 -07003132 }
3133
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003134 CellLayout.CellInfo longClickCellInfo = null;
3135 View itemUnderLongClick = null;
3136 if (v.getTag() instanceof ItemInfo) {
3137 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003138 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003139 itemUnderLongClick = longClickCellInfo.cell;
3140 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003141 }
3142
Winson Chung3d503fb2011-07-13 17:25:49 -07003143 // The hotseat touch handling does not go through Workspace, and we always allow long press
3144 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003145 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003146 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003147 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003148 // User long pressed on empty space
3149 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3150 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003151 if (mWorkspace.isInOverviewMode()) {
3152 mWorkspace.startReordering(v);
3153 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003154 showOverviewMode(true);
Hyunyoung Song645764e2016-06-06 14:19:02 -07003155 mHotseat.requestDisallowInterceptTouchEvent(true);
3156
Adam Cohendedbd962013-07-11 14:21:49 -07003157 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003158 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003159 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3160 mHotseat.getOrderInHotseat(
3161 longClickCellInfo.cellX,
3162 longClickCellInfo.cellY));
3163 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003164 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003165 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003166 }
3167 }
3168 }
3169 return true;
3170 }
3171
Winson Chung3d503fb2011-07-13 17:25:49 -07003172 boolean isHotseatLayout(View layout) {
3173 return mHotseat != null && layout != null &&
3174 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3175 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003176
Winson Chung3d503fb2011-07-13 17:25:49 -07003177 /**
3178 * Returns the CellLayout of the specified container at the specified screen.
3179 */
Sunny Goyal92820592015-03-02 11:31:35 -08003180 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003181 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3182 if (mHotseat != null) {
3183 return mHotseat.getLayout();
3184 } else {
3185 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003186 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003187 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003188 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003189 }
3190 }
3191
Winson Chungb745afb2015-03-02 11:51:23 -08003192 /**
3193 * For overridden classes.
3194 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003195 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003196 return isAppsViewVisible();
3197 }
3198
3199 public boolean isAppsViewVisible() {
3200 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3201 }
3202
3203 public boolean isWidgetsViewVisible() {
3204 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003205 }
3206
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003207 private void setWorkspaceBackground(int background) {
3208 switch (background) {
3209 case WORKSPACE_BACKGROUND_TRANSPARENT:
3210 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3211 break;
3212 case WORKSPACE_BACKGROUND_BLACK:
3213 getWindow().setBackgroundDrawable(null);
3214 break;
3215 default:
3216 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3217 }
Craig Mautner360310b2012-10-26 15:13:08 -07003218 }
3219
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003220 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003221 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3222 int curflags = getWindow().getAttributes().flags
3223 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3224 if (wpflags != curflags) {
3225 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3226 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003227 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003228 }
3229
Michael Jurkae326f182011-11-21 14:05:46 -08003230 @Override
3231 public void onTrimMemory(int level) {
3232 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003233 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3234 // The widget preview db can result in holding onto over
3235 // 3MB of memory for caching which isn't necessary.
3236 SQLiteDatabase.releaseMemory();
3237
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003238 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003239 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003240 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003241 if (mLauncherCallbacks != null) {
3242 mLauncherCallbacks.onTrimMemory(level);
3243 }
Michael Jurkae326f182011-11-21 14:05:46 -08003244 }
3245
Winson5c6bdbb2015-09-03 11:36:19 -07003246 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003247 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003248 }
3249
Winson5c6bdbb2015-09-03 11:36:19 -07003250 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003251 boolean changed = mState != State.WORKSPACE ||
3252 mWorkspace.getState() != Workspace.State.NORMAL;
3253 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003254 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003255 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003256 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003257
Michael Jurkab3e22d92011-10-31 15:58:33 -07003258 // Set focus to the AppsCustomize button
3259 if (mAllAppsButton != null) {
3260 mAllAppsButton.requestFocus();
3261 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003262 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003263
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003264 // Change the state *after* we've called all the transition code
3265 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003266
Winson Chung5fb63472011-02-02 17:03:37 -08003267 // Resume the auto-advance of widgets
3268 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003269 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003270
Winson Chung0f785722015-04-08 10:27:49 -07003271 if (changed) {
3272 // Send an accessibility event to announce the context change
3273 getWindow().getDecorView()
3274 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003275 }
Winson5c6bdbb2015-09-03 11:36:19 -07003276 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003277 }
3278
Winsone9f27272015-10-13 10:47:51 -07003279 /**
3280 * Shows the overview button.
3281 */
Adam Cohened307df2013-10-02 09:37:31 -07003282 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003283 showOverviewMode(animated, false);
3284 }
3285
3286 /**
3287 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3288 * onto one of the overview panel buttons.
3289 */
3290 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3291 Runnable postAnimRunnable = null;
3292 if (requestButtonFocus) {
3293 postAnimRunnable = new Runnable() {
3294 @Override
3295 public void run() {
3296 // Hitting the menu button when in touch mode does not trigger touch mode to
3297 // be disabled, so if requested, force focus on one of the overview panel
3298 // buttons.
3299 mOverviewPanel.requestFocusFromTouch();
3300 }
3301 };
3302 }
Adam Cohened307df2013-10-02 09:37:31 -07003303 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003304 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003305 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003306 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003307 }
3308
Winson Chungb745afb2015-03-02 11:51:23 -08003309 /**
3310 * Shows the apps view.
3311 */
Hyunyoung Song645764e2016-06-06 14:19:02 -07003312 public void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003313 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003314 if (resetListToTop) {
3315 mAppsView.scrollToTop();
3316 }
Winson Chung4ac30062015-05-08 17:34:17 -07003317 if (updatePredictedApps) {
3318 tryAndUpdatePredictedApps();
3319 }
Winson Chung76648c52015-07-10 14:33:23 -07003320 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003321 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003322
Winson Chungb745afb2015-03-02 11:51:23 -08003323 /**
3324 * Shows the widgets view.
3325 */
3326 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003327 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003328 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003329 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003330 }
Winson Chung76648c52015-07-10 14:33:23 -07003331 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003332
3333 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003334 @Override
3335 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003336 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003337 }
3338 });
Winson Chungb745afb2015-03-02 11:51:23 -08003339 }
3340
3341 /**
3342 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003343 *
3344 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003345 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003346 // TODO: calling method should use the return value so that when {@code false} is returned
3347 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003348 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003349 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3350 mState != State.WIDGETS_SPRING_LOADED) {
3351 return false;
3352 }
3353 if (toState != State.APPS && toState != State.WIDGETS) {
3354 return false;
3355 }
Winson Chungb745afb2015-03-02 11:51:23 -08003356
3357 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003358 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3359 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003360 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003361 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003362 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003363
Michael Jurkab3e22d92011-10-31 15:58:33 -07003364 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003365 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003366
3367 // Pause the auto-advance of widgets until we are out of AllApps
3368 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003369 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003370 closeFolder();
3371
3372 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003373 getWindow().getDecorView()
3374 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003375 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003376 }
3377
Winson Chungcd99cd32015-04-29 11:03:24 -07003378 /**
3379 * Updates the workspace and interaction state on state change, and return the animation to this
3380 * new state.
3381 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003382 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003383 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003384 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003385 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003386 updateInteraction(fromState, toState);
3387 return anim;
3388 }
3389
Jun Mukaif0033da2015-08-04 18:34:30 -07003390 public void onLauncherClingShown() {
3391 // When a launcher cling appears, it should cover the underlying layers, so their focus
3392 // should be blocked.
3393 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3394 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3395 }
3396 }
3397
3398 public void onLauncherClingDismissed() {
3399 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3400 }
3401
Hyunyoung Song3f471442015-04-08 19:01:34 -07003402 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003403 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003404 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003405 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003406 }
Winson Chungb745afb2015-03-02 11:51:23 -08003407
Winson Chung006ee262015-08-03 14:40:11 -07003408 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003409 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003410 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003411
3412 if (isAppsViewVisible()) {
3413 mState = State.APPS_SPRING_LOADED;
3414 } else if (isWidgetsViewVisible()) {
3415 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003416 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003417 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003418 } else {
3419 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003420 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003421 }
Adam Cohen7777d962011-08-18 18:58:38 -07003422
Hyunyoung Song3f471442015-04-08 19:01:34 -07003423 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003424 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003425 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003426
Winson Chunge7a03942011-08-05 15:05:12 -07003427 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003428 @Override
3429 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003430 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003431 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3432 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003433 // Before we show workspace, hide all apps again because
3434 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3435 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003436 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003437 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003438 } else {
3439 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003440 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003441 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003442 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003443 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003444
Tony Wickhame0c33232016-02-08 11:37:04 -08003445 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003446 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3447 || mState == State.WIDGETS_SPRING_LOADED;
3448 }
3449
Michael Jurkad3ef3062010-11-23 16:23:58 -08003450 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003451 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003452 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003453 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003454 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003455 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003456 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3457 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003458 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003459 }
3460
Winson Chung4ac30062015-05-08 17:34:17 -07003461 /**
3462 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3463 * resumed.
3464 */
3465 private void tryAndUpdatePredictedApps() {
3466 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003467 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003468 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003469 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003470 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003471 }
3472 }
3473 }
3474
Michael Jurkab3e22d92011-10-31 15:58:33 -07003475 void lockAllApps() {
3476 // TODO
3477 }
3478
3479 void unlockAllApps() {
3480 // TODO
3481 }
3482
Winsonf768d932015-09-25 16:12:35 -07003483 public boolean launcherCallbacksProvidesSearch() {
3484 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3485 }
3486
Michael Jurkad7c28052012-04-27 15:43:36 -07003487 @Override
3488 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003489 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003490 final List<CharSequence> text = event.getText();
3491 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003492 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003493 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003494 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003495 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003496 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003497 } else if (mWorkspace != null) {
3498 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003499 } else {
3500 text.add(getString(R.string.all_apps_home_button_label));
3501 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003502 return result;
3503 }
3504
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003505 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003506 * If the activity is currently paused, signal that we need to run the passed Runnable
3507 * in onResume.
3508 *
3509 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003510 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3511 * wrong when we're not running, and if the activity comes back to what the configuration was
3512 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003513 *
3514 * Implementation of the method from LauncherModel.Callbacks.
3515 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003516 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003517 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003518 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003519 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003520 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003521 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003522 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003523 }
3524 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003525 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003526 return true;
3527 } else {
3528 return false;
3529 }
3530 }
3531
Michael Jurkac402cd92013-05-20 15:49:32 +02003532 private boolean waitUntilResume(Runnable run) {
3533 return waitUntilResume(run, false);
3534 }
3535
Michael Jurka1e2f4652013-07-08 18:03:46 -07003536 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003537 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003538 }
3539
Michael Jurka7607c2f2013-04-03 14:33:19 -07003540 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003541 * If the activity is currently paused, signal that we need to re-run the loader
3542 * in onResume.
3543 *
3544 * This needs to be called from incoming places where resources might have been loaded
3545 * while we are paused. That is becaues the Configuration might be wrong
3546 * when we're not running, and if it comes back to what it was when we
3547 * were paused, we are not restarted.
3548 *
3549 * Implementation of the method from LauncherModel.Callbacks.
3550 *
3551 * @return true if we are currently paused. The caller might be able to
3552 * skip some work in that case since we will come back again.
3553 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003554 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003555 public boolean setLoadOnResume() {
3556 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003557 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003558 mOnResumeNeedsLoad = true;
3559 return true;
3560 } else {
3561 return false;
3562 }
3563 }
3564
3565 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003566 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003567 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003568 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003569 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003570 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003571 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003572 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003573 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003574 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003575 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003576
Joe Onorato9c1289c2009-08-17 11:03:03 -04003577 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003578 * Clear any pending bind callbacks. This is called when is loader is planning to
3579 * perform a full rebind from scratch.
3580 */
3581 @Override
3582 public void clearPendingBinds() {
3583 mBindOnResumeCallbacks.clear();
3584 if (mPendingExecutor != null) {
3585 mPendingExecutor.markCompleted();
3586 mPendingExecutor = null;
3587 }
3588 }
3589
3590 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003591 * Refreshes the shortcuts shown on the workspace.
3592 *
3593 * Implementation of the method from LauncherModel.Callbacks.
3594 */
3595 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003596 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003597
Winson Chungd64d1762013-08-20 14:37:16 -07003598 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003599 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003600 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003601
Michael Jurka05bf644e2011-11-30 20:28:53 -08003602 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003603 if (mHotseat != null) {
3604 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003605 }
3606 }
3607
Adam Cohendcd297f2013-06-18 13:13:40 -07003608 @Override
3609 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003610 // Make sure the first screen is always at the start.
3611 if (orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
3612 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3613 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3614 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Adam Cohen5084cba2013-09-03 12:01:16 -07003615 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003616 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003617
3618 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003619 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003620 if (hasCustomContentToLeft()) {
3621 mWorkspace.createCustomContentContainer();
3622 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003623 }
Winson Chung64359a52013-07-08 17:17:08 -07003624 }
3625
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003626 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003627 int count = orderedScreenIds.size();
3628 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003629 long screenId = orderedScreenIds.get(i);
3630 if (screenId != Workspace.FIRST_SCREEN_ID) {
3631 // No need to bind the first screen, as its always bound.
3632 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3633 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003634 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003635 }
3636
Winson Chungd64d1762013-08-20 14:37:16 -07003637 public void bindAppsAdded(final ArrayList<Long> newScreens,
3638 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003639 final ArrayList<ItemInfo> addAnimated,
3640 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003641 Runnable r = new Runnable() {
3642 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003643 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003644 }
3645 };
3646 if (waitUntilResume(r)) {
3647 return;
3648 }
3649
Winson Chungd64d1762013-08-20 14:37:16 -07003650 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003651 if (newScreens != null) {
3652 bindAddScreens(newScreens);
3653 }
Winson Chungd64d1762013-08-20 14:37:16 -07003654
3655 // We add the items without animation on non-visible pages, and with
3656 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003657 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003658 bindItems(addNotAnimated, 0,
3659 addNotAnimated.size(), false);
3660 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003661 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003662 bindItems(addAnimated, 0,
3663 addAnimated.size(), true);
3664 }
Winson Chungc58497e2013-09-03 17:48:37 -07003665
Winson Chung87412982013-10-03 18:34:14 -07003666 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003667 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003668
Winson Chungb745afb2015-03-02 11:51:23 -08003669 if (addedApps != null && mAppsView != null) {
3670 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003671 }
Winson Chungd64d1762013-08-20 14:37:16 -07003672 }
3673
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003674 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003675 * Bind the items start-end from the list.
3676 *
3677 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003678 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003679 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003680 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3681 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003682 Runnable r = new Runnable() {
3683 public void run() {
3684 bindItems(shortcuts, start, end, forceAnimateIcons);
3685 }
3686 };
3687 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003688 return;
3689 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003690
Winson Chungf0c6ae02012-03-21 16:10:31 -07003691 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003692 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3693 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003694 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003695 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003696 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003697 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003698 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003699
3700 // Short circuit if we are loading dock items for a configuration which has no dock
3701 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3702 mHotseat == null) {
3703 continue;
3704 }
3705
Sunny Goyal639e9062015-08-19 19:17:06 -07003706 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003707 switch (item.itemType) {
3708 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3709 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003710 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003711 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003712 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003713 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003714 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003715 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003716 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003717 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003718 default:
3719 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003720 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003721
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003722 /*
3723 * Remove colliding items.
3724 */
3725 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3726 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3727 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3728 View v = cl.getChildAt(item.cellX, item.cellY);
3729 Object tag = v.getTag();
3730 String desc = "Collision while binding workspace item: " + item
3731 + ". Collides with " + tag;
3732 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3733 throw (new RuntimeException(desc));
3734 } else {
3735 Log.d(TAG, desc);
3736 LauncherModel.deleteItemFromDatabase(this, item);
3737 continue;
3738 }
3739 }
3740 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003741 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3742 item.cellY, 1, 1);
3743 if (animateIcons) {
3744 // Animate all the applications up now
3745 view.setAlpha(0f);
3746 view.setScaleX(0f);
3747 view.setScaleY(0f);
3748 bounceAnims.add(createNewAppBounceAnimation(view, i));
3749 newShortcutsScreenId = item.screenId;
3750 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003751 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003752
Winson Chung997a9232013-07-24 15:33:46 -07003753 if (animateIcons) {
3754 // Animate to the correct page
3755 if (newShortcutsScreenId > -1) {
3756 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003757 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003758 final Runnable startBounceAnimRunnable = new Runnable() {
3759 public void run() {
3760 anim.playTogether(bounceAnims);
3761 anim.start();
3762 }
3763 };
Winson Chung997a9232013-07-24 15:33:46 -07003764 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003765 // We post the animation slightly delayed to prevent slowdowns
3766 // when we are loading right after we return to launcher.
3767 mWorkspace.postDelayed(new Runnable() {
3768 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003769 if (mWorkspace != null) {
3770 mWorkspace.snapToPage(newScreenIndex);
3771 mWorkspace.postDelayed(startBounceAnimRunnable,
3772 NEW_APPS_ANIMATION_DELAY);
3773 }
Winson Chung94d67682013-09-25 16:29:40 -07003774 }
3775 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003776 } else {
3777 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003778 }
3779 }
Winson Chung64359a52013-07-08 17:17:08 -07003780 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003781 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003782 }
3783
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003784 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3785 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3786 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003787 view.updateAppWidget(null);
3788 view.setOnClickListener(this);
3789 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003790 mWorkspace.requestLayout();
3791 }
3792
Joe Onorato9c1289c2009-08-17 11:03:03 -04003793 /**
3794 * Add the views for a widget to the workspace.
3795 *
3796 * Implementation of the method from LauncherModel.Callbacks.
3797 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003798 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003799 Runnable r = new Runnable() {
3800 public void run() {
3801 bindAppWidget(item);
3802 }
3803 };
3804 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003805 return;
3806 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003807
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003808 if (mIsSafeModeEnabled) {
3809 bindSafeModeWidget(item);
3810 return;
3811 }
3812
Daniel Sandler843e8602010-06-07 14:59:01 -04003813 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3814 if (DEBUG_WIDGETS) {
3815 Log.d(TAG, "bindAppWidget: " + item);
3816 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003817
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003818 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003819
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003820 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3821 // If the provider is not ready, bind as a pending widget.
3822 appWidgetInfo = null;
3823 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3824 // The widget id is not valid. Try to find the widget based on the provider info.
3825 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3826 } else {
3827 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3828 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003829
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003830 // If the provider is ready, but the width is not yet restored, try to restore it.
3831 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3832 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003833 if (appWidgetInfo == null) {
3834 if (DEBUG_WIDGETS) {
3835 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3836 + " belongs to component " + item.providerName
3837 + ", as the povider is null");
3838 }
3839 LauncherModel.deleteItemFromDatabase(this, item);
3840 return;
3841 }
Sunny Goyalff572272014-07-23 13:58:07 -07003842
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003843 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003844 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003845 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3846 // Id has not been allocated yet. Allocate a new id.
3847 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3848 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003849
Sunny Goyald478c832016-04-01 12:04:16 -07003850 // Also try to bind the widget. If the bind fails, the user will be shown
3851 // a click to setup UI, which will ask for the bind permission.
3852 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3853 pendingInfo.spanX = item.spanX;
3854 pendingInfo.spanY = item.spanY;
3855 pendingInfo.minSpanX = item.minSpanX;
3856 pendingInfo.minSpanY = item.minSpanY;
3857 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
3858 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3859 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003860
Sunny Goyald478c832016-04-01 12:04:16 -07003861 // Bind succeeded
3862 if (success) {
3863 // If the widget has a configure activity, it is still needs to set it up,
3864 // otherwise the widget is ready to go.
3865 item.restoreStatus = (appWidgetInfo.configure == null)
3866 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3867 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003868 }
Sunny Goyald478c832016-04-01 12:04:16 -07003869
3870 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003871 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003872 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003873 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003874 // The widget was marked as UI not ready, but there is no configure activity to
3875 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003876 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3877 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003878 }
Sunny Goyalff572272014-07-23 13:58:07 -07003879 }
3880
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003881 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003882 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003883 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3884 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003885 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003886
Sunny Goyal56c73602015-09-25 12:55:01 -07003887 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003888 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003889 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003890 deleteWidgetInfo(item);
3891 return;
3892 }
3893
Sunny Goyal233ee962015-08-03 13:05:01 -07003894 item.minSpanX = appWidgetInfo.minSpanX;
3895 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003896 addAppWidgetToWorkspace(
3897 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3898 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003899 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003900 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003901 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003902 view.updateAppWidget(null);
3903 view.setOnClickListener(this);
3904 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003905 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003906 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003907
Daniel Sandler843e8602010-06-07 14:59:01 -04003908 if (DEBUG_WIDGETS) {
3909 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3910 + (SystemClock.uptimeMillis()-start) + "ms");
3911 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003912 }
3913
Sunny Goyalff572272014-07-23 13:58:07 -07003914 /**
3915 * Restores a pending widget.
3916 *
3917 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003918 */
Sunny Goyald478c832016-04-01 12:04:16 -07003919 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003920 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3921 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3922 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003923 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003924 }
3925
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003926 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003927 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003928
3929 mWorkspace.reinflateWidgetsIfNecessary();
3930 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003931 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003932 }
3933
Adam Cohen1462de32012-07-24 22:34:36 -07003934 public void onPageBoundSynchronously(int page) {
3935 mSynchronouslyBoundPages.add(page);
3936 }
3937
Sunny Goyal527c7d32015-08-28 15:19:36 -07003938 @Override
3939 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3940 if (mPendingExecutor != null) {
3941 mPendingExecutor.markCompleted();
3942 }
3943 mPendingExecutor = executor;
3944 executor.attachTo(this);
3945 }
3946
3947 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3948 if (mPendingExecutor == executor) {
3949 mPendingExecutor = null;
3950 }
3951 }
3952
Joe Onorato9c1289c2009-08-17 11:03:03 -04003953 /**
3954 * Callback saying that there aren't any more items to bind.
3955 *
3956 * Implementation of the method from LauncherModel.Callbacks.
3957 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003958 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003959 Runnable r = new Runnable() {
3960 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003961 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003962 }
3963 };
3964 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003965 return;
3966 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003967 if (mSavedState != null) {
3968 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003969 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003970 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003971
Joe Onorato9c1289c2009-08-17 11:03:03 -04003972 mSavedState = null;
3973 }
3974
Adam Cohen1462de32012-07-24 22:34:36 -07003975 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003976
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003977 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003978
3979 // If we received the result of any pending adds while the loader was running (e.g. the
3980 // widget configuration forced an orientation change), process them now.
3981 if (sPendingAddItem != null) {
3982 final long screenId = completeAdd(sPendingAddItem);
3983
3984 // TODO: this moves the user to the page where the pending item was added. Ideally,
3985 // the screen would be guaranteed to exist after bind, and the page would be set through
3986 // the workspace restore process.
3987 mWorkspace.post(new Runnable() {
3988 @Override
3989 public void run() {
3990 mWorkspace.snapToScreenId(screenId);
3991 }
3992 });
3993 sPendingAddItem = null;
3994 }
3995
Sunny Goyal756adbc2015-04-16 15:20:51 -07003996 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003997
3998 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003999 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004000 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004001 }
4002
Winson Chunga0b7e862013-09-05 16:03:15 -07004003 public boolean isAllAppsButtonRank(int rank) {
4004 if (mHotseat != null) {
4005 return mHotseat.isAllAppsButtonRank(rank);
4006 }
4007 return false;
4008 }
4009
Winson Chunga2413752012-04-03 14:22:34 -07004010 private boolean canRunNewAppsAnimation() {
4011 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004012 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4013 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004014 }
4015
Winson Chungc9168342013-06-26 14:54:55 -07004016 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004017 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004018 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4019 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004020 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004021 return bounceAnim;
4022 }
4023
Adam Cohen27772732013-11-11 14:00:56 +00004024 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004025 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004026 }
4027
Tony Wickham55616cd2015-09-23 14:55:17 -07004028 public int getSearchBarHeight() {
4029 if (mLauncherCallbacks != null) {
4030 return mLauncherCallbacks.getSearchBarHeight();
4031 }
4032 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4033 }
4034
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004035 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004036 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4037 * multiple calls to bind the same list.)
4038 */
4039 @Thunk ArrayList<AppInfo> mTmpAppsList;
4040 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4041 public void run() {
4042 bindAllApplications(mTmpAppsList);
4043 mTmpAppsList = null;
4044 }
4045 };
4046
4047 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004048 * Add the icons for all apps.
4049 *
4050 * Implementation of the method from LauncherModel.Callbacks.
4051 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004052 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004053 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4054 mTmpAppsList = apps;
4055 return;
4056 }
4057
Winson Chungb745afb2015-03-02 11:51:23 -08004058 if (mAppsView != null) {
4059 mAppsView.setApps(apps);
4060 }
Adam Cohen9211d422014-10-07 18:14:53 -07004061 if (mLauncherCallbacks != null) {
4062 mLauncherCallbacks.bindAllApplications(apps);
4063 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004064 }
4065
4066 /**
4067 * A package was updated.
4068 *
4069 * Implementation of the method from LauncherModel.Callbacks.
4070 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004071 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004072 Runnable r = new Runnable() {
4073 public void run() {
4074 bindAppsUpdated(apps);
4075 }
4076 };
4077 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004078 return;
4079 }
4080
Winson Chungb745afb2015-03-02 11:51:23 -08004081 if (mAppsView != null) {
4082 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004083 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004084 }
4085
Sunny Goyal4390ace2014-10-13 11:33:11 -07004086 @Override
4087 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4088 Runnable r = new Runnable() {
4089 public void run() {
4090 bindWidgetsRestored(widgets);
4091 }
4092 };
4093 if (waitUntilResume(r)) {
4094 return;
4095 }
4096 mWorkspace.widgetsRestored(widgets);
4097 }
4098
Joe Onorato9c1289c2009-08-17 11:03:03 -04004099 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004100 * Some shortcuts were updated in the background.
4101 *
4102 * Implementation of the method from LauncherModel.Callbacks.
4103 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004104 @Override
4105 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4106 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004107 Runnable r = new Runnable() {
4108 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004109 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004110 }
4111 };
4112 if (waitUntilResume(r)) {
4113 return;
4114 }
4115
Sunny Goyal4390ace2014-10-13 11:33:11 -07004116 if (!updated.isEmpty()) {
4117 mWorkspace.updateShortcuts(updated);
4118 }
4119
4120 if (!removed.isEmpty()) {
4121 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4122 for (ShortcutInfo si : removed) {
4123 removedComponents.add(si.getTargetComponent());
4124 }
4125 mWorkspace.removeItemsByComponentName(removedComponents, user);
4126 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004127 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004128 }
4129 }
4130
4131 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004132 * Update the state of a package, typically related to install state.
4133 *
4134 * Implementation of the method from LauncherModel.Callbacks.
4135 */
Sunny Goyale755d462014-07-22 13:48:29 -07004136 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004137 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4138 Runnable r = new Runnable() {
4139 public void run() {
4140 bindRestoreItemsChange(updates);
4141 }
4142 };
4143 if (waitUntilResume(r)) {
4144 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004145 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004146
Sunny Goyal756adbc2015-04-16 15:20:51 -07004147 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004148 }
4149
4150 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004151 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004152 * in addition to specific applications to remove, the reason being that
4153 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004154 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004155 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004156 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004157 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004158 public void bindWorkspaceComponentsRemoved(
4159 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4160 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004161 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004162 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004163 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004164 }
Winson Chungd64d1762013-08-20 14:37:16 -07004165 };
4166 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004167 return;
4168 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004169 if (!packageNames.isEmpty()) {
4170 mWorkspace.removeItemsByPackageName(packageNames, user);
4171 }
4172 if (!components.isEmpty()) {
4173 mWorkspace.removeItemsByComponentName(components, user);
4174 }
4175 // Notify the drag controller
4176 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004177
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004178 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004179
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004180 @Override
4181 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4182 Runnable r = new Runnable() {
4183 public void run() {
4184 bindAppInfosRemoved(appInfos);
4185 }
4186 };
4187 if (waitUntilResume(r)) {
4188 return;
Joe Onorato36115782010-06-17 13:28:48 -04004189 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004190
Winson Chungdf95eb12013-10-16 14:57:07 -07004191 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004192 if (mAppsView != null) {
4193 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004194 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004195 }
Joe Onoratobe386092009-11-17 17:32:16 -08004196
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004197 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004198 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004199 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004200 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004201 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004202
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004203 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004204 public void bindWidgetsModel(WidgetsModel model) {
4205 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004206 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004207 return;
4208 }
4209
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004210 if (mWidgetsView != null && model != null) {
4211 mWidgetsView.addWidgets(model);
4212 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004213 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004214 }
4215
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004216 @Override
4217 public void notifyWidgetProvidersChanged() {
4218 if (mWorkspace.getState().shouldUpdateWidget) {
4219 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4220 }
4221 }
4222
Winson Chung400438b2011-01-16 17:53:48 -08004223 private int mapConfigurationOriActivityInfoOri(int configOri) {
4224 final Display d = getWindowManager().getDefaultDisplay();
4225 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4226 switch (d.getRotation()) {
4227 case Surface.ROTATION_0:
4228 case Surface.ROTATION_180:
4229 // We are currently in the same basic orientation as the natural orientation
4230 naturalOri = configOri;
4231 break;
4232 case Surface.ROTATION_90:
4233 case Surface.ROTATION_270:
4234 // We are currently in the other basic orientation to the natural orientation
4235 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4236 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4237 break;
4238 }
4239
4240 int[] oriMap = {
4241 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4242 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4243 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4244 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4245 };
4246 // Since the map starts at portrait, we need to offset if this device's natural orientation
4247 // is landscape.
4248 int indexOffset = 0;
4249 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4250 indexOffset = 1;
4251 }
4252 return oriMap[(d.getRotation() + indexOffset) % 4];
4253 }
Adam Cohen446e9402011-09-15 18:21:21 -07004254
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004255 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004256 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004257 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004258 if (Utilities.ATLEAST_JB_MR2) {
4259 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4260 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004261 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4262 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004263 }
Winson Chung4b919f82012-05-01 10:44:08 -07004264 }
4265 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004266
Winson Chung4b919f82012-05-01 10:44:08 -07004267 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004268 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004269 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004270 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004271 } else {
4272 mHandler.postDelayed(new Runnable() {
4273 public void run() {
4274 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4275 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004276 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004277 }
Winson Chung4b919f82012-05-01 10:44:08 -07004278 }
Winson Chung400438b2011-01-16 17:53:48 -08004279 }
4280
Adam Cohenea90f832014-05-21 15:03:34 -07004281 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004282 * To be overridden by subclasses to indicate that there is an activity to launch
4283 * before showing the standard launcher experience.
4284 */
4285 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004286 if (mLauncherCallbacks != null) {
4287 return mLauncherCallbacks.hasFirstRunActivity();
4288 }
Adam Cohen432609a2014-03-13 17:03:22 -07004289 return false;
4290 }
4291
4292 /**
4293 * To be overridden by subclasses to launch any first run activity
4294 */
4295 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004296 if (mLauncherCallbacks != null) {
4297 return mLauncherCallbacks.getFirstRunActivity();
4298 }
Adam Cohen432609a2014-03-13 17:03:22 -07004299 return null;
4300 }
4301
Winson Chunga6945242014-01-08 14:04:34 -08004302 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004303 return !ActivityManager.isRunningInTestHarness() &&
4304 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004305 }
4306
Adam Cohen4a9423d2014-03-28 14:22:31 -07004307 protected boolean hasRunFirstRunActivity() {
4308 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4309 }
4310
Adam Cohen432609a2014-03-13 17:03:22 -07004311 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004312 if (shouldRunFirstRunActivity() &&
4313 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004314 Intent firstRunIntent = getFirstRunActivity();
4315 if (firstRunIntent != null) {
4316 startActivity(firstRunIntent);
4317 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004318 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004319 }
4320 }
Adam Cohen432609a2014-03-13 17:03:22 -07004321 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004322 }
4323
4324 private void markFirstRunActivityShown() {
4325 SharedPreferences.Editor editor = mSharedPrefs.edit();
4326 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4327 editor.apply();
4328 }
4329
Adam Cohen432609a2014-03-13 17:03:22 -07004330 /**
4331 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4332 * screen that must be displayed and dismissed.
4333 */
4334 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004335 if (mLauncherCallbacks != null) {
4336 return mLauncherCallbacks.hasDismissableIntroScreen();
4337 }
Adam Cohen432609a2014-03-13 17:03:22 -07004338 return false;
4339 }
4340
4341 /**
4342 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4343 */
4344 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004345 if (mLauncherCallbacks != null) {
4346 return mLauncherCallbacks.getIntroScreen();
4347 }
Adam Cohen432609a2014-03-13 17:03:22 -07004348 return null;
4349 }
4350
4351 /**
4352 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4353 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4354 */
4355 private boolean shouldShowIntroScreen() {
4356 return hasDismissableIntroScreen() &&
4357 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4358 }
4359
4360 protected void showIntroScreen() {
4361 View introScreen = getIntroScreen();
4362 changeWallpaperVisiblity(false);
4363 if (introScreen != null) {
4364 mDragLayer.showOverlayView(introScreen);
4365 }
4366 }
4367
4368 public void dismissIntroScreen() {
4369 markIntroScreenDismissed();
4370 if (showFirstRunActivity()) {
4371 // We delay hiding the intro view until the first run activity is showing. This
4372 // avoids a blip.
4373 mWorkspace.postDelayed(new Runnable() {
4374 @Override
4375 public void run() {
4376 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004377 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004378 }
4379 }, ACTIVITY_START_DELAY);
4380 } else {
4381 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004382 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004383 }
4384 changeWallpaperVisiblity(true);
4385 }
4386
4387 private void markIntroScreenDismissed() {
4388 SharedPreferences.Editor editor = mSharedPrefs.edit();
4389 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4390 editor.apply();
4391 }
4392
Adam Cohen091440a2015-03-18 14:16:05 -07004393 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004394 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4395 // on the device, then we always show the first run cling experience (or if there is no
4396 // launcher2). Otherwise, we prompt the user upon started for migration
4397 LauncherClings launcherClings = new LauncherClings(this);
4398 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004399 mClings = launcherClings;
Sunny Goyalded0fdb2016-05-23 15:55:41 -07004400 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004401 }
4402 }
4403
Winson Chung5ffd51d2015-06-25 11:36:50 -07004404 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004405 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004406 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004407 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004408 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004409 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004410 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4411 if (userHandle != null) {
4412 user = UserHandleCompat.fromUser(userHandle);
4413 }
4414 }
4415 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004416 }
4417
4418 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004419 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004420 if (user == null) {
4421 user = UserHandleCompat.myUserHandle();
4422 }
4423
4424 // Called from search suggestion, add the profile extra to the intent to ensure that we
4425 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004426 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004427 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004428 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004429 return null;
4430 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004431 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004432 }
4433
Winson Chung5ffd51d2015-06-25 11:36:50 -07004434 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004435 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4436 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004437 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004438 UserHandleCompat.myUserHandle());
4439 }
4440
Winson Chung5ffd51d2015-06-25 11:36:50 -07004441 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004442 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4443 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004444 mWorkspace.onExternalDragStartedWithItem(dragView);
4445 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004446 }
4447
Winson Chung5ffd51d2015-06-25 11:36:50 -07004448 protected void moveWorkspaceToDefaultScreen() {
4449 mWorkspace.moveToDefaultScreen(false);
4450 }
4451
Winson Chung80baf5a2010-08-09 16:03:15 -07004452 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004453 * Returns a FastBitmapDrawable with the icon, accurately sized.
4454 */
4455 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4456 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4457 d.setFilterBitmap(true);
4458 resizeIconDrawable(d);
4459 return d;
4460 }
4461
4462 /**
4463 * Resizes an icon drawable to the correct icon size.
4464 */
Winson5fbe0742015-09-30 15:33:00 -07004465 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004466 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004467 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004468 }
4469
4470 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004471 * Prints out out state for debugging.
4472 */
4473 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004474 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004475 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004476 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4477 Log.d(TAG, "mRestoring=" + mRestoring);
4478 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004479 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004480 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004481
Daniel Sandler325dc232013-06-05 22:57:57 -04004482 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004483 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004484
4485 @Override
4486 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4487 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004488 // Dump workspace
4489 writer.println(prefix + "Workspace Items");
4490 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4491 writer.println(prefix + " Homescreen " + i);
4492
4493 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4494 for (int j = 0; j < layout.getChildCount(); j++) {
4495 Object tag = layout.getChildAt(j).getTag();
4496 if (tag != null) {
4497 writer.println(prefix + " " + tag.toString());
4498 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004499 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004500 }
Sunny Goyala1365452015-10-01 15:46:24 -07004501
4502 writer.println(prefix + " Hotseat");
4503 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4504 for (int j = 0; j < layout.getChildCount(); j++) {
4505 Object tag = layout.getChildAt(j).getTag();
4506 if (tag != null) {
4507 writer.println(prefix + " " + tag.toString());
4508 }
4509 }
4510
Sunny Goyal713edfc2016-05-06 09:58:34 -07004511 try {
4512 FileLog.flushAll(writer);
4513 } catch (Exception e) {
4514 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004515 }
4516
Adam Cohen9211d422014-10-07 18:14:53 -07004517 if (mLauncherCallbacks != null) {
4518 mLauncherCallbacks.dump(prefix, fd, writer, args);
4519 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004520 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004521
Adam Cohen59400422014-03-05 18:07:04 -08004522 public static CustomAppWidget getCustomAppWidget(String name) {
4523 return sCustomAppWidgets.get(name);
4524 }
4525
4526 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4527 return sCustomAppWidgets;
4528 }
4529
Sunny Goyal29538332016-02-18 09:10:19 -08004530 public static List<View> getFolderContents(View icon) {
4531 if (icon instanceof FolderIcon) {
4532 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4533 } else {
4534 return Collections.EMPTY_LIST;
4535 }
4536 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004537
4538 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4539
4540 @Override
4541 public void onSharedPreferenceChanged(
4542 SharedPreferences sharedPreferences, String key) {
4543 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4544 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4545 if (!waitUntilResume(this, true)) {
4546 run();
4547 }
4548 }
4549 }
4550
4551 @Override
4552 public void run() {
4553 setOrientation();
4554 }
4555 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004556}