blob: cc31de208336e8e0d29683a41a108ab810f60f50 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyal28c6b962015-10-12 11:42:05 -070019import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070025import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000026import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080037import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Sunny Goyal756cd262015-08-20 12:33:21 -070039import android.content.ContentValues;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040041import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070044import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070046import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070050import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070053import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070056import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020059import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020061import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080062import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070063import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070064import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070066import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.HapticFeedbackConstants;
74import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.MotionEvent;
77import android.view.Surface;
78import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070079import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080080import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070081import android.view.ViewGroup;
82import android.view.ViewTreeObserver;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080084import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070085import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070086import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080090import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070092
Sunny Goyal651077b2014-06-30 14:15:31 -070093import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070094import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyalae502842016-06-17 08:43:56 -070095import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070096import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070097import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080098import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -070099import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100100import com.android.launcher3.compat.LauncherActivityInfoCompat;
101import com.android.launcher3.compat.LauncherAppsCompat;
102import com.android.launcher3.compat.UserHandleCompat;
103import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800104import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700105import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700106import com.android.launcher3.dragndrop.DragController;
107import com.android.launcher3.dragndrop.DragLayer;
108import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -0700109import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000110import com.android.launcher3.folder.Folder;
111import com.android.launcher3.folder.FolderIcon;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700112import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700113import com.android.launcher3.logging.FileLog;
Sunny Goyald5bd67d2016-03-11 01:10:19 -0800114import com.android.launcher3.logging.LoggerUtils;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700115import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700116import com.android.launcher3.model.WidgetsModel;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700117import com.android.launcher3.pageindicators.PageIndicator;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800118import com.android.launcher3.userevent.nano.LauncherLogProto;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700119import com.android.launcher3.util.ComponentKey;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700120import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700121import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal322d5562015-06-25 19:35:49 -0700122import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700123import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700124import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700125import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700126import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700127import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700128
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700129import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700130import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700131import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700132import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800133import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700134import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700135import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700136import java.util.List;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800137import java.util.Locale;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700138
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139/**
140 * Default launcher application.
141 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100142public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700143 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
144 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700145 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700146 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700147 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Daniel Sandlerf061f822013-06-27 13:59:36 -0400149 static final boolean PROFILE_STARTUP = false;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700150 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700151 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400152 static final boolean DEBUG_RESUME_TIME = false;
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700153 static final boolean DEBUG_LOGGING = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700156 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700157 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700158 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159
Michael Jurka8b805b12012-04-18 14:23:14 -0700160 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700161 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700162 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700163
Sunny Goyal28c6b962015-10-12 11:42:05 -0700164 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
165
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700166 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
167 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
168 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
169
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700170 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
171
Mathew Inwood876a8462013-06-14 14:12:41 +0100172 /**
173 * IntentStarter uses request codes starting with this. This must be greater than all activity
174 * request codes used internally.
175 */
176 protected static final int REQUEST_LAST = 100;
177
Michael Jurka0a457bf2012-11-19 14:05:05 -0800178 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700179 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800180 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
Winson Chung2672ff92012-05-04 16:22:30 -0700182 // The Intent extra that defines whether to ignore the launch animation
183 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400184 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700185
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700186 public static final String ACTION_APPWIDGET_HOST_RESET =
187 "com.android.launcher3.intent.ACTION_APPWIDGET_HOST_RESET";
188
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 // Type: int
190 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700191 // Type: int
192 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700193 // Type: Content Values / parcelable
194 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700195 // Type: parcelable
196 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000197 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800198 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199
Adam Cohen432609a2014-03-13 17:03:22 -0700200 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800201 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
202
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700203 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700204 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
205 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700206
Adam Cohen091440a2015-03-18 14:16:05 -0700207 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700208 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700209
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700210 private boolean mIsSafeModeEnabled;
211
Joe Onorato9c1289c2009-08-17 11:03:03 -0400212 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800213 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700214 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700215 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
Winson Chunga2413752012-04-03 14:22:34 -0700217 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700218 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
219 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700220 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700221
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700222 private final BroadcastReceiver mUiBroadcastReceiver = new BroadcastReceiver() {
223
224 @Override
225 public void onReceive(Context context, Intent intent) {
226 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
227 closeSystemDialogs();
228 } else if (ACTION_APPWIDGET_HOST_RESET.equals(intent.getAction())) {
229 if (mAppWidgetHost != null) {
230 mAppWidgetHost.startListening();
231 }
232 }
233 }
234 };
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800235
Adam Cohen091440a2015-03-18 14:16:05 -0700236 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700237 private View mLauncherView;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700238 private PageIndicator mPageIndicator;
Adam Cohen091440a2015-03-18 14:16:05 -0700239 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800240 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700241
242 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700243
Sunny Goyalffe83f12014-08-14 17:39:34 -0700244 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700245 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700246
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700247 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800248 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800249 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700250
Michael Jurka0280c3b2010-09-17 15:00:07 -0700251 private int[] mTmpAddItemCellCoordinates = new int[2];
252
Sunny Goyal316490e2015-06-02 09:38:28 -0700253 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800254 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700255
Michael Jurka838a4ca2011-02-07 13:33:06 -0800256 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700257 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700258
Sunny Goyal47328fd2016-05-25 18:56:41 -0700259 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700260
261 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700262 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700263 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700264
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700265 // Main container view and the model for the widget tray screen.
266 @Thunk WidgetsContainerView mWidgetsView;
267 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700268
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700270 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
271 // scroll issues (because the workspace may not have been measured yet) and extra work.
272 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
273 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274
275 private SpannableStringBuilder mDefaultKeySsb = null;
276
Adam Cohen091440a2015-03-18 14:16:05 -0700277 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400278
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800279 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280 private boolean mRestoring;
281 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700282 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283
Michael Jurka1e2f4652013-07-08 18:03:46 -0700284 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700285 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700286 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700287
Joe Onorato9c1289c2009-08-17 11:03:03 -0400288 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800289 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700290 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700291 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Adam Cohen091440a2015-03-18 14:16:05 -0700292 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800293 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700294 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800295 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400296
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700297 /** Maps launcher activity components to their list of shortcut ids. */
298 private MultiHashMap<ComponentKey, String> mDeepShortcutMap = new MultiHashMap<>();
299
Sunny Goyal639e9062015-08-19 19:17:06 -0700300 private LauncherClings mClings;
301
Adam Cohen61f560d2013-09-30 15:58:20 -0700302 private View.OnTouchListener mHapticFeedbackTouchListener;
303
Adam Cohended9f8d2010-11-03 13:25:16 -0700304 // Related to the auto-advancing of widgets
305 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700306 private static final int ADVANCE_INTERVAL = 20000;
307 private static final int ADVANCE_STAGGER = 250;
308
309 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700310 private long mAutoAdvanceSentTime;
311 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700312 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700313
Winson Chung400438b2011-01-16 17:53:48 -0800314 // Determines how long to wait after a rotation before restoring the screen orientation to
315 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700316 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800317
Adam Cohen091440a2015-03-18 14:16:05 -0700318 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700319
Adam Cohen1462de32012-07-24 22:34:36 -0700320 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700321 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700322
Winson Chung46353de2012-02-16 14:05:10 -0800323 // We only want to get the SharedPreferences once since it does an FS stat each time we get
324 // it from the context.
325 private SharedPreferences mSharedPrefs;
326
Winson Chungf0c6ae02012-03-21 16:10:31 -0700327 // Holds the page that we need to animate to, and the icon views that we need to animate up
328 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700329 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700330 private Bitmap mFolderIconBitmap;
331 private Canvas mFolderIconCanvas;
332 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700333
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700334 private DeviceProfile mDeviceProfile;
335
Adam Cohen4b66bf32015-09-18 12:15:19 -0700336 private boolean mMoveToDefaultScreenFromNewIntent;
337
Winson Chung13eb5272015-05-11 16:30:13 -0700338 // This is set to the view that launched the activity that navigated the user away from
339 // launcher. Since there is no callback for when the activity has finished launching, enable
340 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800341 private BubbleTextView mWaitingForResume;
342
Adam Cohen59400422014-03-05 18:07:04 -0800343 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
344 new HashMap<String, CustomAppWidget>();
345
Adam Cohen59400422014-03-05 18:07:04 -0800346 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700347 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
348 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800349 }
350 }
351
Adam Cohen091440a2015-03-18 14:16:05 -0700352 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700353 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700354 if (mWorkspace != null) {
355 mWorkspace.buildPageHardwareLayers();
356 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700357 }
358 };
359
Adam Cohendb364c32014-05-20 14:23:40 -0700360 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800361
Adam Cohen091440a2015-03-18 14:16:05 -0700362 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800363 int requestCode;
364 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700365 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700366 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800367 int cellX;
368 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700369 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800370 }
371
Hyunyoung Songaa953652016-04-19 18:30:24 -0700372 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800373
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700374 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700375 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400376
377 @Thunk void setOrientation() {
378 if (mRotationEnabled) {
379 unlockScreenOrientation(true);
380 } else {
381 setRequestedOrientation(
382 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700383 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400384 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700385
Sunny Goyal745bad92016-05-02 10:54:12 -0700386 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700387
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 @Override
389 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700390 if (DEBUG_STRICT_MODE) {
391 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
392 .detectDiskReads()
393 .detectDiskWrites()
394 .detectNetwork() // or .detectAll() for all detectable problems
395 .penaltyLog()
396 .build());
397 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
398 .detectLeakedSqlLiteObjects()
399 .detectLeakedClosableObjects()
400 .penaltyLog()
401 .penaltyDeath()
402 .build());
403 }
404
Adam Cohen9211d422014-10-07 18:14:53 -0700405 if (mLauncherCallbacks != null) {
406 mLauncherCallbacks.preOnCreate();
407 }
408
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400410
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400411 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700412
Adam Cohen2e6da152015-05-06 11:42:25 -0700413 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700414 mDeviceProfile = getResources().getConfiguration().orientation
415 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700416 app.getInvariantDeviceProfile().landscapeProfile
417 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700418
Sunny Goyalf7258242015-10-19 16:59:07 -0700419 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700420 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800421 mModel = app.setLauncher(this);
422 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700423 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700424
Joe Onorato41a12d22009-10-31 18:30:00 -0400425 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700426 mAllAppsController = new AllAppsTransitionController(this);
427 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700428
Sunny Goyalffe83f12014-08-14 17:39:34 -0700429 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700430
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700431 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
432 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700433
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700434 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
435 // this also ensures that any synchronous binding below doesn't re-trigger another
436 // LauncherModel load.
437 mPaused = false;
438
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200440 android.os.Debug.startMethodTracing(
441 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442 }
443
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700445
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700447 mDeviceProfile.layout(this);
Tony Wickham827cef22016-03-17 15:39:39 -0700448 mExtractedColors = new ExtractedColors();
449 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450
Tony Wickhame2217252016-03-22 16:34:23 -0700451 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
452 .addAccessibilityStateChangeListener(this);
453
Joe Onorato7c312c12009-08-13 21:36:53 -0700454 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700455
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 mSavedState = savedInstanceState;
457 restoreState(mSavedState);
458
459 if (PROFILE_STARTUP) {
460 android.os.Debug.stopMethodTracing();
461 }
462
463 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700464 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700465 // If the user leaves launcher, then we should just load items asynchronously when
466 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700467 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700468 } else {
469 // We only load the page synchronously if the user rotates (or triggers a
470 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700471 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700472 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800473 }
474
475 // For handling default keys
476 mDefaultKeySsb = new SpannableStringBuilder();
477 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800478
479 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700480 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
481 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800482
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800483 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700484 // In case we are on a device with locked rotation, we should look at preferences to check
485 // if the user has specifically allowed rotation.
486 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700487 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700488 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
489 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700490 }
491
492 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
493 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400494 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700495
Adam Cohen9211d422014-10-07 18:14:53 -0700496 if (mLauncherCallbacks != null) {
497 mLauncherCallbacks.onCreate(savedInstanceState);
498 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700499
500 if (shouldShowIntroScreen()) {
501 showIntroScreen();
502 } else {
503 showFirstRunActivity();
504 showFirstRunClings();
505 }
Adam Cohen9211d422014-10-07 18:14:53 -0700506 }
507
Sunny Goyal7779d622015-06-11 16:18:39 -0700508 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700509 public void onExtractedColorsChanged() {
510 loadExtractedColorsAndColorItems();
511 }
512
513 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700514 // TODO: do this in pre-N as well, once the extraction part is complete.
515 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700516 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700517 mHotseat.updateColor(mExtractedColors, !mPaused);
Tony Wickham770217c2016-05-18 15:16:40 -0700518 if (mPageIndicator != null) {
519 mPageIndicator.updateColor(mExtractedColors);
520 }
Tony Wickham827cef22016-03-17 15:39:39 -0700521 }
522 }
523
Adam Cohen9211d422014-10-07 18:14:53 -0700524 private LauncherCallbacks mLauncherCallbacks;
525
526 public void onPostCreate(Bundle savedInstanceState) {
527 super.onPostCreate(savedInstanceState);
528 if (mLauncherCallbacks != null) {
529 mLauncherCallbacks.onPostCreate(savedInstanceState);
530 }
531 }
532
Sunny Goyal32554d12015-12-03 15:31:25 -0800533 /**
534 * Call this after onCreate to set or clear overlay.
535 */
536 public void setLauncherOverlay(LauncherOverlay overlay) {
537 if (overlay != null) {
538 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
539 }
540 mWorkspace.setLauncherOverlay(overlay);
541 }
542
Adam Cohen9211d422014-10-07 18:14:53 -0700543 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
544 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700545 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700546 private boolean mWorkspaceImportanceStored = false;
547 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700548 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800549 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700550 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
551
552 @Override
553 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700554 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700555 return;
556 }
557 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700558 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700559 if (mWorkspace != null) {
560 mWorkspaceImportanceForAccessibility =
561 mWorkspace.getImportantForAccessibility();
562 mWorkspace.setImportantForAccessibility(
563 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
564 mWorkspaceImportanceStored = true;
565 }
566 if (mHotseat != null) {
567 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
568 mHotseat.setImportantForAccessibility(
569 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
570 mHotseatImportanceStored = true;
571 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700572 }
573
574 @Override
575 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700576 if (mWorkspaceImportanceStored && mWorkspace != null) {
577 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
578 }
579 if (mHotseatImportanceStored && mHotseat != null) {
580 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
581 }
582 mWorkspaceImportanceStored = false;
583 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700584 }
585 });
Adam Cohen9211d422014-10-07 18:14:53 -0700586 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700587 }
588
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700589 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700590 public void onLauncherProviderChange() {
591 if (mLauncherCallbacks != null) {
592 mLauncherCallbacks.onLauncherProviderChange();
593 }
594 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700595
Adam Cohen9211d422014-10-07 18:14:53 -0700596 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700597 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700598 if (mLauncherCallbacks != null) {
599 return mLauncherCallbacks.hasCustomContentToLeft();
600 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700601 return false;
602 }
603
Dave Hawkeya8881582013-09-17 15:55:33 -0600604 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500605 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600606 * {@link #addToCustomContentPage}. This will only be invoked if
607 * {@link #hasCustomContentToLeft()} is {@code true}.
608 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500609 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700610 if (mLauncherCallbacks != null) {
611 mLauncherCallbacks.populateCustomContentContainer();
612 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600613 }
614
615 /**
616 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
617 * ensure the custom content page is added or removed if necessary.
618 */
619 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600620 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600621 // Not bound yet, wait for bindScreens to be called.
622 return;
623 }
624
625 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
626 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500627 mWorkspace.createCustomContentContainer();
628 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600629 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
630 mWorkspace.removeCustomContentPage();
631 }
632 }
633
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800634 /**
635 * Logger object is a singleton and does not have to be coupled with the foreground activity.
636 * Since most user event logging is done on the UI, the object is retrieved from the
637 * callback for convenience.
638 */
Hyunyoung Songaa953652016-04-19 18:30:24 -0700639 private UserEventDispatcher createUserEventDispatcher() {
640 return new UserEventDispatcher() {
641 @Override
642 public void dispatchUserEvent(LauncherLogProto.LauncherEvent ev, Intent intent) {
643 if (!DEBUG_LOGGING) {
644 return;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800645 }
Hyunyoung Songaa953652016-04-19 18:30:24 -0700646 Log.d("UserEvent", String.format(Locale.US,
647 "action:%s\nchild:%s\nparent:%s\nelapsed container %d ms session %d ms",
648 LoggerUtils.getActionStr(ev.action),
649 LoggerUtils.getTargetStr(ev.srcTarget[0]),
650 LoggerUtils.getTargetStr(ev.srcTarget[1]),
651 ev.elapsedContainerMillis,
652 ev.elapsedSessionMillis));
653 }
654 };
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800655 }
656
Hyunyoung Songaa953652016-04-19 18:30:24 -0700657 public UserEventDispatcher getUserEventDispatcher() {
658 if (mLauncherCallbacks != null) {
659 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
660 if (dispatcher != null) {
661 return dispatcher;
662 }
663 }
664
665 if (mUserEventDispatcher == null) {
666 mUserEventDispatcher = createUserEventDispatcher();
667 }
668 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800669 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100670
Hyunyoung Song3f471442015-04-08 19:01:34 -0700671 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700672 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
673 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700674 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700675 }
676
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000677 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700678 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
679 // This cast is safe as long as the id < 0x00FFFFFF
680 // Since we jail all the dynamically generated views, there should be no clashes
681 // with any other views.
682 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000683 }
684
685 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700686 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
687 * a configuration step, this allows the proper animations to run after other transitions.
688 */
Adam Cohendb364c32014-05-20 14:23:40 -0700689 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700690 long screenId = args.screenId;
691 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
692 // When the screen id represents an actual screen (as opposed to a rank) we make sure
693 // that the drop page actually exists.
694 screenId = ensurePendingDropLayoutExists(args.screenId);
695 }
Adam Cohendb364c32014-05-20 14:23:40 -0700696
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800697 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800698 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700699 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700700 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700701 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800702 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700703 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700704 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700705 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyald478c832016-04-01 12:04:16 -0700706 completeRestoreAppWidget(args.appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700707 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700708 case REQUEST_BIND_PENDING_APPWIDGET: {
709 int widgetId = args.appWidgetId;
710 LauncherAppWidgetInfo info =
711 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
712 if (info != null) {
713 // Since the view was just bound, also launch the configure activity if needed
714 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
715 .getLauncherAppWidgetInfo(widgetId);
716 if (provider != null && provider.configure != null) {
717 startRestoredWidgetReconfigActivity(provider, info);
718 }
719 }
720 break;
721 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800722 }
Michael Jurka27614d22012-04-02 06:40:22 -0700723 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
724 // if you turned the screen off and then back while in All Apps, Launcher would not
725 // return to the workspace. Clearing mAddInfo.container here fixes this issue
726 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700727 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800728 }
729
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800730 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700731 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700732 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700733 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700734 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800735 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700736
Adam Cohenad4e15c2013-10-17 16:21:35 -0700737 Runnable exitSpringLoaded = new Runnable() {
738 @Override
739 public void run() {
740 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
741 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
742 }
743 };
744
Michael Jurka8b805b12012-04-18 14:23:14 -0700745 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700746 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700747 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700748 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
749 if (resultCode == RESULT_CANCELED) {
750 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700751 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700752 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700753 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800754 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700755 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700756 }
757 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200758 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
759 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700760 // User could have free-scrolled between pages before picking a wallpaper; make sure
761 // we move to the closest one now.
762 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700763 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200764 }
765 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700766 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200767
Adam Cohened66b2b2012-01-23 17:28:51 -0800768 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700769 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700770
Adam Cohendb364c32014-05-20 14:23:40 -0700771 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800772 // We have special handling for widgets
773 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800774 final int appWidgetId;
775 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
776 : -1;
777 if (widgetId < 0) {
778 appWidgetId = pendingAddWidgetId;
779 } else {
780 appWidgetId = widgetId;
781 }
782
Adam Cohenad4e15c2013-10-17 16:21:35 -0700783 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800784 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700785 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
786 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700787 result = RESULT_CANCELED;
788 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700789 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700790 @Override
791 public void run() {
792 exitSpringLoadedDragModeDelayed(false, 0, null);
793 }
794 };
Adam Cohendb364c32014-05-20 14:23:40 -0700795 if (workspaceLocked) {
796 // No need to remove the empty screen if we're mid-binding, as the
797 // the bind will not add the empty screen.
798 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
799 } else {
800 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
801 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
802 }
Winson Chung5aaab772012-04-27 15:24:02 -0700803 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700804 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700805 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
806 // When the screen id represents an actual screen (as opposed to a rank)
807 // we make sure that the drop page actually exists.
808 mPendingAddInfo.screenId =
809 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
810 }
Adam Cohendb364c32014-05-20 14:23:40 -0700811 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
812
813 dropLayout.setDropPending(true);
814 final Runnable onComplete = new Runnable() {
815 @Override
816 public void run() {
817 completeTwoStageWidgetDrop(resultCode, appWidgetId);
818 dropLayout.setDropPending(false);
819 }
820 };
821 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
822 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
823 } else {
824 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
825 mPendingAddInfo);
826 sPendingAddItem = args;
827 }
Winson Chung5aaab772012-04-27 15:24:02 -0700828 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800829 return;
830 }
831
Sunny Goyald478c832016-04-01 12:04:16 -0700832 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
833 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700834 if (resultCode == RESULT_OK) {
835 // Update the widget view.
836 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
837 pendingAddWidgetId, mPendingAddInfo);
838 if (workspaceLocked) {
839 sPendingAddItem = args;
840 } else {
841 completeAdd(args);
842 }
843 }
844 // Leave the widget in the pending state if the user canceled the configure.
845 return;
846 }
847
Sunny Goyalaad90582015-07-09 14:22:50 -0700848 if (requestCode == REQUEST_CREATE_SHORTCUT) {
849 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
850 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
851 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
852 mPendingAddInfo);
853 if (isWorkspaceLocked()) {
854 sPendingAddItem = args;
855 } else {
856 completeAdd(args);
857 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
858 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
859 }
860 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700861 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
862 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800865 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800866
867 }
868
869 @Override
870 protected void onActivityResult(
871 final int requestCode, final int resultCode, final Intent data) {
872 handleActivityResult(requestCode, resultCode, data);
873 if (mLauncherCallbacks != null) {
874 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
875 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800876 }
877
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600878 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700879 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600880 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700881 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
882 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
883 View v = null;
884 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
885 if (layout != null) {
886 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
887 }
888 Intent intent = sPendingAddItem.intent;
889 sPendingAddItem = null;
890 if (grantResults.length > 0
891 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700892 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700893 } else {
894 // TODO: Show a snack bar with link to settings
895 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
896 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
897 }
898 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600899 if (mLauncherCallbacks != null) {
900 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
901 grantResults);
902 }
903 }
904
Adam Cohendb364c32014-05-20 14:23:40 -0700905 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
906 appWidgetId, ItemInfo info) {
907 PendingAddArguments args = new PendingAddArguments();
908 args.requestCode = requestCode;
909 args.intent = data;
910 args.container = info.container;
911 args.screenId = info.screenId;
912 args.cellX = info.cellX;
913 args.cellY = info.cellY;
914 args.appWidgetId = appWidgetId;
915 return args;
916 }
917
918 /**
919 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
920 *
921 * @param screenId the screen id to check
922 * @return the new screen, or screenId if it exists
923 */
924 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800925 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700926 if (dropLayout == null) {
927 // it's possible that the add screen was removed because it was
928 // empty and a re-bind occurred
929 mWorkspace.addExtraEmptyScreen();
930 return mWorkspace.commitExtraEmptyScreen();
931 } else {
932 return screenId;
933 }
934 }
935
Adam Cohen091440a2015-03-18 14:16:05 -0700936 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800937 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800938 Runnable onCompleteRunnable = null;
939 int animationType = 0;
940
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700941 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800942 if (resultCode == RESULT_OK) {
943 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
944 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700945 mPendingAddWidgetInfo);
946 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800947 onCompleteRunnable = new Runnable() {
948 @Override
949 public void run() {
950 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700951 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700952 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
953 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800954 }
955 };
956 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800957 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800958 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800959 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700960 if (mDragLayer.getAnimatedView() != null) {
961 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
962 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
963 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700964 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700965 // The animated view may be null in the case of a rotation during widget configuration
966 onCompleteRunnable.run();
967 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800968 }
969
970 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700971 protected void onStop() {
972 super.onStop();
973 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700974
975 if (mLauncherCallbacks != null) {
976 mLauncherCallbacks.onStop();
977 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700978 }
979
980 @Override
981 protected void onStart() {
982 super.onStart();
983 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700984
985 if (mLauncherCallbacks != null) {
986 mLauncherCallbacks.onStart();
987 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700988 }
989
990 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200992 long startTime = 0;
993 if (DEBUG_RESUME_TIME) {
994 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400995 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200996 }
Adam Cohen9211d422014-10-07 18:14:53 -0700997
998 if (mLauncherCallbacks != null) {
999 mLauncherCallbacks.preOnResume();
1000 }
1001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -07001003 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001004
Winson Chung4a2afa32012-07-19 14:53:05 -07001005 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001006 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001007 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001008 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -07001009 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -07001010 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -07001011 // view after launching an app, as they may be depending on the UI to be static to
1012 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -07001013 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07001014 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08001015 } else if (mOnResumeState == State.WIDGETS) {
1016 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001017 }
1018 mOnResumeState = State.NONE;
1019
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001020 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001021 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1022 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001023
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001024 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001025 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001026 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001027 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001028 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001029 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001031 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001032 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1033 // execute them here
1034 long startTimeCallbacks = 0;
1035 if (DEBUG_RESUME_TIME) {
1036 startTimeCallbacks = System.currentTimeMillis();
1037 }
1038
Michael Jurka1e2f4652013-07-08 18:03:46 -07001039 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1040 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001041 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001042 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001043 if (DEBUG_RESUME_TIME) {
1044 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1045 (System.currentTimeMillis() - startTimeCallbacks));
1046 }
Michael Jurka447bf842013-05-15 14:52:15 +02001047 }
Michael Jurka54554252013-08-01 12:52:23 +02001048 if (mOnResumeCallbacks.size() > 0) {
1049 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1050 mOnResumeCallbacks.get(i).run();
1051 }
1052 mOnResumeCallbacks.clear();
1053 }
Winson Chunge4e50662012-01-23 14:45:13 -08001054
1055 // Reset the pressed state of icons that were locked in the press state while activities
1056 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001057 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001058 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001059 mWaitingForResume.setStayPressed(false);
1060 }
Winson Chung82963d52013-10-09 11:20:57 -07001061
Adam Cohen06dff352012-06-01 17:17:08 -07001062 // It is possible that widgets can receive updates while launcher is not in the foreground.
1063 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1064 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1065 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001066 if (!isWorkspaceLoading()) {
1067 getWorkspace().reinflateWidgetsIfNecessary();
1068 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001069
Michael Jurka447bf842013-05-15 14:52:15 +02001070 if (DEBUG_RESUME_TIME) {
1071 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1072 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001073
Adam Cohen4b66bf32015-09-18 12:15:19 -07001074 // We want to suppress callbacks about CustomContent being shown if we have just received
1075 // onNewIntent while the user was present within launcher. In that case, we post a call
1076 // to move the user to the main screen (which will occur after onResume). We don't want to
1077 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1078 // suppress here.
1079 if (mWorkspace.getCustomContentCallbacks() != null
1080 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001081 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001082 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001083 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1084 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001085 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001086 }
1087 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001088 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001089 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001090 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001091
Sunny Goyal756adbc2015-04-16 15:20:51 -07001092 if (!isWorkspaceLoading()) {
1093 // Process any items that were added while Launcher was away.
1094 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1095 }
Adam Cohen9211d422014-10-07 18:14:53 -07001096
1097 if (mLauncherCallbacks != null) {
1098 mLauncherCallbacks.onResume();
1099 }
Adam Cohen06dff352012-06-01 17:17:08 -07001100 }
1101
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001103 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001104 // Ensure that items added to Launcher are queued until Launcher returns
1105 InstallShortcutReceiver.enableInstallQueue();
1106
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001107 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001108 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001109 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001110 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001111
1112 // We call onHide() aggressively. The custom content callbacks should be able to
1113 // debounce excess onHide calls.
1114 if (mWorkspace.getCustomContentCallbacks() != null) {
1115 mWorkspace.getCustomContentCallbacks().onHide();
1116 }
Romain Guycbb89e42009-06-08 15:52:54 -07001117
Adam Cohen9211d422014-10-07 18:14:53 -07001118 if (mLauncherCallbacks != null) {
1119 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001120 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001121 }
1122
1123 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001124 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1125 // by a onResume or by scrolling otherwise.
1126 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001127
1128 // Custom content is completely hidden
1129 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001130
1131 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1132 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001133
1134 // Indicates whether the user is allowed to scroll away from the custom content.
1135 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001136 }
1137
Adam Cohenc2d6e892014-10-16 09:49:24 -07001138 public interface LauncherOverlay {
1139
1140 /**
1141 * Touch interaction leading to overscroll has begun
1142 */
1143 public void onScrollInteractionBegin();
1144
1145 /**
1146 * Touch interaction related to overscroll has ended
1147 */
1148 public void onScrollInteractionEnd();
1149
1150 /**
1151 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1152 * screen (or in the case of RTL, the rightmost screen).
1153 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001154 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001155
1156 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001157 * Called when the launcher is ready to use the overlay
1158 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001159 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001160 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001161 }
1162
Jun Mukai8af0cd82015-05-12 12:32:12 -07001163 public interface LauncherSearchCallbacks {
1164 /**
1165 * Called when the search overlay is shown.
1166 */
1167 public void onSearchOverlayOpened();
1168
1169 /**
1170 * Called when the search overlay is dismissed.
1171 */
1172 public void onSearchOverlayClosed();
1173 }
1174
Adam Cohenc2d6e892014-10-16 09:49:24 -07001175 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001176 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001177 }
1178
1179 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1180
Sunny Goyalc86df472016-02-25 09:19:38 -08001181 public void onScrollChanged(float progress) {
1182 if (mWorkspace != null) {
1183 mWorkspace.onOverlayScrollChanged(progress);
1184 }
1185 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001186 }
1187
Jorim Jaggid017f882014-01-14 17:08:48 -08001188 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001189 if (mLauncherCallbacks != null) {
1190 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001191 } else {
1192 // On devices with a locked orientation, we will at least have the allow rotation
1193 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001194 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001195 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001196 }
1197
Adam Cohen9211d422014-10-07 18:14:53 -07001198 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001199 CustomContentCallbacks callbacks, String description) {
1200 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001201 }
1202
Adam Cohenedb40762013-07-18 16:45:45 -07001203 // The custom content needs to offset its content to account for the QSB
1204 public int getTopOffsetForCustomContent() {
1205 return mWorkspace.getPaddingTop();
1206 }
1207
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001208 @Override
1209 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001210 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001211 if (mModel.isCurrentCallbacks(this)) {
1212 mModel.stopLoader();
1213 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001214 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1215
Romain Guy13c2e7b2010-03-10 19:45:00 -08001216 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001217 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001218
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001219 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001220 @Override
1221 public void onWindowFocusChanged(boolean hasFocus) {
1222 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001223 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001224
1225 if (mLauncherCallbacks != null) {
1226 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1227 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001228 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001229
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 private boolean acceptFilter() {
1231 final InputMethodManager inputManager = (InputMethodManager)
1232 getSystemService(Context.INPUT_METHOD_SERVICE);
1233 return !inputManager.isFullscreenMode();
1234 }
1235
1236 @Override
1237 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001238 final int uniChar = event.getUnicodeChar();
1239 final boolean handled = super.onKeyDown(keyCode, event);
1240 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1241 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1243 keyCode, event);
1244 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001245 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001246 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001247 // showSearchDialog()
1248 // If there are multiple keystrokes before the search dialog takes focus,
1249 // onSearchRequested() will be called for every keystroke,
1250 // but it is idempotent, so it's fine.
1251 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 }
1253 }
1254
Joe Onorato8a9625e2010-01-28 15:55:35 -08001255 // Eat the long press event so the keyboard doesn't come up.
1256 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1257 return true;
1258 }
1259
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 return handled;
1261 }
1262
Winson46163472015-09-22 17:31:56 -07001263 @Override
1264 public boolean onKeyUp(int keyCode, KeyEvent event) {
1265 if (keyCode == KeyEvent.KEYCODE_MENU) {
1266 // Ignore the menu key if we are currently dragging or are on the custom content screen
1267 if (!isOnCustomContent() && !mDragController.isDragging()) {
1268 // Close any open folders
1269 closeFolder();
1270
1271 // Stop resizing any widgets
1272 mWorkspace.exitWidgetResizeMode();
1273
1274 // Show the overview mode if we are on the workspace
1275 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1276 !mWorkspace.isSwitchingState()) {
1277 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001278 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001279 }
1280 }
1281 return true;
1282 }
1283 return super.onKeyUp(keyCode, event);
1284 }
1285
Karl Rosaen138a0412009-04-23 19:00:21 -07001286 private String getTypedText() {
1287 return mDefaultKeySsb.toString();
1288 }
1289
1290 private void clearTypedText() {
1291 mDefaultKeySsb.clear();
1292 mDefaultKeySsb.clearSpans();
1293 Selection.setSelection(mDefaultKeySsb, 0);
1294 }
1295
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001297 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1298 * State
1299 */
1300 private static State intToState(int stateOrdinal) {
1301 State state = State.WORKSPACE;
1302 final State[] stateValues = State.values();
1303 for (int i = 0; i < stateValues.length; i++) {
1304 if (stateValues[i].ordinal() == stateOrdinal) {
1305 state = stateValues[i];
1306 break;
1307 }
1308 }
1309 return state;
1310 }
1311
1312 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 * Restores the previous state, if it exists.
1314 *
1315 * @param savedState The previous state.
1316 */
1317 private void restoreState(Bundle savedState) {
1318 if (savedState == null) {
1319 return;
1320 }
1321
Michael Jurka883f55b2010-10-21 15:47:14 -07001322 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001323 if (state == State.APPS || state == State.WIDGETS) {
1324 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001325 }
1326
Adam Cohen21cd0022013-10-09 18:57:02 -07001327 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1328 PagedView.INVALID_RESTORE_PAGE);
1329 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001330 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 }
1332
Sunny Goyal756cd262015-08-20 12:33:21 -07001333 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1334 if (itemValues != null) {
1335 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001336 AppWidgetProviderInfo info = savedState.getParcelable(
1337 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001338 mPendingAddWidgetInfo = info == null ?
1339 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1340
Adam Cohen4637b5a2013-11-04 18:21:24 -08001341 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001342 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 mRestoring = true;
1344 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 }
1346
1347 /**
1348 * Finds all the views we need and configure them properly.
1349 */
1350 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001351 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001352 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001353 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
1354
Winson Chung4c98d922011-05-31 16:50:48 -07001355 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Hyunyoung Song7d2fc812016-06-15 12:51:30 -07001356 mPageIndicator = (PageIndicator) mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001357
Sunny Goyal784f9c32016-03-23 16:56:54 -07001358 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1359 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1360 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001361 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001362
Winson Chung4c98d922011-05-31 16:50:48 -07001363 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001364
1365 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001366
Winson Chung3d503fb2011-07-13 17:25:49 -07001367 // Setup the hotseat
1368 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1369 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001370 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001371 }
1372
Winsone9f27272015-10-13 10:47:51 -07001373 // Setup the overview panel
1374 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001375
Winson Chung4c98d922011-05-31 16:50:48 -07001376 // Setup the workspace
1377 mWorkspace.setHapticFeedbackEnabled(false);
1378 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001379 mWorkspace.setup(mDragController);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001380 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001381 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001382
Tony Wickham34d2c912015-09-11 09:27:58 -07001383 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001384 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001385
Winson Chungef7f8742015-06-04 17:18:17 -07001386 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001387 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001388 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001389 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1390 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1391 } else {
1392 mAppsView.setSearchBarController(new DefaultAppSearchController());
1393 }
Craig Mautner360310b2012-10-26 15:13:08 -07001394
Winson Chung3d503fb2011-07-13 17:25:49 -07001395 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001396 mDragController.setDragScoller(mWorkspace);
1397 mDragController.setScrollView(mDragLayer);
1398 mDragController.setMoveTarget(mWorkspace);
1399 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001400 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001401
Sunny Goyal322d5562015-06-25 19:35:49 -07001402 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1403 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001404 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 }
1406
Winsone9f27272015-10-13 10:47:51 -07001407 private void setupOverviewPanel() {
1408 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1409
1410 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1411 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1412 @Override
1413 public boolean onLongClick(View v) {
1414 return v.performClick();
1415 }
1416 };
1417
1418 // Bind wallpaper button actions
1419 View wallpaperButton = findViewById(R.id.wallpaper_button);
1420 wallpaperButton.setOnClickListener(new OnClickListener() {
1421 @Override
1422 public void onClick(View view) {
1423 if (!mWorkspace.isSwitchingState()) {
1424 onClickWallpaperPicker(view);
1425 }
1426 }
1427 });
1428 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1429 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1430
1431 // Bind widget button actions
1432 mWidgetsButton = findViewById(R.id.widget_button);
1433 mWidgetsButton.setOnClickListener(new OnClickListener() {
1434 @Override
1435 public void onClick(View view) {
1436 if (!mWorkspace.isSwitchingState()) {
1437 onClickAddWidgetButton(view);
1438 }
1439 }
1440 });
1441 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1442 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1443
1444 // Bind settings actions
1445 View settingsButton = findViewById(R.id.settings_button);
1446 boolean hasSettings = hasSettings();
1447 if (hasSettings) {
1448 settingsButton.setOnClickListener(new OnClickListener() {
1449 @Override
1450 public void onClick(View view) {
1451 if (!mWorkspace.isSwitchingState()) {
1452 onClickSettingsButton(view);
1453 }
1454 }
1455 });
1456 settingsButton.setOnLongClickListener(performClickOnLongClick);
1457 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1458 } else {
1459 settingsButton.setVisibility(View.GONE);
1460 }
1461
1462 mOverviewPanel.setAlpha(0f);
1463 }
1464
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001466 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001467 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001468 */
1469 public void setAllAppsButton(View allAppsButton) {
1470 mAllAppsButton = allAppsButton;
1471 }
1472
Sunny Goyalbb011da2016-06-15 15:42:29 -07001473 public View getStartViewForAllAppsRevealAnimation() {
1474 return FeatureFlags.NO_ALL_APPS_ICON ? mPageIndicator : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001475 }
1476
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001477 public View getWidgetsButton() {
1478 return mWidgetsButton;
1479 }
1480
Anjali Koppal5ad44842014-03-10 20:34:39 -07001481 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 * Creates a view representing a shortcut.
1483 *
1484 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001486 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001487 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 }
1489
1490 /**
1491 * Creates a view representing a shortcut inflated from the specified resource.
1492 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 * @param parent The group the shortcut belongs to.
1494 * @param info The data structure describing the shortcut.
1495 *
1496 * @return A View inflated from layoutResId.
1497 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001498 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001499 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001500 parent, false);
1501 favorite.applyFromShortcutInfo(info, mIconCache);
1502 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001504 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 return favorite;
1506 }
1507
1508 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 * Add a shortcut to the workspace.
1510 *
1511 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001513 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001514 int cellY) {
1515 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001516 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001517
Sunny Goyal5c97f512015-05-19 16:03:28 -07001518 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001519 if (info == null) {
1520 return;
1521 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001522 final View view = createShortcut(info);
1523
Sunny Goyal5c97f512015-05-19 16:03:28 -07001524 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001525 // First we check if we already know the exact location where we want to add this item.
1526 if (cellX >= 0 && cellY >= 0) {
1527 cellXY[0] = cellX;
1528 cellXY[1] = cellY;
1529 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001530
1531 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001532 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001533 true, null,null)) {
1534 return;
1535 }
1536 DragObject dragObject = new DragObject();
1537 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001538 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1539 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001540 return;
1541 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001542 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001543 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001544 }
1545
1546 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001547 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001548 return;
1549 }
1550
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001551 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552
1553 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001554 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001555 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556 }
1557 }
1558
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001560 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001562 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 */
Adam Cohen091440a2015-03-18 14:16:05 -07001564 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001565 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1566
1567 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001568 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001569 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001570 }
Romain Guycbb89e42009-06-08 15:52:54 -07001571
Adam Cohen59400422014-03-05 18:07:04 -08001572 if (appWidgetInfo.isCustomWidget) {
1573 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001574 }
1575
Adam Cohen59400422014-03-05 18:07:04 -08001576 LauncherAppWidgetInfo launcherInfo;
1577 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1578 launcherInfo.spanX = info.spanX;
1579 launcherInfo.spanY = info.spanY;
1580 launcherInfo.minSpanX = info.minSpanX;
1581 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001582 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001583
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001585 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586
1587 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001588 if (hostView == null) {
1589 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001590 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001591 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001592 hostView.setVisibility(View.VISIBLE);
1593 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001595 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596 }
Romain Guycbb89e42009-06-08 15:52:54 -07001597
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001598 private void addAppWidgetToWorkspace(
1599 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001600 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001601 hostView.setTag(item);
1602 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001603
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001604 hostView.setFocusable(true);
1605 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001606
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001607 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001608 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1609
1610 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001611 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001612 }
1613 }
1614
Adam Cohended9f8d2010-11-03 13:25:16 -07001615 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1616 @Override
1617 public void onReceive(Context context, Intent intent) {
1618 final String action = intent.getAction();
1619 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1620 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001621 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001622 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001623
Winson Chungc100e8e2011-08-09 16:02:43 -07001624 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001625 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001626 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001627 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001628 if (!showWorkspace(false)) {
1629 // If we are already on the workspace, then manually reset all apps
1630 mAppsView.reset();
1631 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001632 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001633 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1634 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001635 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001636 }
1637 }
1638 };
1639
1640 @Override
1641 public void onAttachedToWindow() {
1642 super.onAttachedToWindow();
1643
1644 // Listen for broadcasts related to user-presence
1645 final IntentFilter filter = new IntentFilter();
1646 filter.addAction(Intent.ACTION_SCREEN_OFF);
1647 filter.addAction(Intent.ACTION_USER_PRESENT);
1648 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001649 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001650 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001651 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001652
1653 if (mLauncherCallbacks != null) {
1654 mLauncherCallbacks.onAttachedToWindow();
1655 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001656 }
1657
1658 @Override
1659 public void onDetachedFromWindow() {
1660 super.onDetachedFromWindow();
1661 mVisible = false;
1662
Adam Cohend113e0c2010-11-11 10:48:05 -08001663 if (mAttached) {
1664 unregisterReceiver(mReceiver);
1665 mAttached = false;
1666 }
Winson Chungb745afb2015-03-02 11:51:23 -08001667 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001668
1669 if (mLauncherCallbacks != null) {
1670 mLauncherCallbacks.onDetachedFromWindow();
1671 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001672 }
1673
1674 public void onWindowVisibilityChanged(int visibility) {
1675 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001676 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001677 // The following code used to be in onResume, but it turns out onResume is called when
1678 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1679 // is a more appropriate event to handle
1680 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001681 if (!mWorkspaceLoading) {
1682 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001683 // We want to let Launcher draw itself at least once before we force it to build
1684 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001685 // apps is nice and speedy.
1686 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001687 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001688 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001689 if (mStarted) return;
1690 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001691 // We delay the layer building a bit in order to give
1692 // other message processing a time to run. In particular
1693 // this avoids a delay in hiding the IME if it was
1694 // currently shown, because doing that may involve
1695 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001696 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001697 final ViewTreeObserver.OnDrawListener listener = this;
1698 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001699 public void run() {
1700 if (mWorkspace != null &&
1701 mWorkspace.getViewTreeObserver() != null) {
1702 mWorkspace.getViewTreeObserver().
1703 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001704 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001705 }
1706 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001707 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001708 }
1709 });
1710 }
1711 clearTypedText();
1712 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001713 }
1714
Adam Cohen091440a2015-03-18 14:16:05 -07001715 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001716 mHandler.removeMessages(ADVANCE_MSG);
1717 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1718 mHandler.sendMessageDelayed(msg, delay);
1719 mAutoAdvanceSentTime = System.currentTimeMillis();
1720 }
1721
Adam Cohen091440a2015-03-18 14:16:05 -07001722 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001723 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1724 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1725 mAutoAdvanceRunning = autoAdvanceRunning;
1726 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001727 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001728 sendAdvanceMessage(delay);
1729 } else {
1730 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001731 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001732 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1733 }
1734 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001735 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001736 }
1737 }
1738 }
1739
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001740 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1741
Adam Cohended9f8d2010-11-03 13:25:16 -07001742 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001743 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001744 if (msg.what == ADVANCE_MSG) {
1745 int i = 0;
1746 for (View key: mWidgetsToAdvance.keySet()) {
1747 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001748 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001749 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001750 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001751 public void run() {
1752 ((Advanceable) v).advance();
1753 }
1754 }, delay);
1755 }
1756 i++;
1757 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001758 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001759 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001760 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001761 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001762 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001763
Winsonc0b52fe2015-09-09 16:38:15 -07001764 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001765 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001766 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1767 if (v instanceof Advanceable) {
1768 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001769 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001770 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001771 }
1772 }
1773
Winsonc0b52fe2015-09-09 16:38:15 -07001774 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001775 if (mWidgetsToAdvance.containsKey(hostView)) {
1776 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001777 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001778 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001779 }
1780
Hyunyoung Song3f471442015-04-08 19:01:34 -07001781 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001782 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1783 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001784 }
1785
Winson Chunga6945242014-01-08 14:04:34 -08001786 public DragLayer getDragLayer() {
1787 return mDragLayer;
1788 }
1789
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001790 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001791 return mAppsView;
1792 }
1793
Hyunyoung Song3f471442015-04-08 19:01:34 -07001794 public WidgetsContainerView getWidgetsView() {
1795 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001796 }
1797
Winson Chunga6945242014-01-08 14:04:34 -08001798 public Workspace getWorkspace() {
1799 return mWorkspace;
1800 }
1801
1802 public Hotseat getHotseat() {
1803 return mHotseat;
1804 }
1805
Jorim Jaggid017f882014-01-14 17:08:48 -08001806 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001807 return mOverviewPanel;
1808 }
1809
Sunny Goyal47328fd2016-05-25 18:56:41 -07001810 public DropTargetBar getDropTargetBar() {
1811 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001812 }
1813
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001814 public LauncherAppWidgetHost getAppWidgetHost() {
1815 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001816 }
Romain Guycbb89e42009-06-08 15:52:54 -07001817
Winson Chunga9abd0e2010-10-27 17:18:37 -07001818 public LauncherModel getModel() {
1819 return mModel;
1820 }
1821
Adam Cohen79d90c52016-04-22 13:29:20 -07001822 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001823 return mSharedPrefs;
1824 }
1825
Adam Cohen2e6da152015-05-06 11:42:25 -07001826 public DeviceProfile getDeviceProfile() {
1827 return mDeviceProfile;
1828 }
1829
Bjorn Bringertc459e522013-06-07 19:36:01 +01001830 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001831 getWindow().closeAllPanels();
1832
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001833 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001834 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001835 }
1836
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837 @Override
1838 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001839 long startTime = 0;
1840 if (DEBUG_RESUME_TIME) {
1841 startTime = System.currentTimeMillis();
1842 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001843 super.onNewIntent(intent);
1844
1845 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001846 Folder openFolder = mWorkspace.getOpenFolder();
1847 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1848 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1849 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1850 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1851 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001852 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001853 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001854
Adam Cohen6fecd412013-10-02 17:41:50 -07001855 if (mWorkspace == null) {
1856 // Can be cases where mWorkspace is null, this prevents a NPE
1857 return;
1858 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001859 // In all these cases, only animate if we're already on home
1860 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001861
Sunny Goyal935fca12015-10-13 10:19:01 -07001862 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001863 exitSpringLoadedDragMode();
1864
1865 // If we are already on home, then just animate back to the workspace,
1866 // otherwise, just wait until onResume to set the state back to Workspace
1867 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001868 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001869 } else {
1870 mOnResumeState = State.WORKSPACE;
1871 }
1872
1873 final View v = getWindow().peekDecorView();
1874 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001875 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001876 INPUT_METHOD_SERVICE);
1877 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1878 }
1879
Winson Chungb745afb2015-03-02 11:51:23 -08001880 // Reset the apps view
1881 if (!alreadyOnHome && mAppsView != null) {
1882 mAppsView.scrollToTop();
1883 }
1884
Hyunyoung Song3f471442015-04-08 19:01:34 -07001885 // Reset the widgets view
1886 if (!alreadyOnHome && mWidgetsView != null) {
1887 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001888 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001889
Adam Cohen9211d422014-10-07 18:14:53 -07001890 if (mLauncherCallbacks != null) {
1891 mLauncherCallbacks.onHomeIntent();
1892 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001893 }
Adam Cohened307df2013-10-02 09:37:31 -07001894
Adam Cohen9211d422014-10-07 18:14:53 -07001895 if (mLauncherCallbacks != null) {
1896 mLauncherCallbacks.onNewIntent(intent);
1897 }
Winson15f8b172015-08-19 11:02:39 -07001898
1899 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1900 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1901 // animation.
1902 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001903 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1904 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001905 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1906 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001907
1908 // We use this flag to suppress noisy callbacks above custom content state
1909 // from onResume.
1910 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001911 mWorkspace.post(new Runnable() {
1912 @Override
1913 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001914 if (mWorkspace != null) {
1915 mWorkspace.moveToDefaultScreen(true);
1916 }
Winson15f8b172015-08-19 11:02:39 -07001917 }
1918 });
1919 }
1920 }
1921
1922 if (DEBUG_RESUME_TIME) {
1923 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1924 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001925 }
1926
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001928 public void onRestoreInstanceState(Bundle state) {
1929 super.onRestoreInstanceState(state);
1930 for (int page: mSynchronouslyBoundPages) {
1931 mWorkspace.restoreInstanceStateForChild(page);
1932 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933 }
1934
1935 @Override
1936 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001937 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001938 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1939 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001940
Adam Cohen21cd0022013-10-09 18:57:02 -07001941 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001942 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943
Michael Jurka883f55b2010-10-21 15:47:14 -07001944 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001945 // We close any open folder since it will not be re-opened, and we need to make sure
1946 // this state is reflected.
1947 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1948 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949
Adam Cohendcd297f2013-06-18 13:13:40 -07001950 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001951 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001952 ContentValues itemValues = new ContentValues();
1953 mPendingAddInfo.writeToValues(itemValues);
1954 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001955 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001956 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 }
1958
Adam Cohen9211d422014-10-07 18:14:53 -07001959 if (mLauncherCallbacks != null) {
1960 mLauncherCallbacks.onSaveInstanceState(outState);
1961 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 }
1963
1964 @Override
1965 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001967
Winson Chunge7a03942011-08-05 15:05:12 -07001968 // Remove all pending runnables
1969 mHandler.removeMessages(ADVANCE_MSG);
1970 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001971 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001972 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001973
Winson Chungcd2b0142011-06-08 16:02:26 -07001974 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001975 // It's possible to receive onDestroy after a new Launcher activity has
1976 // been created. In this case, don't interfere with the new Launcher.
1977 if (mModel.isCurrentCallbacks(this)) {
1978 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001979 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001980 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001981
Sunny Goyal745bad92016-05-02 10:54:12 -07001982 if (mRotationPrefChangeHandler != null) {
1983 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1984 }
1985
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001987 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001989 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001991 mAppWidgetHost = null;
1992
1993 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994
1995 TextKeyListener.getInstance().release();
1996
Tony Wickhame2217252016-03-22 16:34:23 -07001997 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1998 .removeAccessibilityStateChangeListener(this);
1999
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002000 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002001
Michael Jurka2ecf9952012-06-18 12:52:28 -07002002 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002003
2004 if (mLauncherCallbacks != null) {
2005 mLauncherCallbacks.onDestroy();
2006 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 }
2008
Sunny Goyalae502842016-06-17 08:43:56 -07002009 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
2010 return mAccessibilityDelegate;
2011 }
2012
Adam Cohena9cf38f2011-05-02 15:36:58 -07002013 public DragController getDragController() {
2014 return mDragController;
2015 }
2016
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 @Override
2018 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002019 onStartForResult(requestCode);
2020 super.startActivityForResult(intent, requestCode);
2021 }
2022
2023 @Override
2024 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2025 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2026 onStartForResult(requestCode);
2027 try {
2028 super.startIntentSenderForResult(intent, requestCode,
2029 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2030 } catch (IntentSender.SendIntentException e) {
2031 throw new ActivityNotFoundException();
2032 }
2033 }
2034
2035 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002036 if (requestCode >= 0) {
2037 setWaitingForResult(true);
2038 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 }
2040
Winson Chung70d72102011-08-12 11:24:35 -07002041 /**
2042 * Indicates that we want global search for this activity by setting the globalSearch
2043 * argument for {@link #startSearch} to true.
2044 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002046 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002048
Karl Rosaen138a0412009-04-23 19:00:21 -07002049 if (initialQuery == null) {
2050 // Use any text typed in the launcher as the initial query
2051 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002052 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 if (appSearchData == null) {
2054 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002055 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002057
2058 // TODO send proper bounds.
2059 Rect sourceBounds = null;
Romain Guycbb89e42009-06-08 15:52:54 -07002060
Ian Parkinson047036e2014-09-01 15:40:53 +01002061 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002062 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002063 if (clearTextImmediately) {
2064 clearTypedText();
2065 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002066
2067 // We need to show the workspace after starting the search
2068 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002069 }
2070
Ian Parkinson047036e2014-09-01 15:40:53 +01002071 /**
2072 * Start a text search.
2073 *
2074 * @return {@code true} if the search will start immediately, so any further keypresses
2075 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2076 * to buffer keypresses.
2077 */
2078 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002079 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002080 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2081 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2082 sourceBounds);
2083 }
2084
Michael Jurkaa33411c2012-06-14 16:18:21 -07002085 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002086 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002087 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002088 }
2089
2090 /**
2091 * Starts the global search activity. This code is a copied from SearchManager
2092 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002093 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002094 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002095 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002096 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2097 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2098 if (globalSearchActivity == null) {
2099 Log.w(TAG, "No global search activity found.");
2100 return;
2101 }
2102 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2103 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2104 intent.setComponent(globalSearchActivity);
2105 // Make sure that we have a Bundle to put source in
2106 if (appSearchData == null) {
2107 appSearchData = new Bundle();
2108 } else {
2109 appSearchData = new Bundle(appSearchData);
2110 }
Adam Cohen9211d422014-10-07 18:14:53 -07002111 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002112 if (!appSearchData.containsKey("source")) {
2113 appSearchData.putString("source", getPackageName());
2114 }
2115 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2116 if (!TextUtils.isEmpty(initialQuery)) {
2117 intent.putExtra(SearchManager.QUERY, initialQuery);
2118 }
2119 if (selectInitialQuery) {
2120 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2121 }
2122 intent.setSourceBounds(sourceBounds);
2123 try {
2124 startActivity(intent);
2125 } catch (ActivityNotFoundException ex) {
2126 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2127 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002128 }
2129
Yura4f93ec62014-02-04 14:15:21 +00002130 public boolean isOnCustomContent() {
2131 return mWorkspace.isOnOrMovingToCustomContent();
2132 }
2133
Winson Chung70d72102011-08-12 11:24:35 -07002134 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002135 public boolean onPrepareOptionsMenu(Menu menu) {
2136 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002137 if (mLauncherCallbacks != null) {
2138 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2139 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002140 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002141 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002142
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002143 @Override
2144 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002145 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002146 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002147 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002148 }
2149
Joe Onorato9c1289c2009-08-17 11:03:03 -04002150 public boolean isWorkspaceLocked() {
2151 return mWorkspaceLoading || mWaitingForResult;
2152 }
2153
Adam Cohen517a7f52014-03-01 12:12:59 -08002154 public boolean isWorkspaceLoading() {
2155 return mWorkspaceLoading;
2156 }
2157
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002158 private void setWorkspaceLoading(boolean value) {
2159 boolean isLocked = isWorkspaceLocked();
2160 mWorkspaceLoading = value;
2161 if (isLocked != isWorkspaceLocked()) {
2162 onWorkspaceLockedChanged();
2163 }
2164 }
2165
2166 private void setWaitingForResult(boolean value) {
2167 boolean isLocked = isWorkspaceLocked();
2168 mWaitingForResult = value;
2169 if (isLocked != isWorkspaceLocked()) {
2170 onWorkspaceLockedChanged();
2171 }
2172 }
2173
Adam Cohen9211d422014-10-07 18:14:53 -07002174 protected void onWorkspaceLockedChanged() {
2175 if (mLauncherCallbacks != null) {
2176 mLauncherCallbacks.onWorkspaceLockedChanged();
2177 }
2178 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002179
Michael Jurka0280c3b2010-09-17 15:00:07 -07002180 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002181 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002182 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002183 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2184 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002185 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002186 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002187
Tony Wickhama0628cc2015-10-14 15:23:04 -07002188 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002189 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002190 if (LOGD) {
2191 Log.d(TAG, "Adding widget from drop");
2192 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002193 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2194 }
2195
Sunny Goyale6b63a32015-01-16 16:14:29 -08002196 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002197 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2198 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002199 if (appWidgetInfo.configure != null) {
2200 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002201 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002202
Bjorn Bringert7984c942009-12-09 15:38:25 +00002203 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002204 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2205 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2206
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002207 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002208 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002209 Runnable onComplete = new Runnable() {
2210 @Override
2211 public void run() {
2212 // Exit spring loaded mode if necessary after adding the widget
2213 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2214 null);
2215 }
2216 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002217 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002218 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002219 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002220 }
2221 }
Romain Guycbb89e42009-06-08 15:52:54 -07002222
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002223 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002224 // Close any folders that may be open.
2225 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002226 mWorkspace.moveToCustomContentScreen(animate);
2227 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002228
2229 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2230 int[] cell, int spanX, int spanY) {
2231 switch (info.itemType) {
2232 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2233 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2234 int span[] = new int[2];
2235 span[0] = spanX;
2236 span[1] = spanY;
2237 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2238 container, screenId, cell, span);
2239 break;
2240 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2241 processShortcutFromDrop(info.componentName, container, screenId, cell);
2242 break;
2243 default:
2244 throw new IllegalStateException("Unknown item type: " + info.itemType);
2245 }
2246 }
2247
Adam Cohenfbba09b2011-07-18 21:43:05 -07002248 /**
2249 * Process a shortcut drop.
2250 *
2251 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002252 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002253 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002254 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002255 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2256 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002257 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002258 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002259 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002260
2261 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002262 mPendingAddInfo.cellX = cell[0];
2263 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002264 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002265
2266 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2267 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002268 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002269 }
2270
Adam Cohenfbba09b2011-07-18 21:43:05 -07002271 /**
2272 * Process a widget drop.
2273 *
2274 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002275 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002276 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002277 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002278 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2279 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002280 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002281 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002282 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002283 mPendingAddInfo.minSpanX = info.minSpanX;
2284 mPendingAddInfo.minSpanY = info.minSpanY;
2285
Adam Cohenfbba09b2011-07-18 21:43:05 -07002286 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002287 mPendingAddInfo.cellX = cell[0];
2288 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002289 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002290 if (span != null) {
2291 mPendingAddInfo.spanX = span[0];
2292 mPendingAddInfo.spanY = span[1];
2293 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002294
Adam Cohened66b2b2012-01-23 17:28:51 -08002295 AppWidgetHostView hostView = info.boundWidget;
2296 int appWidgetId;
2297 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002298 // In the case where we've prebound the widget, we remove it from the DragLayer
2299 if (LOGD) {
2300 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2301 }
2302 getDragLayer().removeView(hostView);
2303
Adam Cohened66b2b2012-01-23 17:28:51 -08002304 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002305 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002306
2307 // Clear the boundWidget so that it doesn't get destroyed.
2308 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002309 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002310 // In this case, we either need to start an activity to get permission to bind
2311 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002312 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002313 Bundle options = info.bindOptions;
2314
Sunny Goyalffe83f12014-08-14 17:39:34 -07002315 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2316 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002317 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002318 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002319 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002320 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002321 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2322 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2323 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002324 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2325 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002326 // TODO: we need to make sure that this accounts for the options bundle.
2327 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002328 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2329 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002330 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002331 }
2332
Adam Cohendcd297f2013-06-18 13:13:40 -07002333 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002334 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002335 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002336 folderInfo.title = getText(R.string.folder_name);
2337
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002338 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002339 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2340 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002341
2342 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002343 FolderIcon newFolder =
2344 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002345 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002346 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002347 // Force measure the new folder icon
2348 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2349 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002350 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002351 }
Romain Guycbb89e42009-06-08 15:52:54 -07002352
Winsonc0b52fe2015-09-09 16:38:15 -07002353 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002354 * Unbinds the view for the specified item, and removes the item and all its children.
2355 *
2356 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002357 * @param itemInfo the {@link ItemInfo} for this view.
2358 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002359 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002360 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002361 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002362 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002363 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2364 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002365 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002366 } else {
2367 mWorkspace.removeWorkspaceItem(v);
2368 }
Winsonc0b52fe2015-09-09 16:38:15 -07002369 if (deleteFromDb) {
2370 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2371 }
2372 } else if (itemInfo instanceof FolderInfo) {
2373 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002374 if (v instanceof FolderIcon) {
2375 ((FolderIcon) v).removeListeners();
2376 }
Winsonc0b52fe2015-09-09 16:38:15 -07002377 mWorkspace.removeWorkspaceItem(v);
2378 if (deleteFromDb) {
2379 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2380 }
2381 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2382 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002383 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002384 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002385 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002386 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002387 }
2388 } else {
2389 return false;
2390 }
2391 return true;
2392 }
2393
2394 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002395 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002396 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002397 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002398 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002399 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002400 // Deleting an app widget ID is a void call but writes to disk before returning
2401 // to the caller...
2402 new AsyncTask<Void, Void, Void>() {
2403 public Void doInBackground(Void ... args) {
2404 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2405 return null;
2406 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002407 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002408 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002409 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002410 }
2411
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002412 @Override
2413 public boolean dispatchKeyEvent(KeyEvent event) {
2414 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2415 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002416 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002417 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002418 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002419 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002420 dumpState();
2421 return true;
2422 }
2423 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002424 }
2425 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2426 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002427 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002428 return true;
2429 }
2430 }
2431
2432 return super.dispatchKeyEvent(event);
2433 }
2434
Joe Onorato88ec0992009-11-19 13:16:06 -08002435 @Override
2436 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002437 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2438 return;
2439 }
2440
Sunny Goyal45478022015-06-08 16:52:41 -07002441 if (mDragController.isDragging()) {
2442 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002443 return;
2444 }
2445
Winson Chungb745afb2015-03-02 11:51:23 -08002446 if (isAppsViewVisible()) {
2447 showWorkspace(true);
2448 } else if (isWidgetsViewVisible()) {
2449 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002450 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002451 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002452 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002453 Folder openFolder = mWorkspace.getOpenFolder();
2454 if (openFolder.isEditingName()) {
2455 openFolder.dismissEditingName();
2456 } else {
2457 closeFolder();
2458 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002459 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002460 mWorkspace.exitWidgetResizeMode();
2461
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002462 // Back button is a no-op here, but give at least some feedback for the button press
2463 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002464 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002465 }
2466
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002467 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002468 * Launches the intent referred by the clicked shortcut.
2469 *
2470 * @param v The view representing the clicked shortcut.
2471 */
2472 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002473 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2474 // view has detached (it's possible for this to happen if the view is removed mid touch).
2475 if (v.getWindowToken() == null) {
2476 return;
2477 }
2478
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002479 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002480 return;
2481 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002482
Adam Cohen1697b792013-09-17 19:08:21 -07002483 if (v instanceof Workspace) {
2484 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002485 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002486 }
2487 return;
2488 }
2489
2490 if (v instanceof CellLayout) {
2491 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002492 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2493 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002494 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002495 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002496 }
2497
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002498 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002499 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002500 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002501 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002502 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002503 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002504 }
Sunny Goyalbb011da2016-06-15 15:42:29 -07002505 } else if (v instanceof PageIndicator || (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002506 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002507 } else if (tag instanceof AppInfo) {
2508 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002509 } else if (tag instanceof LauncherAppWidgetInfo) {
2510 if (v instanceof PendingAppWidgetHostView) {
2511 onClickPendingWidget((PendingAppWidgetHostView) v);
2512 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002513 }
2514 }
2515
Sunny Goyal70660032015-05-14 00:07:08 -07002516 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002517 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002518 return false;
2519 }
2520
Michael Jurkaaf442092010-06-10 17:01:57 -07002521 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002522 * Event handler for the app widget view which has not fully restored.
2523 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002524 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002525 if (mIsSafeModeEnabled) {
2526 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2527 return;
2528 }
2529
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002530 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002531 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002532 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2533 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2534 // This should not happen, as we make sure that an Id is allocated during bind.
2535 return;
2536 }
2537 LauncherAppWidgetProviderInfo appWidgetInfo =
2538 mAppWidgetManager.findProvider(info.providerName, info.user);
2539 if (appWidgetInfo != null) {
2540 mPendingAddWidgetId = info.appWidgetId;
2541 mPendingAddInfo.copyFrom(info);
2542 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002543
Sunny Goyald478c832016-04-01 12:04:16 -07002544 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2545 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingAddWidgetId);
2546 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
2547 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2548 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2549 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2550 }
2551 } else {
2552 LauncherAppWidgetProviderInfo appWidgetInfo =
2553 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2554 if (appWidgetInfo != null) {
2555 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2556 }
Sunny Goyalff572272014-07-23 13:58:07 -07002557 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002558 } else if (info.installProgress < 0) {
2559 // The install has not been queued
2560 final String packageName = info.providerName.getPackageName();
2561 showBrokenAppInstallDialog(packageName,
2562 new DialogInterface.OnClickListener() {
2563 public void onClick(DialogInterface dialog, int id) {
2564 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2565 }
2566 });
2567 } else {
2568 // Download has started.
2569 final String packageName = info.providerName.getPackageName();
2570 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002571 }
2572 }
2573
Sunny Goyald478c832016-04-01 12:04:16 -07002574 private void startRestoredWidgetReconfigActivity(
2575 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
2576 mPendingAddWidgetInfo = provider;
2577 mPendingAddInfo.copyFrom(info);
2578 mPendingAddWidgetId = info.appWidgetId;
2579 mAppWidgetManager.startConfigActivity(provider,
2580 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2581 }
2582
Sunny Goyalff572272014-07-23 13:58:07 -07002583 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002584 * Event handler for the "grid" button that appears on the home screen, which
2585 * enters all apps mode.
2586 *
2587 * @param v The view that was clicked.
2588 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002589 protected void onClickAllAppsButton(View v) {
2590 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002591 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002592 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002593 true /* updatePredictedApps */, false /* focusSearchBar */);
2594 }
2595 }
2596
2597 protected void onLongClickAllAppsButton(View v) {
2598 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2599 if (!isAppsViewVisible()) {
2600 showAppsView(true /* animated */, false /* resetListToTop */,
2601 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002602 }
2603 }
2604
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002605 private void showBrokenAppInstallDialog(final String packageName,
2606 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002607 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002608 .setTitle(R.string.abandoned_promises_title)
2609 .setMessage(R.string.abandoned_promise_explanation)
2610 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2611 .setNeutralButton(R.string.abandoned_clean_this,
2612 new DialogInterface.OnClickListener() {
2613 public void onClick(DialogInterface dialog, int id) {
2614 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2615 mWorkspace.removeAbandonedPromise(packageName, user);
2616 }
2617 })
2618 .create().show();
2619 return;
2620 }
2621
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002622 /**
2623 * Event handler for an app shortcut click.
2624 *
2625 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2626 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002627 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002628 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2629 Object tag = v.getTag();
2630 if (!(tag instanceof ShortcutInfo)) {
2631 throw new IllegalArgumentException("Input must be a Shortcut");
2632 }
2633
2634 // Open shortcut
2635 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002636
2637 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002638 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2639 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002640 // Launch activity anyway, framework will tell the user why the app is suspended.
2641 } else {
2642 int error = R.string.activity_not_available;
2643 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2644 error = R.string.safemode_shortcut_error;
2645 }
2646 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2647 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002648 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002649 }
2650
Chris Wren40c5ed32014-06-24 18:24:23 -04002651 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002652 if ((v instanceof BubbleTextView)
2653 && shortcut.isPromise()
2654 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002655 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002656 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002657 new DialogInterface.OnClickListener() {
2658 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002659 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002660 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002661 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002662 return;
2663 }
2664
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002665 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002666 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002667 }
2668
Sunny Goyala7ce1662016-05-31 15:01:35 -07002669 private void startAppShortcutOrInfoActivity(View v) {
2670 ItemInfo item = (ItemInfo) v.getTag();
2671 Intent intent = item.getIntent();
2672 if (intent == null) {
2673 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002674 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002675 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002676 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002677
2678 if (success && v instanceof BubbleTextView) {
2679 mWaitingForResume = (BubbleTextView) v;
2680 mWaitingForResume.setStayPressed(true);
2681 }
2682 }
2683
2684 /**
2685 * Event handler for a folder icon click.
2686 *
2687 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2688 */
2689 protected void onClickFolderIcon(View v) {
2690 if (LOGD) Log.d(TAG, "onClickFolder");
2691 if (!(v instanceof FolderIcon)){
2692 throw new IllegalArgumentException("Input must be a FolderIcon");
2693 }
2694
2695 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002696 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002697 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002698 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002699 }
Michael Jurka5130e402011-10-13 04:55:35 -07002700 }
2701
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002702 /**
2703 * Event handler for the (Add) Widgets button that appears after a long press
2704 * on the home screen.
2705 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002706 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002707 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002708 if (mIsSafeModeEnabled) {
2709 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2710 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002711 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002712 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002713 }
2714
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002715 /**
2716 * Event handler for the wallpaper picker button that appears after a long press
2717 * on the home screen.
2718 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002719 protected void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002720 if (!Utilities.isWallapaperAllowed(this)) {
2721 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2722 return;
2723 }
2724
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002725 String pickerPackage = getString(R.string.wallpaper_picker_package);
2726 if (TextUtils.isEmpty(pickerPackage)) {
2727 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2728 }
2729
Tony Wickham785f7a52015-08-31 17:28:32 -07002730 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2731 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002732 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER)
2733 .setPackage(pickerPackage)
2734 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset),
2735 REQUEST_PICK_WALLPAPER);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002736 }
2737
2738 /**
2739 * Event handler for a click on the settings button that appears after a long press
2740 * on the home screen.
2741 */
Sunny Goyal745bad92016-05-02 10:54:12 -07002742 private void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002743 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002744 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2745 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002746 }
2747
Adam Cohen61f560d2013-09-30 15:58:20 -07002748 public View.OnTouchListener getHapticFeedbackTouchListener() {
2749 if (mHapticFeedbackTouchListener == null) {
2750 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002751 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002752 @Override
2753 public boolean onTouch(View v, MotionEvent event) {
2754 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2755 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2756 }
2757 return false;
2758 }
2759 };
2760 }
2761 return mHapticFeedbackTouchListener;
2762 }
2763
Tony Wickhame2217252016-03-22 16:34:23 -07002764 @Override
2765 public void onAccessibilityStateChanged(boolean enabled) {
2766 mDragLayer.onAccessibilityStateChanged(enabled);
2767 }
2768
Adam Cohen9211d422014-10-07 18:14:53 -07002769 public void onDragStarted(View view) {
2770 if (isOnCustomContent()) {
2771 // Custom content screen doesn't participate in drag and drop. If on custom
2772 // content screen, move to default.
2773 moveWorkspaceToDefaultScreen();
2774 }
Adam Cohen9211d422014-10-07 18:14:53 -07002775 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002776
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002777 /**
2778 * Called when the user stops interacting with the launcher.
2779 * This implies that the user is now on the homescreen and is not doing housekeeping.
2780 */
Adam Cohen9211d422014-10-07 18:14:53 -07002781 protected void onInteractionEnd() {
2782 if (mLauncherCallbacks != null) {
2783 mLauncherCallbacks.onInteractionEnd();
2784 }
2785 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002786
2787 /**
2788 * Called when the user starts interacting with the launcher.
2789 * The possible interactions are:
2790 * - open all apps
2791 * - reorder an app shortcut, or a widget
2792 * - open the overview mode.
2793 * This is a good time to stop doing things that only make sense
2794 * when the user is on the homescreen and not doing housekeeping.
2795 */
Adam Cohen9211d422014-10-07 18:14:53 -07002796 protected void onInteractionBegin() {
2797 if (mLauncherCallbacks != null) {
2798 mLauncherCallbacks.onInteractionBegin();
2799 }
2800 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002801
Winson Chungcd99cd32015-04-29 11:03:24 -07002802 /** Updates the interaction state. */
2803 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002804 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002805 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002806 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2807 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002808 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002809 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002810 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002811 onInteractionEnd();
2812 }
2813 }
2814
Sunny Goyala7ce1662016-05-31 15:01:35 -07002815 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002816 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002817 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2818 try {
2819 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2820 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2821 // is enabled by default on NYC.
2822 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2823 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002824
2825 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2826 String id = ((ShortcutInfo) info).getDeepShortcutId();
2827 String packageName = intent.getPackage();
2828 LauncherAppsCompat.getInstance(this).startShortcut(
2829 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
2830 } else {
2831 // Could be launching some bookkeeping activity
2832 startActivity(intent, optsBundle);
2833 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002834 } finally {
2835 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002836 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002837 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002838 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2839 // corresponding permission. Show the appropriate permission prompt if that
2840 // is the case.
2841 if (intent.getComponent() == null
2842 && Intent.ACTION_CALL.equals(intent.getAction())
2843 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2844 PackageManager.PERMISSION_GRANTED) {
2845 // TODO: Rename sPendingAddItem to a generic name.
2846 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2847 0, info);
2848 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2849 REQUEST_PERMISSION_CALL_PHONE);
2850 } else {
2851 // No idea why this was thrown.
2852 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002853 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002854 }
2855 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002856
Sunny Goyala7ce1662016-05-31 15:01:35 -07002857 private Bundle getActivityLaunchOptions(View v) {
2858 if (Utilities.ATLEAST_MARSHMALLOW) {
2859 int left = 0, top = 0;
2860 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2861 if (v instanceof TextView) {
2862 // Launch from center of icon, not entire view
2863 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2864 if (icon != null) {
2865 Rect bounds = icon.getBounds();
2866 left = (width - bounds.width()) / 2;
2867 top = v.getPaddingTop();
2868 width = bounds.width();
2869 height = bounds.height();
2870 }
2871 }
2872 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2873 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2874 // On L devices, we use the device default slide-up transition.
2875 // On L MR1 devices, we use a custom version of the slide-up transition which
2876 // doesn't have the delay present in the device default.
2877 return ActivityOptions.makeCustomAnimation(
2878 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2879 }
2880 return null;
2881 }
2882
2883 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002884 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2885 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2886 return false;
2887 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002888 // Only launch using the new animation if the shortcut has not opted out (this is a
2889 // private contract between launcher and may be ignored in the future).
2890 boolean useLaunchAnimation = (v != null) &&
2891 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2892 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2893
2894 UserHandleCompat user = null;
2895 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2896 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2897 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002898 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002899
2900 // Prepare intent
2901 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2902 if (v != null) {
2903 int[] pos = new int[2];
2904 v.getLocationOnScreen(pos);
2905 intent.setSourceBounds(
2906 new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2907 }
2908 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002909 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2910 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
2911 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002912 // Shortcuts need some special checks due to legacy reasons.
2913 startShortcutIntentSafely(intent, optsBundle, item);
2914 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2915 // Could be launching some bookkeeping activity
2916 startActivity(intent, optsBundle);
2917 } else {
2918 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2919 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2920 }
2921 return true;
2922 } catch (ActivityNotFoundException|SecurityException e) {
2923 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2924 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2925 }
2926 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002927 }
2928
Adam Cohen268c4752012-06-06 17:47:33 -07002929 /**
2930 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2931 * in the DragLayer in the exact absolute location of the original FolderIcon.
2932 */
2933 private void copyFolderIconToImage(FolderIcon fi) {
2934 final int width = fi.getMeasuredWidth();
2935 final int height = fi.getMeasuredHeight();
2936
2937 // Lazy load ImageView, Bitmap and Canvas
2938 if (mFolderIconImageView == null) {
2939 mFolderIconImageView = new ImageView(this);
2940 }
2941 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2942 mFolderIconBitmap.getHeight() != height) {
2943 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2944 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2945 }
2946
2947 DragLayer.LayoutParams lp;
2948 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2949 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2950 } else {
2951 lp = new DragLayer.LayoutParams(width, height);
2952 }
2953
Adam Cohen307fe232012-08-16 17:55:58 -07002954 // The layout from which the folder is being opened may be scaled, adjust the starting
2955 // view size by this scale factor.
2956 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002957 lp.customPosition = true;
2958 lp.x = mRectForFolderAnimation.left;
2959 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002960 lp.width = (int) (scale * width);
2961 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002962
2963 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2964 fi.draw(mFolderIconCanvas);
2965 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002966 if (fi.getFolder() != null) {
2967 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2968 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002969 }
Adam Cohen268c4752012-06-06 17:47:33 -07002970 // Just in case this image view is still in the drag layer from a previous animation,
2971 // we remove it and re-add it.
2972 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2973 mDragLayer.removeView(mFolderIconImageView);
2974 }
2975 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002976 if (fi.getFolder() != null) {
2977 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002978 }
Adam Cohen268c4752012-06-06 17:47:33 -07002979 }
2980
Adam Cohen37b2a492016-04-06 18:36:06 -07002981 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002982 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002983 FolderInfo info = (FolderInfo) fi.getTag();
2984 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2985 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002986 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2987 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002988 }
2989
Adam Cohen268c4752012-06-06 17:47:33 -07002990 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2991 copyFolderIconToImage(fi);
2992 fi.setVisibility(View.INVISIBLE);
2993
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002994 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2995 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002996 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002997 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2998 }
2999 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003000 oa.start();
3001 }
3002
Sunny Goyal935fca12015-10-13 10:19:01 -07003003 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003004 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003005 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003006
Adam Cohen268c4752012-06-06 17:47:33 -07003007 // We remove and re-draw the FolderIcon in-case it has changed
3008 mDragLayer.removeView(mFolderIconImageView);
3009 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08003010
3011 if (cl != null) {
3012 cl.clearFolderLeaveBehind();
3013 }
3014
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003015 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003016 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003017 oa.addListener(new AnimatorListenerAdapter() {
3018 @Override
3019 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003020 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003021 // Remove the ImageView copy of the FolderIcon and make the original visible.
3022 mDragLayer.removeView(mFolderIconImageView);
3023 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003024 }
3025 }
3026 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003027 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003028 if (!animate) {
3029 oa.end();
3030 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003031 }
3032
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003033 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003034 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003035 * is animated relative to the specified View. If the View is null, no animation
3036 * is played.
3037 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003038 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003039 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003040 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003041
Adam Cohenfb91f302012-06-11 15:45:18 -07003042 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003043 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3044 if (openFolder != null && openFolder != folder) {
3045 // Close any open folder before opening a folder.
3046 closeFolder();
3047 }
3048
Adam Cohena9cf38f2011-05-02 15:36:58 -07003049 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003050
Adam Cohena9cf38f2011-05-02 15:36:58 -07003051 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003052
Sunny Goyalf4066152015-04-15 09:42:19 -07003053 // While the folder is open, the position of the icon cannot change.
3054 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3055
Adam Cohen4554ee12011-08-03 16:13:21 -07003056 // Just verify that the folder hasn't already been added to the DragLayer.
3057 // There was a one-off crash where the folder had a parent already.
3058 if (folder.getParent() == null) {
3059 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003060 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003061 } else {
3062 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3063 folder.getParent() + ").");
3064 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003065 folder.animateOpen();
3066
3067 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003068
3069 // Notify the accessibility manager that this folder "window" has appeared and occluded
3070 // the workspace items
3071 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3072 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003073 }
3074
3075 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003076 closeFolder(true);
3077 }
3078
3079 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003080 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003081 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003082 if (folder.isEditingName()) {
3083 folder.dismissEditingName();
3084 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003085 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003086 }
3087 }
3088
Sunny Goyal935fca12015-10-13 10:19:01 -07003089 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003090 animate &= !Utilities.isPowerSaverOn(this);
3091
Adam Cohen4554ee12011-08-03 16:13:21 -07003092 folder.getInfo().opened = false;
3093
3094 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3095 if (parent != null) {
3096 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003097 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003098 if (fi != null) {
3099 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3100 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003101 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003102 if (animate) {
3103 folder.animateClosed();
3104 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003105 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003106 }
Winson Chung83ca4802013-04-12 15:10:52 -07003107
Sunny Goyal935fca12015-10-13 10:19:01 -07003108 // Notify the accessibility manager that this folder "window" has disappeared and no
3109 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003110 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003111 }
3112
Tony Wickhamdadb3042016-02-24 11:07:00 -08003113 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003114 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003115 if (!isDraggingEnabled()) return false;
3116 if (isWorkspaceLocked()) return false;
3117 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003118
Sunny Goyalbb011da2016-06-15 15:42:29 -07003119 if (v == mAllAppsButton && mAllAppsButton != null) {
Winson Chung76648c52015-07-10 14:33:23 -07003120 onLongClickAllAppsButton(v);
3121 return true;
3122 }
3123
Adam Cohen1697b792013-09-17 19:08:21 -07003124 if (v instanceof Workspace) {
3125 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003126 if (!mWorkspace.isTouchActive()) {
3127 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003128 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3129 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3130 return true;
3131 } else {
3132 return false;
3133 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003134 } else {
3135 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003136 }
Adam Cohen1697b792013-09-17 19:08:21 -07003137 }
3138
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003139 CellLayout.CellInfo longClickCellInfo = null;
3140 View itemUnderLongClick = null;
3141 if (v.getTag() instanceof ItemInfo) {
3142 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003143 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003144 itemUnderLongClick = longClickCellInfo.cell;
3145 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003146 }
3147
Winson Chung3d503fb2011-07-13 17:25:49 -07003148 // The hotseat touch handling does not go through Workspace, and we always allow long press
3149 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003150 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003151 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003152 // User long pressed on empty space
3153 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3154 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003155 if (mWorkspace.isInOverviewMode()) {
3156 mWorkspace.startReordering(v);
3157 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003158 showOverviewMode(true);
Hyunyoung Song645764e2016-06-06 14:19:02 -07003159 mHotseat.requestDisallowInterceptTouchEvent(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003160 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003161 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003162 final boolean isAllAppsButton =
3163 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3164 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3165 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003166 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003167 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003168 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003169 }
3170 }
3171 }
3172 return true;
3173 }
3174
Winson Chung3d503fb2011-07-13 17:25:49 -07003175 boolean isHotseatLayout(View layout) {
3176 return mHotseat != null && layout != null &&
3177 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3178 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003179
Winson Chung3d503fb2011-07-13 17:25:49 -07003180 /**
3181 * Returns the CellLayout of the specified container at the specified screen.
3182 */
Sunny Goyal92820592015-03-02 11:31:35 -08003183 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003184 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3185 if (mHotseat != null) {
3186 return mHotseat.getLayout();
3187 } else {
3188 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003189 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003190 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003191 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003192 }
3193 }
3194
Winson Chungb745afb2015-03-02 11:51:23 -08003195 /**
3196 * For overridden classes.
3197 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003198 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003199 return isAppsViewVisible();
3200 }
3201
3202 public boolean isAppsViewVisible() {
3203 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3204 }
3205
3206 public boolean isWidgetsViewVisible() {
3207 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003208 }
3209
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003210 private void setWorkspaceBackground(int background) {
3211 switch (background) {
3212 case WORKSPACE_BACKGROUND_TRANSPARENT:
3213 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3214 break;
3215 case WORKSPACE_BACKGROUND_BLACK:
3216 getWindow().setBackgroundDrawable(null);
3217 break;
3218 default:
3219 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3220 }
Craig Mautner360310b2012-10-26 15:13:08 -07003221 }
3222
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003223 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003224 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3225 int curflags = getWindow().getAttributes().flags
3226 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3227 if (wpflags != curflags) {
3228 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3229 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003230 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003231 }
3232
Michael Jurkae326f182011-11-21 14:05:46 -08003233 @Override
3234 public void onTrimMemory(int level) {
3235 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003236 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3237 // The widget preview db can result in holding onto over
3238 // 3MB of memory for caching which isn't necessary.
3239 SQLiteDatabase.releaseMemory();
3240
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003241 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003242 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003243 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003244 if (mLauncherCallbacks != null) {
3245 mLauncherCallbacks.onTrimMemory(level);
3246 }
Michael Jurkae326f182011-11-21 14:05:46 -08003247 }
3248
Winson5c6bdbb2015-09-03 11:36:19 -07003249 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003250 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003251 }
3252
Winson5c6bdbb2015-09-03 11:36:19 -07003253 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003254 boolean changed = mState != State.WORKSPACE ||
3255 mWorkspace.getState() != Workspace.State.NORMAL;
3256 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003257 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003258 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003259 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003260
Michael Jurkab3e22d92011-10-31 15:58:33 -07003261 // Set focus to the AppsCustomize button
3262 if (mAllAppsButton != null) {
3263 mAllAppsButton.requestFocus();
3264 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003265 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003266
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003267 // Change the state *after* we've called all the transition code
3268 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003269
Winson Chung5fb63472011-02-02 17:03:37 -08003270 // Resume the auto-advance of widgets
3271 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003272 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003273
Winson Chung0f785722015-04-08 10:27:49 -07003274 if (changed) {
3275 // Send an accessibility event to announce the context change
3276 getWindow().getDecorView()
3277 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003278 }
Winson5c6bdbb2015-09-03 11:36:19 -07003279 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003280 }
3281
Winsone9f27272015-10-13 10:47:51 -07003282 /**
3283 * Shows the overview button.
3284 */
Adam Cohened307df2013-10-02 09:37:31 -07003285 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003286 showOverviewMode(animated, false);
3287 }
3288
3289 /**
3290 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3291 * onto one of the overview panel buttons.
3292 */
3293 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3294 Runnable postAnimRunnable = null;
3295 if (requestButtonFocus) {
3296 postAnimRunnable = new Runnable() {
3297 @Override
3298 public void run() {
3299 // Hitting the menu button when in touch mode does not trigger touch mode to
3300 // be disabled, so if requested, force focus on one of the overview panel
3301 // buttons.
3302 mOverviewPanel.requestFocusFromTouch();
3303 }
3304 };
3305 }
Adam Cohened307df2013-10-02 09:37:31 -07003306 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003307 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003308 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003309 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003310 }
3311
Winson Chungb745afb2015-03-02 11:51:23 -08003312 /**
3313 * Shows the apps view.
3314 */
Hyunyoung Song645764e2016-06-06 14:19:02 -07003315 public void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003316 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003317 if (resetListToTop) {
3318 mAppsView.scrollToTop();
3319 }
Winson Chung4ac30062015-05-08 17:34:17 -07003320 if (updatePredictedApps) {
3321 tryAndUpdatePredictedApps();
3322 }
Winson Chung76648c52015-07-10 14:33:23 -07003323 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003324 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003325
Winson Chungb745afb2015-03-02 11:51:23 -08003326 /**
3327 * Shows the widgets view.
3328 */
3329 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003330 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003331 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003332 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003333 }
Winson Chung76648c52015-07-10 14:33:23 -07003334 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003335
3336 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003337 @Override
3338 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003339 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003340 }
3341 });
Winson Chungb745afb2015-03-02 11:51:23 -08003342 }
3343
3344 /**
3345 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003346 *
3347 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003348 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003349 // TODO: calling method should use the return value so that when {@code false} is returned
3350 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003351 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003352 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3353 mState != State.WIDGETS_SPRING_LOADED) {
3354 return false;
3355 }
3356 if (toState != State.APPS && toState != State.WIDGETS) {
3357 return false;
3358 }
Winson Chungb745afb2015-03-02 11:51:23 -08003359
3360 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003361 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3362 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003363 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003364 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003365 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003366
Michael Jurkab3e22d92011-10-31 15:58:33 -07003367 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003368 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003369
3370 // Pause the auto-advance of widgets until we are out of AllApps
3371 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003372 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003373 closeFolder();
3374
3375 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003376 getWindow().getDecorView()
3377 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003378 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003379 }
3380
Winson Chungcd99cd32015-04-29 11:03:24 -07003381 /**
3382 * Updates the workspace and interaction state on state change, and return the animation to this
3383 * new state.
3384 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003385 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003386 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003387 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003388 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003389 updateInteraction(fromState, toState);
3390 return anim;
3391 }
3392
Jun Mukaif0033da2015-08-04 18:34:30 -07003393 public void onLauncherClingShown() {
3394 // When a launcher cling appears, it should cover the underlying layers, so their focus
3395 // should be blocked.
3396 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3397 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3398 }
3399 }
3400
3401 public void onLauncherClingDismissed() {
3402 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3403 }
3404
Hyunyoung Song3f471442015-04-08 19:01:34 -07003405 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003406 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003407 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003408 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003409 }
Winson Chungb745afb2015-03-02 11:51:23 -08003410
Winson Chung006ee262015-08-03 14:40:11 -07003411 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003412 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003413 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003414
3415 if (isAppsViewVisible()) {
3416 mState = State.APPS_SPRING_LOADED;
3417 } else if (isWidgetsViewVisible()) {
3418 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003419 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003420 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003421 } else {
3422 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003423 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003424 }
Adam Cohen7777d962011-08-18 18:58:38 -07003425
Hyunyoung Song3f471442015-04-08 19:01:34 -07003426 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003427 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003428 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003429
Winson Chunge7a03942011-08-05 15:05:12 -07003430 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003431 @Override
3432 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003433 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003434 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3435 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003436 // Before we show workspace, hide all apps again because
3437 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3438 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003439 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003440 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003441 } else {
3442 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003443 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003444 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003445 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003446 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003447
Tony Wickhame0c33232016-02-08 11:37:04 -08003448 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003449 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3450 || mState == State.WIDGETS_SPRING_LOADED;
3451 }
3452
Michael Jurkad3ef3062010-11-23 16:23:58 -08003453 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003454 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003455 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003456 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003457 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003458 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003459 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3460 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003461 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003462 }
3463
Winson Chung4ac30062015-05-08 17:34:17 -07003464 /**
3465 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3466 * resumed.
3467 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003468 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003469 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003470 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003471 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003472 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003473 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003474 }
3475 }
3476 }
3477
Michael Jurkab3e22d92011-10-31 15:58:33 -07003478 void lockAllApps() {
3479 // TODO
3480 }
3481
3482 void unlockAllApps() {
3483 // TODO
3484 }
3485
Winsonf768d932015-09-25 16:12:35 -07003486 public boolean launcherCallbacksProvidesSearch() {
3487 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3488 }
3489
Michael Jurkad7c28052012-04-27 15:43:36 -07003490 @Override
3491 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003492 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003493 final List<CharSequence> text = event.getText();
3494 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003495 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003496 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003497 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003498 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003499 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003500 } else if (mWorkspace != null) {
3501 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003502 } else {
3503 text.add(getString(R.string.all_apps_home_button_label));
3504 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003505 return result;
3506 }
3507
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003508 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003509 * If the activity is currently paused, signal that we need to run the passed Runnable
3510 * in onResume.
3511 *
3512 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003513 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3514 * wrong when we're not running, and if the activity comes back to what the configuration was
3515 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003516 *
3517 * Implementation of the method from LauncherModel.Callbacks.
3518 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003519 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003520 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003521 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003522 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003523 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003524 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003525 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003526 }
3527 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003528 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003529 return true;
3530 } else {
3531 return false;
3532 }
3533 }
3534
Michael Jurkac402cd92013-05-20 15:49:32 +02003535 private boolean waitUntilResume(Runnable run) {
3536 return waitUntilResume(run, false);
3537 }
3538
Michael Jurka1e2f4652013-07-08 18:03:46 -07003539 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003540 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003541 }
3542
Michael Jurka7607c2f2013-04-03 14:33:19 -07003543 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003544 * If the activity is currently paused, signal that we need to re-run the loader
3545 * in onResume.
3546 *
3547 * This needs to be called from incoming places where resources might have been loaded
3548 * while we are paused. That is becaues the Configuration might be wrong
3549 * when we're not running, and if it comes back to what it was when we
3550 * were paused, we are not restarted.
3551 *
3552 * Implementation of the method from LauncherModel.Callbacks.
3553 *
3554 * @return true if we are currently paused. The caller might be able to
3555 * skip some work in that case since we will come back again.
3556 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003557 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003558 public boolean setLoadOnResume() {
3559 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003560 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003561 mOnResumeNeedsLoad = true;
3562 return true;
3563 } else {
3564 return false;
3565 }
3566 }
3567
3568 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003569 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003570 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003571 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003572 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003573 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003574 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003575 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003576 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003577 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003578 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003579
Joe Onorato9c1289c2009-08-17 11:03:03 -04003580 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003581 * Clear any pending bind callbacks. This is called when is loader is planning to
3582 * perform a full rebind from scratch.
3583 */
3584 @Override
3585 public void clearPendingBinds() {
3586 mBindOnResumeCallbacks.clear();
3587 if (mPendingExecutor != null) {
3588 mPendingExecutor.markCompleted();
3589 mPendingExecutor = null;
3590 }
3591 }
3592
3593 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003594 * Refreshes the shortcuts shown on the workspace.
3595 *
3596 * Implementation of the method from LauncherModel.Callbacks.
3597 */
3598 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003599 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003600
Winson Chungd64d1762013-08-20 14:37:16 -07003601 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003602 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003603 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003604
Michael Jurka05bf644e2011-11-30 20:28:53 -08003605 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003606 if (mHotseat != null) {
3607 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003608 }
3609 }
3610
Adam Cohendcd297f2013-06-18 13:13:40 -07003611 @Override
3612 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003613 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003614 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3615 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003616 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3617 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3618 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003619 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3620 // If there are no screens, we need to have an empty screen
3621 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003622 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003623 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003624
3625 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003626 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003627 if (hasCustomContentToLeft()) {
3628 mWorkspace.createCustomContentContainer();
3629 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003630 }
Winson Chung64359a52013-07-08 17:17:08 -07003631 }
3632
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003633 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003634 int count = orderedScreenIds.size();
3635 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003636 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003637 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003638 // No need to bind the first screen, as its always bound.
3639 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3640 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003641 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003642 }
3643
Winson Chungd64d1762013-08-20 14:37:16 -07003644 public void bindAppsAdded(final ArrayList<Long> newScreens,
3645 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003646 final ArrayList<ItemInfo> addAnimated,
3647 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003648 Runnable r = new Runnable() {
3649 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003650 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003651 }
3652 };
3653 if (waitUntilResume(r)) {
3654 return;
3655 }
3656
Winson Chungd64d1762013-08-20 14:37:16 -07003657 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003658 if (newScreens != null) {
3659 bindAddScreens(newScreens);
3660 }
Winson Chungd64d1762013-08-20 14:37:16 -07003661
3662 // We add the items without animation on non-visible pages, and with
3663 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003664 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003665 bindItems(addNotAnimated, 0,
3666 addNotAnimated.size(), false);
3667 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003668 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003669 bindItems(addAnimated, 0,
3670 addAnimated.size(), true);
3671 }
Winson Chungc58497e2013-09-03 17:48:37 -07003672
Winson Chung87412982013-10-03 18:34:14 -07003673 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003674 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003675
Winson Chungb745afb2015-03-02 11:51:23 -08003676 if (addedApps != null && mAppsView != null) {
3677 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003678 }
Winson Chungd64d1762013-08-20 14:37:16 -07003679 }
3680
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003681 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003682 * Bind the items start-end from the list.
3683 *
3684 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003685 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003686 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003687 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3688 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003689 Runnable r = new Runnable() {
3690 public void run() {
3691 bindItems(shortcuts, start, end, forceAnimateIcons);
3692 }
3693 };
3694 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003695 return;
3696 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003697
Winson Chungf0c6ae02012-03-21 16:10:31 -07003698 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003699 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3700 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003701 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003702 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003703 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003704 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003705 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003706
3707 // Short circuit if we are loading dock items for a configuration which has no dock
3708 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3709 mHotseat == null) {
3710 continue;
3711 }
3712
Sunny Goyal639e9062015-08-19 19:17:06 -07003713 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003714 switch (item.itemType) {
3715 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3716 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003717 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003718 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003719 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003720 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003721 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003722 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003723 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003724 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003725 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003726 default:
3727 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003728 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003729
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003730 /*
3731 * Remove colliding items.
3732 */
3733 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3734 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3735 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3736 View v = cl.getChildAt(item.cellX, item.cellY);
3737 Object tag = v.getTag();
3738 String desc = "Collision while binding workspace item: " + item
3739 + ". Collides with " + tag;
3740 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3741 throw (new RuntimeException(desc));
3742 } else {
3743 Log.d(TAG, desc);
3744 LauncherModel.deleteItemFromDatabase(this, item);
3745 continue;
3746 }
3747 }
3748 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003749 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3750 item.cellY, 1, 1);
3751 if (animateIcons) {
3752 // Animate all the applications up now
3753 view.setAlpha(0f);
3754 view.setScaleX(0f);
3755 view.setScaleY(0f);
3756 bounceAnims.add(createNewAppBounceAnimation(view, i));
3757 newShortcutsScreenId = item.screenId;
3758 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003759 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003760
Winson Chung997a9232013-07-24 15:33:46 -07003761 if (animateIcons) {
3762 // Animate to the correct page
3763 if (newShortcutsScreenId > -1) {
3764 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003765 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003766 final Runnable startBounceAnimRunnable = new Runnable() {
3767 public void run() {
3768 anim.playTogether(bounceAnims);
3769 anim.start();
3770 }
3771 };
Winson Chung997a9232013-07-24 15:33:46 -07003772 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003773 // We post the animation slightly delayed to prevent slowdowns
3774 // when we are loading right after we return to launcher.
3775 mWorkspace.postDelayed(new Runnable() {
3776 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003777 if (mWorkspace != null) {
3778 mWorkspace.snapToPage(newScreenIndex);
3779 mWorkspace.postDelayed(startBounceAnimRunnable,
3780 NEW_APPS_ANIMATION_DELAY);
3781 }
Winson Chung94d67682013-09-25 16:29:40 -07003782 }
3783 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003784 } else {
3785 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003786 }
3787 }
Winson Chung64359a52013-07-08 17:17:08 -07003788 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003789 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003790 }
3791
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003792 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3793 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3794 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003795 view.updateAppWidget(null);
3796 view.setOnClickListener(this);
3797 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003798 mWorkspace.requestLayout();
3799 }
3800
Joe Onorato9c1289c2009-08-17 11:03:03 -04003801 /**
3802 * Add the views for a widget to the workspace.
3803 *
3804 * Implementation of the method from LauncherModel.Callbacks.
3805 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003806 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003807 Runnable r = new Runnable() {
3808 public void run() {
3809 bindAppWidget(item);
3810 }
3811 };
3812 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003813 return;
3814 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003815
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003816 if (mIsSafeModeEnabled) {
3817 bindSafeModeWidget(item);
3818 return;
3819 }
3820
Daniel Sandler843e8602010-06-07 14:59:01 -04003821 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3822 if (DEBUG_WIDGETS) {
3823 Log.d(TAG, "bindAppWidget: " + item);
3824 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003825
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003826 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003827
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003828 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3829 // If the provider is not ready, bind as a pending widget.
3830 appWidgetInfo = null;
3831 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3832 // The widget id is not valid. Try to find the widget based on the provider info.
3833 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3834 } else {
3835 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3836 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003837
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003838 // If the provider is ready, but the width is not yet restored, try to restore it.
3839 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3840 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003841 if (appWidgetInfo == null) {
3842 if (DEBUG_WIDGETS) {
3843 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3844 + " belongs to component " + item.providerName
3845 + ", as the povider is null");
3846 }
3847 LauncherModel.deleteItemFromDatabase(this, item);
3848 return;
3849 }
Sunny Goyalff572272014-07-23 13:58:07 -07003850
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003851 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003852 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003853 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3854 // Id has not been allocated yet. Allocate a new id.
3855 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3856 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003857
Sunny Goyald478c832016-04-01 12:04:16 -07003858 // Also try to bind the widget. If the bind fails, the user will be shown
3859 // a click to setup UI, which will ask for the bind permission.
3860 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3861 pendingInfo.spanX = item.spanX;
3862 pendingInfo.spanY = item.spanY;
3863 pendingInfo.minSpanX = item.minSpanX;
3864 pendingInfo.minSpanY = item.minSpanY;
3865 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
3866 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3867 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003868
Sunny Goyald478c832016-04-01 12:04:16 -07003869 // Bind succeeded
3870 if (success) {
3871 // If the widget has a configure activity, it is still needs to set it up,
3872 // otherwise the widget is ready to go.
3873 item.restoreStatus = (appWidgetInfo.configure == null)
3874 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3875 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003876 }
Sunny Goyald478c832016-04-01 12:04:16 -07003877
3878 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003879 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003880 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003881 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003882 // The widget was marked as UI not ready, but there is no configure activity to
3883 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003884 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3885 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003886 }
Sunny Goyalff572272014-07-23 13:58:07 -07003887 }
3888
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003889 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003890 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003891 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3892 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003893 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003894
Sunny Goyal56c73602015-09-25 12:55:01 -07003895 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003896 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003897 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003898 deleteWidgetInfo(item);
3899 return;
3900 }
3901
Sunny Goyal233ee962015-08-03 13:05:01 -07003902 item.minSpanX = appWidgetInfo.minSpanX;
3903 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003904 addAppWidgetToWorkspace(
3905 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3906 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003907 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003908 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003909 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003910 view.updateAppWidget(null);
3911 view.setOnClickListener(this);
3912 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003913 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003914 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003915
Daniel Sandler843e8602010-06-07 14:59:01 -04003916 if (DEBUG_WIDGETS) {
3917 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3918 + (SystemClock.uptimeMillis()-start) + "ms");
3919 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003920 }
3921
Sunny Goyalff572272014-07-23 13:58:07 -07003922 /**
3923 * Restores a pending widget.
3924 *
3925 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003926 */
Sunny Goyald478c832016-04-01 12:04:16 -07003927 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003928 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3929 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3930 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003931 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003932 }
3933
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003934 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003935 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003936
3937 mWorkspace.reinflateWidgetsIfNecessary();
3938 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003939 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003940 }
3941
Adam Cohen1462de32012-07-24 22:34:36 -07003942 public void onPageBoundSynchronously(int page) {
3943 mSynchronouslyBoundPages.add(page);
3944 }
3945
Sunny Goyal527c7d32015-08-28 15:19:36 -07003946 @Override
3947 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3948 if (mPendingExecutor != null) {
3949 mPendingExecutor.markCompleted();
3950 }
3951 mPendingExecutor = executor;
3952 executor.attachTo(this);
3953 }
3954
3955 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3956 if (mPendingExecutor == executor) {
3957 mPendingExecutor = null;
3958 }
3959 }
3960
Joe Onorato9c1289c2009-08-17 11:03:03 -04003961 /**
3962 * Callback saying that there aren't any more items to bind.
3963 *
3964 * Implementation of the method from LauncherModel.Callbacks.
3965 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003966 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003967 Runnable r = new Runnable() {
3968 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003969 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003970 }
3971 };
3972 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003973 return;
3974 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003975 if (mSavedState != null) {
3976 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003977 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003978 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003979
Joe Onorato9c1289c2009-08-17 11:03:03 -04003980 mSavedState = null;
3981 }
3982
Adam Cohen1462de32012-07-24 22:34:36 -07003983 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003984
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003985 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003986
3987 // If we received the result of any pending adds while the loader was running (e.g. the
3988 // widget configuration forced an orientation change), process them now.
3989 if (sPendingAddItem != null) {
3990 final long screenId = completeAdd(sPendingAddItem);
3991
3992 // TODO: this moves the user to the page where the pending item was added. Ideally,
3993 // the screen would be guaranteed to exist after bind, and the page would be set through
3994 // the workspace restore process.
3995 mWorkspace.post(new Runnable() {
3996 @Override
3997 public void run() {
3998 mWorkspace.snapToScreenId(screenId);
3999 }
4000 });
4001 sPendingAddItem = null;
4002 }
4003
Sunny Goyal756adbc2015-04-16 15:20:51 -07004004 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004005
4006 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004007 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004008 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004009 }
4010
Winson Chunga2413752012-04-03 14:22:34 -07004011 private boolean canRunNewAppsAnimation() {
4012 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004013 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4014 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004015 }
4016
Winson Chungc9168342013-06-26 14:54:55 -07004017 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004018 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004019 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4020 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004021 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004022 return bounceAnim;
4023 }
4024
Adam Cohen27772732013-11-11 14:00:56 +00004025 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004026 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004027 }
4028
Tony Wickham55616cd2015-09-23 14:55:17 -07004029 public int getSearchBarHeight() {
4030 if (mLauncherCallbacks != null) {
4031 return mLauncherCallbacks.getSearchBarHeight();
4032 }
4033 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4034 }
4035
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004036 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004037 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4038 * multiple calls to bind the same list.)
4039 */
4040 @Thunk ArrayList<AppInfo> mTmpAppsList;
4041 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4042 public void run() {
4043 bindAllApplications(mTmpAppsList);
4044 mTmpAppsList = null;
4045 }
4046 };
4047
4048 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004049 * Add the icons for all apps.
4050 *
4051 * Implementation of the method from LauncherModel.Callbacks.
4052 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004053 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004054 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4055 mTmpAppsList = apps;
4056 return;
4057 }
4058
Winson Chungb745afb2015-03-02 11:51:23 -08004059 if (mAppsView != null) {
4060 mAppsView.setApps(apps);
4061 }
Adam Cohen9211d422014-10-07 18:14:53 -07004062 if (mLauncherCallbacks != null) {
4063 mLauncherCallbacks.bindAllApplications(apps);
4064 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004065 }
4066
4067 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004068 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
4069 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
4070 */
4071 @Override
4072 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
4073 mDeepShortcutMap = deepShortcutMapCopy;
4074 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
4075 }
4076
4077 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004078 * A package was updated.
4079 *
4080 * Implementation of the method from LauncherModel.Callbacks.
4081 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004082 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004083 Runnable r = new Runnable() {
4084 public void run() {
4085 bindAppsUpdated(apps);
4086 }
4087 };
4088 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004089 return;
4090 }
4091
Winson Chungb745afb2015-03-02 11:51:23 -08004092 if (mAppsView != null) {
4093 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004094 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004095 }
4096
Sunny Goyal4390ace2014-10-13 11:33:11 -07004097 @Override
4098 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4099 Runnable r = new Runnable() {
4100 public void run() {
4101 bindWidgetsRestored(widgets);
4102 }
4103 };
4104 if (waitUntilResume(r)) {
4105 return;
4106 }
4107 mWorkspace.widgetsRestored(widgets);
4108 }
4109
Joe Onorato9c1289c2009-08-17 11:03:03 -04004110 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004111 * Some shortcuts were updated in the background.
4112 *
4113 * Implementation of the method from LauncherModel.Callbacks.
4114 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004115 @Override
4116 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4117 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004118 Runnable r = new Runnable() {
4119 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004120 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004121 }
4122 };
4123 if (waitUntilResume(r)) {
4124 return;
4125 }
4126
Sunny Goyal4390ace2014-10-13 11:33:11 -07004127 if (!updated.isEmpty()) {
4128 mWorkspace.updateShortcuts(updated);
4129 }
4130
4131 if (!removed.isEmpty()) {
4132 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4133 for (ShortcutInfo si : removed) {
4134 removedComponents.add(si.getTargetComponent());
4135 }
4136 mWorkspace.removeItemsByComponentName(removedComponents, user);
4137 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004138 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004139 }
4140 }
4141
4142 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004143 * Update the state of a package, typically related to install state.
4144 *
4145 * Implementation of the method from LauncherModel.Callbacks.
4146 */
Sunny Goyale755d462014-07-22 13:48:29 -07004147 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004148 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4149 Runnable r = new Runnable() {
4150 public void run() {
4151 bindRestoreItemsChange(updates);
4152 }
4153 };
4154 if (waitUntilResume(r)) {
4155 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004156 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004157
Sunny Goyal756adbc2015-04-16 15:20:51 -07004158 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004159 }
4160
4161 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004162 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004163 * in addition to specific applications to remove, the reason being that
4164 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004165 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004166 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004167 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004168 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004169 public void bindWorkspaceComponentsRemoved(
4170 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4171 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004172 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004173 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004174 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004175 }
Winson Chungd64d1762013-08-20 14:37:16 -07004176 };
4177 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004178 return;
4179 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004180 if (!packageNames.isEmpty()) {
4181 mWorkspace.removeItemsByPackageName(packageNames, user);
4182 }
4183 if (!components.isEmpty()) {
4184 mWorkspace.removeItemsByComponentName(components, user);
4185 }
4186 // Notify the drag controller
4187 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004188
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004189 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004190
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004191 @Override
4192 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4193 Runnable r = new Runnable() {
4194 public void run() {
4195 bindAppInfosRemoved(appInfos);
4196 }
4197 };
4198 if (waitUntilResume(r)) {
4199 return;
Joe Onorato36115782010-06-17 13:28:48 -04004200 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004201
Winson Chungdf95eb12013-10-16 14:57:07 -07004202 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004203 if (mAppsView != null) {
4204 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004205 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004206 }
Joe Onoratobe386092009-11-17 17:32:16 -08004207
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004208 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004209 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004210 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004211 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004212 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004213
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004214 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004215 public void bindWidgetsModel(WidgetsModel model) {
4216 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004217 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004218 return;
4219 }
4220
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004221 if (mWidgetsView != null && model != null) {
4222 mWidgetsView.addWidgets(model);
4223 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004224 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004225 }
4226
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004227 @Override
4228 public void notifyWidgetProvidersChanged() {
4229 if (mWorkspace.getState().shouldUpdateWidget) {
4230 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4231 }
4232 }
4233
Winson Chung400438b2011-01-16 17:53:48 -08004234 private int mapConfigurationOriActivityInfoOri(int configOri) {
4235 final Display d = getWindowManager().getDefaultDisplay();
4236 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4237 switch (d.getRotation()) {
4238 case Surface.ROTATION_0:
4239 case Surface.ROTATION_180:
4240 // We are currently in the same basic orientation as the natural orientation
4241 naturalOri = configOri;
4242 break;
4243 case Surface.ROTATION_90:
4244 case Surface.ROTATION_270:
4245 // We are currently in the other basic orientation to the natural orientation
4246 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4247 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4248 break;
4249 }
4250
4251 int[] oriMap = {
4252 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4253 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4254 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4255 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4256 };
4257 // Since the map starts at portrait, we need to offset if this device's natural orientation
4258 // is landscape.
4259 int indexOffset = 0;
4260 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4261 indexOffset = 1;
4262 }
4263 return oriMap[(d.getRotation() + indexOffset) % 4];
4264 }
Adam Cohen446e9402011-09-15 18:21:21 -07004265
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004266 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004267 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004268 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004269 if (Utilities.ATLEAST_JB_MR2) {
4270 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4271 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004272 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4273 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004274 }
Winson Chung4b919f82012-05-01 10:44:08 -07004275 }
4276 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004277
Winson Chung4b919f82012-05-01 10:44:08 -07004278 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004279 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004280 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004281 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004282 } else {
4283 mHandler.postDelayed(new Runnable() {
4284 public void run() {
4285 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4286 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004287 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004288 }
Winson Chung4b919f82012-05-01 10:44:08 -07004289 }
Winson Chung400438b2011-01-16 17:53:48 -08004290 }
4291
Adam Cohenea90f832014-05-21 15:03:34 -07004292 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004293 * To be overridden by subclasses to indicate that there is an activity to launch
4294 * before showing the standard launcher experience.
4295 */
4296 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004297 if (mLauncherCallbacks != null) {
4298 return mLauncherCallbacks.hasFirstRunActivity();
4299 }
Adam Cohen432609a2014-03-13 17:03:22 -07004300 return false;
4301 }
4302
4303 /**
4304 * To be overridden by subclasses to launch any first run activity
4305 */
4306 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004307 if (mLauncherCallbacks != null) {
4308 return mLauncherCallbacks.getFirstRunActivity();
4309 }
Adam Cohen432609a2014-03-13 17:03:22 -07004310 return null;
4311 }
4312
Winson Chunga6945242014-01-08 14:04:34 -08004313 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004314 return !ActivityManager.isRunningInTestHarness() &&
4315 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004316 }
4317
Adam Cohen4a9423d2014-03-28 14:22:31 -07004318 protected boolean hasRunFirstRunActivity() {
4319 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4320 }
4321
Adam Cohen432609a2014-03-13 17:03:22 -07004322 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004323 if (shouldRunFirstRunActivity() &&
4324 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004325 Intent firstRunIntent = getFirstRunActivity();
4326 if (firstRunIntent != null) {
4327 startActivity(firstRunIntent);
4328 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004329 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004330 }
4331 }
Adam Cohen432609a2014-03-13 17:03:22 -07004332 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004333 }
4334
4335 private void markFirstRunActivityShown() {
4336 SharedPreferences.Editor editor = mSharedPrefs.edit();
4337 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4338 editor.apply();
4339 }
4340
Adam Cohen432609a2014-03-13 17:03:22 -07004341 /**
4342 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4343 * screen that must be displayed and dismissed.
4344 */
4345 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004346 if (mLauncherCallbacks != null) {
4347 return mLauncherCallbacks.hasDismissableIntroScreen();
4348 }
Adam Cohen432609a2014-03-13 17:03:22 -07004349 return false;
4350 }
4351
4352 /**
4353 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4354 */
4355 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004356 if (mLauncherCallbacks != null) {
4357 return mLauncherCallbacks.getIntroScreen();
4358 }
Adam Cohen432609a2014-03-13 17:03:22 -07004359 return null;
4360 }
4361
4362 /**
4363 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4364 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4365 */
4366 private boolean shouldShowIntroScreen() {
4367 return hasDismissableIntroScreen() &&
4368 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4369 }
4370
4371 protected void showIntroScreen() {
4372 View introScreen = getIntroScreen();
4373 changeWallpaperVisiblity(false);
4374 if (introScreen != null) {
4375 mDragLayer.showOverlayView(introScreen);
4376 }
4377 }
4378
4379 public void dismissIntroScreen() {
4380 markIntroScreenDismissed();
4381 if (showFirstRunActivity()) {
4382 // We delay hiding the intro view until the first run activity is showing. This
4383 // avoids a blip.
4384 mWorkspace.postDelayed(new Runnable() {
4385 @Override
4386 public void run() {
4387 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004388 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004389 }
4390 }, ACTIVITY_START_DELAY);
4391 } else {
4392 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004393 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004394 }
4395 changeWallpaperVisiblity(true);
4396 }
4397
4398 private void markIntroScreenDismissed() {
4399 SharedPreferences.Editor editor = mSharedPrefs.edit();
4400 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4401 editor.apply();
4402 }
4403
Adam Cohen091440a2015-03-18 14:16:05 -07004404 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004405 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4406 // on the device, then we always show the first run cling experience (or if there is no
4407 // launcher2). Otherwise, we prompt the user upon started for migration
4408 LauncherClings launcherClings = new LauncherClings(this);
4409 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004410 mClings = launcherClings;
Sunny Goyalded0fdb2016-05-23 15:55:41 -07004411 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004412 }
4413 }
4414
Winson Chung5ffd51d2015-06-25 11:36:50 -07004415 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004416 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004417 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004418 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004419 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004420 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004421 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4422 if (userHandle != null) {
4423 user = UserHandleCompat.fromUser(userHandle);
4424 }
4425 }
4426 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004427 }
4428
4429 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004430 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004431 if (user == null) {
4432 user = UserHandleCompat.myUserHandle();
4433 }
4434
4435 // Called from search suggestion, add the profile extra to the intent to ensure that we
4436 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004437 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004438 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004439 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004440 return null;
4441 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004442 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004443 }
4444
Winson Chung5ffd51d2015-06-25 11:36:50 -07004445 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004446 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4447 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004448 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004449 UserHandleCompat.myUserHandle());
4450 }
4451
Winson Chung5ffd51d2015-06-25 11:36:50 -07004452 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004453 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4454 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004455 mWorkspace.onExternalDragStartedWithItem(dragView);
4456 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004457 }
4458
Winson Chung5ffd51d2015-06-25 11:36:50 -07004459 protected void moveWorkspaceToDefaultScreen() {
4460 mWorkspace.moveToDefaultScreen(false);
4461 }
4462
Winson Chung80baf5a2010-08-09 16:03:15 -07004463 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004464 * Returns a FastBitmapDrawable with the icon, accurately sized.
4465 */
4466 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4467 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4468 d.setFilterBitmap(true);
4469 resizeIconDrawable(d);
4470 return d;
4471 }
4472
4473 /**
4474 * Resizes an icon drawable to the correct icon size.
4475 */
Winson5fbe0742015-09-30 15:33:00 -07004476 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004477 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004478 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004479 }
4480
4481 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004482 * Prints out out state for debugging.
4483 */
4484 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004485 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004486 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004487 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4488 Log.d(TAG, "mRestoring=" + mRestoring);
4489 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004490 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004491 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004492
Daniel Sandler325dc232013-06-05 22:57:57 -04004493 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004494 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004495
4496 @Override
4497 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4498 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004499 // Dump workspace
4500 writer.println(prefix + "Workspace Items");
4501 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4502 writer.println(prefix + " Homescreen " + i);
4503
4504 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4505 for (int j = 0; j < layout.getChildCount(); j++) {
4506 Object tag = layout.getChildAt(j).getTag();
4507 if (tag != null) {
4508 writer.println(prefix + " " + tag.toString());
4509 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004510 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004511 }
Sunny Goyala1365452015-10-01 15:46:24 -07004512
4513 writer.println(prefix + " Hotseat");
4514 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4515 for (int j = 0; j < layout.getChildCount(); j++) {
4516 Object tag = layout.getChildAt(j).getTag();
4517 if (tag != null) {
4518 writer.println(prefix + " " + tag.toString());
4519 }
4520 }
4521
Sunny Goyal713edfc2016-05-06 09:58:34 -07004522 try {
4523 FileLog.flushAll(writer);
4524 } catch (Exception e) {
4525 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004526 }
4527
Adam Cohen9211d422014-10-07 18:14:53 -07004528 if (mLauncherCallbacks != null) {
4529 mLauncherCallbacks.dump(prefix, fd, writer, args);
4530 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004531 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004532
Adam Cohen59400422014-03-05 18:07:04 -08004533 public static CustomAppWidget getCustomAppWidget(String name) {
4534 return sCustomAppWidgets.get(name);
4535 }
4536
4537 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4538 return sCustomAppWidgets;
4539 }
4540
Sunny Goyal29538332016-02-18 09:10:19 -08004541 public static List<View> getFolderContents(View icon) {
4542 if (icon instanceof FolderIcon) {
4543 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4544 } else {
4545 return Collections.EMPTY_LIST;
4546 }
4547 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004548
4549 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4550
4551 @Override
4552 public void onSharedPreferenceChanged(
4553 SharedPreferences sharedPreferences, String key) {
4554 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4555 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4556 if (!waitUntilResume(this, true)) {
4557 run();
4558 }
4559 }
4560 }
4561
4562 @Override
4563 public void run() {
4564 setOrientation();
4565 }
4566 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004567}