blob: 651e91b7b2ccbdf0c5604afcd60fe70c999866ed [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyal28c6b962015-10-12 11:42:05 -070019import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070025import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000026import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080037import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Sunny Goyal756cd262015-08-20 12:33:21 -070039import android.content.ContentValues;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070041import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040042import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070044import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070045import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070046import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070047import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070048import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.pm.PackageManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070051import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070052import android.graphics.Bitmap;
53import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070054import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070055import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070057import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020060import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.os.Bundle;
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 Goyale26d1002016-06-20 14:52:14 -070066import android.os.Trace;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070067import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.HapticFeedbackConstants;
75import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070077import android.view.MotionEvent;
78import android.view.Surface;
79import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070080import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080081import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.view.ViewGroup;
83import android.view.ViewTreeObserver;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080085import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070086import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070087import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080091import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070093
Sunny Goyal651077b2014-06-30 14:15:31 -070094import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070095import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyalae502842016-06-17 08:43:56 -070096import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070097import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070098import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080099import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700100import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100101import com.android.launcher3.compat.LauncherActivityInfoCompat;
102import com.android.launcher3.compat.LauncherAppsCompat;
103import com.android.launcher3.compat.UserHandleCompat;
104import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800105import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700106import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700107import com.android.launcher3.dragndrop.DragController;
108import com.android.launcher3.dragndrop.DragLayer;
109import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -0700110import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000111import com.android.launcher3.folder.Folder;
112import com.android.launcher3.folder.FolderIcon;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700113import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700114import com.android.launcher3.logging.FileLog;
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;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700118import com.android.launcher3.shortcuts.DeepShortcutManager;
Tony Wickham49c8d292016-07-01 11:44:34 -0700119import com.android.launcher3.shortcuts.DeepShortcutsContainer;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700120import com.android.launcher3.util.ComponentKey;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700121import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700122import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal322d5562015-06-25 19:35:49 -0700123import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700124import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700125import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700126import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700127import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700128import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700129
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700130import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700131import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700132import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700133import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800134import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700135import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700136import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700137import java.util.List;
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
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700149 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700150 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400151 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700154 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700155 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700156 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
Michael Jurka8b805b12012-04-18 14:23:14 -0700158 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700159 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700160 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700161
Sunny Goyal28c6b962015-10-12 11:42:05 -0700162 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
163
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700164 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
165 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
166 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
167
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700168 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
169
Mathew Inwood876a8462013-06-14 14:12:41 +0100170 /**
171 * IntentStarter uses request codes starting with this. This must be greater than all activity
172 * request codes used internally.
173 */
174 protected static final int REQUEST_LAST = 100;
175
Michael Jurka0a457bf2012-11-19 14:05:05 -0800176 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700177 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800178 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
Winson Chung2672ff92012-05-04 16:22:30 -0700180 // The Intent extra that defines whether to ignore the launch animation
181 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400182 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700183
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700184 public static final String ACTION_APPWIDGET_HOST_RESET =
185 "com.android.launcher3.intent.ACTION_APPWIDGET_HOST_RESET";
186
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 // Type: int
188 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700189 // Type: int
190 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700191 // Type: Content Values / parcelable
192 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700193 // Type: parcelable
194 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000195 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800196 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197
Adam Cohen432609a2014-03-13 17:03:22 -0700198 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800199 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
200
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700201 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700202 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
203 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700204
Adam Cohen091440a2015-03-18 14:16:05 -0700205 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700206 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700207
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700208 private boolean mIsSafeModeEnabled;
209
Joe Onorato9c1289c2009-08-17 11:03:03 -0400210 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800211 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700212 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700213 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214
Winson Chunga2413752012-04-03 14:22:34 -0700215 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700216 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
217 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700218 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700219
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700220 private final BroadcastReceiver mUiBroadcastReceiver = new BroadcastReceiver() {
221
222 @Override
223 public void onReceive(Context context, Intent intent) {
224 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
225 closeSystemDialogs();
226 } else if (ACTION_APPWIDGET_HOST_RESET.equals(intent.getAction())) {
227 if (mAppWidgetHost != null) {
228 mAppWidgetHost.startListening();
229 }
230 }
231 }
232 };
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800233
Adam Cohen091440a2015-03-18 14:16:05 -0700234 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700235 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700236 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800237 private DragController mDragController;
Sunny Goyal6178f132016-07-11 17:30:03 -0700238 private View mQsbContainer;
Sunny Goyal322d5562015-06-25 19:35:49 -0700239
240 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700241
Sunny Goyalffe83f12014-08-14 17:39:34 -0700242 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700243 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700244
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700245 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800246 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800247 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700248
Michael Jurka0280c3b2010-09-17 15:00:07 -0700249 private int[] mTmpAddItemCellCoordinates = new int[2];
250
Sunny Goyal316490e2015-06-02 09:38:28 -0700251 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800252 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700253
Michael Jurka838a4ca2011-02-07 13:33:06 -0800254 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700255 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700256
Sunny Goyal47328fd2016-05-25 18:56:41 -0700257 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700258
259 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700260 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700261 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700262
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700263 // Main container view and the model for the widget tray screen.
264 @Thunk WidgetsContainerView mWidgetsView;
265 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700266
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700268 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
269 // scroll issues (because the workspace may not have been measured yet) and extra work.
270 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
271 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272
273 private SpannableStringBuilder mDefaultKeySsb = null;
274
Adam Cohen091440a2015-03-18 14:16:05 -0700275 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400276
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800277 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278 private boolean mRestoring;
279 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700280 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281
Michael Jurka1e2f4652013-07-08 18:03:46 -0700282 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700283 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700284 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700285
Joe Onorato9c1289c2009-08-17 11:03:03 -0400286 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800287 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700288 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700289 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Adam Cohen091440a2015-03-18 14:16:05 -0700290 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800291 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700292 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800293 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400294
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700295 /** Maps launcher activity components to their list of shortcut ids. */
296 private MultiHashMap<ComponentKey, String> mDeepShortcutMap = new MultiHashMap<>();
297
Adam Cohen61f560d2013-09-30 15:58:20 -0700298 private View.OnTouchListener mHapticFeedbackTouchListener;
299
Adam Cohended9f8d2010-11-03 13:25:16 -0700300 // Related to the auto-advancing of widgets
301 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700302 private static final int ADVANCE_INTERVAL = 20000;
303 private static final int ADVANCE_STAGGER = 250;
304
305 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700306 private long mAutoAdvanceSentTime;
307 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700308 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700309
Winson Chung400438b2011-01-16 17:53:48 -0800310 // Determines how long to wait after a rotation before restoring the screen orientation to
311 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700312 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800313
Adam Cohen091440a2015-03-18 14:16:05 -0700314 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700315
Adam Cohen1462de32012-07-24 22:34:36 -0700316 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700317 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700318
Winson Chung46353de2012-02-16 14:05:10 -0800319 // We only want to get the SharedPreferences once since it does an FS stat each time we get
320 // it from the context.
321 private SharedPreferences mSharedPrefs;
322
Winson Chungf0c6ae02012-03-21 16:10:31 -0700323 // Holds the page that we need to animate to, and the icon views that we need to animate up
324 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700325 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700326 private Bitmap mFolderIconBitmap;
327 private Canvas mFolderIconCanvas;
328 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700329
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700330 private DeviceProfile mDeviceProfile;
331
Adam Cohen4b66bf32015-09-18 12:15:19 -0700332 private boolean mMoveToDefaultScreenFromNewIntent;
333
Winson Chung13eb5272015-05-11 16:30:13 -0700334 // This is set to the view that launched the activity that navigated the user away from
335 // launcher. Since there is no callback for when the activity has finished launching, enable
336 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800337 private BubbleTextView mWaitingForResume;
338
Adam Cohen59400422014-03-05 18:07:04 -0800339 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
340 new HashMap<String, CustomAppWidget>();
341
Adam Cohen59400422014-03-05 18:07:04 -0800342 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700343 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
344 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800345 }
346 }
347
Adam Cohen091440a2015-03-18 14:16:05 -0700348 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700349 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700350 if (mWorkspace != null) {
351 mWorkspace.buildPageHardwareLayers();
352 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700353 }
354 };
355
Adam Cohendb364c32014-05-20 14:23:40 -0700356 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800357
Adam Cohen091440a2015-03-18 14:16:05 -0700358 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800359 int requestCode;
360 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700361 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700362 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800363 int cellX;
364 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700365 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800366 }
367
Hyunyoung Songaa953652016-04-19 18:30:24 -0700368 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800369
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700370 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700371 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400372
373 @Thunk void setOrientation() {
374 if (mRotationEnabled) {
375 unlockScreenOrientation(true);
376 } else {
377 setRequestedOrientation(
378 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700379 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400380 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700381
Sunny Goyal745bad92016-05-02 10:54:12 -0700382 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700383
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 @Override
385 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700386 if (DEBUG_STRICT_MODE) {
387 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
388 .detectDiskReads()
389 .detectDiskWrites()
390 .detectNetwork() // or .detectAll() for all detectable problems
391 .penaltyLog()
392 .build());
393 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
394 .detectLeakedSqlLiteObjects()
395 .detectLeakedClosableObjects()
396 .penaltyLog()
397 .penaltyDeath()
398 .build());
399 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700400 if (LauncherAppState.PROFILE_STARTUP) {
401 Trace.beginSection("Launcher-onCreate");
402 }
Winson Chunga2413752012-04-03 14:22:34 -0700403
Adam Cohen9211d422014-10-07 18:14:53 -0700404 if (mLauncherCallbacks != null) {
405 mLauncherCallbacks.preOnCreate();
406 }
407
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400409
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400410 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700411
Adam Cohen2e6da152015-05-06 11:42:25 -0700412 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700413 mDeviceProfile = getResources().getConfiguration().orientation
414 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700415 app.getInvariantDeviceProfile().landscapeProfile
416 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700417
Sunny Goyalf7258242015-10-19 16:59:07 -0700418 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700419 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800420 mModel = app.setLauncher(this);
421 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700422 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700423
Joe Onorato41a12d22009-10-31 18:30:00 -0400424 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700425 mAllAppsController = new AllAppsTransitionController(this);
426 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700427
Sunny Goyalffe83f12014-08-14 17:39:34 -0700428 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700429
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700430 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
431 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700432
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700433 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
434 // this also ensures that any synchronous binding below doesn't re-trigger another
435 // LauncherModel load.
436 mPaused = false;
437
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700439
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 setupViews();
Winson1f064272016-07-18 17:18:02 -0700441 mDeviceProfile.layout(this, false /* notifyListeners */);
Tony Wickham827cef22016-03-17 15:39:39 -0700442 mExtractedColors = new ExtractedColors();
443 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444
Tony Wickhame2217252016-03-22 16:34:23 -0700445 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
446 .addAccessibilityStateChangeListener(this);
447
Joe Onorato7c312c12009-08-13 21:36:53 -0700448 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700449
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 mSavedState = savedInstanceState;
451 restoreState(mSavedState);
452
Sunny Goyale26d1002016-06-20 14:52:14 -0700453 if (LauncherAppState.PROFILE_STARTUP) {
454 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 }
456
457 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700458 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700459 // If the user leaves launcher, then we should just load items asynchronously when
460 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700461 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700462 } else {
463 // We only load the page synchronously if the user rotates (or triggers a
464 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700465 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700466 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467 }
468
469 // For handling default keys
470 mDefaultKeySsb = new SpannableStringBuilder();
471 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800472
473 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700474 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
475 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800476
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800477 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700478 // In case we are on a device with locked rotation, we should look at preferences to check
479 // if the user has specifically allowed rotation.
480 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700481 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700482 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
483 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700484 }
485
486 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
487 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400488 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700489
Adam Cohen9211d422014-10-07 18:14:53 -0700490 if (mLauncherCallbacks != null) {
491 mLauncherCallbacks.onCreate(savedInstanceState);
492 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700493
494 if (shouldShowIntroScreen()) {
495 showIntroScreen();
496 } else {
497 showFirstRunActivity();
Adam Cohenc3e12c72014-10-31 16:15:36 -0700498 }
Adam Cohen9211d422014-10-07 18:14:53 -0700499 }
500
Sunny Goyal7779d622015-06-11 16:18:39 -0700501 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700502 public void onExtractedColorsChanged() {
503 loadExtractedColorsAndColorItems();
504 }
505
506 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700507 // TODO: do this in pre-N as well, once the extraction part is complete.
508 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700509 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700510 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700511 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham827cef22016-03-17 15:39:39 -0700512 }
513 }
514
Adam Cohen9211d422014-10-07 18:14:53 -0700515 private LauncherCallbacks mLauncherCallbacks;
516
517 public void onPostCreate(Bundle savedInstanceState) {
518 super.onPostCreate(savedInstanceState);
519 if (mLauncherCallbacks != null) {
520 mLauncherCallbacks.onPostCreate(savedInstanceState);
521 }
522 }
523
Winson1f064272016-07-18 17:18:02 -0700524 public void onInsetsChanged(Rect insets) {
525 mDeviceProfile.updateInsets(insets);
526 mDeviceProfile.layout(this, true /* notifyListeners */);
527 }
528
Sunny Goyal32554d12015-12-03 15:31:25 -0800529 /**
530 * Call this after onCreate to set or clear overlay.
531 */
532 public void setLauncherOverlay(LauncherOverlay overlay) {
533 if (overlay != null) {
534 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
535 }
536 mWorkspace.setLauncherOverlay(overlay);
537 }
538
Adam Cohen9211d422014-10-07 18:14:53 -0700539 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
540 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700541 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700542 private boolean mWorkspaceImportanceStored = false;
543 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700544 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800545 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700546 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
547
548 @Override
549 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700550 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700551 return;
552 }
553 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700554 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700555 if (mWorkspace != null) {
556 mWorkspaceImportanceForAccessibility =
557 mWorkspace.getImportantForAccessibility();
558 mWorkspace.setImportantForAccessibility(
559 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
560 mWorkspaceImportanceStored = true;
561 }
562 if (mHotseat != null) {
563 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
564 mHotseat.setImportantForAccessibility(
565 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
566 mHotseatImportanceStored = true;
567 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700568 }
569
570 @Override
571 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700572 if (mWorkspaceImportanceStored && mWorkspace != null) {
573 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
574 }
575 if (mHotseatImportanceStored && mHotseat != null) {
576 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
577 }
578 mWorkspaceImportanceStored = false;
579 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700580 }
581 });
Adam Cohen9211d422014-10-07 18:14:53 -0700582 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700583 }
584
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700585 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700586 public void onLauncherProviderChange() {
587 if (mLauncherCallbacks != null) {
588 mLauncherCallbacks.onLauncherProviderChange();
589 }
590 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700591
Adam Cohen9211d422014-10-07 18:14:53 -0700592 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700593 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700594 if (mLauncherCallbacks != null) {
595 return mLauncherCallbacks.hasCustomContentToLeft();
596 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700597 return false;
598 }
599
Dave Hawkeya8881582013-09-17 15:55:33 -0600600 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500601 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600602 * {@link #addToCustomContentPage}. This will only be invoked if
603 * {@link #hasCustomContentToLeft()} is {@code true}.
604 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500605 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700606 if (mLauncherCallbacks != null) {
607 mLauncherCallbacks.populateCustomContentContainer();
608 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600609 }
610
611 /**
612 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
613 * ensure the custom content page is added or removed if necessary.
614 */
615 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600616 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600617 // Not bound yet, wait for bindScreens to be called.
618 return;
619 }
620
621 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
622 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500623 mWorkspace.createCustomContentContainer();
624 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600625 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
626 mWorkspace.removeCustomContentPage();
627 }
628 }
629
Hyunyoung Songaa953652016-04-19 18:30:24 -0700630 public UserEventDispatcher getUserEventDispatcher() {
631 if (mLauncherCallbacks != null) {
632 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
633 if (dispatcher != null) {
634 return dispatcher;
635 }
636 }
637
Sunny Goyal64976d52016-06-20 14:56:28 -0700638 // Logger object is a singleton and does not have to be coupled with the foreground
639 // activity. Since most user event logging is done on the UI, the object is retrieved
640 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700641 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700642 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700643 }
644 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800645 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100646
Hyunyoung Song3f471442015-04-08 19:01:34 -0700647 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700648 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
649 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700650 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700651 }
652
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000653 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700654 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
655 // This cast is safe as long as the id < 0x00FFFFFF
656 // Since we jail all the dynamically generated views, there should be no clashes
657 // with any other views.
658 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000659 }
660
661 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700662 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
663 * a configuration step, this allows the proper animations to run after other transitions.
664 */
Adam Cohendb364c32014-05-20 14:23:40 -0700665 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700666 long screenId = args.screenId;
667 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
668 // When the screen id represents an actual screen (as opposed to a rank) we make sure
669 // that the drop page actually exists.
670 screenId = ensurePendingDropLayoutExists(args.screenId);
671 }
Adam Cohendb364c32014-05-20 14:23:40 -0700672
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800673 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800674 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700675 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700676 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700677 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800678 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700679 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700680 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700681 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyald478c832016-04-01 12:04:16 -0700682 completeRestoreAppWidget(args.appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700683 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700684 case REQUEST_BIND_PENDING_APPWIDGET: {
685 int widgetId = args.appWidgetId;
686 LauncherAppWidgetInfo info =
687 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
688 if (info != null) {
689 // Since the view was just bound, also launch the configure activity if needed
690 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
691 .getLauncherAppWidgetInfo(widgetId);
692 if (provider != null && provider.configure != null) {
693 startRestoredWidgetReconfigActivity(provider, info);
694 }
695 }
696 break;
697 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800698 }
Michael Jurka27614d22012-04-02 06:40:22 -0700699 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
700 // if you turned the screen off and then back while in All Apps, Launcher would not
701 // return to the workspace. Clearing mAddInfo.container here fixes this issue
702 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700703 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800704 }
705
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800706 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700707 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700708 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700709 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700710 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800711 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700712
Adam Cohenad4e15c2013-10-17 16:21:35 -0700713 Runnable exitSpringLoaded = new Runnable() {
714 @Override
715 public void run() {
716 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
717 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
718 }
719 };
720
Michael Jurka8b805b12012-04-18 14:23:14 -0700721 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700722 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700723 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700724 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
725 if (resultCode == RESULT_CANCELED) {
726 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700727 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700728 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700729 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800730 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700731 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700732 }
733 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200734 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
735 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700736 // User could have free-scrolled between pages before picking a wallpaper; make sure
737 // we move to the closest one now.
738 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700739 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200740 }
741 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700742 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200743
Adam Cohened66b2b2012-01-23 17:28:51 -0800744 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700745 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700746
Adam Cohendb364c32014-05-20 14:23:40 -0700747 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800748 // We have special handling for widgets
749 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800750 final int appWidgetId;
751 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
752 : -1;
753 if (widgetId < 0) {
754 appWidgetId = pendingAddWidgetId;
755 } else {
756 appWidgetId = widgetId;
757 }
758
Adam Cohenad4e15c2013-10-17 16:21:35 -0700759 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800760 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700761 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
762 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700763 result = RESULT_CANCELED;
764 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700765 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700766 @Override
767 public void run() {
768 exitSpringLoadedDragModeDelayed(false, 0, null);
769 }
770 };
Adam Cohendb364c32014-05-20 14:23:40 -0700771 if (workspaceLocked) {
772 // No need to remove the empty screen if we're mid-binding, as the
773 // the bind will not add the empty screen.
774 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
775 } else {
776 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
777 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
778 }
Winson Chung5aaab772012-04-27 15:24:02 -0700779 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700780 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700781 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
782 // When the screen id represents an actual screen (as opposed to a rank)
783 // we make sure that the drop page actually exists.
784 mPendingAddInfo.screenId =
785 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
786 }
Adam Cohendb364c32014-05-20 14:23:40 -0700787 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
788
789 dropLayout.setDropPending(true);
790 final Runnable onComplete = new Runnable() {
791 @Override
792 public void run() {
793 completeTwoStageWidgetDrop(resultCode, appWidgetId);
794 dropLayout.setDropPending(false);
795 }
796 };
797 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
798 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
799 } else {
800 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
801 mPendingAddInfo);
802 sPendingAddItem = args;
803 }
Winson Chung5aaab772012-04-27 15:24:02 -0700804 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800805 return;
806 }
807
Sunny Goyald478c832016-04-01 12:04:16 -0700808 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
809 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700810 if (resultCode == RESULT_OK) {
811 // Update the widget view.
812 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
813 pendingAddWidgetId, mPendingAddInfo);
814 if (workspaceLocked) {
815 sPendingAddItem = args;
816 } else {
817 completeAdd(args);
818 }
819 }
820 // Leave the widget in the pending state if the user canceled the configure.
821 return;
822 }
823
Sunny Goyalaad90582015-07-09 14:22:50 -0700824 if (requestCode == REQUEST_CREATE_SHORTCUT) {
825 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
826 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
827 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
828 mPendingAddInfo);
829 if (isWorkspaceLocked()) {
830 sPendingAddItem = args;
831 } else {
832 completeAdd(args);
833 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
834 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
835 }
836 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700837 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
838 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800841 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800842
843 }
844
845 @Override
846 protected void onActivityResult(
847 final int requestCode, final int resultCode, final Intent data) {
848 handleActivityResult(requestCode, resultCode, data);
849 if (mLauncherCallbacks != null) {
850 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
851 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800852 }
853
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600854 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700855 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600856 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700857 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
858 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
859 View v = null;
860 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
861 if (layout != null) {
862 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
863 }
864 Intent intent = sPendingAddItem.intent;
865 sPendingAddItem = null;
866 if (grantResults.length > 0
867 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700868 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700869 } else {
870 // TODO: Show a snack bar with link to settings
871 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
872 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
873 }
874 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600875 if (mLauncherCallbacks != null) {
876 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
877 grantResults);
878 }
879 }
880
Adam Cohendb364c32014-05-20 14:23:40 -0700881 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
882 appWidgetId, ItemInfo info) {
883 PendingAddArguments args = new PendingAddArguments();
884 args.requestCode = requestCode;
885 args.intent = data;
886 args.container = info.container;
887 args.screenId = info.screenId;
888 args.cellX = info.cellX;
889 args.cellY = info.cellY;
890 args.appWidgetId = appWidgetId;
891 return args;
892 }
893
894 /**
895 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
896 *
897 * @param screenId the screen id to check
898 * @return the new screen, or screenId if it exists
899 */
900 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800901 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700902 if (dropLayout == null) {
903 // it's possible that the add screen was removed because it was
904 // empty and a re-bind occurred
905 mWorkspace.addExtraEmptyScreen();
906 return mWorkspace.commitExtraEmptyScreen();
907 } else {
908 return screenId;
909 }
910 }
911
Adam Cohen091440a2015-03-18 14:16:05 -0700912 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800913 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800914 Runnable onCompleteRunnable = null;
915 int animationType = 0;
916
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700917 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800918 if (resultCode == RESULT_OK) {
919 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
920 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700921 mPendingAddWidgetInfo);
922 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800923 onCompleteRunnable = new Runnable() {
924 @Override
925 public void run() {
926 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700927 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700928 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
929 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800930 }
931 };
932 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800933 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800934 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800935 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700936 if (mDragLayer.getAnimatedView() != null) {
937 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
938 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
939 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700940 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700941 // The animated view may be null in the case of a rotation during widget configuration
942 onCompleteRunnable.run();
943 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 }
945
946 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700947 protected void onStop() {
948 super.onStop();
949 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700950
951 if (mLauncherCallbacks != null) {
952 mLauncherCallbacks.onStop();
953 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700954
955 if (Utilities.isNycMR1OrAbove()) {
956 mAppWidgetHost.stopListening();
957 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700958 }
959
960 @Override
961 protected void onStart() {
962 super.onStart();
963 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700964
965 if (mLauncherCallbacks != null) {
966 mLauncherCallbacks.onStart();
967 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700968
969 if (Utilities.isNycMR1OrAbove()) {
970 mAppWidgetHost.startListening();
971 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700972 }
973
974 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200976 long startTime = 0;
977 if (DEBUG_RESUME_TIME) {
978 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400979 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200980 }
Adam Cohen9211d422014-10-07 18:14:53 -0700981
982 if (mLauncherCallbacks != null) {
983 mLauncherCallbacks.preOnResume();
984 }
985
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800986 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700987 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700988
Winson Chung4a2afa32012-07-19 14:53:05 -0700989 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700990 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700991 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800992 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700993 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700994 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700995 // view after launching an app, as they may be depending on the UI to be static to
996 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700997 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700998 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800999 } else if (mOnResumeState == State.WIDGETS) {
1000 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001001 }
1002 mOnResumeState = State.NONE;
1003
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001004 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001005 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1006 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001007
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001008 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001009 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001010 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001011 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001012 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001013 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001014 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001015 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001016 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1017 // execute them here
1018 long startTimeCallbacks = 0;
1019 if (DEBUG_RESUME_TIME) {
1020 startTimeCallbacks = System.currentTimeMillis();
1021 }
1022
Michael Jurka1e2f4652013-07-08 18:03:46 -07001023 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1024 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001025 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001026 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001027 if (DEBUG_RESUME_TIME) {
1028 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1029 (System.currentTimeMillis() - startTimeCallbacks));
1030 }
Michael Jurka447bf842013-05-15 14:52:15 +02001031 }
Michael Jurka54554252013-08-01 12:52:23 +02001032 if (mOnResumeCallbacks.size() > 0) {
1033 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1034 mOnResumeCallbacks.get(i).run();
1035 }
1036 mOnResumeCallbacks.clear();
1037 }
Winson Chunge4e50662012-01-23 14:45:13 -08001038
1039 // Reset the pressed state of icons that were locked in the press state while activities
1040 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001041 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001042 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001043 mWaitingForResume.setStayPressed(false);
1044 }
Winson Chung82963d52013-10-09 11:20:57 -07001045
Adam Cohen06dff352012-06-01 17:17:08 -07001046 // It is possible that widgets can receive updates while launcher is not in the foreground.
1047 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1048 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1049 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001050 if (!isWorkspaceLoading()) {
1051 getWorkspace().reinflateWidgetsIfNecessary();
1052 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001053
Michael Jurka447bf842013-05-15 14:52:15 +02001054 if (DEBUG_RESUME_TIME) {
1055 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1056 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001057
Adam Cohen4b66bf32015-09-18 12:15:19 -07001058 // We want to suppress callbacks about CustomContent being shown if we have just received
1059 // onNewIntent while the user was present within launcher. In that case, we post a call
1060 // to move the user to the main screen (which will occur after onResume). We don't want to
1061 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1062 // suppress here.
1063 if (mWorkspace.getCustomContentCallbacks() != null
1064 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001065 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001066 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001067 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1068 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001069 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001070 }
1071 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001072 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001073 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001074 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001075
Sunny Goyal756adbc2015-04-16 15:20:51 -07001076 if (!isWorkspaceLoading()) {
1077 // Process any items that were added while Launcher was away.
1078 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1079 }
Adam Cohen9211d422014-10-07 18:14:53 -07001080
1081 if (mLauncherCallbacks != null) {
1082 mLauncherCallbacks.onResume();
1083 }
Adam Cohen06dff352012-06-01 17:17:08 -07001084 }
1085
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001087 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001088 // Ensure that items added to Launcher are queued until Launcher returns
1089 InstallShortcutReceiver.enableInstallQueue();
1090
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001091 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001092 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001093 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001094 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001095
1096 // We call onHide() aggressively. The custom content callbacks should be able to
1097 // debounce excess onHide calls.
1098 if (mWorkspace.getCustomContentCallbacks() != null) {
1099 mWorkspace.getCustomContentCallbacks().onHide();
1100 }
Romain Guycbb89e42009-06-08 15:52:54 -07001101
Adam Cohen9211d422014-10-07 18:14:53 -07001102 if (mLauncherCallbacks != null) {
1103 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001104 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001105 }
1106
1107 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001108 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1109 // by a onResume or by scrolling otherwise.
1110 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001111
1112 // Custom content is completely hidden
1113 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001114
1115 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1116 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001117
1118 // Indicates whether the user is allowed to scroll away from the custom content.
1119 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001120 }
1121
Adam Cohenc2d6e892014-10-16 09:49:24 -07001122 public interface LauncherOverlay {
1123
1124 /**
1125 * Touch interaction leading to overscroll has begun
1126 */
1127 public void onScrollInteractionBegin();
1128
1129 /**
1130 * Touch interaction related to overscroll has ended
1131 */
1132 public void onScrollInteractionEnd();
1133
1134 /**
1135 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1136 * screen (or in the case of RTL, the rightmost screen).
1137 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001138 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001139
1140 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001141 * Called when the launcher is ready to use the overlay
1142 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001143 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001144 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001145 }
1146
Jun Mukai8af0cd82015-05-12 12:32:12 -07001147 public interface LauncherSearchCallbacks {
1148 /**
1149 * Called when the search overlay is shown.
1150 */
1151 public void onSearchOverlayOpened();
1152
1153 /**
1154 * Called when the search overlay is dismissed.
1155 */
1156 public void onSearchOverlayClosed();
1157 }
1158
Adam Cohenc2d6e892014-10-16 09:49:24 -07001159 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001160 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001161 }
1162
1163 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1164
Sunny Goyalc86df472016-02-25 09:19:38 -08001165 public void onScrollChanged(float progress) {
1166 if (mWorkspace != null) {
1167 mWorkspace.onOverlayScrollChanged(progress);
1168 }
1169 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001170 }
1171
Jorim Jaggid017f882014-01-14 17:08:48 -08001172 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001173 if (mLauncherCallbacks != null) {
1174 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001175 } else {
1176 // On devices with a locked orientation, we will at least have the allow rotation
1177 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001178 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001179 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001180 }
1181
Adam Cohen9211d422014-10-07 18:14:53 -07001182 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001183 CustomContentCallbacks callbacks, String description) {
1184 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001185 }
1186
Adam Cohenedb40762013-07-18 16:45:45 -07001187 // The custom content needs to offset its content to account for the QSB
1188 public int getTopOffsetForCustomContent() {
1189 return mWorkspace.getPaddingTop();
1190 }
1191
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001192 @Override
1193 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001194 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001195 if (mModel.isCurrentCallbacks(this)) {
1196 mModel.stopLoader();
1197 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001198 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1199
Romain Guy13c2e7b2010-03-10 19:45:00 -08001200 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001201 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001202
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001203 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001204 @Override
1205 public void onWindowFocusChanged(boolean hasFocus) {
1206 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001207 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001208
1209 if (mLauncherCallbacks != null) {
1210 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1211 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001212 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001213
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001214 private boolean acceptFilter() {
1215 final InputMethodManager inputManager = (InputMethodManager)
1216 getSystemService(Context.INPUT_METHOD_SERVICE);
1217 return !inputManager.isFullscreenMode();
1218 }
1219
1220 @Override
1221 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001222 final int uniChar = event.getUnicodeChar();
1223 final boolean handled = super.onKeyDown(keyCode, event);
1224 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1225 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1227 keyCode, event);
1228 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001229 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001230 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001231 // showSearchDialog()
1232 // If there are multiple keystrokes before the search dialog takes focus,
1233 // onSearchRequested() will be called for every keystroke,
1234 // but it is idempotent, so it's fine.
1235 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 }
1237 }
1238
Joe Onorato8a9625e2010-01-28 15:55:35 -08001239 // Eat the long press event so the keyboard doesn't come up.
1240 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1241 return true;
1242 }
1243
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 return handled;
1245 }
1246
Winson46163472015-09-22 17:31:56 -07001247 @Override
1248 public boolean onKeyUp(int keyCode, KeyEvent event) {
1249 if (keyCode == KeyEvent.KEYCODE_MENU) {
1250 // Ignore the menu key if we are currently dragging or are on the custom content screen
1251 if (!isOnCustomContent() && !mDragController.isDragging()) {
1252 // Close any open folders
1253 closeFolder();
1254
Tony Wickham49c8d292016-07-01 11:44:34 -07001255 // Close any shortcuts containers
1256 closeShortcutsContainer();
1257
Winson46163472015-09-22 17:31:56 -07001258 // Stop resizing any widgets
1259 mWorkspace.exitWidgetResizeMode();
1260
1261 // Show the overview mode if we are on the workspace
1262 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1263 !mWorkspace.isSwitchingState()) {
1264 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001265 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001266 }
1267 }
1268 return true;
1269 }
1270 return super.onKeyUp(keyCode, event);
1271 }
1272
Karl Rosaen138a0412009-04-23 19:00:21 -07001273 private String getTypedText() {
1274 return mDefaultKeySsb.toString();
1275 }
1276
1277 private void clearTypedText() {
1278 mDefaultKeySsb.clear();
1279 mDefaultKeySsb.clearSpans();
1280 Selection.setSelection(mDefaultKeySsb, 0);
1281 }
1282
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001284 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1285 * State
1286 */
1287 private static State intToState(int stateOrdinal) {
1288 State state = State.WORKSPACE;
1289 final State[] stateValues = State.values();
1290 for (int i = 0; i < stateValues.length; i++) {
1291 if (stateValues[i].ordinal() == stateOrdinal) {
1292 state = stateValues[i];
1293 break;
1294 }
1295 }
1296 return state;
1297 }
1298
1299 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 * Restores the previous state, if it exists.
1301 *
1302 * @param savedState The previous state.
1303 */
1304 private void restoreState(Bundle savedState) {
1305 if (savedState == null) {
1306 return;
1307 }
1308
Michael Jurka883f55b2010-10-21 15:47:14 -07001309 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001310 if (state == State.APPS || state == State.WIDGETS) {
1311 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001312 }
1313
Adam Cohen21cd0022013-10-09 18:57:02 -07001314 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1315 PagedView.INVALID_RESTORE_PAGE);
1316 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001317 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 }
1319
Sunny Goyal756cd262015-08-20 12:33:21 -07001320 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1321 if (itemValues != null) {
1322 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001323 AppWidgetProviderInfo info = savedState.getParcelable(
1324 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001325 mPendingAddWidgetInfo = info == null ?
1326 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1327
Adam Cohen4637b5a2013-11-04 18:21:24 -08001328 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001329 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 mRestoring = true;
1331 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 }
1333
1334 /**
1335 * Finds all the views we need and configure them properly.
1336 */
1337 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001338 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001339 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001340 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001341 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyal6178f132016-07-11 17:30:03 -07001342 mQsbContainer = mDragLayer.findViewById(mDeviceProfile.isVerticalBarLayout()
1343 ? R.id.workspace_blocked_row : R.id.qsb_container);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001344 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001345
Sunny Goyal784f9c32016-03-23 16:56:54 -07001346 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1347 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1348 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001349 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001350
Winson Chung4c98d922011-05-31 16:50:48 -07001351 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001352
1353 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001354
Winson Chung3d503fb2011-07-13 17:25:49 -07001355 // Setup the hotseat
1356 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1357 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001358 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001359 }
1360
Winsone9f27272015-10-13 10:47:51 -07001361 // Setup the overview panel
1362 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001363
Winson Chung4c98d922011-05-31 16:50:48 -07001364 // Setup the workspace
1365 mWorkspace.setHapticFeedbackEnabled(false);
1366 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001367 mWorkspace.setup(mDragController);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001368 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001369 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001370
Tony Wickham34d2c912015-09-11 09:27:58 -07001371 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001372 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001373
Winson Chungef7f8742015-06-04 17:18:17 -07001374 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001375 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001376 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001377 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1378 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1379 } else {
1380 mAppsView.setSearchBarController(new DefaultAppSearchController());
1381 }
Craig Mautner360310b2012-10-26 15:13:08 -07001382
Winson Chung3d503fb2011-07-13 17:25:49 -07001383 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001384 mDragController.setDragScoller(mWorkspace);
1385 mDragController.setScrollView(mDragLayer);
1386 mDragController.setMoveTarget(mWorkspace);
1387 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001388 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001389
Peter Schiller310a9882016-06-30 13:52:36 -07001390 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1391 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1392 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001393
Sunny Goyal322d5562015-06-25 19:35:49 -07001394 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1395 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001396 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 }
1398
Winsone9f27272015-10-13 10:47:51 -07001399 private void setupOverviewPanel() {
1400 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1401
1402 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1403 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1404 @Override
1405 public boolean onLongClick(View v) {
1406 return v.performClick();
1407 }
1408 };
1409
1410 // Bind wallpaper button actions
1411 View wallpaperButton = findViewById(R.id.wallpaper_button);
1412 wallpaperButton.setOnClickListener(new OnClickListener() {
1413 @Override
1414 public void onClick(View view) {
1415 if (!mWorkspace.isSwitchingState()) {
1416 onClickWallpaperPicker(view);
1417 }
1418 }
1419 });
1420 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1421 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1422
1423 // Bind widget button actions
1424 mWidgetsButton = findViewById(R.id.widget_button);
1425 mWidgetsButton.setOnClickListener(new OnClickListener() {
1426 @Override
1427 public void onClick(View view) {
1428 if (!mWorkspace.isSwitchingState()) {
1429 onClickAddWidgetButton(view);
1430 }
1431 }
1432 });
1433 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1434 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1435
1436 // Bind settings actions
1437 View settingsButton = findViewById(R.id.settings_button);
1438 boolean hasSettings = hasSettings();
1439 if (hasSettings) {
1440 settingsButton.setOnClickListener(new OnClickListener() {
1441 @Override
1442 public void onClick(View view) {
1443 if (!mWorkspace.isSwitchingState()) {
1444 onClickSettingsButton(view);
1445 }
1446 }
1447 });
1448 settingsButton.setOnLongClickListener(performClickOnLongClick);
1449 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1450 } else {
1451 settingsButton.setVisibility(View.GONE);
1452 }
1453
1454 mOverviewPanel.setAlpha(0f);
1455 }
1456
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001458 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001459 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001460 */
1461 public void setAllAppsButton(View allAppsButton) {
1462 mAllAppsButton = allAppsButton;
1463 }
1464
Sunny Goyalbb011da2016-06-15 15:42:29 -07001465 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001466 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001467 }
1468
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001469 public View getWidgetsButton() {
1470 return mWidgetsButton;
1471 }
1472
Anjali Koppal5ad44842014-03-10 20:34:39 -07001473 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 * Creates a view representing a shortcut.
1475 *
1476 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001478 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001479 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 }
1481
1482 /**
1483 * Creates a view representing a shortcut inflated from the specified resource.
1484 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 * @param parent The group the shortcut belongs to.
1486 * @param info The data structure describing the shortcut.
1487 *
1488 * @return A View inflated from layoutResId.
1489 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001490 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001491 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001492 parent, false);
1493 favorite.applyFromShortcutInfo(info, mIconCache);
1494 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001496 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 return favorite;
1498 }
1499
1500 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 * Add a shortcut to the workspace.
1502 *
1503 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001505 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001506 int cellY) {
1507 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001508 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001509
Sunny Goyal5c97f512015-05-19 16:03:28 -07001510 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001511 if (info == null) {
1512 return;
1513 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001514 final View view = createShortcut(info);
1515
Sunny Goyal5c97f512015-05-19 16:03:28 -07001516 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001517 // First we check if we already know the exact location where we want to add this item.
1518 if (cellX >= 0 && cellY >= 0) {
1519 cellXY[0] = cellX;
1520 cellXY[1] = cellY;
1521 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001522
1523 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001524 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001525 true, null,null)) {
1526 return;
1527 }
1528 DragObject dragObject = new DragObject();
1529 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001530 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1531 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001532 return;
1533 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001534 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001535 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001536 }
1537
1538 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001539 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001540 return;
1541 }
1542
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001543 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544
1545 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001546 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001547 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 }
1549 }
1550
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001552 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001554 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 */
Adam Cohen091440a2015-03-18 14:16:05 -07001556 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001557 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1558
1559 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001560 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001561 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001562 }
Romain Guycbb89e42009-06-08 15:52:54 -07001563
Adam Cohen59400422014-03-05 18:07:04 -08001564 if (appWidgetInfo.isCustomWidget) {
1565 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001566 }
1567
Adam Cohen59400422014-03-05 18:07:04 -08001568 LauncherAppWidgetInfo launcherInfo;
1569 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1570 launcherInfo.spanX = info.spanX;
1571 launcherInfo.spanY = info.spanY;
1572 launcherInfo.minSpanX = info.minSpanX;
1573 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001574 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001575
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001577 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578
1579 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001580 if (hostView == null) {
1581 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001582 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001583 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001584 hostView.setVisibility(View.VISIBLE);
1585 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001587 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588 }
Romain Guycbb89e42009-06-08 15:52:54 -07001589
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001590 private void addAppWidgetToWorkspace(
1591 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001592 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001593 hostView.setTag(item);
1594 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001595
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001596 hostView.setFocusable(true);
1597 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001598
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001599 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001600 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1601
1602 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001603 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001604 }
1605 }
1606
Adam Cohended9f8d2010-11-03 13:25:16 -07001607 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1608 @Override
1609 public void onReceive(Context context, Intent intent) {
1610 final String action = intent.getAction();
1611 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1612 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001613 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001614 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001615
Winson Chungc100e8e2011-08-09 16:02:43 -07001616 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001617 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001618 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001619 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001620 if (!showWorkspace(false)) {
1621 // If we are already on the workspace, then manually reset all apps
1622 mAppsView.reset();
1623 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001624 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001625 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1626 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001627 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001628 }
1629 }
1630 };
1631
1632 @Override
1633 public void onAttachedToWindow() {
1634 super.onAttachedToWindow();
1635
1636 // Listen for broadcasts related to user-presence
1637 final IntentFilter filter = new IntentFilter();
1638 filter.addAction(Intent.ACTION_SCREEN_OFF);
1639 filter.addAction(Intent.ACTION_USER_PRESENT);
1640 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001641 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001642 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001643 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001644
1645 if (mLauncherCallbacks != null) {
1646 mLauncherCallbacks.onAttachedToWindow();
1647 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001648 }
1649
1650 @Override
1651 public void onDetachedFromWindow() {
1652 super.onDetachedFromWindow();
1653 mVisible = false;
1654
Adam Cohend113e0c2010-11-11 10:48:05 -08001655 if (mAttached) {
1656 unregisterReceiver(mReceiver);
1657 mAttached = false;
1658 }
Winson Chungb745afb2015-03-02 11:51:23 -08001659 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001660
1661 if (mLauncherCallbacks != null) {
1662 mLauncherCallbacks.onDetachedFromWindow();
1663 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001664 }
1665
1666 public void onWindowVisibilityChanged(int visibility) {
1667 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001668 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001669 // The following code used to be in onResume, but it turns out onResume is called when
1670 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1671 // is a more appropriate event to handle
1672 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001673 if (!mWorkspaceLoading) {
1674 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001675 // We want to let Launcher draw itself at least once before we force it to build
1676 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001677 // apps is nice and speedy.
1678 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001679 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001680 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001681 if (mStarted) return;
1682 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001683 // We delay the layer building a bit in order to give
1684 // other message processing a time to run. In particular
1685 // this avoids a delay in hiding the IME if it was
1686 // currently shown, because doing that may involve
1687 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001688 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001689 final ViewTreeObserver.OnDrawListener listener = this;
1690 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001691 public void run() {
1692 if (mWorkspace != null &&
1693 mWorkspace.getViewTreeObserver() != null) {
1694 mWorkspace.getViewTreeObserver().
1695 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001696 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001697 }
1698 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001699 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001700 }
1701 });
1702 }
1703 clearTypedText();
1704 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001705 }
1706
Adam Cohen091440a2015-03-18 14:16:05 -07001707 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001708 mHandler.removeMessages(ADVANCE_MSG);
1709 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1710 mHandler.sendMessageDelayed(msg, delay);
1711 mAutoAdvanceSentTime = System.currentTimeMillis();
1712 }
1713
Adam Cohen091440a2015-03-18 14:16:05 -07001714 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001715 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1716 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1717 mAutoAdvanceRunning = autoAdvanceRunning;
1718 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001719 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001720 sendAdvanceMessage(delay);
1721 } else {
1722 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001723 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001724 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1725 }
1726 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001727 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001728 }
1729 }
1730 }
1731
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001732 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1733
Adam Cohended9f8d2010-11-03 13:25:16 -07001734 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001735 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001736 if (msg.what == ADVANCE_MSG) {
1737 int i = 0;
1738 for (View key: mWidgetsToAdvance.keySet()) {
1739 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001740 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001741 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001742 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001743 public void run() {
1744 ((Advanceable) v).advance();
1745 }
1746 }, delay);
1747 }
1748 i++;
1749 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001750 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001751 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001752 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001753 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001754 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001755
Winsonc0b52fe2015-09-09 16:38:15 -07001756 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001757 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001758 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1759 if (v instanceof Advanceable) {
1760 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001761 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001762 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001763 }
1764 }
1765
Winsonc0b52fe2015-09-09 16:38:15 -07001766 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001767 if (mWidgetsToAdvance.containsKey(hostView)) {
1768 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001769 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001770 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001771 }
1772
Hyunyoung Song3f471442015-04-08 19:01:34 -07001773 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001774 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1775 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001776 }
1777
Winson Chunga6945242014-01-08 14:04:34 -08001778 public DragLayer getDragLayer() {
1779 return mDragLayer;
1780 }
1781
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001782 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001783 return mAppsView;
1784 }
1785
Hyunyoung Song3f471442015-04-08 19:01:34 -07001786 public WidgetsContainerView getWidgetsView() {
1787 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001788 }
1789
Winson Chunga6945242014-01-08 14:04:34 -08001790 public Workspace getWorkspace() {
1791 return mWorkspace;
1792 }
1793
Sunny Goyal6178f132016-07-11 17:30:03 -07001794 public View getQsbContainer() {
1795 return mQsbContainer;
1796 }
1797
Winson Chunga6945242014-01-08 14:04:34 -08001798 public Hotseat getHotseat() {
1799 return mHotseat;
1800 }
1801
Jorim Jaggid017f882014-01-14 17:08:48 -08001802 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001803 return mOverviewPanel;
1804 }
1805
Sunny Goyal47328fd2016-05-25 18:56:41 -07001806 public DropTargetBar getDropTargetBar() {
1807 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001808 }
1809
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001810 public LauncherAppWidgetHost getAppWidgetHost() {
1811 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 }
Romain Guycbb89e42009-06-08 15:52:54 -07001813
Winson Chunga9abd0e2010-10-27 17:18:37 -07001814 public LauncherModel getModel() {
1815 return mModel;
1816 }
1817
Adam Cohen79d90c52016-04-22 13:29:20 -07001818 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001819 return mSharedPrefs;
1820 }
1821
Adam Cohen2e6da152015-05-06 11:42:25 -07001822 public DeviceProfile getDeviceProfile() {
1823 return mDeviceProfile;
1824 }
1825
Bjorn Bringertc459e522013-06-07 19:36:01 +01001826 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001827 getWindow().closeAllPanels();
1828
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001829 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001830 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001831 }
1832
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833 @Override
1834 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001835 long startTime = 0;
1836 if (DEBUG_RESUME_TIME) {
1837 startTime = System.currentTimeMillis();
1838 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001839 super.onNewIntent(intent);
1840
1841 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001842 Folder openFolder = mWorkspace.getOpenFolder();
1843 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1844 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1845 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1846 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1847 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001848 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001849 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001850
Adam Cohen6fecd412013-10-02 17:41:50 -07001851 if (mWorkspace == null) {
1852 // Can be cases where mWorkspace is null, this prevents a NPE
1853 return;
1854 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001855 // In all these cases, only animate if we're already on home
1856 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001857
Sunny Goyal935fca12015-10-13 10:19:01 -07001858 closeFolder(alreadyOnHome);
Tony Wickham49c8d292016-07-01 11:44:34 -07001859 closeShortcutsContainer();
Adam Cohen6fecd412013-10-02 17:41:50 -07001860 exitSpringLoadedDragMode();
1861
1862 // If we are already on home, then just animate back to the workspace,
1863 // otherwise, just wait until onResume to set the state back to Workspace
1864 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001865 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001866 } else {
1867 mOnResumeState = State.WORKSPACE;
1868 }
1869
1870 final View v = getWindow().peekDecorView();
1871 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001872 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001873 INPUT_METHOD_SERVICE);
1874 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1875 }
1876
Winson Chungb745afb2015-03-02 11:51:23 -08001877 // Reset the apps view
1878 if (!alreadyOnHome && mAppsView != null) {
1879 mAppsView.scrollToTop();
1880 }
1881
Hyunyoung Song3f471442015-04-08 19:01:34 -07001882 // Reset the widgets view
1883 if (!alreadyOnHome && mWidgetsView != null) {
1884 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001885 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001886
Adam Cohen9211d422014-10-07 18:14:53 -07001887 if (mLauncherCallbacks != null) {
1888 mLauncherCallbacks.onHomeIntent();
1889 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001890 }
Adam Cohened307df2013-10-02 09:37:31 -07001891
Adam Cohen9211d422014-10-07 18:14:53 -07001892 if (mLauncherCallbacks != null) {
1893 mLauncherCallbacks.onNewIntent(intent);
1894 }
Winson15f8b172015-08-19 11:02:39 -07001895
1896 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1897 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1898 // animation.
1899 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001900 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1901 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001902 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1903 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001904
1905 // We use this flag to suppress noisy callbacks above custom content state
1906 // from onResume.
1907 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001908 mWorkspace.post(new Runnable() {
1909 @Override
1910 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001911 if (mWorkspace != null) {
1912 mWorkspace.moveToDefaultScreen(true);
1913 }
Winson15f8b172015-08-19 11:02:39 -07001914 }
1915 });
1916 }
1917 }
1918
1919 if (DEBUG_RESUME_TIME) {
1920 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1921 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001922 }
1923
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001925 public void onRestoreInstanceState(Bundle state) {
1926 super.onRestoreInstanceState(state);
1927 for (int page: mSynchronouslyBoundPages) {
1928 mWorkspace.restoreInstanceStateForChild(page);
1929 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930 }
1931
1932 @Override
1933 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001934 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001935 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1936 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001937
Adam Cohen21cd0022013-10-09 18:57:02 -07001938 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001939 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940
Michael Jurka883f55b2010-10-21 15:47:14 -07001941 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001942 // We close any open folder since it will not be re-opened, and we need to make sure
1943 // this state is reflected.
1944 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1945 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946
Tony Wickham49c8d292016-07-01 11:44:34 -07001947 closeShortcutsContainer();
1948
Adam Cohendcd297f2013-06-18 13:13:40 -07001949 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001950 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001951 ContentValues itemValues = new ContentValues();
1952 mPendingAddInfo.writeToValues(itemValues);
1953 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001954 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001955 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956 }
1957
Adam Cohen9211d422014-10-07 18:14:53 -07001958 if (mLauncherCallbacks != null) {
1959 mLauncherCallbacks.onSaveInstanceState(outState);
1960 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001961 }
1962
1963 @Override
1964 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001966
Winson Chunge7a03942011-08-05 15:05:12 -07001967 // Remove all pending runnables
1968 mHandler.removeMessages(ADVANCE_MSG);
1969 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001970 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001971 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001972
Winson Chungcd2b0142011-06-08 16:02:26 -07001973 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001974 // It's possible to receive onDestroy after a new Launcher activity has
1975 // been created. In this case, don't interfere with the new Launcher.
1976 if (mModel.isCurrentCallbacks(this)) {
1977 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001978 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001979 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001980
Sunny Goyal745bad92016-05-02 10:54:12 -07001981 if (mRotationPrefChangeHandler != null) {
1982 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1983 }
1984
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001986 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001988 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001990 mAppWidgetHost = null;
1991
1992 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993
1994 TextKeyListener.getInstance().release();
1995
Tony Wickhame2217252016-03-22 16:34:23 -07001996 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1997 .removeAccessibilityStateChangeListener(this);
1998
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001999 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002000
Michael Jurka2ecf9952012-06-18 12:52:28 -07002001 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002002
2003 if (mLauncherCallbacks != null) {
2004 mLauncherCallbacks.onDestroy();
2005 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002006 }
2007
Sunny Goyalae502842016-06-17 08:43:56 -07002008 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
2009 return mAccessibilityDelegate;
2010 }
2011
Adam Cohena9cf38f2011-05-02 15:36:58 -07002012 public DragController getDragController() {
2013 return mDragController;
2014 }
2015
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002016 @Override
2017 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002018 onStartForResult(requestCode);
2019 super.startActivityForResult(intent, requestCode);
2020 }
2021
2022 @Override
2023 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2024 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2025 onStartForResult(requestCode);
2026 try {
2027 super.startIntentSenderForResult(intent, requestCode,
2028 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2029 } catch (IntentSender.SendIntentException e) {
2030 throw new ActivityNotFoundException();
2031 }
2032 }
2033
2034 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002035 if (requestCode >= 0) {
2036 setWaitingForResult(true);
2037 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 }
2039
Winson Chung70d72102011-08-12 11:24:35 -07002040 /**
2041 * Indicates that we want global search for this activity by setting the globalSearch
2042 * argument for {@link #startSearch} to true.
2043 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002044 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002045 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002047
Karl Rosaen138a0412009-04-23 19:00:21 -07002048 if (initialQuery == null) {
2049 // Use any text typed in the launcher as the initial query
2050 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002051 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002052 if (appSearchData == null) {
2053 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002054 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002056
2057 // TODO send proper bounds.
2058 Rect sourceBounds = null;
Romain Guycbb89e42009-06-08 15:52:54 -07002059
Ian Parkinson047036e2014-09-01 15:40:53 +01002060 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002061 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002062 if (clearTextImmediately) {
2063 clearTypedText();
2064 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002065
2066 // We need to show the workspace after starting the search
2067 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002068 }
2069
Ian Parkinson047036e2014-09-01 15:40:53 +01002070 /**
2071 * Start a text search.
2072 *
2073 * @return {@code true} if the search will start immediately, so any further keypresses
2074 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2075 * to buffer keypresses.
2076 */
2077 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002078 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002079 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2080 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2081 sourceBounds);
2082 }
2083
Michael Jurkaa33411c2012-06-14 16:18:21 -07002084 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002085 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002086 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002087 }
2088
2089 /**
2090 * Starts the global search activity. This code is a copied from SearchManager
2091 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002092 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002093 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002094 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002095 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2096 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2097 if (globalSearchActivity == null) {
2098 Log.w(TAG, "No global search activity found.");
2099 return;
2100 }
2101 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2102 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2103 intent.setComponent(globalSearchActivity);
2104 // Make sure that we have a Bundle to put source in
2105 if (appSearchData == null) {
2106 appSearchData = new Bundle();
2107 } else {
2108 appSearchData = new Bundle(appSearchData);
2109 }
Adam Cohen9211d422014-10-07 18:14:53 -07002110 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002111 if (!appSearchData.containsKey("source")) {
2112 appSearchData.putString("source", getPackageName());
2113 }
2114 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2115 if (!TextUtils.isEmpty(initialQuery)) {
2116 intent.putExtra(SearchManager.QUERY, initialQuery);
2117 }
2118 if (selectInitialQuery) {
2119 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2120 }
2121 intent.setSourceBounds(sourceBounds);
2122 try {
2123 startActivity(intent);
2124 } catch (ActivityNotFoundException ex) {
2125 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2126 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002127 }
2128
Yura4f93ec62014-02-04 14:15:21 +00002129 public boolean isOnCustomContent() {
2130 return mWorkspace.isOnOrMovingToCustomContent();
2131 }
2132
Winson Chung70d72102011-08-12 11:24:35 -07002133 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002134 public boolean onPrepareOptionsMenu(Menu menu) {
2135 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002136 if (mLauncherCallbacks != null) {
2137 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2138 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002139 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002140 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002141
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002142 @Override
2143 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002144 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002145 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002146 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002147 }
2148
Joe Onorato9c1289c2009-08-17 11:03:03 -04002149 public boolean isWorkspaceLocked() {
2150 return mWorkspaceLoading || mWaitingForResult;
2151 }
2152
Adam Cohen517a7f52014-03-01 12:12:59 -08002153 public boolean isWorkspaceLoading() {
2154 return mWorkspaceLoading;
2155 }
2156
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002157 private void setWorkspaceLoading(boolean value) {
2158 boolean isLocked = isWorkspaceLocked();
2159 mWorkspaceLoading = value;
2160 if (isLocked != isWorkspaceLocked()) {
2161 onWorkspaceLockedChanged();
2162 }
2163 }
2164
2165 private void setWaitingForResult(boolean value) {
2166 boolean isLocked = isWorkspaceLocked();
2167 mWaitingForResult = value;
2168 if (isLocked != isWorkspaceLocked()) {
2169 onWorkspaceLockedChanged();
2170 }
2171 }
2172
Adam Cohen9211d422014-10-07 18:14:53 -07002173 protected void onWorkspaceLockedChanged() {
2174 if (mLauncherCallbacks != null) {
2175 mLauncherCallbacks.onWorkspaceLockedChanged();
2176 }
2177 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002178
Michael Jurka0280c3b2010-09-17 15:00:07 -07002179 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002180 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002181 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002182 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2183 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002184 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002185 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002186
Tony Wickhama0628cc2015-10-14 15:23:04 -07002187 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002188 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002189 if (LOGD) {
2190 Log.d(TAG, "Adding widget from drop");
2191 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002192 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2193 }
2194
Sunny Goyale6b63a32015-01-16 16:14:29 -08002195 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002196 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2197 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002198 if (appWidgetInfo.configure != null) {
2199 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002200 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002201
Bjorn Bringert7984c942009-12-09 15:38:25 +00002202 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002203 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2204 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2205
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002206 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002207 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002208 Runnable onComplete = new Runnable() {
2209 @Override
2210 public void run() {
2211 // Exit spring loaded mode if necessary after adding the widget
2212 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2213 null);
2214 }
2215 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002216 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002217 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002218 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002219 }
2220 }
Romain Guycbb89e42009-06-08 15:52:54 -07002221
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002222 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002223 // Close any folders that may be open.
2224 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002225 mWorkspace.moveToCustomContentScreen(animate);
2226 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002227
2228 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2229 int[] cell, int spanX, int spanY) {
2230 switch (info.itemType) {
2231 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2232 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2233 int span[] = new int[2];
2234 span[0] = spanX;
2235 span[1] = spanY;
2236 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2237 container, screenId, cell, span);
2238 break;
2239 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2240 processShortcutFromDrop(info.componentName, container, screenId, cell);
2241 break;
2242 default:
2243 throw new IllegalStateException("Unknown item type: " + info.itemType);
2244 }
2245 }
2246
Adam Cohenfbba09b2011-07-18 21:43:05 -07002247 /**
2248 * Process a shortcut drop.
2249 *
2250 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002251 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002252 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002253 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002254 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2255 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002256 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002257 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002258 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002259
2260 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002261 mPendingAddInfo.cellX = cell[0];
2262 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002263 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002264
2265 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2266 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002267 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002268 }
2269
Adam Cohenfbba09b2011-07-18 21:43:05 -07002270 /**
2271 * Process a widget drop.
2272 *
2273 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002274 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002275 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002276 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002277 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2278 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002279 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002280 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002281 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002282 mPendingAddInfo.minSpanX = info.minSpanX;
2283 mPendingAddInfo.minSpanY = info.minSpanY;
2284
Adam Cohenfbba09b2011-07-18 21:43:05 -07002285 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002286 mPendingAddInfo.cellX = cell[0];
2287 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002288 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002289 if (span != null) {
2290 mPendingAddInfo.spanX = span[0];
2291 mPendingAddInfo.spanY = span[1];
2292 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002293
Adam Cohened66b2b2012-01-23 17:28:51 -08002294 AppWidgetHostView hostView = info.boundWidget;
2295 int appWidgetId;
2296 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002297 // In the case where we've prebound the widget, we remove it from the DragLayer
2298 if (LOGD) {
2299 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2300 }
2301 getDragLayer().removeView(hostView);
2302
Adam Cohened66b2b2012-01-23 17:28:51 -08002303 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002304 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002305
2306 // Clear the boundWidget so that it doesn't get destroyed.
2307 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002308 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002309 // In this case, we either need to start an activity to get permission to bind
2310 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002311 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002312 Bundle options = info.bindOptions;
2313
Sunny Goyalffe83f12014-08-14 17:39:34 -07002314 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2315 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002316 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002317 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002318 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002319 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002320 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2321 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2322 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002323 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2324 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002325 // TODO: we need to make sure that this accounts for the options bundle.
2326 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002327 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2328 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002329 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002330 }
2331
Adam Cohendcd297f2013-06-18 13:13:40 -07002332 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002333 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002334 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002335 folderInfo.title = getText(R.string.folder_name);
2336
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002337 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002338 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2339 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002340
2341 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002342 FolderIcon newFolder =
2343 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002344 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002345 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002346 // Force measure the new folder icon
2347 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2348 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002349 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002350 }
Romain Guycbb89e42009-06-08 15:52:54 -07002351
Winsonc0b52fe2015-09-09 16:38:15 -07002352 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002353 * Unbinds the view for the specified item, and removes the item and all its children.
2354 *
2355 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002356 * @param itemInfo the {@link ItemInfo} for this view.
2357 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002358 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002359 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002360 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002361 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002362 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2363 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002364 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002365 } else {
2366 mWorkspace.removeWorkspaceItem(v);
2367 }
Winsonc0b52fe2015-09-09 16:38:15 -07002368 if (deleteFromDb) {
2369 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2370 }
2371 } else if (itemInfo instanceof FolderInfo) {
2372 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002373 if (v instanceof FolderIcon) {
2374 ((FolderIcon) v).removeListeners();
2375 }
Winsonc0b52fe2015-09-09 16:38:15 -07002376 mWorkspace.removeWorkspaceItem(v);
2377 if (deleteFromDb) {
2378 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2379 }
2380 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2381 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002382 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002383 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002384 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002385 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002386 }
2387 } else {
2388 return false;
2389 }
2390 return true;
2391 }
2392
2393 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002394 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002395 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002396 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002397 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002398 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002399 // Deleting an app widget ID is a void call but writes to disk before returning
2400 // to the caller...
2401 new AsyncTask<Void, Void, Void>() {
2402 public Void doInBackground(Void ... args) {
2403 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2404 return null;
2405 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002406 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002407 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002408 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002409 }
2410
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411 @Override
2412 public boolean dispatchKeyEvent(KeyEvent event) {
2413 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2414 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002415 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002416 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002417 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002418 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002419 dumpState();
2420 return true;
2421 }
2422 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002423 }
2424 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2425 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002426 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002427 return true;
2428 }
2429 }
2430
2431 return super.dispatchKeyEvent(event);
2432 }
2433
Joe Onorato88ec0992009-11-19 13:16:06 -08002434 @Override
2435 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002436 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2437 return;
2438 }
2439
Sunny Goyal45478022015-06-08 16:52:41 -07002440 if (mDragController.isDragging()) {
2441 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002442 return;
2443 }
2444
Tony Wickham49c8d292016-07-01 11:44:34 -07002445 if (getOpenShortcutsContainer() != null) {
2446 closeShortcutsContainer();
2447 } else if (isAppsViewVisible()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002448 showWorkspace(true);
2449 } else if (isWidgetsViewVisible()) {
2450 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002451 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002452 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002453 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002454 Folder openFolder = mWorkspace.getOpenFolder();
2455 if (openFolder.isEditingName()) {
2456 openFolder.dismissEditingName();
2457 } else {
2458 closeFolder();
2459 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002460 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002461 mWorkspace.exitWidgetResizeMode();
2462
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002463 // Back button is a no-op here, but give at least some feedback for the button press
2464 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002465 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002466 }
2467
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002468 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002469 * Launches the intent referred by the clicked shortcut.
2470 *
2471 * @param v The view representing the clicked shortcut.
2472 */
2473 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002474 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2475 // view has detached (it's possible for this to happen if the view is removed mid touch).
2476 if (v.getWindowToken() == null) {
2477 return;
2478 }
2479
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002480 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002481 return;
2482 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002483
Adam Cohen1697b792013-09-17 19:08:21 -07002484 if (v instanceof Workspace) {
2485 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002486 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002487 }
2488 return;
2489 }
2490
2491 if (v instanceof CellLayout) {
2492 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002493 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2494 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002495 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002496 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002497 }
2498
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002499 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002500 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002501 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002502 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002503 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002504 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002505 }
Sunny Goyalbb011da2016-06-15 15:42:29 -07002506 } else if (v instanceof PageIndicator || (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002507 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002508 } else if (tag instanceof AppInfo) {
2509 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002510 } else if (tag instanceof LauncherAppWidgetInfo) {
2511 if (v instanceof PendingAppWidgetHostView) {
2512 onClickPendingWidget((PendingAppWidgetHostView) v);
2513 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002514 }
2515 }
2516
Sunny Goyal70660032015-05-14 00:07:08 -07002517 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002518 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002519 return false;
2520 }
2521
Michael Jurkaaf442092010-06-10 17:01:57 -07002522 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002523 * Event handler for the app widget view which has not fully restored.
2524 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002525 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002526 if (mIsSafeModeEnabled) {
2527 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2528 return;
2529 }
2530
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002531 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002532 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002533 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2534 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2535 // This should not happen, as we make sure that an Id is allocated during bind.
2536 return;
2537 }
2538 LauncherAppWidgetProviderInfo appWidgetInfo =
2539 mAppWidgetManager.findProvider(info.providerName, info.user);
2540 if (appWidgetInfo != null) {
2541 mPendingAddWidgetId = info.appWidgetId;
2542 mPendingAddInfo.copyFrom(info);
2543 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002544
Sunny Goyald478c832016-04-01 12:04:16 -07002545 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2546 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingAddWidgetId);
2547 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
2548 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2549 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2550 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2551 }
2552 } else {
2553 LauncherAppWidgetProviderInfo appWidgetInfo =
2554 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2555 if (appWidgetInfo != null) {
2556 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2557 }
Sunny Goyalff572272014-07-23 13:58:07 -07002558 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002559 } else if (info.installProgress < 0) {
2560 // The install has not been queued
2561 final String packageName = info.providerName.getPackageName();
2562 showBrokenAppInstallDialog(packageName,
2563 new DialogInterface.OnClickListener() {
2564 public void onClick(DialogInterface dialog, int id) {
2565 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2566 }
2567 });
2568 } else {
2569 // Download has started.
2570 final String packageName = info.providerName.getPackageName();
2571 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002572 }
2573 }
2574
Sunny Goyald478c832016-04-01 12:04:16 -07002575 private void startRestoredWidgetReconfigActivity(
2576 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
2577 mPendingAddWidgetInfo = provider;
2578 mPendingAddInfo.copyFrom(info);
2579 mPendingAddWidgetId = info.appWidgetId;
2580 mAppWidgetManager.startConfigActivity(provider,
2581 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2582 }
2583
Sunny Goyalff572272014-07-23 13:58:07 -07002584 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002585 * Event handler for the "grid" button that appears on the home screen, which
2586 * enters all apps mode.
2587 *
2588 * @param v The view that was clicked.
2589 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002590 protected void onClickAllAppsButton(View v) {
2591 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002592 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002593 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002594 true /* updatePredictedApps */, false /* focusSearchBar */);
2595 }
2596 }
2597
2598 protected void onLongClickAllAppsButton(View v) {
2599 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2600 if (!isAppsViewVisible()) {
2601 showAppsView(true /* animated */, false /* resetListToTop */,
2602 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002603 }
2604 }
2605
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002606 private void showBrokenAppInstallDialog(final String packageName,
2607 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002608 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002609 .setTitle(R.string.abandoned_promises_title)
2610 .setMessage(R.string.abandoned_promise_explanation)
2611 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2612 .setNeutralButton(R.string.abandoned_clean_this,
2613 new DialogInterface.OnClickListener() {
2614 public void onClick(DialogInterface dialog, int id) {
2615 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2616 mWorkspace.removeAbandonedPromise(packageName, user);
2617 }
2618 })
2619 .create().show();
2620 return;
2621 }
2622
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002623 /**
2624 * Event handler for an app shortcut click.
2625 *
2626 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2627 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002628 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002629 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2630 Object tag = v.getTag();
2631 if (!(tag instanceof ShortcutInfo)) {
2632 throw new IllegalArgumentException("Input must be a Shortcut");
2633 }
2634
2635 // Open shortcut
2636 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002637
2638 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002639 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2640 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002641 // Launch activity anyway, framework will tell the user why the app is suspended.
2642 } else {
2643 int error = R.string.activity_not_available;
2644 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2645 error = R.string.safemode_shortcut_error;
2646 }
2647 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2648 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002649 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002650 }
2651
Chris Wren40c5ed32014-06-24 18:24:23 -04002652 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002653 if ((v instanceof BubbleTextView)
2654 && shortcut.isPromise()
2655 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002656 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002657 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002658 new DialogInterface.OnClickListener() {
2659 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002660 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002661 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002662 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002663 return;
2664 }
2665
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002666 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002667 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002668 }
2669
Sunny Goyala7ce1662016-05-31 15:01:35 -07002670 private void startAppShortcutOrInfoActivity(View v) {
2671 ItemInfo item = (ItemInfo) v.getTag();
2672 Intent intent = item.getIntent();
2673 if (intent == null) {
2674 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002675 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002676 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002677 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002678
2679 if (success && v instanceof BubbleTextView) {
2680 mWaitingForResume = (BubbleTextView) v;
2681 mWaitingForResume.setStayPressed(true);
2682 }
2683 }
2684
2685 /**
2686 * Event handler for a folder icon click.
2687 *
2688 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2689 */
2690 protected void onClickFolderIcon(View v) {
2691 if (LOGD) Log.d(TAG, "onClickFolder");
2692 if (!(v instanceof FolderIcon)){
2693 throw new IllegalArgumentException("Input must be a FolderIcon");
2694 }
2695
2696 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002697 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002698 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002699 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002700 }
Michael Jurka5130e402011-10-13 04:55:35 -07002701 }
2702
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002703 /**
2704 * Event handler for the (Add) Widgets button that appears after a long press
2705 * on the home screen.
2706 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002707 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002708 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002709 if (mIsSafeModeEnabled) {
2710 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2711 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002712 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002713 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002714 }
2715
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002716 /**
2717 * Event handler for the wallpaper picker button that appears after a long press
2718 * on the home screen.
2719 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002720 public void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002721 if (!Utilities.isWallapaperAllowed(this)) {
2722 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2723 return;
2724 }
2725
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002726 String pickerPackage = getString(R.string.wallpaper_picker_package);
2727 if (TextUtils.isEmpty(pickerPackage)) {
2728 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2729 }
2730
Tony Wickham785f7a52015-08-31 17:28:32 -07002731 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2732 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002733 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER)
2734 .setPackage(pickerPackage)
2735 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset),
2736 REQUEST_PICK_WALLPAPER);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002737 }
2738
2739 /**
2740 * Event handler for a click on the settings button that appears after a long press
2741 * on the home screen.
2742 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002743 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002744 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002745 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2746 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002747 }
2748
Adam Cohen61f560d2013-09-30 15:58:20 -07002749 public View.OnTouchListener getHapticFeedbackTouchListener() {
2750 if (mHapticFeedbackTouchListener == null) {
2751 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002752 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002753 @Override
2754 public boolean onTouch(View v, MotionEvent event) {
2755 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2756 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2757 }
2758 return false;
2759 }
2760 };
2761 }
2762 return mHapticFeedbackTouchListener;
2763 }
2764
Tony Wickhame2217252016-03-22 16:34:23 -07002765 @Override
2766 public void onAccessibilityStateChanged(boolean enabled) {
2767 mDragLayer.onAccessibilityStateChanged(enabled);
2768 }
2769
Adam Cohen9211d422014-10-07 18:14:53 -07002770 public void onDragStarted(View view) {
2771 if (isOnCustomContent()) {
2772 // Custom content screen doesn't participate in drag and drop. If on custom
2773 // content screen, move to default.
2774 moveWorkspaceToDefaultScreen();
2775 }
Adam Cohen9211d422014-10-07 18:14:53 -07002776 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002777
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002778 /**
2779 * Called when the user stops interacting with the launcher.
2780 * This implies that the user is now on the homescreen and is not doing housekeeping.
2781 */
Adam Cohen9211d422014-10-07 18:14:53 -07002782 protected void onInteractionEnd() {
2783 if (mLauncherCallbacks != null) {
2784 mLauncherCallbacks.onInteractionEnd();
2785 }
2786 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002787
2788 /**
2789 * Called when the user starts interacting with the launcher.
2790 * The possible interactions are:
2791 * - open all apps
2792 * - reorder an app shortcut, or a widget
2793 * - open the overview mode.
2794 * This is a good time to stop doing things that only make sense
2795 * when the user is on the homescreen and not doing housekeeping.
2796 */
Adam Cohen9211d422014-10-07 18:14:53 -07002797 protected void onInteractionBegin() {
2798 if (mLauncherCallbacks != null) {
2799 mLauncherCallbacks.onInteractionBegin();
2800 }
2801 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002802
Winson Chungcd99cd32015-04-29 11:03:24 -07002803 /** Updates the interaction state. */
2804 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002805 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002806 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002807 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2808 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002809 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002810 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002811 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002812 onInteractionEnd();
2813 }
2814 }
2815
Sunny Goyala7ce1662016-05-31 15:01:35 -07002816 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002817 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002818 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2819 try {
2820 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2821 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2822 // is enabled by default on NYC.
2823 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2824 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002825
2826 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2827 String id = ((ShortcutInfo) info).getDeepShortcutId();
2828 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002829 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2830 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002831 } else {
2832 // Could be launching some bookkeeping activity
2833 startActivity(intent, optsBundle);
2834 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002835 } finally {
2836 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002837 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002838 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002839 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2840 // corresponding permission. Show the appropriate permission prompt if that
2841 // is the case.
2842 if (intent.getComponent() == null
2843 && Intent.ACTION_CALL.equals(intent.getAction())
2844 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2845 PackageManager.PERMISSION_GRANTED) {
2846 // TODO: Rename sPendingAddItem to a generic name.
2847 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2848 0, info);
2849 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2850 REQUEST_PERMISSION_CALL_PHONE);
2851 } else {
2852 // No idea why this was thrown.
2853 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002854 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002855 }
2856 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002857
Sunny Goyala7ce1662016-05-31 15:01:35 -07002858 private Bundle getActivityLaunchOptions(View v) {
2859 if (Utilities.ATLEAST_MARSHMALLOW) {
2860 int left = 0, top = 0;
2861 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2862 if (v instanceof TextView) {
2863 // Launch from center of icon, not entire view
2864 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2865 if (icon != null) {
2866 Rect bounds = icon.getBounds();
2867 left = (width - bounds.width()) / 2;
2868 top = v.getPaddingTop();
2869 width = bounds.width();
2870 height = bounds.height();
2871 }
2872 }
2873 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2874 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2875 // On L devices, we use the device default slide-up transition.
2876 // On L MR1 devices, we use a custom version of the slide-up transition which
2877 // doesn't have the delay present in the device default.
2878 return ActivityOptions.makeCustomAnimation(
2879 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2880 }
2881 return null;
2882 }
2883
2884 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002885 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2886 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2887 return false;
2888 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002889 // Only launch using the new animation if the shortcut has not opted out (this is a
2890 // private contract between launcher and may be ignored in the future).
2891 boolean useLaunchAnimation = (v != null) &&
2892 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2893 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2894
2895 UserHandleCompat user = null;
2896 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2897 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2898 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002899 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002900
2901 // Prepare intent
2902 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2903 if (v != null) {
2904 int[] pos = new int[2];
2905 v.getLocationOnScreen(pos);
2906 intent.setSourceBounds(
2907 new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2908 }
2909 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002910 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2911 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
2912 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002913 // Shortcuts need some special checks due to legacy reasons.
2914 startShortcutIntentSafely(intent, optsBundle, item);
2915 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2916 // Could be launching some bookkeeping activity
2917 startActivity(intent, optsBundle);
2918 } else {
2919 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2920 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2921 }
2922 return true;
2923 } catch (ActivityNotFoundException|SecurityException e) {
2924 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2925 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2926 }
2927 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002928 }
2929
Adam Cohen268c4752012-06-06 17:47:33 -07002930 /**
2931 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2932 * in the DragLayer in the exact absolute location of the original FolderIcon.
2933 */
2934 private void copyFolderIconToImage(FolderIcon fi) {
2935 final int width = fi.getMeasuredWidth();
2936 final int height = fi.getMeasuredHeight();
2937
2938 // Lazy load ImageView, Bitmap and Canvas
2939 if (mFolderIconImageView == null) {
2940 mFolderIconImageView = new ImageView(this);
2941 }
2942 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2943 mFolderIconBitmap.getHeight() != height) {
2944 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2945 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2946 }
2947
2948 DragLayer.LayoutParams lp;
2949 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2950 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2951 } else {
2952 lp = new DragLayer.LayoutParams(width, height);
2953 }
2954
Adam Cohen307fe232012-08-16 17:55:58 -07002955 // The layout from which the folder is being opened may be scaled, adjust the starting
2956 // view size by this scale factor.
2957 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002958 lp.customPosition = true;
2959 lp.x = mRectForFolderAnimation.left;
2960 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002961 lp.width = (int) (scale * width);
2962 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002963
2964 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2965 fi.draw(mFolderIconCanvas);
2966 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002967 if (fi.getFolder() != null) {
2968 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2969 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002970 }
Adam Cohen268c4752012-06-06 17:47:33 -07002971 // Just in case this image view is still in the drag layer from a previous animation,
2972 // we remove it and re-add it.
2973 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2974 mDragLayer.removeView(mFolderIconImageView);
2975 }
2976 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002977 if (fi.getFolder() != null) {
2978 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002979 }
Adam Cohen268c4752012-06-06 17:47:33 -07002980 }
2981
Adam Cohen37b2a492016-04-06 18:36:06 -07002982 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002983 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002984 FolderInfo info = (FolderInfo) fi.getTag();
2985 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2986 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002987 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2988 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002989 }
2990
Adam Cohen268c4752012-06-06 17:47:33 -07002991 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2992 copyFolderIconToImage(fi);
2993 fi.setVisibility(View.INVISIBLE);
2994
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002995 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2996 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002997 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002998 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2999 }
3000 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003001 oa.start();
3002 }
3003
Sunny Goyal935fca12015-10-13 10:19:01 -07003004 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003005 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003006 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003007
Adam Cohen268c4752012-06-06 17:47:33 -07003008 // We remove and re-draw the FolderIcon in-case it has changed
3009 mDragLayer.removeView(mFolderIconImageView);
3010 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08003011
3012 if (cl != null) {
3013 cl.clearFolderLeaveBehind();
3014 }
3015
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003016 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003017 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003018 oa.addListener(new AnimatorListenerAdapter() {
3019 @Override
3020 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003021 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003022 // Remove the ImageView copy of the FolderIcon and make the original visible.
3023 mDragLayer.removeView(mFolderIconImageView);
3024 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003025 }
3026 }
3027 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003028 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003029 if (!animate) {
3030 oa.end();
3031 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003032 }
3033
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003034 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003035 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003036 * is animated relative to the specified View. If the View is null, no animation
3037 * is played.
3038 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003039 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003040 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003041 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003042
Adam Cohenfb91f302012-06-11 15:45:18 -07003043 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003044 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3045 if (openFolder != null && openFolder != folder) {
3046 // Close any open folder before opening a folder.
3047 closeFolder();
3048 }
3049
Adam Cohena9cf38f2011-05-02 15:36:58 -07003050 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003051
Adam Cohena9cf38f2011-05-02 15:36:58 -07003052 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003053
Sunny Goyalf4066152015-04-15 09:42:19 -07003054 // While the folder is open, the position of the icon cannot change.
3055 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3056
Adam Cohen4554ee12011-08-03 16:13:21 -07003057 // Just verify that the folder hasn't already been added to the DragLayer.
3058 // There was a one-off crash where the folder had a parent already.
3059 if (folder.getParent() == null) {
3060 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003061 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003062 } else {
3063 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3064 folder.getParent() + ").");
3065 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003066 folder.animateOpen();
3067
3068 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003069
3070 // Notify the accessibility manager that this folder "window" has appeared and occluded
3071 // the workspace items
3072 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3073 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003074 }
3075
3076 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003077 closeFolder(true);
3078 }
3079
3080 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003081 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003082 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003083 if (folder.isEditingName()) {
3084 folder.dismissEditingName();
3085 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003086 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003087 }
3088 }
3089
Sunny Goyal935fca12015-10-13 10:19:01 -07003090 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003091 animate &= !Utilities.isPowerSaverOn(this);
3092
Adam Cohen4554ee12011-08-03 16:13:21 -07003093 folder.getInfo().opened = false;
3094
3095 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3096 if (parent != null) {
3097 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003098 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003099 if (fi != null) {
3100 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3101 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003102 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003103 if (animate) {
3104 folder.animateClosed();
3105 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003106 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003107 }
Winson Chung83ca4802013-04-12 15:10:52 -07003108
Sunny Goyal935fca12015-10-13 10:19:01 -07003109 // Notify the accessibility manager that this folder "window" has disappeared and no
3110 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003111 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003112 }
3113
Tony Wickham49c8d292016-07-01 11:44:34 -07003114 public void closeShortcutsContainer() {
3115 DeepShortcutsContainer deepShortcutsContainer = getOpenShortcutsContainer();
3116 if (deepShortcutsContainer != null) {
3117 mDragController.removeDragListener(deepShortcutsContainer);
3118 mDragLayer.removeView(deepShortcutsContainer);
3119 }
3120 }
3121
3122 /**
3123 * @return The open shortcuts container, or null if there is none
3124 */
3125 public DeepShortcutsContainer getOpenShortcutsContainer() {
3126 return (DeepShortcutsContainer) mDragLayer.findViewById(R.id.deep_shortcuts_container);
3127 }
3128
Tony Wickhamdadb3042016-02-24 11:07:00 -08003129 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003130 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003131 if (!isDraggingEnabled()) return false;
3132 if (isWorkspaceLocked()) return false;
3133 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003134
Sunny Goyalbb011da2016-06-15 15:42:29 -07003135 if (v == mAllAppsButton && mAllAppsButton != null) {
Winson Chung76648c52015-07-10 14:33:23 -07003136 onLongClickAllAppsButton(v);
3137 return true;
3138 }
3139
Adam Cohen1697b792013-09-17 19:08:21 -07003140 if (v instanceof Workspace) {
3141 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003142 if (!mWorkspace.isTouchActive()) {
3143 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003144 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3145 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3146 return true;
3147 } else {
3148 return false;
3149 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003150 } else {
3151 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003152 }
Adam Cohen1697b792013-09-17 19:08:21 -07003153 }
3154
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003155 CellLayout.CellInfo longClickCellInfo = null;
3156 View itemUnderLongClick = null;
3157 if (v.getTag() instanceof ItemInfo) {
3158 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003159 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003160 itemUnderLongClick = longClickCellInfo.cell;
3161 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003162 }
3163
Winson Chung3d503fb2011-07-13 17:25:49 -07003164 // The hotseat touch handling does not go through Workspace, and we always allow long press
3165 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003166 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003167 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003168 // User long pressed on empty space
3169 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3170 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003171 if (mWorkspace.isInOverviewMode()) {
3172 mWorkspace.startReordering(v);
3173 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003174 showOverviewMode(true);
Hyunyoung Song645764e2016-06-06 14:19:02 -07003175 mHotseat.requestDisallowInterceptTouchEvent(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003176 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003177 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003178 final boolean isAllAppsButton =
3179 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3180 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3181 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003182 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003183 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003184 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003185 }
3186 }
3187 }
3188 return true;
3189 }
3190
Winson Chung3d503fb2011-07-13 17:25:49 -07003191 boolean isHotseatLayout(View layout) {
3192 return mHotseat != null && layout != null &&
3193 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3194 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003195
Winson Chung3d503fb2011-07-13 17:25:49 -07003196 /**
3197 * Returns the CellLayout of the specified container at the specified screen.
3198 */
Sunny Goyal92820592015-03-02 11:31:35 -08003199 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003200 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3201 if (mHotseat != null) {
3202 return mHotseat.getLayout();
3203 } else {
3204 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003205 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003206 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003207 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003208 }
3209 }
3210
Winson Chungb745afb2015-03-02 11:51:23 -08003211 /**
3212 * For overridden classes.
3213 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003214 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003215 return isAppsViewVisible();
3216 }
3217
3218 public boolean isAppsViewVisible() {
3219 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3220 }
3221
3222 public boolean isWidgetsViewVisible() {
3223 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003224 }
3225
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003226 private void setWorkspaceBackground(int background) {
3227 switch (background) {
3228 case WORKSPACE_BACKGROUND_TRANSPARENT:
3229 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3230 break;
3231 case WORKSPACE_BACKGROUND_BLACK:
3232 getWindow().setBackgroundDrawable(null);
3233 break;
3234 default:
3235 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3236 }
Craig Mautner360310b2012-10-26 15:13:08 -07003237 }
3238
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003239 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003240 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3241 int curflags = getWindow().getAttributes().flags
3242 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3243 if (wpflags != curflags) {
3244 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3245 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003246 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003247 }
3248
Michael Jurkae326f182011-11-21 14:05:46 -08003249 @Override
3250 public void onTrimMemory(int level) {
3251 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003252 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3253 // The widget preview db can result in holding onto over
3254 // 3MB of memory for caching which isn't necessary.
3255 SQLiteDatabase.releaseMemory();
3256
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003257 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003258 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003259 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003260 if (mLauncherCallbacks != null) {
3261 mLauncherCallbacks.onTrimMemory(level);
3262 }
Michael Jurkae326f182011-11-21 14:05:46 -08003263 }
3264
Winson5c6bdbb2015-09-03 11:36:19 -07003265 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003266 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003267 }
3268
Winson5c6bdbb2015-09-03 11:36:19 -07003269 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003270 boolean changed = mState != State.WORKSPACE ||
3271 mWorkspace.getState() != Workspace.State.NORMAL;
3272 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003273 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003274 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003275 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003276
Michael Jurkab3e22d92011-10-31 15:58:33 -07003277 // Set focus to the AppsCustomize button
3278 if (mAllAppsButton != null) {
3279 mAllAppsButton.requestFocus();
3280 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003281 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003282
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003283 // Change the state *after* we've called all the transition code
3284 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003285
Winson Chung5fb63472011-02-02 17:03:37 -08003286 // Resume the auto-advance of widgets
3287 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003288 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003289
Winson Chung0f785722015-04-08 10:27:49 -07003290 if (changed) {
3291 // Send an accessibility event to announce the context change
3292 getWindow().getDecorView()
3293 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003294 }
Winson5c6bdbb2015-09-03 11:36:19 -07003295 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003296 }
3297
Winsone9f27272015-10-13 10:47:51 -07003298 /**
3299 * Shows the overview button.
3300 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07003301 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003302 showOverviewMode(animated, false);
3303 }
3304
3305 /**
3306 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3307 * onto one of the overview panel buttons.
3308 */
3309 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3310 Runnable postAnimRunnable = null;
3311 if (requestButtonFocus) {
3312 postAnimRunnable = new Runnable() {
3313 @Override
3314 public void run() {
3315 // Hitting the menu button when in touch mode does not trigger touch mode to
3316 // be disabled, so if requested, force focus on one of the overview panel
3317 // buttons.
3318 mOverviewPanel.requestFocusFromTouch();
3319 }
3320 };
3321 }
Adam Cohened307df2013-10-02 09:37:31 -07003322 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003323 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003324 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003325 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003326 }
3327
Winson Chungb745afb2015-03-02 11:51:23 -08003328 /**
3329 * Shows the apps view.
3330 */
Hyunyoung Song645764e2016-06-06 14:19:02 -07003331 public void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003332 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003333 if (resetListToTop) {
3334 mAppsView.scrollToTop();
3335 }
Winson Chung4ac30062015-05-08 17:34:17 -07003336 if (updatePredictedApps) {
3337 tryAndUpdatePredictedApps();
3338 }
Winson Chung76648c52015-07-10 14:33:23 -07003339 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003340 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003341
Winson Chungb745afb2015-03-02 11:51:23 -08003342 /**
3343 * Shows the widgets view.
3344 */
3345 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003346 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003347 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003348 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003349 }
Winson Chung76648c52015-07-10 14:33:23 -07003350 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003351
3352 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003353 @Override
3354 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003355 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003356 }
3357 });
Winson Chungb745afb2015-03-02 11:51:23 -08003358 }
3359
3360 /**
3361 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003362 *
3363 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003364 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003365 // TODO: calling method should use the return value so that when {@code false} is returned
3366 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003367 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003368 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3369 mState != State.WIDGETS_SPRING_LOADED) {
3370 return false;
3371 }
3372 if (toState != State.APPS && toState != State.WIDGETS) {
3373 return false;
3374 }
Winson Chungb745afb2015-03-02 11:51:23 -08003375
3376 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003377 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3378 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003379 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003380 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003381 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003382
Michael Jurkab3e22d92011-10-31 15:58:33 -07003383 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003384 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003385
3386 // Pause the auto-advance of widgets until we are out of AllApps
3387 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003388 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003389 closeFolder();
Tony Wickham49c8d292016-07-01 11:44:34 -07003390 closeShortcutsContainer();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003391
3392 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003393 getWindow().getDecorView()
3394 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003395 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003396 }
3397
Winson Chungcd99cd32015-04-29 11:03:24 -07003398 /**
3399 * Updates the workspace and interaction state on state change, and return the animation to this
3400 * new state.
3401 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003402 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003403 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003404 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003405 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003406 updateInteraction(fromState, toState);
3407 return anim;
3408 }
3409
Hyunyoung Song3f471442015-04-08 19:01:34 -07003410 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003411 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003412 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003413 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003414 }
Winson Chungb745afb2015-03-02 11:51:23 -08003415
Winson Chung006ee262015-08-03 14:40:11 -07003416 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003417 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003418 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003419
3420 if (isAppsViewVisible()) {
3421 mState = State.APPS_SPRING_LOADED;
3422 } else if (isWidgetsViewVisible()) {
3423 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003424 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003425 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003426 } else {
3427 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003428 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003429 }
Adam Cohen7777d962011-08-18 18:58:38 -07003430
Hyunyoung Song3f471442015-04-08 19:01:34 -07003431 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003432 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003433 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003434
Winson Chunge7a03942011-08-05 15:05:12 -07003435 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003436 @Override
3437 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003438 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003439 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3440 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003441 // Before we show workspace, hide all apps again because
3442 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3443 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003444 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003445 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003446 } else {
3447 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003448 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003449 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003450 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003451 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003452
Tony Wickhame0c33232016-02-08 11:37:04 -08003453 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003454 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3455 || mState == State.WIDGETS_SPRING_LOADED;
3456 }
3457
Michael Jurkad3ef3062010-11-23 16:23:58 -08003458 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003459 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003460 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003461 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003462 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003463 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003464 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3465 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003466 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003467 }
3468
Winson Chung4ac30062015-05-08 17:34:17 -07003469 /**
3470 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3471 * resumed.
3472 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003473 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003474 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003475 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003476 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003477 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003478 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003479 }
3480 }
3481 }
3482
Michael Jurkab3e22d92011-10-31 15:58:33 -07003483 void lockAllApps() {
3484 // TODO
3485 }
3486
3487 void unlockAllApps() {
3488 // TODO
3489 }
3490
Winsonf768d932015-09-25 16:12:35 -07003491 public boolean launcherCallbacksProvidesSearch() {
3492 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3493 }
3494
Michael Jurkad7c28052012-04-27 15:43:36 -07003495 @Override
3496 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003497 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003498 final List<CharSequence> text = event.getText();
3499 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003500 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003501 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003502 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003503 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003504 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003505 } else if (mWorkspace != null) {
3506 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003507 } else {
3508 text.add(getString(R.string.all_apps_home_button_label));
3509 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003510 return result;
3511 }
3512
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003513 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003514 * If the activity is currently paused, signal that we need to run the passed Runnable
3515 * in onResume.
3516 *
3517 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003518 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3519 * wrong when we're not running, and if the activity comes back to what the configuration was
3520 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003521 *
3522 * Implementation of the method from LauncherModel.Callbacks.
3523 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003524 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003525 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003526 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003527 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003528 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003529 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003530 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003531 }
3532 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003533 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003534 return true;
3535 } else {
3536 return false;
3537 }
3538 }
3539
Michael Jurkac402cd92013-05-20 15:49:32 +02003540 private boolean waitUntilResume(Runnable run) {
3541 return waitUntilResume(run, false);
3542 }
3543
Michael Jurka1e2f4652013-07-08 18:03:46 -07003544 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003545 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003546 }
3547
Michael Jurka7607c2f2013-04-03 14:33:19 -07003548 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003549 * If the activity is currently paused, signal that we need to re-run the loader
3550 * in onResume.
3551 *
3552 * This needs to be called from incoming places where resources might have been loaded
3553 * while we are paused. That is becaues the Configuration might be wrong
3554 * when we're not running, and if it comes back to what it was when we
3555 * were paused, we are not restarted.
3556 *
3557 * Implementation of the method from LauncherModel.Callbacks.
3558 *
3559 * @return true if we are currently paused. The caller might be able to
3560 * skip some work in that case since we will come back again.
3561 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003562 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003563 public boolean setLoadOnResume() {
3564 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003565 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003566 mOnResumeNeedsLoad = true;
3567 return true;
3568 } else {
3569 return false;
3570 }
3571 }
3572
3573 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003574 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003575 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003576 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003577 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003578 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003579 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003580 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003581 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003582 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003583 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003584
Joe Onorato9c1289c2009-08-17 11:03:03 -04003585 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003586 * Clear any pending bind callbacks. This is called when is loader is planning to
3587 * perform a full rebind from scratch.
3588 */
3589 @Override
3590 public void clearPendingBinds() {
3591 mBindOnResumeCallbacks.clear();
3592 if (mPendingExecutor != null) {
3593 mPendingExecutor.markCompleted();
3594 mPendingExecutor = null;
3595 }
3596 }
3597
3598 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003599 * Refreshes the shortcuts shown on the workspace.
3600 *
3601 * Implementation of the method from LauncherModel.Callbacks.
3602 */
3603 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003604 if (LauncherAppState.PROFILE_STARTUP) {
3605 Trace.beginSection("Starting page bind");
3606 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003607 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003608
Winson Chungd64d1762013-08-20 14:37:16 -07003609 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003610 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003611 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003612
Michael Jurka05bf644e2011-11-30 20:28:53 -08003613 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003614 if (mHotseat != null) {
3615 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003616 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003617 if (LauncherAppState.PROFILE_STARTUP) {
3618 Trace.endSection();
3619 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003620 }
3621
Adam Cohendcd297f2013-06-18 13:13:40 -07003622 @Override
3623 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003624 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003625 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3626 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003627 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3628 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3629 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003630 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3631 // If there are no screens, we need to have an empty screen
3632 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003633 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003634 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003635
3636 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003637 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003638 if (hasCustomContentToLeft()) {
3639 mWorkspace.createCustomContentContainer();
3640 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003641 }
Winson Chung64359a52013-07-08 17:17:08 -07003642 }
3643
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003644 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003645 int count = orderedScreenIds.size();
3646 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003647 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003648 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003649 // No need to bind the first screen, as its always bound.
3650 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3651 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003652 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003653 }
3654
Winson Chungd64d1762013-08-20 14:37:16 -07003655 public void bindAppsAdded(final ArrayList<Long> newScreens,
3656 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003657 final ArrayList<ItemInfo> addAnimated,
3658 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003659 Runnable r = new Runnable() {
3660 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003661 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003662 }
3663 };
3664 if (waitUntilResume(r)) {
3665 return;
3666 }
3667
Winson Chungd64d1762013-08-20 14:37:16 -07003668 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003669 if (newScreens != null) {
3670 bindAddScreens(newScreens);
3671 }
Winson Chungd64d1762013-08-20 14:37:16 -07003672
3673 // We add the items without animation on non-visible pages, and with
3674 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003675 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003676 bindItems(addNotAnimated, 0,
3677 addNotAnimated.size(), false);
3678 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003679 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003680 bindItems(addAnimated, 0,
3681 addAnimated.size(), true);
3682 }
Winson Chungc58497e2013-09-03 17:48:37 -07003683
Winson Chung87412982013-10-03 18:34:14 -07003684 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003685 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003686
Winson Chungb745afb2015-03-02 11:51:23 -08003687 if (addedApps != null && mAppsView != null) {
3688 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003689 }
Winson Chungd64d1762013-08-20 14:37:16 -07003690 }
3691
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003692 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003693 * Bind the items start-end from the list.
3694 *
3695 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003696 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003697 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003698 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3699 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003700 Runnable r = new Runnable() {
3701 public void run() {
3702 bindItems(shortcuts, start, end, forceAnimateIcons);
3703 }
3704 };
3705 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003706 return;
3707 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003708
Winson Chungf0c6ae02012-03-21 16:10:31 -07003709 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003710 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3711 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003712 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003713 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003714 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003715 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003716 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003717
3718 // Short circuit if we are loading dock items for a configuration which has no dock
3719 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3720 mHotseat == null) {
3721 continue;
3722 }
3723
Sunny Goyal639e9062015-08-19 19:17:06 -07003724 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003725 switch (item.itemType) {
3726 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3727 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003728 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003729 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003730 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003731 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003732 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003733 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003734 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003735 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003736 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003737 default:
3738 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003739 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003740
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003741 /*
3742 * Remove colliding items.
3743 */
3744 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3745 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3746 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3747 View v = cl.getChildAt(item.cellX, item.cellY);
3748 Object tag = v.getTag();
3749 String desc = "Collision while binding workspace item: " + item
3750 + ". Collides with " + tag;
3751 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3752 throw (new RuntimeException(desc));
3753 } else {
3754 Log.d(TAG, desc);
3755 LauncherModel.deleteItemFromDatabase(this, item);
3756 continue;
3757 }
3758 }
3759 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003760 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3761 item.cellY, 1, 1);
3762 if (animateIcons) {
3763 // Animate all the applications up now
3764 view.setAlpha(0f);
3765 view.setScaleX(0f);
3766 view.setScaleY(0f);
3767 bounceAnims.add(createNewAppBounceAnimation(view, i));
3768 newShortcutsScreenId = item.screenId;
3769 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003770 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003771
Winson Chung997a9232013-07-24 15:33:46 -07003772 if (animateIcons) {
3773 // Animate to the correct page
3774 if (newShortcutsScreenId > -1) {
3775 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003776 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003777 final Runnable startBounceAnimRunnable = new Runnable() {
3778 public void run() {
3779 anim.playTogether(bounceAnims);
3780 anim.start();
3781 }
3782 };
Winson Chung997a9232013-07-24 15:33:46 -07003783 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003784 // We post the animation slightly delayed to prevent slowdowns
3785 // when we are loading right after we return to launcher.
3786 mWorkspace.postDelayed(new Runnable() {
3787 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003788 if (mWorkspace != null) {
3789 mWorkspace.snapToPage(newScreenIndex);
3790 mWorkspace.postDelayed(startBounceAnimRunnable,
3791 NEW_APPS_ANIMATION_DELAY);
3792 }
Winson Chung94d67682013-09-25 16:29:40 -07003793 }
3794 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003795 } else {
3796 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003797 }
3798 }
Winson Chung64359a52013-07-08 17:17:08 -07003799 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003800 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003801 }
3802
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003803 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3804 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3805 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003806 view.updateAppWidget(null);
3807 view.setOnClickListener(this);
3808 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003809 mWorkspace.requestLayout();
3810 }
3811
Joe Onorato9c1289c2009-08-17 11:03:03 -04003812 /**
3813 * Add the views for a widget to the workspace.
3814 *
3815 * Implementation of the method from LauncherModel.Callbacks.
3816 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003817 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003818 Runnable r = new Runnable() {
3819 public void run() {
3820 bindAppWidget(item);
3821 }
3822 };
3823 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003824 return;
3825 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003826
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003827 if (mIsSafeModeEnabled) {
3828 bindSafeModeWidget(item);
3829 return;
3830 }
3831
Daniel Sandler843e8602010-06-07 14:59:01 -04003832 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3833 if (DEBUG_WIDGETS) {
3834 Log.d(TAG, "bindAppWidget: " + item);
3835 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003836
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003837 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003838
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003839 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3840 // If the provider is not ready, bind as a pending widget.
3841 appWidgetInfo = null;
3842 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3843 // The widget id is not valid. Try to find the widget based on the provider info.
3844 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3845 } else {
3846 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3847 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003848
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003849 // If the provider is ready, but the width is not yet restored, try to restore it.
3850 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3851 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003852 if (appWidgetInfo == null) {
3853 if (DEBUG_WIDGETS) {
3854 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3855 + " belongs to component " + item.providerName
3856 + ", as the povider is null");
3857 }
3858 LauncherModel.deleteItemFromDatabase(this, item);
3859 return;
3860 }
Sunny Goyalff572272014-07-23 13:58:07 -07003861
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003862 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003863 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003864 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3865 // Id has not been allocated yet. Allocate a new id.
3866 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3867 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003868
Sunny Goyald478c832016-04-01 12:04:16 -07003869 // Also try to bind the widget. If the bind fails, the user will be shown
3870 // a click to setup UI, which will ask for the bind permission.
3871 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3872 pendingInfo.spanX = item.spanX;
3873 pendingInfo.spanY = item.spanY;
3874 pendingInfo.minSpanX = item.minSpanX;
3875 pendingInfo.minSpanY = item.minSpanY;
3876 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
3877 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3878 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003879
Sunny Goyald478c832016-04-01 12:04:16 -07003880 // Bind succeeded
3881 if (success) {
3882 // If the widget has a configure activity, it is still needs to set it up,
3883 // otherwise the widget is ready to go.
3884 item.restoreStatus = (appWidgetInfo.configure == null)
3885 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3886 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003887 }
Sunny Goyald478c832016-04-01 12:04:16 -07003888
3889 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003890 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003891 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003892 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003893 // The widget was marked as UI not ready, but there is no configure activity to
3894 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003895 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3896 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003897 }
Sunny Goyalff572272014-07-23 13:58:07 -07003898 }
3899
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003900 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003901 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003902 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3903 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003904 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003905
Sunny Goyal56c73602015-09-25 12:55:01 -07003906 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003907 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003908 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003909 deleteWidgetInfo(item);
3910 return;
3911 }
3912
Sunny Goyal233ee962015-08-03 13:05:01 -07003913 item.minSpanX = appWidgetInfo.minSpanX;
3914 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003915 addAppWidgetToWorkspace(
3916 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3917 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003918 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003919 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003920 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003921 view.updateAppWidget(null);
3922 view.setOnClickListener(this);
3923 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003924 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003925 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003926
Daniel Sandler843e8602010-06-07 14:59:01 -04003927 if (DEBUG_WIDGETS) {
3928 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3929 + (SystemClock.uptimeMillis()-start) + "ms");
3930 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003931 }
3932
Sunny Goyalff572272014-07-23 13:58:07 -07003933 /**
3934 * Restores a pending widget.
3935 *
3936 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003937 */
Sunny Goyald478c832016-04-01 12:04:16 -07003938 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003939 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3940 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3941 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003942 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003943 }
3944
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003945 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003946 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003947
3948 mWorkspace.reinflateWidgetsIfNecessary();
3949 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003950 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003951 }
3952
Adam Cohen1462de32012-07-24 22:34:36 -07003953 public void onPageBoundSynchronously(int page) {
3954 mSynchronouslyBoundPages.add(page);
3955 }
3956
Sunny Goyal527c7d32015-08-28 15:19:36 -07003957 @Override
3958 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3959 if (mPendingExecutor != null) {
3960 mPendingExecutor.markCompleted();
3961 }
3962 mPendingExecutor = executor;
3963 executor.attachTo(this);
3964 }
3965
3966 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3967 if (mPendingExecutor == executor) {
3968 mPendingExecutor = null;
3969 }
3970 }
3971
Joe Onorato9c1289c2009-08-17 11:03:03 -04003972 /**
3973 * Callback saying that there aren't any more items to bind.
3974 *
3975 * Implementation of the method from LauncherModel.Callbacks.
3976 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003977 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003978 Runnable r = new Runnable() {
3979 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003980 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003981 }
3982 };
3983 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003984 return;
3985 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003986 if (LauncherAppState.PROFILE_STARTUP) {
3987 Trace.beginSection("Page bind completed");
3988 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003989 if (mSavedState != null) {
3990 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003991 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003992 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003993
Joe Onorato9c1289c2009-08-17 11:03:03 -04003994 mSavedState = null;
3995 }
3996
Adam Cohen1462de32012-07-24 22:34:36 -07003997 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003998
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003999 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07004000
4001 // If we received the result of any pending adds while the loader was running (e.g. the
4002 // widget configuration forced an orientation change), process them now.
4003 if (sPendingAddItem != null) {
4004 final long screenId = completeAdd(sPendingAddItem);
4005
4006 // TODO: this moves the user to the page where the pending item was added. Ideally,
4007 // the screen would be guaranteed to exist after bind, and the page would be set through
4008 // the workspace restore process.
4009 mWorkspace.post(new Runnable() {
4010 @Override
4011 public void run() {
4012 mWorkspace.snapToScreenId(screenId);
4013 }
4014 });
4015 sPendingAddItem = null;
4016 }
4017
Sunny Goyal756adbc2015-04-16 15:20:51 -07004018 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004019
4020 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004021 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004022 }
Sunny Goyale26d1002016-06-20 14:52:14 -07004023 if (LauncherAppState.PROFILE_STARTUP) {
4024 Trace.endSection();
4025 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004026 }
4027
Winson Chunga2413752012-04-03 14:22:34 -07004028 private boolean canRunNewAppsAnimation() {
4029 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07004030 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07004031 }
4032
Winson Chungc9168342013-06-26 14:54:55 -07004033 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004034 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004035 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4036 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004037 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004038 return bounceAnim;
4039 }
4040
Adam Cohen27772732013-11-11 14:00:56 +00004041 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004042 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004043 }
4044
Tony Wickham55616cd2015-09-23 14:55:17 -07004045 public int getSearchBarHeight() {
4046 if (mLauncherCallbacks != null) {
4047 return mLauncherCallbacks.getSearchBarHeight();
4048 }
4049 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4050 }
4051
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004052 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004053 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4054 * multiple calls to bind the same list.)
4055 */
4056 @Thunk ArrayList<AppInfo> mTmpAppsList;
4057 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4058 public void run() {
4059 bindAllApplications(mTmpAppsList);
4060 mTmpAppsList = null;
4061 }
4062 };
4063
4064 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004065 * Add the icons for all apps.
4066 *
4067 * Implementation of the method from LauncherModel.Callbacks.
4068 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004069 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004070 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4071 mTmpAppsList = apps;
4072 return;
4073 }
4074
Winson Chungb745afb2015-03-02 11:51:23 -08004075 if (mAppsView != null) {
4076 mAppsView.setApps(apps);
4077 }
Adam Cohen9211d422014-10-07 18:14:53 -07004078 if (mLauncherCallbacks != null) {
4079 mLauncherCallbacks.bindAllApplications(apps);
4080 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004081 }
4082
4083 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004084 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
4085 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
4086 */
4087 @Override
4088 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
4089 mDeepShortcutMap = deepShortcutMapCopy;
4090 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
4091 }
4092
Tony Wickham1bce7fd2016-04-28 17:39:03 -07004093 public List<String> getShortcutIdsForItem(ItemInfo info) {
4094 if (!DeepShortcutManager.supportsShortcuts(info)) {
4095 return Collections.EMPTY_LIST;
4096 }
4097 ComponentName component = info.getTargetComponent();
4098 List<String> ids = mDeepShortcutMap.get(new ComponentKey(component, info.user));
4099 return ids == null ? Collections.EMPTY_LIST : ids;
4100 }
4101
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004102 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004103 * A package was updated.
4104 *
4105 * Implementation of the method from LauncherModel.Callbacks.
4106 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004107 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004108 Runnable r = new Runnable() {
4109 public void run() {
4110 bindAppsUpdated(apps);
4111 }
4112 };
4113 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004114 return;
4115 }
4116
Winson Chungb745afb2015-03-02 11:51:23 -08004117 if (mAppsView != null) {
4118 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004119 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004120 }
4121
Sunny Goyal4390ace2014-10-13 11:33:11 -07004122 @Override
4123 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4124 Runnable r = new Runnable() {
4125 public void run() {
4126 bindWidgetsRestored(widgets);
4127 }
4128 };
4129 if (waitUntilResume(r)) {
4130 return;
4131 }
4132 mWorkspace.widgetsRestored(widgets);
4133 }
4134
Joe Onorato9c1289c2009-08-17 11:03:03 -04004135 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004136 * Some shortcuts were updated in the background.
4137 *
4138 * Implementation of the method from LauncherModel.Callbacks.
4139 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004140 @Override
4141 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4142 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004143 Runnable r = new Runnable() {
4144 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004145 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004146 }
4147 };
4148 if (waitUntilResume(r)) {
4149 return;
4150 }
4151
Sunny Goyal4390ace2014-10-13 11:33:11 -07004152 if (!updated.isEmpty()) {
4153 mWorkspace.updateShortcuts(updated);
4154 }
4155
4156 if (!removed.isEmpty()) {
4157 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4158 for (ShortcutInfo si : removed) {
4159 removedComponents.add(si.getTargetComponent());
4160 }
4161 mWorkspace.removeItemsByComponentName(removedComponents, user);
4162 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004163 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004164 }
4165 }
4166
4167 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004168 * Update the state of a package, typically related to install state.
4169 *
4170 * Implementation of the method from LauncherModel.Callbacks.
4171 */
Sunny Goyale755d462014-07-22 13:48:29 -07004172 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004173 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4174 Runnable r = new Runnable() {
4175 public void run() {
4176 bindRestoreItemsChange(updates);
4177 }
4178 };
4179 if (waitUntilResume(r)) {
4180 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004181 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004182
Sunny Goyal756adbc2015-04-16 15:20:51 -07004183 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004184 }
4185
4186 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004187 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004188 * in addition to specific applications to remove, the reason being that
4189 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004190 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004191 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004192 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004193 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004194 public void bindWorkspaceComponentsRemoved(
4195 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4196 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004197 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004198 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004199 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004200 }
Winson Chungd64d1762013-08-20 14:37:16 -07004201 };
4202 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004203 return;
4204 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004205 if (!packageNames.isEmpty()) {
4206 mWorkspace.removeItemsByPackageName(packageNames, user);
4207 }
4208 if (!components.isEmpty()) {
4209 mWorkspace.removeItemsByComponentName(components, user);
4210 }
4211 // Notify the drag controller
4212 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004213
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004214 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004215
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004216 @Override
4217 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4218 Runnable r = new Runnable() {
4219 public void run() {
4220 bindAppInfosRemoved(appInfos);
4221 }
4222 };
4223 if (waitUntilResume(r)) {
4224 return;
Joe Onorato36115782010-06-17 13:28:48 -04004225 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004226
Winson Chungdf95eb12013-10-16 14:57:07 -07004227 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004228 if (mAppsView != null) {
4229 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004230 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004231 }
Joe Onoratobe386092009-11-17 17:32:16 -08004232
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004233 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004234 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004235 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004236 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004237 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004238
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004239 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004240 public void bindWidgetsModel(WidgetsModel model) {
4241 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004242 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004243 return;
4244 }
4245
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004246 if (mWidgetsView != null && model != null) {
4247 mWidgetsView.addWidgets(model);
4248 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004249 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004250 }
4251
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004252 @Override
4253 public void notifyWidgetProvidersChanged() {
4254 if (mWorkspace.getState().shouldUpdateWidget) {
4255 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4256 }
4257 }
4258
Winson Chung400438b2011-01-16 17:53:48 -08004259 private int mapConfigurationOriActivityInfoOri(int configOri) {
4260 final Display d = getWindowManager().getDefaultDisplay();
4261 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4262 switch (d.getRotation()) {
4263 case Surface.ROTATION_0:
4264 case Surface.ROTATION_180:
4265 // We are currently in the same basic orientation as the natural orientation
4266 naturalOri = configOri;
4267 break;
4268 case Surface.ROTATION_90:
4269 case Surface.ROTATION_270:
4270 // We are currently in the other basic orientation to the natural orientation
4271 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4272 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4273 break;
4274 }
4275
4276 int[] oriMap = {
4277 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4278 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4279 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4280 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4281 };
4282 // Since the map starts at portrait, we need to offset if this device's natural orientation
4283 // is landscape.
4284 int indexOffset = 0;
4285 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4286 indexOffset = 1;
4287 }
4288 return oriMap[(d.getRotation() + indexOffset) % 4];
4289 }
Adam Cohen446e9402011-09-15 18:21:21 -07004290
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004291 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004292 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004293 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004294 if (Utilities.ATLEAST_JB_MR2) {
4295 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4296 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004297 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4298 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004299 }
Winson Chung4b919f82012-05-01 10:44:08 -07004300 }
4301 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004302
Winson Chung4b919f82012-05-01 10:44:08 -07004303 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004304 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004305 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004306 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004307 } else {
4308 mHandler.postDelayed(new Runnable() {
4309 public void run() {
4310 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4311 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004312 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004313 }
Winson Chung4b919f82012-05-01 10:44:08 -07004314 }
Winson Chung400438b2011-01-16 17:53:48 -08004315 }
4316
Adam Cohenea90f832014-05-21 15:03:34 -07004317 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004318 * To be overridden by subclasses to indicate that there is an activity to launch
4319 * before showing the standard launcher experience.
4320 */
4321 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004322 if (mLauncherCallbacks != null) {
4323 return mLauncherCallbacks.hasFirstRunActivity();
4324 }
Adam Cohen432609a2014-03-13 17:03:22 -07004325 return false;
4326 }
4327
4328 /**
4329 * To be overridden by subclasses to launch any first run activity
4330 */
4331 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004332 if (mLauncherCallbacks != null) {
4333 return mLauncherCallbacks.getFirstRunActivity();
4334 }
Adam Cohen432609a2014-03-13 17:03:22 -07004335 return null;
4336 }
4337
Winson Chunga6945242014-01-08 14:04:34 -08004338 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004339 return !ActivityManager.isRunningInTestHarness() &&
4340 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004341 }
4342
Adam Cohen4a9423d2014-03-28 14:22:31 -07004343 protected boolean hasRunFirstRunActivity() {
4344 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4345 }
4346
Adam Cohen432609a2014-03-13 17:03:22 -07004347 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004348 if (shouldRunFirstRunActivity() &&
4349 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004350 Intent firstRunIntent = getFirstRunActivity();
4351 if (firstRunIntent != null) {
4352 startActivity(firstRunIntent);
4353 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004354 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004355 }
4356 }
Adam Cohen432609a2014-03-13 17:03:22 -07004357 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004358 }
4359
4360 private void markFirstRunActivityShown() {
4361 SharedPreferences.Editor editor = mSharedPrefs.edit();
4362 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4363 editor.apply();
4364 }
4365
Adam Cohen432609a2014-03-13 17:03:22 -07004366 /**
4367 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4368 * screen that must be displayed and dismissed.
4369 */
4370 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004371 if (mLauncherCallbacks != null) {
4372 return mLauncherCallbacks.hasDismissableIntroScreen();
4373 }
Adam Cohen432609a2014-03-13 17:03:22 -07004374 return false;
4375 }
4376
4377 /**
4378 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4379 */
4380 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004381 if (mLauncherCallbacks != null) {
4382 return mLauncherCallbacks.getIntroScreen();
4383 }
Adam Cohen432609a2014-03-13 17:03:22 -07004384 return null;
4385 }
4386
4387 /**
4388 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4389 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4390 */
4391 private boolean shouldShowIntroScreen() {
4392 return hasDismissableIntroScreen() &&
4393 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4394 }
4395
4396 protected void showIntroScreen() {
4397 View introScreen = getIntroScreen();
4398 changeWallpaperVisiblity(false);
4399 if (introScreen != null) {
4400 mDragLayer.showOverlayView(introScreen);
4401 }
4402 }
4403
Winson Chung5ffd51d2015-06-25 11:36:50 -07004404 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004405 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004406 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004407 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004408 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004409 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004410 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4411 if (userHandle != null) {
4412 user = UserHandleCompat.fromUser(userHandle);
4413 }
4414 }
4415 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004416 }
4417
4418 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004419 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004420 if (user == null) {
4421 user = UserHandleCompat.myUserHandle();
4422 }
4423
4424 // Called from search suggestion, add the profile extra to the intent to ensure that we
4425 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004426 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004427 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004428 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004429 return null;
4430 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004431 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004432 }
4433
Winson Chung5ffd51d2015-06-25 11:36:50 -07004434 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004435 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4436 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004437 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004438 UserHandleCompat.myUserHandle());
4439 }
4440
Winson Chung5ffd51d2015-06-25 11:36:50 -07004441 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004442 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4443 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004444 mWorkspace.onExternalDragStartedWithItem(dragView);
4445 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004446 }
4447
Winson Chung5ffd51d2015-06-25 11:36:50 -07004448 protected void moveWorkspaceToDefaultScreen() {
4449 mWorkspace.moveToDefaultScreen(false);
4450 }
4451
Winson Chung80baf5a2010-08-09 16:03:15 -07004452 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004453 * Returns a FastBitmapDrawable with the icon, accurately sized.
4454 */
4455 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4456 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4457 d.setFilterBitmap(true);
4458 resizeIconDrawable(d);
4459 return d;
4460 }
4461
4462 /**
4463 * Resizes an icon drawable to the correct icon size.
4464 */
Winson5fbe0742015-09-30 15:33:00 -07004465 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004466 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004467 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004468 }
4469
4470 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004471 * Prints out out state for debugging.
4472 */
4473 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004474 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004475 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004476 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4477 Log.d(TAG, "mRestoring=" + mRestoring);
4478 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004479 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004480 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004481
Daniel Sandler325dc232013-06-05 22:57:57 -04004482 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004483 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004484
4485 @Override
4486 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4487 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004488 // Dump workspace
4489 writer.println(prefix + "Workspace Items");
4490 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4491 writer.println(prefix + " Homescreen " + i);
4492
4493 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4494 for (int j = 0; j < layout.getChildCount(); j++) {
4495 Object tag = layout.getChildAt(j).getTag();
4496 if (tag != null) {
4497 writer.println(prefix + " " + tag.toString());
4498 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004499 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004500 }
Sunny Goyala1365452015-10-01 15:46:24 -07004501
4502 writer.println(prefix + " Hotseat");
4503 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4504 for (int j = 0; j < layout.getChildCount(); j++) {
4505 Object tag = layout.getChildAt(j).getTag();
4506 if (tag != null) {
4507 writer.println(prefix + " " + tag.toString());
4508 }
4509 }
4510
Sunny Goyal713edfc2016-05-06 09:58:34 -07004511 try {
4512 FileLog.flushAll(writer);
4513 } catch (Exception e) {
4514 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004515 }
4516
Adam Cohen9211d422014-10-07 18:14:53 -07004517 if (mLauncherCallbacks != null) {
4518 mLauncherCallbacks.dump(prefix, fd, writer, args);
4519 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004520 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004521
Adam Cohen59400422014-03-05 18:07:04 -08004522 public static CustomAppWidget getCustomAppWidget(String name) {
4523 return sCustomAppWidgets.get(name);
4524 }
4525
4526 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4527 return sCustomAppWidgets;
4528 }
4529
Sunny Goyal29538332016-02-18 09:10:19 -08004530 public static List<View> getFolderContents(View icon) {
4531 if (icon instanceof FolderIcon) {
4532 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4533 } else {
4534 return Collections.EMPTY_LIST;
4535 }
4536 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004537
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004538 public static Launcher getLauncher(Context context) {
4539 if (context instanceof Launcher) {
4540 return (Launcher) context;
4541 }
4542 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4543 }
4544
Sunny Goyal745bad92016-05-02 10:54:12 -07004545 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4546
4547 @Override
4548 public void onSharedPreferenceChanged(
4549 SharedPreferences sharedPreferences, String key) {
4550 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4551 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4552 if (!waitUntilResume(this, true)) {
4553 run();
4554 }
4555 }
4556 }
4557
4558 @Override
4559 public void run() {
4560 setOrientation();
4561 }
4562 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004563}