blob: 5a25069e70bcd05fcfc2eadaa7a35b5513bb7f00 [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;
Winson Chungc7450e32012-04-17 17:34:08 -070028import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040029import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080031import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080036import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070039import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040040import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070042import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070043import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070045import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.pm.PackageManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070049import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070050import android.graphics.Bitmap;
51import android.graphics.Canvas;
52import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070053import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070054import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070055import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020056import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080058import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070059import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070060import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040061import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070062import android.os.Trace;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070063import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.text.Selection;
65import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070066import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070068import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070069import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070070import android.view.HapticFeedbackConstants;
71import android.view.KeyEvent;
Sunny Goyal66b24572016-09-21 15:57:55 -070072import android.view.KeyboardShortcutGroup;
73import android.view.KeyboardShortcutInfo;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.MotionEvent;
76import android.view.Surface;
77import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070078import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080079import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.ViewGroup;
81import android.view.ViewTreeObserver;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080082import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070083import android.view.accessibility.AccessibilityManager;
Sunny Goyal66b24572016-09-21 15:57:55 -070084import android.view.accessibility.AccessibilityNodeInfo;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070085import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080089import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070091
Sunny Goyal651077b2014-06-30 14:15:31 -070092import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070093import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyalae502842016-06-17 08:43:56 -070094import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070095import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070096import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080097import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -070098import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010099import com.android.launcher3.compat.LauncherActivityInfoCompat;
100import com.android.launcher3.compat.LauncherAppsCompat;
101import com.android.launcher3.compat.UserHandleCompat;
102import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800103import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700104import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700105import com.android.launcher3.dragndrop.DragController;
106import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700107import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700108import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -0700109import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000110import com.android.launcher3.folder.Folder;
111import com.android.launcher3.folder.FolderIcon;
Sunny Goyal66b24572016-09-21 15:57:55 -0700112import com.android.launcher3.keyboard.CustomActionsPopup;
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;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700120import com.android.launcher3.shortcuts.ShortcutKey;
Hyunyoung Song5aa27142016-07-21 11:48:37 -0700121import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal2100c782016-08-22 16:00:03 -0700122import com.android.launcher3.util.ActivityResultInfo;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700123import com.android.launcher3.util.ComponentKey;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700124import com.android.launcher3.util.ItemInfoMatcher;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700125import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700126import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal2100c782016-08-22 16:00:03 -0700127import com.android.launcher3.util.PendingRequestArgs;
Sunny Goyal322d5562015-06-25 19:35:49 -0700128import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700129import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700130import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700131import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700132import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700133import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700134
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700135import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700136import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700137import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700138import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800139import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700140import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700141import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700142import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700143
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144/**
145 * Default launcher application.
146 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100147public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700148 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
149 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700150 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700151 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700152 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700154 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700155 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400156 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700157
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700159 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700160 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700161 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162
Michael Jurka8b805b12012-04-18 14:23:14 -0700163 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700164 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700165 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700166
Sunny Goyal28c6b962015-10-12 11:42:05 -0700167 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
168
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700169 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
170
Mathew Inwood876a8462013-06-14 14:12:41 +0100171 /**
172 * IntentStarter uses request codes starting with this. This must be greater than all activity
173 * request codes used internally.
174 */
175 protected static final int REQUEST_LAST = 100;
176
Michael Jurka0a457bf2012-11-19 14:05:05 -0800177 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700178 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800179 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180
Winson Chung2672ff92012-05-04 16:22:30 -0700181 // The Intent extra that defines whether to ignore the launch animation
182 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400183 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700184
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700185 public static final String ACTION_APPWIDGET_HOST_RESET =
186 "com.android.launcher3.intent.ACTION_APPWIDGET_HOST_RESET";
187
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 // Type: int
189 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700190 // Type: int
191 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal2100c782016-08-22 16:00:03 -0700192 // Type: PendingRequestArgs
193 private static final String RUNTIME_STATE_PENDING_REQUEST_ARGS = "launcher.request_args";
194 // Type: ActivityResultInfo
195 private static final String RUNTIME_STATE_PENDING_ACTIVITY_RESULT = "launcher.activity_result";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700197 static final String APPS_VIEW_SHOWN = "launcher.apps_view_shown";
Adam Cohenb54a5982014-01-08 15:21:04 -0800198
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700199 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700200 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
201 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700202
Adam Cohen091440a2015-03-18 14:16:05 -0700203 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700204 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700205
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700206 private boolean mIsSafeModeEnabled;
207
Joe Onorato9c1289c2009-08-17 11:03:03 -0400208 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800209 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700210 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700211 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212
Winson Chunga2413752012-04-03 14:22:34 -0700213 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700214 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
215 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700216 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700217
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700218 private final BroadcastReceiver mUiBroadcastReceiver = new BroadcastReceiver() {
219
220 @Override
221 public void onReceive(Context context, Intent intent) {
222 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
223 closeSystemDialogs();
224 } else if (ACTION_APPWIDGET_HOST_RESET.equals(intent.getAction())) {
225 if (mAppWidgetHost != null) {
226 mAppWidgetHost.startListening();
227 }
228 }
229 }
230 };
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800231
Adam Cohen091440a2015-03-18 14:16:05 -0700232 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700233 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700234 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800235 private DragController mDragController;
Sunny Goyal6178f132016-07-11 17:30:03 -0700236 private View mQsbContainer;
Sunny Goyal322d5562015-06-25 19:35:49 -0700237
238 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700239
Sunny Goyalffe83f12014-08-14 17:39:34 -0700240 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700241 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700242
Michael Jurka0280c3b2010-09-17 15:00:07 -0700243 private int[] mTmpAddItemCellCoordinates = new int[2];
244
Sunny Goyal316490e2015-06-02 09:38:28 -0700245 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800246 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700247
Michael Jurka838a4ca2011-02-07 13:33:06 -0800248 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700249 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700250
Sunny Goyal47328fd2016-05-25 18:56:41 -0700251 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700252
253 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700254 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700255 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700256
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700257 // Main container view and the model for the widget tray screen.
258 @Thunk WidgetsContainerView mWidgetsView;
259 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700260
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700262 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
263 // scroll issues (because the workspace may not have been measured yet) and extra work.
264 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
265 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266
267 private SpannableStringBuilder mDefaultKeySsb = null;
268
Adam Cohen091440a2015-03-18 14:16:05 -0700269 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400270
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800271 private boolean mPaused = true;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700272 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273
Michael Jurka1e2f4652013-07-08 18:03:46 -0700274 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700275 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700276 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700277
Joe Onorato9c1289c2009-08-17 11:03:03 -0400278 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800279 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700280 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700281 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Hyunyoung Songc001cf52016-07-21 17:32:43 -0700282 private boolean mIsResumeFromActionScreenOff;
Adam Cohen091440a2015-03-18 14:16:05 -0700283 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800284 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700285 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800286 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400287
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700288 /** Maps launcher activity components to their list of shortcut ids. */
289 private MultiHashMap<ComponentKey, String> mDeepShortcutMap = new MultiHashMap<>();
290
Adam Cohen61f560d2013-09-30 15:58:20 -0700291 private View.OnTouchListener mHapticFeedbackTouchListener;
292
Adam Cohended9f8d2010-11-03 13:25:16 -0700293 // Related to the auto-advancing of widgets
294 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700295 private static final int ADVANCE_INTERVAL = 20000;
296 private static final int ADVANCE_STAGGER = 250;
297
298 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700299 private long mAutoAdvanceSentTime;
300 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700301 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700302
Winson Chung400438b2011-01-16 17:53:48 -0800303 // Determines how long to wait after a rotation before restoring the screen orientation to
304 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700305 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800306
Adam Cohen1462de32012-07-24 22:34:36 -0700307 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
308
Winson Chung46353de2012-02-16 14:05:10 -0800309 // We only want to get the SharedPreferences once since it does an FS stat each time we get
310 // it from the context.
311 private SharedPreferences mSharedPrefs;
312
Winson Chungf0c6ae02012-03-21 16:10:31 -0700313 // Holds the page that we need to animate to, and the icon views that we need to animate up
314 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700315 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700316 private Bitmap mFolderIconBitmap;
317 private Canvas mFolderIconCanvas;
318 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700319
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700320 private DeviceProfile mDeviceProfile;
321
Adam Cohen4b66bf32015-09-18 12:15:19 -0700322 private boolean mMoveToDefaultScreenFromNewIntent;
323
Winson Chung13eb5272015-05-11 16:30:13 -0700324 // This is set to the view that launched the activity that navigated the user away from
325 // launcher. Since there is no callback for when the activity has finished launching, enable
326 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800327 private BubbleTextView mWaitingForResume;
328
Adam Cohen59400422014-03-05 18:07:04 -0800329 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
330 new HashMap<String, CustomAppWidget>();
331
Adam Cohen59400422014-03-05 18:07:04 -0800332 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700333 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
334 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800335 }
336 }
337
Hyunyoung Song06ca7562016-07-29 13:03:54 -0700338 // Exiting spring loaded mode happens with a delay. This runnable object triggers the
339 // state transition. If another state transition happened during this delay,
340 // simply unregister this runnable.
341 private Runnable mExitSpringLoadedModeRunnable;
342
Adam Cohen091440a2015-03-18 14:16:05 -0700343 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700344 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700345 if (mWorkspace != null) {
346 mWorkspace.buildPageHardwareLayers();
347 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700348 }
349 };
350
Sunny Goyal2100c782016-08-22 16:00:03 -0700351 // Activity result which needs to be processed after workspace has loaded.
352 private ActivityResultInfo mPendingActivityResult;
353 /**
354 * Holds extra information required to handle a result from an external call, like
355 * {@link #startActivityForResult(Intent, int)} or {@link #requestPermissions(String[], int)}
356 */
357 private PendingRequestArgs mPendingRequestArgs;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800358
Hyunyoung Songaa953652016-04-19 18:30:24 -0700359 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800360
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700361 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700362 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400363
364 @Thunk void setOrientation() {
365 if (mRotationEnabled) {
366 unlockScreenOrientation(true);
367 } else {
368 setRequestedOrientation(
369 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700370 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400371 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700372
Sunny Goyal745bad92016-05-02 10:54:12 -0700373 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700374
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 @Override
376 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700377 if (DEBUG_STRICT_MODE) {
378 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
379 .detectDiskReads()
380 .detectDiskWrites()
381 .detectNetwork() // or .detectAll() for all detectable problems
382 .penaltyLog()
383 .build());
384 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
385 .detectLeakedSqlLiteObjects()
386 .detectLeakedClosableObjects()
387 .penaltyLog()
388 .penaltyDeath()
389 .build());
390 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700391 if (LauncherAppState.PROFILE_STARTUP) {
392 Trace.beginSection("Launcher-onCreate");
393 }
Winson Chunga2413752012-04-03 14:22:34 -0700394
Adam Cohen9211d422014-10-07 18:14:53 -0700395 if (mLauncherCallbacks != null) {
396 mLauncherCallbacks.preOnCreate();
397 }
398
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400400
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400401 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700402
Adam Cohen2e6da152015-05-06 11:42:25 -0700403 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700404 mDeviceProfile = getResources().getConfiguration().orientation
405 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700406 app.getInvariantDeviceProfile().landscapeProfile
407 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700408
Sunny Goyalf7258242015-10-19 16:59:07 -0700409 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700410 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800411 mModel = app.setLauncher(this);
412 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700413 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700414
Joe Onorato41a12d22009-10-31 18:30:00 -0400415 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700416 mAllAppsController = new AllAppsTransitionController(this);
417 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700418
Sunny Goyalffe83f12014-08-14 17:39:34 -0700419 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700420
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700421 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
422 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700423
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700424 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
425 // this also ensures that any synchronous binding below doesn't re-trigger another
426 // LauncherModel load.
427 mPaused = false;
428
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800429 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700430
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 setupViews();
Winson1f064272016-07-18 17:18:02 -0700432 mDeviceProfile.layout(this, false /* notifyListeners */);
Tony Wickham827cef22016-03-17 15:39:39 -0700433 mExtractedColors = new ExtractedColors();
434 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800435
Tony Wickhame2217252016-03-22 16:34:23 -0700436 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
437 .addAccessibilityStateChangeListener(this);
438
Joe Onorato7c312c12009-08-13 21:36:53 -0700439 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700440
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 mSavedState = savedInstanceState;
442 restoreState(mSavedState);
443
Sunny Goyale26d1002016-06-20 14:52:14 -0700444 if (LauncherAppState.PROFILE_STARTUP) {
445 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 }
447
Sunny Goyal2100c782016-08-22 16:00:03 -0700448 // We only load the page synchronously if the user rotates (or triggers a
449 // configuration change) while launcher is in the foreground
450 if (!mModel.startLoader(mWorkspace.getRestorePage())) {
451 // If we are not binding synchronously, show a fade in animation when
452 // the first page bind completes.
453 mDragLayer.setAlpha(0);
454 } else {
455 setWorkspaceLoading(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 }
457
458 // For handling default keys
459 mDefaultKeySsb = new SpannableStringBuilder();
460 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800461
462 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700463 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
464 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800465
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800466 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700467 // In case we are on a device with locked rotation, we should look at preferences to check
468 // if the user has specifically allowed rotation.
469 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700470 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700471 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
472 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700473 }
474
475 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
476 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400477 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700478
Adam Cohen9211d422014-10-07 18:14:53 -0700479 if (mLauncherCallbacks != null) {
480 mLauncherCallbacks.onCreate(savedInstanceState);
481 }
482 }
483
Sunny Goyal7779d622015-06-11 16:18:39 -0700484 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700485 public void onExtractedColorsChanged() {
486 loadExtractedColorsAndColorItems();
487 }
488
489 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700490 // TODO: do this in pre-N as well, once the extraction part is complete.
491 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700492 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700493 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700494 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham827cef22016-03-17 15:39:39 -0700495 }
496 }
497
Adam Cohen9211d422014-10-07 18:14:53 -0700498 private LauncherCallbacks mLauncherCallbacks;
499
500 public void onPostCreate(Bundle savedInstanceState) {
501 super.onPostCreate(savedInstanceState);
502 if (mLauncherCallbacks != null) {
503 mLauncherCallbacks.onPostCreate(savedInstanceState);
504 }
505 }
506
Winson1f064272016-07-18 17:18:02 -0700507 public void onInsetsChanged(Rect insets) {
508 mDeviceProfile.updateInsets(insets);
509 mDeviceProfile.layout(this, true /* notifyListeners */);
510 }
511
Sunny Goyal32554d12015-12-03 15:31:25 -0800512 /**
513 * Call this after onCreate to set or clear overlay.
514 */
515 public void setLauncherOverlay(LauncherOverlay overlay) {
516 if (overlay != null) {
517 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
518 }
519 mWorkspace.setLauncherOverlay(overlay);
520 }
521
Adam Cohen9211d422014-10-07 18:14:53 -0700522 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
523 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700524 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700525 private boolean mWorkspaceImportanceStored = false;
526 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700527 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800528 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700529 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
530
531 @Override
532 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700533 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700534 return;
535 }
536 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700537 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700538 if (mWorkspace != null) {
539 mWorkspaceImportanceForAccessibility =
540 mWorkspace.getImportantForAccessibility();
541 mWorkspace.setImportantForAccessibility(
542 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
543 mWorkspaceImportanceStored = true;
544 }
545 if (mHotseat != null) {
546 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
547 mHotseat.setImportantForAccessibility(
548 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
549 mHotseatImportanceStored = true;
550 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700551 }
552
553 @Override
554 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700555 if (mWorkspaceImportanceStored && mWorkspace != null) {
556 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
557 }
558 if (mHotseatImportanceStored && mHotseat != null) {
559 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
560 }
561 mWorkspaceImportanceStored = false;
562 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700563 }
564 });
Adam Cohen9211d422014-10-07 18:14:53 -0700565 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700566 }
567
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700568 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700569 public void onLauncherProviderChange() {
570 if (mLauncherCallbacks != null) {
571 mLauncherCallbacks.onLauncherProviderChange();
572 }
573 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700574
Adam Cohen9211d422014-10-07 18:14:53 -0700575 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700576 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700577 if (mLauncherCallbacks != null) {
578 return mLauncherCallbacks.hasCustomContentToLeft();
579 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700580 return false;
581 }
582
Dave Hawkeya8881582013-09-17 15:55:33 -0600583 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500584 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600585 * {@link #addToCustomContentPage}. This will only be invoked if
586 * {@link #hasCustomContentToLeft()} is {@code true}.
587 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500588 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700589 if (mLauncherCallbacks != null) {
590 mLauncherCallbacks.populateCustomContentContainer();
591 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600592 }
593
594 /**
595 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
596 * ensure the custom content page is added or removed if necessary.
597 */
598 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600599 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600600 // Not bound yet, wait for bindScreens to be called.
601 return;
602 }
603
604 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
605 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500606 mWorkspace.createCustomContentContainer();
607 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600608 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
609 mWorkspace.removeCustomContentPage();
610 }
611 }
612
Hyunyoung Songaa953652016-04-19 18:30:24 -0700613 public UserEventDispatcher getUserEventDispatcher() {
614 if (mLauncherCallbacks != null) {
615 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
616 if (dispatcher != null) {
617 return dispatcher;
618 }
619 }
620
Sunny Goyal64976d52016-06-20 14:56:28 -0700621 // Logger object is a singleton and does not have to be coupled with the foreground
622 // activity. Since most user event logging is done on the UI, the object is retrieved
623 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700624 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700625 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700626 }
627 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800628 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100629
Hyunyoung Song3f471442015-04-08 19:01:34 -0700630 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700631 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
632 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700633 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700634 }
635
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000636 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700637 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
638 // This cast is safe as long as the id < 0x00FFFFFF
639 // Since we jail all the dynamically generated views, there should be no clashes
640 // with any other views.
641 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000642 }
643
644 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700645 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
646 * a configuration step, this allows the proper animations to run after other transitions.
647 */
Sunny Goyal2100c782016-08-22 16:00:03 -0700648 private long completeAdd(
649 int requestCode, Intent intent, int appWidgetId, PendingRequestArgs info) {
650 long screenId = info.screenId;
651 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohen83079e42014-09-19 17:43:08 -0700652 // When the screen id represents an actual screen (as opposed to a rank) we make sure
653 // that the drop page actually exists.
Sunny Goyal2100c782016-08-22 16:00:03 -0700654 screenId = ensurePendingDropLayoutExists(info.screenId);
Adam Cohen83079e42014-09-19 17:43:08 -0700655 }
Adam Cohendb364c32014-05-20 14:23:40 -0700656
Sunny Goyal2100c782016-08-22 16:00:03 -0700657 switch (requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800658 case REQUEST_CREATE_SHORTCUT:
Sunny Goyalfb5096d2016-09-08 14:32:06 -0700659 completeAddShortcut(intent, info.container, screenId, info.cellX, info.cellY, info);
Winson Chungb8472bb2011-08-05 13:49:21 -0700660 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800661 case REQUEST_CREATE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700662 completeAddAppWidget(appWidgetId, info, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700663 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700664 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -0700665 completeRestoreAppWidget(appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700666 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700667 case REQUEST_BIND_PENDING_APPWIDGET: {
Sunny Goyal2100c782016-08-22 16:00:03 -0700668 int widgetId = appWidgetId;
669 LauncherAppWidgetInfo widgetInfo =
Sunny Goyald478c832016-04-01 12:04:16 -0700670 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal2100c782016-08-22 16:00:03 -0700671 if (widgetInfo != null) {
Sunny Goyald478c832016-04-01 12:04:16 -0700672 // Since the view was just bound, also launch the configure activity if needed
673 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
674 .getLauncherAppWidgetInfo(widgetId);
675 if (provider != null && provider.configure != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700676 startRestoredWidgetReconfigActivity(provider, widgetInfo);
Sunny Goyald478c832016-04-01 12:04:16 -0700677 }
678 }
679 break;
680 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800681 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700682
Adam Cohendb364c32014-05-20 14:23:40 -0700683 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800684 }
685
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800686 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700687 final int requestCode, final int resultCode, final Intent data) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700688 if (isWorkspaceLoading()) {
689 // process the result once the workspace has loaded.
690 mPendingActivityResult = new ActivityResultInfo(requestCode, resultCode, data);
691 return;
692 }
693 mPendingActivityResult = null;
694
Winson Chunge341d302013-08-16 14:31:00 -0700695 // Reset the startActivity waiting flag
Sunny Goyal2100c782016-08-22 16:00:03 -0700696 final PendingRequestArgs requestArgs = mPendingRequestArgs;
697 setWaitingForResult(null);
698 if (requestArgs == null) {
699 return;
700 }
701
702 final int pendingAddWidgetId = requestArgs.getWidgetId();
Winson Chunge341d302013-08-16 14:31:00 -0700703
Adam Cohenad4e15c2013-10-17 16:21:35 -0700704 Runnable exitSpringLoaded = new Runnable() {
705 @Override
706 public void run() {
707 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
708 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
709 }
710 };
711
Michael Jurka8b805b12012-04-18 14:23:14 -0700712 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700713 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700714 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700715 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
716 if (resultCode == RESULT_CANCELED) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700717 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId, requestArgs);
Adam Cohen689ff162014-05-08 17:27:56 -0700718 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700719 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700720 } else if (resultCode == RESULT_OK) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700721 addAppWidgetImpl(
722 appWidgetId, requestArgs, null,
723 requestArgs.getWidgetProvider(),
724 ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700725 }
726 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200727 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
728 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700729 // User could have free-scrolled between pages before picking a wallpaper; make sure
730 // we move to the closest one now.
731 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700732 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200733 }
734 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700735 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200736
Adam Cohened66b2b2012-01-23 17:28:51 -0800737 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700738 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700739
Adam Cohened66b2b2012-01-23 17:28:51 -0800740 // We have special handling for widgets
741 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800742 final int appWidgetId;
743 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
744 : -1;
745 if (widgetId < 0) {
746 appWidgetId = pendingAddWidgetId;
747 } else {
748 appWidgetId = widgetId;
749 }
750
Adam Cohenad4e15c2013-10-17 16:21:35 -0700751 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800752 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700753 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
754 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700755 result = RESULT_CANCELED;
Sunny Goyal2100c782016-08-22 16:00:03 -0700756 completeTwoStageWidgetDrop(result, appWidgetId, requestArgs);
Adam Cohendb364c32014-05-20 14:23:40 -0700757 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700758 @Override
759 public void run() {
760 exitSpringLoadedDragModeDelayed(false, 0, null);
761 }
762 };
Adam Cohendb364c32014-05-20 14:23:40 -0700763
Sunny Goyal2100c782016-08-22 16:00:03 -0700764 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
765 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
766 } else {
767 if (requestArgs.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
768 // When the screen id represents an actual screen (as opposed to a rank)
769 // we make sure that the drop page actually exists.
770 requestArgs.screenId =
771 ensurePendingDropLayoutExists(requestArgs.screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700772 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700773 final CellLayout dropLayout =
774 mWorkspace.getScreenWithId(requestArgs.screenId);
775
776 dropLayout.setDropPending(true);
777 final Runnable onComplete = new Runnable() {
778 @Override
779 public void run() {
780 completeTwoStageWidgetDrop(resultCode, appWidgetId, requestArgs);
781 dropLayout.setDropPending(false);
782 }
783 };
784 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
785 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Winson Chung5aaab772012-04-27 15:24:02 -0700786 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800787 return;
788 }
789
Sunny Goyald478c832016-04-01 12:04:16 -0700790 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
791 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700792 if (resultCode == RESULT_OK) {
793 // Update the widget view.
Sunny Goyal2100c782016-08-22 16:00:03 -0700794 completeAdd(requestCode, data, pendingAddWidgetId, requestArgs);
Sunny Goyalff572272014-07-23 13:58:07 -0700795 }
796 // Leave the widget in the pending state if the user canceled the configure.
797 return;
798 }
799
Sunny Goyalaad90582015-07-09 14:22:50 -0700800 if (requestCode == REQUEST_CREATE_SHORTCUT) {
801 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
Sunny Goyal2100c782016-08-22 16:00:03 -0700802 if (resultCode == RESULT_OK && requestArgs.container != ItemInfo.NO_ID) {
803 completeAdd(requestCode, data, -1, requestArgs);
804 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
805 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
806
Sunny Goyalaad90582015-07-09 14:22:50 -0700807 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700808 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
809 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800812 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800813 }
814
815 @Override
816 protected void onActivityResult(
817 final int requestCode, final int resultCode, final Intent data) {
818 handleActivityResult(requestCode, resultCode, data);
819 if (mLauncherCallbacks != null) {
820 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
821 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800822 }
823
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600824 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700825 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600826 int[] grantResults) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700827 PendingRequestArgs pendingArgs = mPendingRequestArgs;
828 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && pendingArgs != null
829 && pendingArgs.getRequestCode() == REQUEST_PERMISSION_CALL_PHONE) {
830 setWaitingForResult(null);
831
Sunny Goyal28c6b962015-10-12 11:42:05 -0700832 View v = null;
Sunny Goyal2100c782016-08-22 16:00:03 -0700833 CellLayout layout = getCellLayout(pendingArgs.container, pendingArgs.screenId);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700834 if (layout != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700835 v = layout.getChildAt(pendingArgs.cellX, pendingArgs.cellY);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700836 }
Sunny Goyal2100c782016-08-22 16:00:03 -0700837 Intent intent = pendingArgs.getPendingIntent();
838
Sunny Goyal28c6b962015-10-12 11:42:05 -0700839 if (grantResults.length > 0
840 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700841 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700842 } else {
843 // TODO: Show a snack bar with link to settings
844 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
Sunny Goyal112ce422016-08-22 16:45:29 -0700845 getString(R.string.derived_app_name)), Toast.LENGTH_SHORT).show();
Sunny Goyal28c6b962015-10-12 11:42:05 -0700846 }
847 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600848 if (mLauncherCallbacks != null) {
849 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
850 grantResults);
851 }
852 }
853
Adam Cohendb364c32014-05-20 14:23:40 -0700854 /**
855 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
856 *
857 * @param screenId the screen id to check
858 * @return the new screen, or screenId if it exists
859 */
860 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800861 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700862 if (dropLayout == null) {
863 // it's possible that the add screen was removed because it was
864 // empty and a re-bind occurred
865 mWorkspace.addExtraEmptyScreen();
866 return mWorkspace.commitExtraEmptyScreen();
867 } else {
868 return screenId;
869 }
870 }
871
Sunny Goyal2100c782016-08-22 16:00:03 -0700872 @Thunk void completeTwoStageWidgetDrop(
873 final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) {
874 CellLayout cellLayout = mWorkspace.getScreenWithId(requestArgs.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800875 Runnable onCompleteRunnable = null;
876 int animationType = 0;
877
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700878 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800879 if (resultCode == RESULT_OK) {
880 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
881 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Sunny Goyal2100c782016-08-22 16:00:03 -0700882 requestArgs.getWidgetProvider());
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700883 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800884 onCompleteRunnable = new Runnable() {
885 @Override
886 public void run() {
Sunny Goyal2100c782016-08-22 16:00:03 -0700887 completeAddAppWidget(appWidgetId, requestArgs, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700888 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
889 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800890 }
891 };
892 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800893 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800894 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800895 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700896 if (mDragLayer.getAnimatedView() != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -0700897 mWorkspace.animateWidgetDrop(requestArgs, cellLayout,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700898 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
899 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700900 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700901 // The animated view may be null in the case of a rotation during widget configuration
902 onCompleteRunnable.run();
903 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 }
905
906 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700907 protected void onStop() {
908 super.onStop();
909 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700910
911 if (mLauncherCallbacks != null) {
912 mLauncherCallbacks.onStop();
913 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700914
915 if (Utilities.isNycMR1OrAbove()) {
916 mAppWidgetHost.stopListening();
917 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700918 }
919
920 @Override
921 protected void onStart() {
922 super.onStart();
923 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700924
925 if (mLauncherCallbacks != null) {
926 mLauncherCallbacks.onStart();
927 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700928
929 if (Utilities.isNycMR1OrAbove()) {
930 mAppWidgetHost.startListening();
931 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700932 }
933
934 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200936 long startTime = 0;
937 if (DEBUG_RESUME_TIME) {
938 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400939 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200940 }
Adam Cohen9211d422014-10-07 18:14:53 -0700941
942 if (mLauncherCallbacks != null) {
943 mLauncherCallbacks.preOnResume();
944 }
945
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700947 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700948
Winson Chung4a2afa32012-07-19 14:53:05 -0700949 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700950 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700951 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800952 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700953 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700954 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700955 // view after launching an app, as they may be depending on the UI to be static to
956 // switch to another app, otherwise, if it was
Hyunyoung Songdd60ce42016-07-27 17:08:38 -0700957 showAppsView(false /* animated */, !launchedFromApp /* updatePredictedApps */,
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700958 mAppsView.shouldRestoreImeState() /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800959 } else if (mOnResumeState == State.WIDGETS) {
960 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700961 }
962 mOnResumeState = State.NONE;
963
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800964 mPaused = false;
Sunny Goyal2100c782016-08-22 16:00:03 -0700965 if (mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700966 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700967 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700968 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700970 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200971 // We might have postponed some bind calls until onResume (see waitUntilResume) --
972 // execute them here
973 long startTimeCallbacks = 0;
974 if (DEBUG_RESUME_TIME) {
975 startTimeCallbacks = System.currentTimeMillis();
976 }
977
Michael Jurka1e2f4652013-07-08 18:03:46 -0700978 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
979 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200980 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700981 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200982 if (DEBUG_RESUME_TIME) {
983 Log.d(TAG, "Time spent processing callbacks in onResume: " +
984 (System.currentTimeMillis() - startTimeCallbacks));
985 }
Michael Jurka447bf842013-05-15 14:52:15 +0200986 }
Michael Jurka54554252013-08-01 12:52:23 +0200987 if (mOnResumeCallbacks.size() > 0) {
988 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
989 mOnResumeCallbacks.get(i).run();
990 }
991 mOnResumeCallbacks.clear();
992 }
Winson Chunge4e50662012-01-23 14:45:13 -0800993
994 // Reset the pressed state of icons that were locked in the press state while activities
995 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800996 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800997 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800998 mWaitingForResume.setStayPressed(false);
999 }
Winson Chung82963d52013-10-09 11:20:57 -07001000
Adam Cohen06dff352012-06-01 17:17:08 -07001001 // It is possible that widgets can receive updates while launcher is not in the foreground.
1002 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1003 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1004 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001005 if (!isWorkspaceLoading()) {
1006 getWorkspace().reinflateWidgetsIfNecessary();
1007 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001008
Michael Jurka447bf842013-05-15 14:52:15 +02001009 if (DEBUG_RESUME_TIME) {
1010 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1011 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001012
Adam Cohen4b66bf32015-09-18 12:15:19 -07001013 // We want to suppress callbacks about CustomContent being shown if we have just received
1014 // onNewIntent while the user was present within launcher. In that case, we post a call
1015 // to move the user to the main screen (which will occur after onResume). We don't want to
1016 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1017 // suppress here.
1018 if (mWorkspace.getCustomContentCallbacks() != null
1019 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001020 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001021 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001022 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1023 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001024 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001025 }
1026 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001027 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001028 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001029 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001030
Sunny Goyal756adbc2015-04-16 15:20:51 -07001031 if (!isWorkspaceLoading()) {
1032 // Process any items that were added while Launcher was away.
1033 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001034
1035 // Refresh shortcuts if the permission changed.
1036 mModel.refreshShortcutsIfRequired();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001037 }
Adam Cohen9211d422014-10-07 18:14:53 -07001038
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001039 if (shouldShowDiscoveryBounce()) {
1040 mAllAppsController.showDiscoveryBounce();
1041 }
1042 mIsResumeFromActionScreenOff = false;
Adam Cohen9211d422014-10-07 18:14:53 -07001043 if (mLauncherCallbacks != null) {
1044 mLauncherCallbacks.onResume();
1045 }
Adam Cohen06dff352012-06-01 17:17:08 -07001046 }
1047
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001048 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001049 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001050 // Ensure that items added to Launcher are queued until Launcher returns
1051 InstallShortcutReceiver.enableInstallQueue();
1052
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001053 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001054 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001055 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001056 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001057
1058 // We call onHide() aggressively. The custom content callbacks should be able to
1059 // debounce excess onHide calls.
1060 if (mWorkspace.getCustomContentCallbacks() != null) {
1061 mWorkspace.getCustomContentCallbacks().onHide();
1062 }
Romain Guycbb89e42009-06-08 15:52:54 -07001063
Adam Cohen9211d422014-10-07 18:14:53 -07001064 if (mLauncherCallbacks != null) {
1065 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001066 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001067 }
1068
1069 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001070 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1071 // by a onResume or by scrolling otherwise.
1072 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001073
1074 // Custom content is completely hidden
1075 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001076
1077 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1078 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001079
1080 // Indicates whether the user is allowed to scroll away from the custom content.
1081 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001082 }
1083
Adam Cohenc2d6e892014-10-16 09:49:24 -07001084 public interface LauncherOverlay {
1085
1086 /**
1087 * Touch interaction leading to overscroll has begun
1088 */
1089 public void onScrollInteractionBegin();
1090
1091 /**
1092 * Touch interaction related to overscroll has ended
1093 */
1094 public void onScrollInteractionEnd();
1095
1096 /**
1097 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1098 * screen (or in the case of RTL, the rightmost screen).
1099 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001100 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001101
1102 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001103 * Called when the launcher is ready to use the overlay
1104 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001105 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001106 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001107 }
1108
Jun Mukai8af0cd82015-05-12 12:32:12 -07001109 public interface LauncherSearchCallbacks {
1110 /**
1111 * Called when the search overlay is shown.
1112 */
1113 public void onSearchOverlayOpened();
1114
1115 /**
1116 * Called when the search overlay is dismissed.
1117 */
1118 public void onSearchOverlayClosed();
1119 }
1120
Adam Cohenc2d6e892014-10-16 09:49:24 -07001121 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001122 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001123 }
1124
1125 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1126
Sunny Goyalc86df472016-02-25 09:19:38 -08001127 public void onScrollChanged(float progress) {
1128 if (mWorkspace != null) {
1129 mWorkspace.onOverlayScrollChanged(progress);
1130 }
1131 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001132 }
1133
Jorim Jaggid017f882014-01-14 17:08:48 -08001134 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001135 if (mLauncherCallbacks != null) {
1136 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001137 } else {
1138 // On devices with a locked orientation, we will at least have the allow rotation
1139 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001140 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001141 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001142 }
1143
Adam Cohen9211d422014-10-07 18:14:53 -07001144 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001145 CustomContentCallbacks callbacks, String description) {
1146 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001147 }
1148
Adam Cohenedb40762013-07-18 16:45:45 -07001149 // The custom content needs to offset its content to account for the QSB
1150 public int getTopOffsetForCustomContent() {
1151 return mWorkspace.getPaddingTop();
1152 }
1153
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001154 @Override
1155 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001156 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001157 if (mModel.isCurrentCallbacks(this)) {
1158 mModel.stopLoader();
1159 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001160 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1161
Romain Guy13c2e7b2010-03-10 19:45:00 -08001162 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001163 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001164
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001165 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001166 @Override
1167 public void onWindowFocusChanged(boolean hasFocus) {
1168 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001169 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001170
1171 if (mLauncherCallbacks != null) {
1172 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1173 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001174 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001175
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 private boolean acceptFilter() {
1177 final InputMethodManager inputManager = (InputMethodManager)
1178 getSystemService(Context.INPUT_METHOD_SERVICE);
1179 return !inputManager.isFullscreenMode();
1180 }
1181
1182 @Override
1183 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001184 final int uniChar = event.getUnicodeChar();
1185 final boolean handled = super.onKeyDown(keyCode, event);
1186 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1187 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1189 keyCode, event);
1190 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001191 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001192 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001193 // showSearchDialog()
1194 // If there are multiple keystrokes before the search dialog takes focus,
1195 // onSearchRequested() will be called for every keystroke,
1196 // but it is idempotent, so it's fine.
1197 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198 }
1199 }
1200
Joe Onorato8a9625e2010-01-28 15:55:35 -08001201 // Eat the long press event so the keyboard doesn't come up.
1202 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1203 return true;
1204 }
1205
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 return handled;
1207 }
1208
Winson46163472015-09-22 17:31:56 -07001209 @Override
1210 public boolean onKeyUp(int keyCode, KeyEvent event) {
1211 if (keyCode == KeyEvent.KEYCODE_MENU) {
1212 // Ignore the menu key if we are currently dragging or are on the custom content screen
1213 if (!isOnCustomContent() && !mDragController.isDragging()) {
1214 // Close any open folders
1215 closeFolder();
1216
Tony Wickham49c8d292016-07-01 11:44:34 -07001217 // Close any shortcuts containers
1218 closeShortcutsContainer();
1219
Winson46163472015-09-22 17:31:56 -07001220 // Stop resizing any widgets
1221 mWorkspace.exitWidgetResizeMode();
1222
1223 // Show the overview mode if we are on the workspace
1224 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1225 !mWorkspace.isSwitchingState()) {
1226 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001227 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001228 }
1229 }
1230 return true;
1231 }
1232 return super.onKeyUp(keyCode, event);
1233 }
1234
Karl Rosaen138a0412009-04-23 19:00:21 -07001235 private String getTypedText() {
1236 return mDefaultKeySsb.toString();
1237 }
1238
Sunny Goyal6f28e712016-09-13 14:19:29 -07001239 @Override
1240 public void clearTypedText() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001241 mDefaultKeySsb.clear();
1242 mDefaultKeySsb.clearSpans();
1243 Selection.setSelection(mDefaultKeySsb, 0);
1244 }
1245
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001247 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1248 * State
1249 */
1250 private static State intToState(int stateOrdinal) {
1251 State state = State.WORKSPACE;
1252 final State[] stateValues = State.values();
1253 for (int i = 0; i < stateValues.length; i++) {
1254 if (stateValues[i].ordinal() == stateOrdinal) {
1255 state = stateValues[i];
1256 break;
1257 }
1258 }
1259 return state;
1260 }
1261
1262 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263 * Restores the previous state, if it exists.
1264 *
1265 * @param savedState The previous state.
1266 */
1267 private void restoreState(Bundle savedState) {
1268 if (savedState == null) {
1269 return;
1270 }
1271
Michael Jurka883f55b2010-10-21 15:47:14 -07001272 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001273 if (state == State.APPS || state == State.WIDGETS) {
1274 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001275 }
1276
Adam Cohen21cd0022013-10-09 18:57:02 -07001277 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1278 PagedView.INVALID_RESTORE_PAGE);
1279 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001280 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001281 }
1282
Sunny Goyal2100c782016-08-22 16:00:03 -07001283 PendingRequestArgs requestArgs = savedState.getParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS);
1284 if (requestArgs != null) {
1285 setWaitingForResult(requestArgs);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001287
1288 mPendingActivityResult = savedState.getParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 }
1290
1291 /**
1292 * Finds all the views we need and configure them properly.
1293 */
1294 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001295 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001296 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001297 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001298 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyal6178f132016-07-11 17:30:03 -07001299 mQsbContainer = mDragLayer.findViewById(mDeviceProfile.isVerticalBarLayout()
1300 ? R.id.workspace_blocked_row : R.id.qsb_container);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001301 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001302
Sunny Goyal784f9c32016-03-23 16:56:54 -07001303 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1304 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1305 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306
Winson Chung4c98d922011-05-31 16:50:48 -07001307 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001308 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001309
Winson Chung3d503fb2011-07-13 17:25:49 -07001310 // Setup the hotseat
1311 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1312 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001313 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001314 }
1315
Winsone9f27272015-10-13 10:47:51 -07001316 // Setup the overview panel
1317 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001318
Winson Chung4c98d922011-05-31 16:50:48 -07001319 // Setup the workspace
1320 mWorkspace.setHapticFeedbackEnabled(false);
1321 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001322 mWorkspace.setup(mDragController);
Winsonc7d2e832016-07-28 12:24:55 -07001323 // Until the workspace is bound, ensure that we keep the wallpaper offset locked to the
1324 // default state, otherwise we will update to the wrong offsets in RTL
1325 mWorkspace.lockWallpaperToDefaultPage();
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001326 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001327 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001328
Tony Wickham34d2c912015-09-11 09:27:58 -07001329 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001330 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001331
Winson Chungef7f8742015-06-04 17:18:17 -07001332 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001333 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001334 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001335 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1336 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1337 } else {
1338 mAppsView.setSearchBarController(new DefaultAppSearchController());
1339 }
Craig Mautner360310b2012-10-26 15:13:08 -07001340
Winson Chung3d503fb2011-07-13 17:25:49 -07001341 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001342 mDragController.setDragScoller(mWorkspace);
1343 mDragController.setScrollView(mDragLayer);
1344 mDragController.setMoveTarget(mWorkspace);
1345 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001346 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001347
Peter Schiller310a9882016-06-30 13:52:36 -07001348 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1349 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1350 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001351
Sunny Goyal322d5562015-06-25 19:35:49 -07001352 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1353 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001354 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 }
1356
Winsone9f27272015-10-13 10:47:51 -07001357 private void setupOverviewPanel() {
1358 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1359
1360 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1361 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1362 @Override
1363 public boolean onLongClick(View v) {
1364 return v.performClick();
1365 }
1366 };
1367
1368 // Bind wallpaper button actions
1369 View wallpaperButton = findViewById(R.id.wallpaper_button);
1370 wallpaperButton.setOnClickListener(new OnClickListener() {
1371 @Override
1372 public void onClick(View view) {
1373 if (!mWorkspace.isSwitchingState()) {
1374 onClickWallpaperPicker(view);
1375 }
1376 }
1377 });
1378 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1379 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1380
1381 // Bind widget button actions
1382 mWidgetsButton = findViewById(R.id.widget_button);
1383 mWidgetsButton.setOnClickListener(new OnClickListener() {
1384 @Override
1385 public void onClick(View view) {
1386 if (!mWorkspace.isSwitchingState()) {
1387 onClickAddWidgetButton(view);
1388 }
1389 }
1390 });
1391 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1392 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1393
1394 // Bind settings actions
1395 View settingsButton = findViewById(R.id.settings_button);
1396 boolean hasSettings = hasSettings();
1397 if (hasSettings) {
1398 settingsButton.setOnClickListener(new OnClickListener() {
1399 @Override
1400 public void onClick(View view) {
1401 if (!mWorkspace.isSwitchingState()) {
1402 onClickSettingsButton(view);
1403 }
1404 }
1405 });
1406 settingsButton.setOnLongClickListener(performClickOnLongClick);
1407 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1408 } else {
1409 settingsButton.setVisibility(View.GONE);
1410 }
1411
1412 mOverviewPanel.setAlpha(0f);
1413 }
1414
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001416 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001417 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001418 */
1419 public void setAllAppsButton(View allAppsButton) {
1420 mAllAppsButton = allAppsButton;
1421 }
1422
Sunny Goyalbb011da2016-06-15 15:42:29 -07001423 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001424 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001425 }
1426
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001427 public View getWidgetsButton() {
1428 return mWidgetsButton;
1429 }
1430
Anjali Koppal5ad44842014-03-10 20:34:39 -07001431 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001432 * Creates a view representing a shortcut.
1433 *
1434 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001436 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001437 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 }
1439
1440 /**
1441 * Creates a view representing a shortcut inflated from the specified resource.
1442 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 * @param parent The group the shortcut belongs to.
1444 * @param info The data structure describing the shortcut.
1445 *
1446 * @return A View inflated from layoutResId.
1447 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001448 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001449 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001450 parent, false);
1451 favorite.applyFromShortcutInfo(info, mIconCache);
1452 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001454 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 return favorite;
1456 }
1457
1458 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 * Add a shortcut to the workspace.
1460 *
1461 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001463 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001464 int cellY, PendingRequestArgs args) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001465 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001466 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001467
Sunny Goyal5c97f512015-05-19 16:03:28 -07001468 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Sunny Goyalfb5096d2016-09-08 14:32:06 -07001469 if (info == null || args.getRequestCode() != REQUEST_CREATE_SHORTCUT ||
1470 args.getPendingIntent().getComponent() == null) {
1471 return;
1472 }
1473 if (!PackageManagerHelper.hasPermissionForActivity(
1474 this, info.intent, args.getPendingIntent().getComponent().getPackageName())) {
1475 // The app is trying to add a shortcut without sufficient permissions
1476 Log.e(TAG, "Ignoring malicious intent " + info.intent.toUri(0));
Adam Cohend9198822011-11-22 16:42:47 -08001477 return;
1478 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001479 final View view = createShortcut(info);
1480
Sunny Goyal5c97f512015-05-19 16:03:28 -07001481 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001482 // First we check if we already know the exact location where we want to add this item.
1483 if (cellX >= 0 && cellY >= 0) {
1484 cellXY[0] = cellX;
1485 cellXY[1] = cellY;
1486 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001487
1488 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001489 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001490 true, null,null)) {
1491 return;
1492 }
1493 DragObject dragObject = new DragObject();
1494 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001495 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1496 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001497 return;
1498 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001499 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001500 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001501 }
1502
1503 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001504 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001505 return;
1506 }
1507
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001508 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509
Sunny Goyal2100c782016-08-22 16:00:03 -07001510 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
1511 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 }
1513
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001515 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001517 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 */
Sunny Goyal2100c782016-08-22 16:00:03 -07001519 @Thunk void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001520 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1521
Adam Cohened66b2b2012-01-23 17:28:51 -08001522 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001523 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001524 }
Romain Guycbb89e42009-06-08 15:52:54 -07001525
Adam Cohen59400422014-03-05 18:07:04 -08001526 if (appWidgetInfo.isCustomWidget) {
1527 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001528 }
1529
Adam Cohen59400422014-03-05 18:07:04 -08001530 LauncherAppWidgetInfo launcherInfo;
1531 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07001532 launcherInfo.spanX = itemInfo.spanX;
1533 launcherInfo.spanY = itemInfo.spanY;
1534 launcherInfo.minSpanX = itemInfo.minSpanX;
1535 launcherInfo.minSpanY = itemInfo.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001536 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001537
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal2100c782016-08-22 16:00:03 -07001539 itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540
Sunny Goyal2100c782016-08-22 16:00:03 -07001541 if (hostView == null) {
1542 // Perform actual inflation because we're live
1543 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 }
Sunny Goyal2100c782016-08-22 16:00:03 -07001545 hostView.setVisibility(View.VISIBLE);
1546 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 }
Romain Guycbb89e42009-06-08 15:52:54 -07001548
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001549 private void addAppWidgetToWorkspace(
1550 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001551 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001552 hostView.setTag(item);
1553 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001554
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001555 hostView.setFocusable(true);
1556 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001557
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001558 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001559 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1560
1561 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001562 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001563 }
1564 }
1565
Adam Cohended9f8d2010-11-03 13:25:16 -07001566 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1567 @Override
1568 public void onReceive(Context context, Intent intent) {
1569 final String action = intent.getAction();
1570 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1571 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001572 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001573 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001574
Winson Chungc100e8e2011-08-09 16:02:43 -07001575 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001576 // processing a multi-step drop
Sunny Goyal2100c782016-08-22 16:00:03 -07001577 if (mAppsView != null && mWidgetsView != null && mPendingRequestArgs == null) {
Winson5c6bdbb2015-09-03 11:36:19 -07001578 if (!showWorkspace(false)) {
1579 // If we are already on the workspace, then manually reset all apps
1580 mAppsView.reset();
1581 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001582 }
Hyunyoung Songc001cf52016-07-21 17:32:43 -07001583 mIsResumeFromActionScreenOff = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001584 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1585 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001586 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001587 }
1588 }
1589 };
1590
1591 @Override
1592 public void onAttachedToWindow() {
1593 super.onAttachedToWindow();
1594
1595 // Listen for broadcasts related to user-presence
1596 final IntentFilter filter = new IntentFilter();
1597 filter.addAction(Intent.ACTION_SCREEN_OFF);
1598 filter.addAction(Intent.ACTION_USER_PRESENT);
1599 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001600 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001601 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001602 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001603
1604 if (mLauncherCallbacks != null) {
1605 mLauncherCallbacks.onAttachedToWindow();
1606 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001607 }
1608
1609 @Override
1610 public void onDetachedFromWindow() {
1611 super.onDetachedFromWindow();
1612 mVisible = false;
1613
Adam Cohend113e0c2010-11-11 10:48:05 -08001614 if (mAttached) {
1615 unregisterReceiver(mReceiver);
1616 mAttached = false;
1617 }
Winson Chungb745afb2015-03-02 11:51:23 -08001618 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001619
1620 if (mLauncherCallbacks != null) {
1621 mLauncherCallbacks.onDetachedFromWindow();
1622 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001623 }
1624
1625 public void onWindowVisibilityChanged(int visibility) {
1626 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001627 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001628 // The following code used to be in onResume, but it turns out onResume is called when
1629 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1630 // is a more appropriate event to handle
1631 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001632 if (!mWorkspaceLoading) {
1633 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001634 // We want to let Launcher draw itself at least once before we force it to build
1635 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001636 // apps is nice and speedy.
1637 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001638 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001639 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001640 if (mStarted) return;
1641 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001642 // We delay the layer building a bit in order to give
1643 // other message processing a time to run. In particular
1644 // this avoids a delay in hiding the IME if it was
1645 // currently shown, because doing that may involve
1646 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001647 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001648 final ViewTreeObserver.OnDrawListener listener = this;
1649 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001650 public void run() {
1651 if (mWorkspace != null &&
1652 mWorkspace.getViewTreeObserver() != null) {
1653 mWorkspace.getViewTreeObserver().
1654 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001655 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001656 }
1657 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001658 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001659 }
1660 });
1661 }
1662 clearTypedText();
1663 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001664 }
1665
Adam Cohen091440a2015-03-18 14:16:05 -07001666 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001667 mHandler.removeMessages(ADVANCE_MSG);
1668 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1669 mHandler.sendMessageDelayed(msg, delay);
1670 mAutoAdvanceSentTime = System.currentTimeMillis();
1671 }
1672
Adam Cohen091440a2015-03-18 14:16:05 -07001673 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001674 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1675 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1676 mAutoAdvanceRunning = autoAdvanceRunning;
1677 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001678 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001679 sendAdvanceMessage(delay);
1680 } else {
1681 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001682 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001683 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1684 }
1685 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001686 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001687 }
1688 }
1689 }
1690
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001691 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1692
Adam Cohended9f8d2010-11-03 13:25:16 -07001693 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001694 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001695 if (msg.what == ADVANCE_MSG) {
1696 int i = 0;
1697 for (View key: mWidgetsToAdvance.keySet()) {
1698 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001699 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001700 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001701 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001702 public void run() {
1703 ((Advanceable) v).advance();
1704 }
1705 }, delay);
1706 }
1707 i++;
1708 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001709 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001710 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001711 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001712 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001713 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001714
Winsonc0b52fe2015-09-09 16:38:15 -07001715 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001716 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001717 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1718 if (v instanceof Advanceable) {
1719 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001720 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001721 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001722 }
1723 }
1724
Winsonc0b52fe2015-09-09 16:38:15 -07001725 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001726 if (mWidgetsToAdvance.containsKey(hostView)) {
1727 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001728 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001729 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001730 }
1731
Hyunyoung Song3f471442015-04-08 19:01:34 -07001732 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001733 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1734 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001735 }
1736
Winson Chunga6945242014-01-08 14:04:34 -08001737 public DragLayer getDragLayer() {
1738 return mDragLayer;
1739 }
1740
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001741 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001742 return mAppsView;
1743 }
1744
Hyunyoung Song3f471442015-04-08 19:01:34 -07001745 public WidgetsContainerView getWidgetsView() {
1746 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001747 }
1748
Winson Chunga6945242014-01-08 14:04:34 -08001749 public Workspace getWorkspace() {
1750 return mWorkspace;
1751 }
1752
Sunny Goyal6178f132016-07-11 17:30:03 -07001753 public View getQsbContainer() {
1754 return mQsbContainer;
1755 }
1756
Winson Chunga6945242014-01-08 14:04:34 -08001757 public Hotseat getHotseat() {
1758 return mHotseat;
1759 }
1760
Jorim Jaggid017f882014-01-14 17:08:48 -08001761 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001762 return mOverviewPanel;
1763 }
1764
Sunny Goyal47328fd2016-05-25 18:56:41 -07001765 public DropTargetBar getDropTargetBar() {
1766 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001767 }
1768
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001769 public LauncherAppWidgetHost getAppWidgetHost() {
1770 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001771 }
Romain Guycbb89e42009-06-08 15:52:54 -07001772
Winson Chunga9abd0e2010-10-27 17:18:37 -07001773 public LauncherModel getModel() {
1774 return mModel;
1775 }
1776
Adam Cohen79d90c52016-04-22 13:29:20 -07001777 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001778 return mSharedPrefs;
1779 }
1780
Adam Cohen2e6da152015-05-06 11:42:25 -07001781 public DeviceProfile getDeviceProfile() {
1782 return mDeviceProfile;
1783 }
1784
Bjorn Bringertc459e522013-06-07 19:36:01 +01001785 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001786 getWindow().closeAllPanels();
1787
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001788 // Whatever we were doing is hereby canceled.
Sunny Goyal2100c782016-08-22 16:00:03 -07001789 setWaitingForResult(null);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001790 }
1791
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 @Override
1793 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001794 long startTime = 0;
1795 if (DEBUG_RESUME_TIME) {
1796 startTime = System.currentTimeMillis();
1797 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 super.onNewIntent(intent);
1799
1800 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001801 Folder openFolder = mWorkspace.getOpenFolder();
1802 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1803 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1804 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1805 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1806 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001807 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001808 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001809
Adam Cohen6fecd412013-10-02 17:41:50 -07001810 if (mWorkspace == null) {
1811 // Can be cases where mWorkspace is null, this prevents a NPE
1812 return;
1813 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001814 // In all these cases, only animate if we're already on home
1815 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001816
Sunny Goyal935fca12015-10-13 10:19:01 -07001817 closeFolder(alreadyOnHome);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001818 closeShortcutsContainer(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001819 exitSpringLoadedDragMode();
1820
1821 // If we are already on home, then just animate back to the workspace,
1822 // otherwise, just wait until onResume to set the state back to Workspace
1823 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001824 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001825 } else {
1826 mOnResumeState = State.WORKSPACE;
1827 }
1828
1829 final View v = getWindow().peekDecorView();
1830 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001831 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001832 INPUT_METHOD_SERVICE);
1833 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1834 }
1835
Winson Chungb745afb2015-03-02 11:51:23 -08001836 // Reset the apps view
1837 if (!alreadyOnHome && mAppsView != null) {
1838 mAppsView.scrollToTop();
1839 }
1840
Hyunyoung Song3f471442015-04-08 19:01:34 -07001841 // Reset the widgets view
1842 if (!alreadyOnHome && mWidgetsView != null) {
1843 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001844 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001845
Adam Cohen9211d422014-10-07 18:14:53 -07001846 if (mLauncherCallbacks != null) {
1847 mLauncherCallbacks.onHomeIntent();
1848 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001849 }
Adam Cohened307df2013-10-02 09:37:31 -07001850
Adam Cohen9211d422014-10-07 18:14:53 -07001851 if (mLauncherCallbacks != null) {
1852 mLauncherCallbacks.onNewIntent(intent);
1853 }
Winson15f8b172015-08-19 11:02:39 -07001854
1855 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1856 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1857 // animation.
1858 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001859 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1860 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001861 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1862 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001863
1864 // We use this flag to suppress noisy callbacks above custom content state
1865 // from onResume.
1866 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001867 mWorkspace.post(new Runnable() {
1868 @Override
1869 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001870 if (mWorkspace != null) {
1871 mWorkspace.moveToDefaultScreen(true);
1872 }
Winson15f8b172015-08-19 11:02:39 -07001873 }
1874 });
1875 }
1876 }
1877
1878 if (DEBUG_RESUME_TIME) {
1879 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1880 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001881 }
1882
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001883 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001884 public void onRestoreInstanceState(Bundle state) {
1885 super.onRestoreInstanceState(state);
1886 for (int page: mSynchronouslyBoundPages) {
1887 mWorkspace.restoreInstanceStateForChild(page);
1888 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 }
1890
1891 @Override
1892 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001893 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001894 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1895 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001896
Adam Cohen21cd0022013-10-09 18:57:02 -07001897 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001898 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899
Michael Jurka883f55b2010-10-21 15:47:14 -07001900 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001901 // We close any open folder since it will not be re-opened, and we need to make sure
1902 // this state is reflected.
1903 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1904 closeFolder(false);
Sunny Goyala2454ad2016-07-22 10:50:11 -07001905 closeShortcutsContainer(false);
Tony Wickham49c8d292016-07-01 11:44:34 -07001906
Sunny Goyal2100c782016-08-22 16:00:03 -07001907 if (mPendingRequestArgs != null) {
1908 outState.putParcelable(RUNTIME_STATE_PENDING_REQUEST_ARGS, mPendingRequestArgs);
1909 }
1910 if (mPendingActivityResult != null) {
1911 outState.putParcelable(RUNTIME_STATE_PENDING_ACTIVITY_RESULT, mPendingActivityResult);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 }
1913
Adam Cohen9211d422014-10-07 18:14:53 -07001914 if (mLauncherCallbacks != null) {
1915 mLauncherCallbacks.onSaveInstanceState(outState);
1916 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 }
1918
1919 @Override
1920 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001922
Winson Chunge7a03942011-08-05 15:05:12 -07001923 // Remove all pending runnables
1924 mHandler.removeMessages(ADVANCE_MSG);
1925 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001926 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001927 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001928
Winson Chungcd2b0142011-06-08 16:02:26 -07001929 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001930 // It's possible to receive onDestroy after a new Launcher activity has
1931 // been created. In this case, don't interfere with the new Launcher.
1932 if (mModel.isCurrentCallbacks(this)) {
1933 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001934 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001935 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001936
Sunny Goyal745bad92016-05-02 10:54:12 -07001937 if (mRotationPrefChangeHandler != null) {
1938 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1939 }
1940
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001942 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001944 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001946 mAppWidgetHost = null;
1947
1948 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949
1950 TextKeyListener.getInstance().release();
1951
Tony Wickhame2217252016-03-22 16:34:23 -07001952 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1953 .removeAccessibilityStateChangeListener(this);
1954
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001955 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001956
Michael Jurka2ecf9952012-06-18 12:52:28 -07001957 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001958
1959 if (mLauncherCallbacks != null) {
1960 mLauncherCallbacks.onDestroy();
1961 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 }
1963
Sunny Goyalae502842016-06-17 08:43:56 -07001964 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1965 return mAccessibilityDelegate;
1966 }
1967
Adam Cohena9cf38f2011-05-02 15:36:58 -07001968 public DragController getDragController() {
1969 return mDragController;
1970 }
1971
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 @Override
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001973 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07001974 super.startActivityForResult(intent, requestCode, options);
Adam Cohen173f7112015-03-27 15:14:00 -07001975 }
1976
1977 @Override
1978 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1979 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
Adam Cohen173f7112015-03-27 15:14:00 -07001980 try {
1981 super.startIntentSenderForResult(intent, requestCode,
1982 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1983 } catch (IntentSender.SendIntentException e) {
1984 throw new ActivityNotFoundException();
1985 }
1986 }
1987
Winson Chung70d72102011-08-12 11:24:35 -07001988 /**
1989 * Indicates that we want global search for this activity by setting the globalSearch
1990 * argument for {@link #startSearch} to true.
1991 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001993 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001995
Karl Rosaen138a0412009-04-23 19:00:21 -07001996 if (initialQuery == null) {
1997 // Use any text typed in the launcher as the initial query
1998 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001999 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 if (appSearchData == null) {
2001 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002002 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002004
Sunny Goyal6f28e712016-09-13 14:19:29 -07002005 if (mLauncherCallbacks == null ||
2006 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
2007 // Starting search from the callbacks failed. Start the default global search.
2008 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
Ian Parkinson047036e2014-09-01 15:40:53 +01002009 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002010
2011 // We need to show the workspace after starting the search
2012 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002013 }
2014
Ian Parkinson047036e2014-09-01 15:40:53 +01002015 /**
Michael Jurkaa33411c2012-06-14 16:18:21 -07002016 * Starts the global search activity. This code is a copied from SearchManager
2017 */
Sunny Goyal6f28e712016-09-13 14:19:29 -07002018 public void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002019 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002020 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002021 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2022 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2023 if (globalSearchActivity == null) {
2024 Log.w(TAG, "No global search activity found.");
2025 return;
2026 }
2027 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2028 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2029 intent.setComponent(globalSearchActivity);
2030 // Make sure that we have a Bundle to put source in
2031 if (appSearchData == null) {
2032 appSearchData = new Bundle();
2033 } else {
2034 appSearchData = new Bundle(appSearchData);
2035 }
Adam Cohen9211d422014-10-07 18:14:53 -07002036 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002037 if (!appSearchData.containsKey("source")) {
2038 appSearchData.putString("source", getPackageName());
2039 }
2040 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2041 if (!TextUtils.isEmpty(initialQuery)) {
2042 intent.putExtra(SearchManager.QUERY, initialQuery);
2043 }
2044 if (selectInitialQuery) {
2045 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2046 }
2047 intent.setSourceBounds(sourceBounds);
2048 try {
2049 startActivity(intent);
2050 } catch (ActivityNotFoundException ex) {
2051 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2052 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 }
2054
Yura4f93ec62014-02-04 14:15:21 +00002055 public boolean isOnCustomContent() {
2056 return mWorkspace.isOnOrMovingToCustomContent();
2057 }
2058
Winson Chung70d72102011-08-12 11:24:35 -07002059 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002060 public boolean onPrepareOptionsMenu(Menu menu) {
2061 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002062 if (mLauncherCallbacks != null) {
2063 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2064 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002065 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002066 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002068 @Override
2069 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002070 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002071 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002072 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002073 }
2074
Joe Onorato9c1289c2009-08-17 11:03:03 -04002075 public boolean isWorkspaceLocked() {
Sunny Goyal2100c782016-08-22 16:00:03 -07002076 return mWorkspaceLoading || mPendingRequestArgs != null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002077 }
2078
Adam Cohen517a7f52014-03-01 12:12:59 -08002079 public boolean isWorkspaceLoading() {
2080 return mWorkspaceLoading;
2081 }
2082
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002083 private void setWorkspaceLoading(boolean value) {
2084 boolean isLocked = isWorkspaceLocked();
2085 mWorkspaceLoading = value;
2086 if (isLocked != isWorkspaceLocked()) {
2087 onWorkspaceLockedChanged();
2088 }
2089 }
2090
Sunny Goyal2100c782016-08-22 16:00:03 -07002091 private void setWaitingForResult(PendingRequestArgs args) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002092 boolean isLocked = isWorkspaceLocked();
Sunny Goyal2100c782016-08-22 16:00:03 -07002093 mPendingRequestArgs = args;
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002094 if (isLocked != isWorkspaceLocked()) {
2095 onWorkspaceLockedChanged();
2096 }
2097 }
2098
Adam Cohen9211d422014-10-07 18:14:53 -07002099 protected void onWorkspaceLockedChanged() {
2100 if (mLauncherCallbacks != null) {
2101 mLauncherCallbacks.onWorkspaceLockedChanged();
2102 }
2103 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002104
Sunny Goyal2100c782016-08-22 16:00:03 -07002105 void addAppWidgetFromDropImpl(int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
2106 LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002107 if (LOGD) {
2108 Log.d(TAG, "Adding widget from drop");
2109 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002110 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2111 }
2112
Sunny Goyal2100c782016-08-22 16:00:03 -07002113 void addAppWidgetImpl(int appWidgetId, ItemInfo info,
2114 AppWidgetHostView boundWidget, LauncherAppWidgetProviderInfo appWidgetInfo,
Adam Cohen59400422014-03-05 18:07:04 -08002115 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002116 if (appWidgetInfo.configure != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002117 setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, appWidgetInfo, info));
Michael Jurkaaf442092010-06-10 17:01:57 -07002118
Bjorn Bringert7984c942009-12-09 15:38:25 +00002119 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002120 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2121 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002122 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002123 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002124 Runnable onComplete = new Runnable() {
2125 @Override
2126 public void run() {
2127 // Exit spring loaded mode if necessary after adding the widget
2128 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2129 null);
2130 }
2131 };
Sunny Goyal2100c782016-08-22 16:00:03 -07002132 completeAddAppWidget(appWidgetId, info, boundWidget, appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002133 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002134 }
2135 }
Romain Guycbb89e42009-06-08 15:52:54 -07002136
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002137 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002138 // Close any folders that may be open.
2139 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002140 mWorkspace.moveToCustomContentScreen(animate);
2141 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002142
2143 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2144 int[] cell, int spanX, int spanY) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002145 info.container = container;
2146 info.screenId = screenId;
2147 if (cell != null) {
2148 info.cellX = cell[0];
2149 info.cellY = cell[1];
2150 }
2151 info.spanX = spanX;
2152 info.spanY = spanY;
2153
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002154 switch (info.itemType) {
2155 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2156 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal2100c782016-08-22 16:00:03 -07002157 addAppWidgetFromDrop((PendingAddWidgetInfo) info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002158 break;
2159 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal2100c782016-08-22 16:00:03 -07002160 processShortcutFromDrop(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002161 break;
2162 default:
2163 throw new IllegalStateException("Unknown item type: " + info.itemType);
2164 }
2165 }
2166
Adam Cohenfbba09b2011-07-18 21:43:05 -07002167 /**
2168 * Process a shortcut drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002169 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002170 private void processShortcutFromDrop(PendingAddItemInfo info) {
Sunny Goyalfb5096d2016-09-08 14:32:06 -07002171 Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT).setComponent(info.componentName);
2172 setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_CREATE_SHORTCUT, intent, info));
2173 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002174 }
2175
Adam Cohenfbba09b2011-07-18 21:43:05 -07002176 /**
2177 * Process a widget drop.
Adam Cohenfbba09b2011-07-18 21:43:05 -07002178 */
Sunny Goyal2100c782016-08-22 16:00:03 -07002179 private void addAppWidgetFromDrop(PendingAddWidgetInfo info) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002180 AppWidgetHostView hostView = info.boundWidget;
2181 int appWidgetId;
2182 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002183 // In the case where we've prebound the widget, we remove it from the DragLayer
2184 if (LOGD) {
2185 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2186 }
2187 getDragLayer().removeView(hostView);
2188
Adam Cohened66b2b2012-01-23 17:28:51 -08002189 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002190 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002191
2192 // Clear the boundWidget so that it doesn't get destroyed.
2193 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002194 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002195 // In this case, we either need to start an activity to get permission to bind
2196 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002197 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002198 Bundle options = info.bindOptions;
2199
Sunny Goyalffe83f12014-08-14 17:39:34 -07002200 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2201 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002202 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002203 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002204 } else {
Sunny Goyal2100c782016-08-22 16:00:03 -07002205 setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, info.info, info));
Michael Jurka8b805b12012-04-18 14:23:14 -07002206 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2207 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2208 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyal2100c782016-08-22 16:00:03 -07002209 mAppWidgetManager.getUser(info.info)
Sunny Goyalffe83f12014-08-14 17:39:34 -07002210 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002211 // TODO: we need to make sure that this accounts for the options bundle.
2212 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002213 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2214 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002215 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002216 }
2217
Adam Cohendcd297f2013-06-18 13:13:40 -07002218 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002219 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002220 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002221 folderInfo.title = getText(R.string.folder_name);
2222
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002223 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002224 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2225 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002226
2227 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002228 FolderIcon newFolder =
2229 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002230 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002231 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002232 // Force measure the new folder icon
2233 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2234 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002235 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002236 }
Romain Guycbb89e42009-06-08 15:52:54 -07002237
Winsonc0b52fe2015-09-09 16:38:15 -07002238 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002239 * Unbinds the view for the specified item, and removes the item and all its children.
2240 *
2241 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002242 * @param itemInfo the {@link ItemInfo} for this view.
2243 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002244 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002245 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002246 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002247 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002248 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2249 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002250 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002251 } else {
2252 mWorkspace.removeWorkspaceItem(v);
2253 }
Winsonc0b52fe2015-09-09 16:38:15 -07002254 if (deleteFromDb) {
2255 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2256 }
2257 } else if (itemInfo instanceof FolderInfo) {
2258 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002259 if (v instanceof FolderIcon) {
2260 ((FolderIcon) v).removeListeners();
2261 }
Winsonc0b52fe2015-09-09 16:38:15 -07002262 mWorkspace.removeWorkspaceItem(v);
2263 if (deleteFromDb) {
2264 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2265 }
2266 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2267 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002268 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002269 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002270 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002271 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002272 }
2273 } else {
2274 return false;
2275 }
2276 return true;
2277 }
2278
2279 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002280 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002281 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002282 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002283 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002284 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002285 // Deleting an app widget ID is a void call but writes to disk before returning
2286 // to the caller...
2287 new AsyncTask<Void, Void, Void>() {
2288 public Void doInBackground(Void ... args) {
2289 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2290 return null;
2291 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002292 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002293 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002294 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002295 }
2296
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002297 @Override
2298 public boolean dispatchKeyEvent(KeyEvent event) {
2299 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2300 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002301 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002302 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002303 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002304 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002305 dumpState();
2306 return true;
2307 }
2308 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002309 }
2310 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2311 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002312 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002313 return true;
2314 }
2315 }
2316
2317 return super.dispatchKeyEvent(event);
2318 }
2319
Joe Onorato88ec0992009-11-19 13:16:06 -08002320 @Override
2321 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002322 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2323 return;
2324 }
2325
Sunny Goyal45478022015-06-08 16:52:41 -07002326 if (mDragController.isDragging()) {
2327 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002328 return;
2329 }
2330
Tony Wickham49c8d292016-07-01 11:44:34 -07002331 if (getOpenShortcutsContainer() != null) {
2332 closeShortcutsContainer();
2333 } else if (isAppsViewVisible()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002334 showWorkspace(true);
2335 } else if (isWidgetsViewVisible()) {
2336 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002337 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002338 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002339 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002340 Folder openFolder = mWorkspace.getOpenFolder();
2341 if (openFolder.isEditingName()) {
2342 openFolder.dismissEditingName();
2343 } else {
2344 closeFolder();
2345 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002346 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002347 mWorkspace.exitWidgetResizeMode();
2348
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002349 // Back button is a no-op here, but give at least some feedback for the button press
2350 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002351 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002352 }
2353
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002354 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002355 * Launches the intent referred by the clicked shortcut.
2356 *
2357 * @param v The view representing the clicked shortcut.
2358 */
2359 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002360 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2361 // view has detached (it's possible for this to happen if the view is removed mid touch).
2362 if (v.getWindowToken() == null) {
2363 return;
2364 }
2365
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002366 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002367 return;
2368 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002369
Adam Cohen1697b792013-09-17 19:08:21 -07002370 if (v instanceof Workspace) {
2371 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002372 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002373 }
2374 return;
2375 }
2376
2377 if (v instanceof CellLayout) {
2378 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002379 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2380 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002381 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002382 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002383 }
2384
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002385 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002386 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002387 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002388 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002389 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002390 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002391 }
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07002392 } else if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
2393 (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002394 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002395 } else if (tag instanceof AppInfo) {
2396 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002397 } else if (tag instanceof LauncherAppWidgetInfo) {
2398 if (v instanceof PendingAppWidgetHostView) {
2399 onClickPendingWidget((PendingAppWidgetHostView) v);
2400 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002401 }
2402 }
2403
Sunny Goyal70660032015-05-14 00:07:08 -07002404 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002405 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002406 return false;
2407 }
2408
Michael Jurkaaf442092010-06-10 17:01:57 -07002409 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002410 * Event handler for the app widget view which has not fully restored.
2411 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002412 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002413 if (mIsSafeModeEnabled) {
2414 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2415 return;
2416 }
2417
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002418 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002419 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002420 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2421 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2422 // This should not happen, as we make sure that an Id is allocated during bind.
2423 return;
2424 }
2425 LauncherAppWidgetProviderInfo appWidgetInfo =
2426 mAppWidgetManager.findProvider(info.providerName, info.user);
2427 if (appWidgetInfo != null) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002428 setWaitingForResult(PendingRequestArgs
2429 .forWidgetInfo(info.appWidgetId, appWidgetInfo, info));
Sunny Goyalff572272014-07-23 13:58:07 -07002430
Sunny Goyald478c832016-04-01 12:04:16 -07002431 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
Sunny Goyal2100c782016-08-22 16:00:03 -07002432 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, info.appWidgetId);
Sunny Goyald478c832016-04-01 12:04:16 -07002433 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
Sunny Goyal2100c782016-08-22 16:00:03 -07002434 mAppWidgetManager.getUser(appWidgetInfo)
Sunny Goyald478c832016-04-01 12:04:16 -07002435 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2436 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2437 }
2438 } else {
2439 LauncherAppWidgetProviderInfo appWidgetInfo =
2440 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2441 if (appWidgetInfo != null) {
2442 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2443 }
Sunny Goyalff572272014-07-23 13:58:07 -07002444 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002445 } else if (info.installProgress < 0) {
2446 // The install has not been queued
2447 final String packageName = info.providerName.getPackageName();
2448 showBrokenAppInstallDialog(packageName,
2449 new DialogInterface.OnClickListener() {
2450 public void onClick(DialogInterface dialog, int id) {
2451 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2452 }
2453 });
2454 } else {
2455 // Download has started.
2456 final String packageName = info.providerName.getPackageName();
2457 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002458 }
2459 }
2460
Sunny Goyald478c832016-04-01 12:04:16 -07002461 private void startRestoredWidgetReconfigActivity(
2462 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002463 setWaitingForResult(PendingRequestArgs.forWidgetInfo(info.appWidgetId, provider, info));
Sunny Goyald478c832016-04-01 12:04:16 -07002464 mAppWidgetManager.startConfigActivity(provider,
2465 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2466 }
2467
Sunny Goyalff572272014-07-23 13:58:07 -07002468 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002469 * Event handler for the "grid" button that appears on the home screen, which
2470 * enters all apps mode.
2471 *
2472 * @param v The view that was clicked.
2473 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002474 protected void onClickAllAppsButton(View v) {
2475 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002476 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002477 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.TAP,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002478 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002479 showAppsView(true /* animated */, true /* updatePredictedApps */,
2480 false /* focusSearchBar */);
Winson Chung76648c52015-07-10 14:33:23 -07002481 }
2482 }
2483
2484 protected void onLongClickAllAppsButton(View v) {
2485 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2486 if (!isAppsViewVisible()) {
Hyunyoung Song1ce0e302016-07-21 12:47:28 -07002487 getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.LONGPRESS,
Hyunyoung Song5aa27142016-07-21 11:48:37 -07002488 LauncherLogProto.ALL_APPS_BUTTON);
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07002489 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07002490 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002491 }
2492 }
2493
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002494 private void showBrokenAppInstallDialog(final String packageName,
2495 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002496 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002497 .setTitle(R.string.abandoned_promises_title)
2498 .setMessage(R.string.abandoned_promise_explanation)
2499 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2500 .setNeutralButton(R.string.abandoned_clean_this,
2501 new DialogInterface.OnClickListener() {
2502 public void onClick(DialogInterface dialog, int id) {
2503 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2504 mWorkspace.removeAbandonedPromise(packageName, user);
2505 }
2506 })
2507 .create().show();
2508 return;
2509 }
2510
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002511 /**
2512 * Event handler for an app shortcut click.
2513 *
2514 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2515 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002516 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002517 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2518 Object tag = v.getTag();
2519 if (!(tag instanceof ShortcutInfo)) {
2520 throw new IllegalArgumentException("Input must be a Shortcut");
2521 }
2522
2523 // Open shortcut
2524 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002525
2526 if (shortcut.isDisabled != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002527 if ((shortcut.isDisabled &
2528 ~ShortcutInfo.FLAG_DISABLED_SUSPENDED &
2529 ~ShortcutInfo.FLAG_DISABLED_QUIET_USER) == 0) {
2530 // If the app is only disabled because of the above flags, launch activity anyway.
2531 // Framework will tell the user why the app is suspended.
Kenny Guy44cba692016-01-21 19:50:02 +00002532 } else {
Tony Wickham4efffc52016-08-04 16:34:49 -07002533 if (!TextUtils.isEmpty(shortcut.disabledMessage)) {
2534 // Use a message specific to this shortcut, if it has one.
2535 Toast.makeText(this, shortcut.disabledMessage, Toast.LENGTH_SHORT).show();
2536 return;
2537 }
2538 // Otherwise just use a generic error message.
Kenny Guy44cba692016-01-21 19:50:02 +00002539 int error = R.string.activity_not_available;
2540 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2541 error = R.string.safemode_shortcut_error;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002542 } else if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_BY_PUBLISHER) != 0 ||
2543 (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_LOCKED_USER) != 0) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002544 error = R.string.shortcut_not_available;
Kenny Guy44cba692016-01-21 19:50:02 +00002545 }
2546 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2547 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002548 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002549 }
2550
Chris Wren40c5ed32014-06-24 18:24:23 -04002551 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002552 if ((v instanceof BubbleTextView)
2553 && shortcut.isPromise()
2554 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002555 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002556 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002557 new DialogInterface.OnClickListener() {
2558 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002559 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002560 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002561 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002562 return;
2563 }
2564
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002565 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002566 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002567 }
2568
Sunny Goyala7ce1662016-05-31 15:01:35 -07002569 private void startAppShortcutOrInfoActivity(View v) {
2570 ItemInfo item = (ItemInfo) v.getTag();
2571 Intent intent = item.getIntent();
2572 if (intent == null) {
2573 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002574 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002575 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002576 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002577
2578 if (success && v instanceof BubbleTextView) {
2579 mWaitingForResume = (BubbleTextView) v;
2580 mWaitingForResume.setStayPressed(true);
2581 }
2582 }
2583
2584 /**
2585 * Event handler for a folder icon click.
2586 *
2587 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2588 */
2589 protected void onClickFolderIcon(View v) {
2590 if (LOGD) Log.d(TAG, "onClickFolder");
2591 if (!(v instanceof FolderIcon)){
2592 throw new IllegalArgumentException("Input must be a FolderIcon");
2593 }
2594
2595 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002596 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002597 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002598 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002599 }
Michael Jurka5130e402011-10-13 04:55:35 -07002600 }
2601
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002602 /**
2603 * Event handler for the (Add) Widgets button that appears after a long press
2604 * on the home screen.
2605 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002606 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002607 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002608 if (mIsSafeModeEnabled) {
2609 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2610 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002611 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002612 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002613 }
2614
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002615 /**
2616 * Event handler for the wallpaper picker button that appears after a long press
2617 * on the home screen.
2618 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002619 public void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002620 if (!Utilities.isWallapaperAllowed(this)) {
2621 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2622 return;
2623 }
2624
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002625 String pickerPackage = getString(R.string.wallpaper_picker_package);
2626 if (TextUtils.isEmpty(pickerPackage)) {
2627 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2628 }
2629
Tony Wickham785f7a52015-08-31 17:28:32 -07002630 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2631 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002632
Sunny Goyal2100c782016-08-22 16:00:03 -07002633 setWaitingForResult(new PendingRequestArgs(new ItemInfo()));
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002634 Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER)
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002635 .setPackage(pickerPackage)
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002636 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset);
2637 intent.setSourceBounds(getViewBounds(v));
2638 startActivityForResult(intent, REQUEST_PICK_WALLPAPER, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002639 }
2640
2641 /**
2642 * Event handler for a click on the settings button that appears after a long press
2643 * on the home screen.
2644 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002645 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002646 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyalf3a09f92016-08-25 09:45:14 -07002647 Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
2648 .setPackage(getPackageName());
2649 intent.setSourceBounds(getViewBounds(v));
2650 startActivity(intent, getActivityLaunchOptions(v));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002651 }
2652
Adam Cohen61f560d2013-09-30 15:58:20 -07002653 public View.OnTouchListener getHapticFeedbackTouchListener() {
2654 if (mHapticFeedbackTouchListener == null) {
2655 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002656 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002657 @Override
2658 public boolean onTouch(View v, MotionEvent event) {
2659 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2660 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2661 }
2662 return false;
2663 }
2664 };
2665 }
2666 return mHapticFeedbackTouchListener;
2667 }
2668
Tony Wickhame2217252016-03-22 16:34:23 -07002669 @Override
2670 public void onAccessibilityStateChanged(boolean enabled) {
2671 mDragLayer.onAccessibilityStateChanged(enabled);
2672 }
2673
Sunny Goyal06e21a22016-08-11 16:02:02 -07002674 public void onDragStarted() {
Adam Cohen9211d422014-10-07 18:14:53 -07002675 if (isOnCustomContent()) {
2676 // Custom content screen doesn't participate in drag and drop. If on custom
2677 // content screen, move to default.
2678 moveWorkspaceToDefaultScreen();
2679 }
Adam Cohen9211d422014-10-07 18:14:53 -07002680 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002681
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002682 /**
2683 * Called when the user stops interacting with the launcher.
2684 * This implies that the user is now on the homescreen and is not doing housekeeping.
2685 */
Adam Cohen9211d422014-10-07 18:14:53 -07002686 protected void onInteractionEnd() {
2687 if (mLauncherCallbacks != null) {
2688 mLauncherCallbacks.onInteractionEnd();
2689 }
2690 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002691
2692 /**
2693 * Called when the user starts interacting with the launcher.
2694 * The possible interactions are:
2695 * - open all apps
2696 * - reorder an app shortcut, or a widget
2697 * - open the overview mode.
2698 * This is a good time to stop doing things that only make sense
2699 * when the user is on the homescreen and not doing housekeeping.
2700 */
Adam Cohen9211d422014-10-07 18:14:53 -07002701 protected void onInteractionBegin() {
2702 if (mLauncherCallbacks != null) {
2703 mLauncherCallbacks.onInteractionBegin();
2704 }
2705 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002706
Winson Chungcd99cd32015-04-29 11:03:24 -07002707 /** Updates the interaction state. */
2708 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002709 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002710 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002711 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2712 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002713 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002714 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002715 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002716 onInteractionEnd();
2717 }
2718 }
2719
Sunny Goyala7ce1662016-05-31 15:01:35 -07002720 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002721 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002722 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2723 try {
2724 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2725 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2726 // is enabled by default on NYC.
2727 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2728 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002729
2730 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2731 String id = ((ShortcutInfo) info).getDeepShortcutId();
2732 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002733 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2734 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002735 } else {
2736 // Could be launching some bookkeeping activity
2737 startActivity(intent, optsBundle);
2738 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002739 } finally {
2740 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002741 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002742 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002743 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2744 // corresponding permission. Show the appropriate permission prompt if that
2745 // is the case.
2746 if (intent.getComponent() == null
2747 && Intent.ACTION_CALL.equals(intent.getAction())
2748 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2749 PackageManager.PERMISSION_GRANTED) {
Sunny Goyal2100c782016-08-22 16:00:03 -07002750
2751 setWaitingForResult(PendingRequestArgs
2752 .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002753 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2754 REQUEST_PERMISSION_CALL_PHONE);
2755 } else {
2756 // No idea why this was thrown.
2757 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002758 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002759 }
2760 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002761
Sunny Goyala7ce1662016-05-31 15:01:35 -07002762 private Bundle getActivityLaunchOptions(View v) {
2763 if (Utilities.ATLEAST_MARSHMALLOW) {
2764 int left = 0, top = 0;
2765 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2766 if (v instanceof TextView) {
2767 // Launch from center of icon, not entire view
2768 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2769 if (icon != null) {
2770 Rect bounds = icon.getBounds();
2771 left = (width - bounds.width()) / 2;
2772 top = v.getPaddingTop();
2773 width = bounds.width();
2774 height = bounds.height();
2775 }
2776 }
2777 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2778 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2779 // On L devices, we use the device default slide-up transition.
2780 // On L MR1 devices, we use a custom version of the slide-up transition which
2781 // doesn't have the delay present in the device default.
2782 return ActivityOptions.makeCustomAnimation(
2783 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2784 }
2785 return null;
2786 }
2787
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002788 private Rect getViewBounds(View v) {
2789 int[] pos = new int[2];
2790 v.getLocationOnScreen(pos);
2791 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
2792 }
2793
Sunny Goyala7ce1662016-05-31 15:01:35 -07002794 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002795 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2796 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2797 return false;
2798 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002799 // Only launch using the new animation if the shortcut has not opted out (this is a
2800 // private contract between launcher and may be ignored in the future).
2801 boolean useLaunchAnimation = (v != null) &&
2802 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2803 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2804
2805 UserHandleCompat user = null;
2806 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2807 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2808 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002809 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002810
2811 // Prepare intent
2812 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2813 if (v != null) {
Sunny Goyal0eca4e22016-07-20 11:43:06 -07002814 intent.setSourceBounds(getViewBounds(v));
Sunny Goyala7ce1662016-05-31 15:01:35 -07002815 }
2816 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002817 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2818 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07002819 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
2820 && ((ShortcutInfo) item).promisedIntent == null) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002821 // Shortcuts need some special checks due to legacy reasons.
2822 startShortcutIntentSafely(intent, optsBundle, item);
2823 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2824 // Could be launching some bookkeeping activity
2825 startActivity(intent, optsBundle);
2826 } else {
2827 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2828 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2829 }
2830 return true;
2831 } catch (ActivityNotFoundException|SecurityException e) {
2832 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2833 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2834 }
2835 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002836 }
2837
Adam Cohen268c4752012-06-06 17:47:33 -07002838 /**
2839 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2840 * in the DragLayer in the exact absolute location of the original FolderIcon.
2841 */
2842 private void copyFolderIconToImage(FolderIcon fi) {
2843 final int width = fi.getMeasuredWidth();
2844 final int height = fi.getMeasuredHeight();
2845
2846 // Lazy load ImageView, Bitmap and Canvas
2847 if (mFolderIconImageView == null) {
2848 mFolderIconImageView = new ImageView(this);
2849 }
2850 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2851 mFolderIconBitmap.getHeight() != height) {
2852 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2853 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2854 }
2855
2856 DragLayer.LayoutParams lp;
2857 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2858 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2859 } else {
2860 lp = new DragLayer.LayoutParams(width, height);
2861 }
2862
Adam Cohen307fe232012-08-16 17:55:58 -07002863 // The layout from which the folder is being opened may be scaled, adjust the starting
2864 // view size by this scale factor.
2865 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002866 lp.customPosition = true;
2867 lp.x = mRectForFolderAnimation.left;
2868 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002869 lp.width = (int) (scale * width);
2870 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002871
2872 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2873 fi.draw(mFolderIconCanvas);
2874 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002875 if (fi.getFolder() != null) {
2876 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2877 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002878 }
Adam Cohen268c4752012-06-06 17:47:33 -07002879 // Just in case this image view is still in the drag layer from a previous animation,
2880 // we remove it and re-add it.
2881 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2882 mDragLayer.removeView(mFolderIconImageView);
2883 }
2884 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002885 if (fi.getFolder() != null) {
2886 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002887 }
Adam Cohen268c4752012-06-06 17:47:33 -07002888 }
2889
Adam Cohen37b2a492016-04-06 18:36:06 -07002890 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002891 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002892 FolderInfo info = (FolderInfo) fi.getTag();
2893 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2894 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002895 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2896 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002897 }
2898
Adam Cohen268c4752012-06-06 17:47:33 -07002899 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2900 copyFolderIconToImage(fi);
2901 fi.setVisibility(View.INVISIBLE);
2902
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002903 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2904 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002905 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002906 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2907 }
2908 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002909 oa.start();
2910 }
2911
Sunny Goyal935fca12015-10-13 10:19:01 -07002912 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002913 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002914 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002915
Adam Cohen268c4752012-06-06 17:47:33 -07002916 // We remove and re-draw the FolderIcon in-case it has changed
2917 mDragLayer.removeView(mFolderIconImageView);
2918 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08002919
2920 if (cl != null) {
2921 cl.clearFolderLeaveBehind();
2922 }
2923
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002924 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002925 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002926 oa.addListener(new AnimatorListenerAdapter() {
2927 @Override
2928 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002929 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07002930 // Remove the ImageView copy of the FolderIcon and make the original visible.
2931 mDragLayer.removeView(mFolderIconImageView);
2932 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002933 }
2934 }
2935 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002936 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07002937 if (!animate) {
2938 oa.end();
2939 }
Adam Cohen2801caf2011-05-13 20:57:39 -07002940 }
2941
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002942 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002943 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002944 * is animated relative to the specified View. If the View is null, no animation
2945 * is played.
2946 *
Sunny Goyal08442b82015-10-21 17:15:08 -07002947 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002948 */
Adam Cohen37b2a492016-04-06 18:36:06 -07002949 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07002950
Adam Cohenfb91f302012-06-11 15:45:18 -07002951 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07002952 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
2953 if (openFolder != null && openFolder != folder) {
2954 // Close any open folder before opening a folder.
2955 closeFolder();
2956 }
2957
Adam Cohena9cf38f2011-05-02 15:36:58 -07002958 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002959
Adam Cohena9cf38f2011-05-02 15:36:58 -07002960 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002961
Sunny Goyalf4066152015-04-15 09:42:19 -07002962 // While the folder is open, the position of the icon cannot change.
2963 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
2964
Adam Cohen4554ee12011-08-03 16:13:21 -07002965 // Just verify that the folder hasn't already been added to the DragLayer.
2966 // There was a one-off crash where the folder had a parent already.
2967 if (folder.getParent() == null) {
2968 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07002969 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07002970 } else {
2971 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2972 folder.getParent() + ").");
2973 }
Adam Cohen37b2a492016-04-06 18:36:06 -07002974 folder.animateOpen();
2975
2976 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002977
2978 // Notify the accessibility manager that this folder "window" has appeared and occluded
2979 // the workspace items
2980 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2981 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002982 }
2983
2984 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07002985 closeFolder(true);
2986 }
2987
2988 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08002989 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002990 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002991 if (folder.isEditingName()) {
2992 folder.dismissEditingName();
2993 }
Sunny Goyal935fca12015-10-13 10:19:01 -07002994 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07002995 }
2996 }
2997
Sunny Goyal935fca12015-10-13 10:19:01 -07002998 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07002999 animate &= !Utilities.isPowerSaverOn(this);
3000
Adam Cohen4554ee12011-08-03 16:13:21 -07003001 folder.getInfo().opened = false;
3002
3003 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3004 if (parent != null) {
3005 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003006 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003007 if (fi != null) {
3008 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3009 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003010 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003011 if (animate) {
3012 folder.animateClosed();
3013 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003014 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003015 }
Winson Chung83ca4802013-04-12 15:10:52 -07003016
Sunny Goyal935fca12015-10-13 10:19:01 -07003017 // Notify the accessibility manager that this folder "window" has disappeared and no
3018 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003019 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003020 }
3021
Tony Wickham49c8d292016-07-01 11:44:34 -07003022 public void closeShortcutsContainer() {
Sunny Goyala2454ad2016-07-22 10:50:11 -07003023 closeShortcutsContainer(true);
3024 }
3025
3026 public void closeShortcutsContainer(boolean animate) {
Tony Wickham49c8d292016-07-01 11:44:34 -07003027 DeepShortcutsContainer deepShortcutsContainer = getOpenShortcutsContainer();
3028 if (deepShortcutsContainer != null) {
Sunny Goyala2454ad2016-07-22 10:50:11 -07003029 if (animate) {
3030 deepShortcutsContainer.animateClose();
3031 } else {
3032 deepShortcutsContainer.close();
3033 }
Tony Wickham49c8d292016-07-01 11:44:34 -07003034 }
3035 }
3036
Sunny Goyal52851aa2016-09-02 10:41:43 -07003037 public View getTopFloatingView() {
3038 View topView = getOpenShortcutsContainer();
3039 if (topView == null) {
3040 topView = getWorkspace().getOpenFolder();
3041 }
3042 return topView;
3043 }
3044
Tony Wickham49c8d292016-07-01 11:44:34 -07003045 /**
3046 * @return The open shortcuts container, or null if there is none
3047 */
3048 public DeepShortcutsContainer getOpenShortcutsContainer() {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07003049 // Iterate in reverse order. Shortcuts container is added later to the dragLayer,
3050 // and will be one of the last views.
3051 for (int i = mDragLayer.getChildCount() - 1; i >= 0; i--) {
3052 View child = mDragLayer.getChildAt(i);
Sunny Goyala2454ad2016-07-22 10:50:11 -07003053 if (child instanceof DeepShortcutsContainer
3054 && ((DeepShortcutsContainer) child).isOpen()) {
Sunny Goyal71b3d1c2016-07-20 17:42:05 -07003055 return (DeepShortcutsContainer) child;
3056 }
3057 }
3058 return null;
Tony Wickham49c8d292016-07-01 11:44:34 -07003059 }
3060
Tony Wickhamdadb3042016-02-24 11:07:00 -08003061 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003062 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003063 if (!isDraggingEnabled()) return false;
3064 if (isWorkspaceLocked()) return false;
3065 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003066
Hyunyoung Song68f98ac2016-07-22 14:22:49 -07003067 if ((FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && v instanceof PageIndicator) ||
3068 (v == mAllAppsButton && mAllAppsButton != null)) {
Winson Chung76648c52015-07-10 14:33:23 -07003069 onLongClickAllAppsButton(v);
3070 return true;
3071 }
3072
Adam Cohen1697b792013-09-17 19:08:21 -07003073 if (v instanceof Workspace) {
3074 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003075 if (!mWorkspace.isTouchActive()) {
3076 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003077 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3078 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3079 return true;
3080 } else {
3081 return false;
3082 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003083 } else {
3084 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003085 }
Adam Cohen1697b792013-09-17 19:08:21 -07003086 }
3087
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003088 CellLayout.CellInfo longClickCellInfo = null;
3089 View itemUnderLongClick = null;
3090 if (v.getTag() instanceof ItemInfo) {
3091 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003092 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003093 itemUnderLongClick = longClickCellInfo.cell;
Sunny Goyal2100c782016-08-22 16:00:03 -07003094 mPendingRequestArgs = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003095 }
3096
Winson Chung3d503fb2011-07-13 17:25:49 -07003097 // The hotseat touch handling does not go through Workspace, and we always allow long press
3098 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003099 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003100 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003101 // User long pressed on empty space
3102 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3103 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003104 if (mWorkspace.isInOverviewMode()) {
3105 mWorkspace.startReordering(v);
3106 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003107 showOverviewMode(true);
Hyunyoung Song645764e2016-06-06 14:19:02 -07003108 mHotseat.requestDisallowInterceptTouchEvent(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003109 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003110 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003111 final boolean isAllAppsButton =
3112 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3113 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3114 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003115 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003116 // User long pressed on an item
Sunny Goyal94b510c2016-08-16 15:36:48 -07003117 mWorkspace.startDrag(longClickCellInfo, new DragOptions());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003118 }
3119 }
3120 }
3121 return true;
3122 }
3123
Winson Chung3d503fb2011-07-13 17:25:49 -07003124 boolean isHotseatLayout(View layout) {
3125 return mHotseat != null && layout != null &&
3126 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3127 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003128
Winson Chung3d503fb2011-07-13 17:25:49 -07003129 /**
3130 * Returns the CellLayout of the specified container at the specified screen.
3131 */
Sunny Goyal92820592015-03-02 11:31:35 -08003132 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003133 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3134 if (mHotseat != null) {
3135 return mHotseat.getLayout();
3136 } else {
3137 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003138 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003139 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003140 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003141 }
3142 }
3143
Winson Chungb745afb2015-03-02 11:51:23 -08003144 /**
3145 * For overridden classes.
3146 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003147 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003148 return isAppsViewVisible();
3149 }
3150
3151 public boolean isAppsViewVisible() {
3152 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3153 }
3154
3155 public boolean isWidgetsViewVisible() {
3156 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003157 }
3158
Michael Jurkae326f182011-11-21 14:05:46 -08003159 @Override
3160 public void onTrimMemory(int level) {
3161 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003162 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3163 // The widget preview db can result in holding onto over
3164 // 3MB of memory for caching which isn't necessary.
3165 SQLiteDatabase.releaseMemory();
3166
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003167 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003168 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003169 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003170 if (mLauncherCallbacks != null) {
3171 mLauncherCallbacks.onTrimMemory(level);
3172 }
Michael Jurkae326f182011-11-21 14:05:46 -08003173 }
3174
Winson5c6bdbb2015-09-03 11:36:19 -07003175 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003176 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003177 }
3178
Winson5c6bdbb2015-09-03 11:36:19 -07003179 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003180 boolean changed = mState != State.WORKSPACE ||
3181 mWorkspace.getState() != Workspace.State.NORMAL;
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003182 if (changed || mAllAppsController.isTransitioning()) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003183 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003184 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003185 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003186
Michael Jurkab3e22d92011-10-31 15:58:33 -07003187 // Set focus to the AppsCustomize button
3188 if (mAllAppsButton != null) {
3189 mAllAppsButton.requestFocus();
3190 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003191 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003192
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003193 // Change the state *after* we've called all the transition code
3194 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003195
Winson Chung5fb63472011-02-02 17:03:37 -08003196 // Resume the auto-advance of widgets
3197 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003198 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003199
Winson Chung0f785722015-04-08 10:27:49 -07003200 if (changed) {
3201 // Send an accessibility event to announce the context change
3202 getWindow().getDecorView()
3203 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003204 }
Winson5c6bdbb2015-09-03 11:36:19 -07003205 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003206 }
3207
Winsone9f27272015-10-13 10:47:51 -07003208 /**
3209 * Shows the overview button.
3210 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07003211 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003212 showOverviewMode(animated, false);
3213 }
3214
3215 /**
3216 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3217 * onto one of the overview panel buttons.
3218 */
3219 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3220 Runnable postAnimRunnable = null;
3221 if (requestButtonFocus) {
3222 postAnimRunnable = new Runnable() {
3223 @Override
3224 public void run() {
3225 // Hitting the menu button when in touch mode does not trigger touch mode to
3226 // be disabled, so if requested, force focus on one of the overview panel
3227 // buttons.
3228 mOverviewPanel.requestFocusFromTouch();
3229 }
3230 };
3231 }
Adam Cohened307df2013-10-02 09:37:31 -07003232 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003233 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003234 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003235 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003236 }
3237
Winson Chungb745afb2015-03-02 11:51:23 -08003238 /**
3239 * Shows the apps view.
3240 */
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003241 public void showAppsView(boolean animated, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003242 boolean focusSearchBar) {
Hyunyoung Songc001cf52016-07-21 17:32:43 -07003243 markAppsViewShown();
Winson Chung4ac30062015-05-08 17:34:17 -07003244 if (updatePredictedApps) {
3245 tryAndUpdatePredictedApps();
3246 }
Winson Chung76648c52015-07-10 14:33:23 -07003247 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003248 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003249
Winson Chungb745afb2015-03-02 11:51:23 -08003250 /**
3251 * Shows the widgets view.
3252 */
3253 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003254 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003255 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003256 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003257 }
Winson Chung76648c52015-07-10 14:33:23 -07003258 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003259
3260 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003261 @Override
3262 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003263 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003264 }
3265 });
Winson Chungb745afb2015-03-02 11:51:23 -08003266 }
3267
3268 /**
3269 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003270 *
3271 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003272 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003273 // TODO: calling method should use the return value so that when {@code false} is returned
3274 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003275 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Song9dcf0a32016-07-27 10:55:51 -07003276 if (!(mState == State.WORKSPACE ||
3277 mState == State.APPS_SPRING_LOADED ||
3278 mState == State.WIDGETS_SPRING_LOADED ||
3279 (mState == State.APPS && mAllAppsController.isTransitioning()))) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003280 return false;
3281 }
3282 if (toState != State.APPS && toState != State.WIDGETS) {
3283 return false;
3284 }
Winson Chungb745afb2015-03-02 11:51:23 -08003285
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003286 // This is a safe and supported transition to bypass spring_loaded mode.
3287 if (mExitSpringLoadedModeRunnable != null) {
3288 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3289 mExitSpringLoadedModeRunnable = null;
3290 }
3291
Winson Chungb745afb2015-03-02 11:51:23 -08003292 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003293 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3294 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003295 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003296 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003297 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003298
Michael Jurkab3e22d92011-10-31 15:58:33 -07003299 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003300 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003301
3302 // Pause the auto-advance of widgets until we are out of AllApps
3303 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003304 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003305 closeFolder();
Tony Wickham49c8d292016-07-01 11:44:34 -07003306 closeShortcutsContainer();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003307
3308 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003309 getWindow().getDecorView()
3310 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003311 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003312 }
3313
Winson Chungcd99cd32015-04-29 11:03:24 -07003314 /**
3315 * Updates the workspace and interaction state on state change, and return the animation to this
3316 * new state.
3317 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003318 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003319 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003320 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003321 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003322 updateInteraction(fromState, toState);
3323 return anim;
3324 }
3325
Hyunyoung Song3f471442015-04-08 19:01:34 -07003326 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003327 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003328 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003329 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003330 }
Winson Chungb745afb2015-03-02 11:51:23 -08003331
Winson Chung006ee262015-08-03 14:40:11 -07003332 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003333 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003334 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003335
3336 if (isAppsViewVisible()) {
3337 mState = State.APPS_SPRING_LOADED;
3338 } else if (isWidgetsViewVisible()) {
3339 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003340 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003341 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003342 } else {
3343 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003344 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003345 }
Adam Cohen7777d962011-08-18 18:58:38 -07003346
Hyunyoung Song3f471442015-04-08 19:01:34 -07003347 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003348 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003349 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003350
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003351 if (mExitSpringLoadedModeRunnable != null) {
3352 mHandler.removeCallbacks(mExitSpringLoadedModeRunnable);
3353 }
3354 mExitSpringLoadedModeRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003355 @Override
3356 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003357 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003358 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3359 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003360 // Before we show workspace, hide all apps again because
3361 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3362 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003363 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003364 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003365 } else {
3366 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003367 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003368 mExitSpringLoadedModeRunnable = null;
Winson Chung557d6ed2011-07-08 15:34:52 -07003369 }
Hyunyoung Song06ca7562016-07-29 13:03:54 -07003370 };
3371 mHandler.postDelayed(mExitSpringLoadedModeRunnable, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003372 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003373
Tony Wickhame0c33232016-02-08 11:37:04 -08003374 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003375 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3376 || mState == State.WIDGETS_SPRING_LOADED;
3377 }
3378
Michael Jurkad3ef3062010-11-23 16:23:58 -08003379 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003380 if (mState == State.APPS_SPRING_LOADED) {
Hyunyoung Songdd60ce42016-07-27 17:08:38 -07003381 showAppsView(true /* animated */,
Winson Chung76648c52015-07-10 14:33:23 -07003382 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003383 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003384 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003385 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3386 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003387 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003388 }
3389
Winson Chung4ac30062015-05-08 17:34:17 -07003390 /**
3391 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3392 * resumed.
3393 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003394 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003395 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003396 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003397 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003398 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003399 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003400 }
3401 }
3402 }
3403
Michael Jurkab3e22d92011-10-31 15:58:33 -07003404 void lockAllApps() {
3405 // TODO
3406 }
3407
3408 void unlockAllApps() {
3409 // TODO
3410 }
3411
Michael Jurkad7c28052012-04-27 15:43:36 -07003412 @Override
3413 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003414 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003415 final List<CharSequence> text = event.getText();
3416 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003417 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003418 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003419 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003420 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003421 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003422 } else if (mWorkspace != null) {
3423 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003424 } else {
3425 text.add(getString(R.string.all_apps_home_button_label));
3426 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003427 return result;
3428 }
3429
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003430 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003431 * If the activity is currently paused, signal that we need to run the passed Runnable
3432 * in onResume.
3433 *
3434 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003435 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3436 * wrong when we're not running, and if the activity comes back to what the configuration was
3437 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003438 *
3439 * Implementation of the method from LauncherModel.Callbacks.
3440 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003441 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003442 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003443 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003444 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003445 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003446 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003447 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003448 }
3449 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003450 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003451 return true;
3452 } else {
3453 return false;
3454 }
3455 }
3456
Michael Jurkac402cd92013-05-20 15:49:32 +02003457 private boolean waitUntilResume(Runnable run) {
3458 return waitUntilResume(run, false);
3459 }
3460
Michael Jurka1e2f4652013-07-08 18:03:46 -07003461 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003462 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003463 }
3464
Michael Jurka7607c2f2013-04-03 14:33:19 -07003465 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003466 * If the activity is currently paused, signal that we need to re-run the loader
3467 * in onResume.
3468 *
3469 * This needs to be called from incoming places where resources might have been loaded
3470 * while we are paused. That is becaues the Configuration might be wrong
3471 * when we're not running, and if it comes back to what it was when we
3472 * were paused, we are not restarted.
3473 *
3474 * Implementation of the method from LauncherModel.Callbacks.
3475 *
3476 * @return true if we are currently paused. The caller might be able to
3477 * skip some work in that case since we will come back again.
3478 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003479 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003480 public boolean setLoadOnResume() {
3481 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003482 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003483 mOnResumeNeedsLoad = true;
3484 return true;
3485 } else {
3486 return false;
3487 }
3488 }
3489
3490 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003491 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003492 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003493 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003494 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003495 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003496 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003497 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003498 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003499 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003500 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003501
Joe Onorato9c1289c2009-08-17 11:03:03 -04003502 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003503 * Clear any pending bind callbacks. This is called when is loader is planning to
3504 * perform a full rebind from scratch.
3505 */
3506 @Override
3507 public void clearPendingBinds() {
3508 mBindOnResumeCallbacks.clear();
3509 if (mPendingExecutor != null) {
3510 mPendingExecutor.markCompleted();
3511 mPendingExecutor = null;
3512 }
3513 }
3514
3515 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003516 * Refreshes the shortcuts shown on the workspace.
3517 *
3518 * Implementation of the method from LauncherModel.Callbacks.
3519 */
3520 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003521 if (LauncherAppState.PROFILE_STARTUP) {
3522 Trace.beginSection("Starting page bind");
3523 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003524 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003525
Winson Chungd64d1762013-08-20 14:37:16 -07003526 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003527 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003528 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003529
Michael Jurka05bf644e2011-11-30 20:28:53 -08003530 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003531 if (mHotseat != null) {
3532 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003533 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003534 if (LauncherAppState.PROFILE_STARTUP) {
3535 Trace.endSection();
3536 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003537 }
3538
Adam Cohendcd297f2013-06-18 13:13:40 -07003539 @Override
3540 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003541 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003542 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3543 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003544 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3545 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3546 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003547 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3548 // If there are no screens, we need to have an empty screen
3549 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003550 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003551 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003552
3553 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003554 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003555 if (hasCustomContentToLeft()) {
3556 mWorkspace.createCustomContentContainer();
3557 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003558 }
Winsonc7d2e832016-07-28 12:24:55 -07003559
3560 // After we have added all the screens, if the wallpaper was locked to the default state,
3561 // then notify to indicate that it can be released and a proper wallpaper offset can be
3562 // computed before the next layout
3563 mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
Winson Chung64359a52013-07-08 17:17:08 -07003564 }
3565
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003566 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003567 int count = orderedScreenIds.size();
3568 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003569 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003570 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003571 // No need to bind the first screen, as its always bound.
3572 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3573 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003574 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003575 }
3576
Winson Chungd64d1762013-08-20 14:37:16 -07003577 public void bindAppsAdded(final ArrayList<Long> newScreens,
3578 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003579 final ArrayList<ItemInfo> addAnimated,
3580 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003581 Runnable r = new Runnable() {
3582 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003583 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003584 }
3585 };
3586 if (waitUntilResume(r)) {
3587 return;
3588 }
3589
Winson Chungd64d1762013-08-20 14:37:16 -07003590 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003591 if (newScreens != null) {
3592 bindAddScreens(newScreens);
3593 }
Winson Chungd64d1762013-08-20 14:37:16 -07003594
3595 // We add the items without animation on non-visible pages, and with
3596 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003597 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003598 bindItems(addNotAnimated, 0,
3599 addNotAnimated.size(), false);
3600 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003601 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003602 bindItems(addAnimated, 0,
3603 addAnimated.size(), true);
3604 }
Winson Chungc58497e2013-09-03 17:48:37 -07003605
Winson Chung87412982013-10-03 18:34:14 -07003606 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003607 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003608
Winson Chungb745afb2015-03-02 11:51:23 -08003609 if (addedApps != null && mAppsView != null) {
3610 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003611 }
Winson Chungd64d1762013-08-20 14:37:16 -07003612 }
3613
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003614 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003615 * Bind the items start-end from the list.
3616 *
3617 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003618 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003619 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003620 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3621 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003622 Runnable r = new Runnable() {
3623 public void run() {
3624 bindItems(shortcuts, start, end, forceAnimateIcons);
3625 }
3626 };
3627 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003628 return;
3629 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003630
Winson Chungf0c6ae02012-03-21 16:10:31 -07003631 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003632 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3633 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003634 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003635 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003636 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003637 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003638 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003639
3640 // Short circuit if we are loading dock items for a configuration which has no dock
3641 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3642 mHotseat == null) {
3643 continue;
3644 }
3645
Sunny Goyal639e9062015-08-19 19:17:06 -07003646 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003647 switch (item.itemType) {
3648 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3649 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003650 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003651 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003652 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003653 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003654 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003655 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003656 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003657 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003658 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003659 default:
3660 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003661 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003662
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003663 /*
3664 * Remove colliding items.
3665 */
3666 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3667 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3668 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3669 View v = cl.getChildAt(item.cellX, item.cellY);
3670 Object tag = v.getTag();
3671 String desc = "Collision while binding workspace item: " + item
3672 + ". Collides with " + tag;
3673 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3674 throw (new RuntimeException(desc));
3675 } else {
3676 Log.d(TAG, desc);
3677 LauncherModel.deleteItemFromDatabase(this, item);
3678 continue;
3679 }
3680 }
3681 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003682 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3683 item.cellY, 1, 1);
3684 if (animateIcons) {
3685 // Animate all the applications up now
3686 view.setAlpha(0f);
3687 view.setScaleX(0f);
3688 view.setScaleY(0f);
3689 bounceAnims.add(createNewAppBounceAnimation(view, i));
3690 newShortcutsScreenId = item.screenId;
3691 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003692 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003693
Winson Chung997a9232013-07-24 15:33:46 -07003694 if (animateIcons) {
3695 // Animate to the correct page
3696 if (newShortcutsScreenId > -1) {
3697 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003698 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003699 final Runnable startBounceAnimRunnable = new Runnable() {
3700 public void run() {
3701 anim.playTogether(bounceAnims);
3702 anim.start();
3703 }
3704 };
Winson Chung997a9232013-07-24 15:33:46 -07003705 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003706 // We post the animation slightly delayed to prevent slowdowns
3707 // when we are loading right after we return to launcher.
3708 mWorkspace.postDelayed(new Runnable() {
3709 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003710 if (mWorkspace != null) {
3711 mWorkspace.snapToPage(newScreenIndex);
3712 mWorkspace.postDelayed(startBounceAnimRunnable,
3713 NEW_APPS_ANIMATION_DELAY);
3714 }
Winson Chung94d67682013-09-25 16:29:40 -07003715 }
3716 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003717 } else {
3718 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003719 }
3720 }
Winson Chung64359a52013-07-08 17:17:08 -07003721 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003722 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003723 }
3724
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003725 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3726 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3727 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003728 view.updateAppWidget(null);
3729 view.setOnClickListener(this);
3730 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003731 mWorkspace.requestLayout();
3732 }
3733
Joe Onorato9c1289c2009-08-17 11:03:03 -04003734 /**
3735 * Add the views for a widget to the workspace.
3736 *
3737 * Implementation of the method from LauncherModel.Callbacks.
3738 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003739 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003740 Runnable r = new Runnable() {
3741 public void run() {
3742 bindAppWidget(item);
3743 }
3744 };
3745 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003746 return;
3747 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003748
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003749 if (mIsSafeModeEnabled) {
3750 bindSafeModeWidget(item);
3751 return;
3752 }
3753
Daniel Sandler843e8602010-06-07 14:59:01 -04003754 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3755 if (DEBUG_WIDGETS) {
3756 Log.d(TAG, "bindAppWidget: " + item);
3757 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003758
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003759 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003760
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003761 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3762 // If the provider is not ready, bind as a pending widget.
3763 appWidgetInfo = null;
3764 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3765 // The widget id is not valid. Try to find the widget based on the provider info.
3766 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3767 } else {
3768 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3769 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003770
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003771 // If the provider is ready, but the width is not yet restored, try to restore it.
3772 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3773 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003774 if (appWidgetInfo == null) {
3775 if (DEBUG_WIDGETS) {
3776 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3777 + " belongs to component " + item.providerName
3778 + ", as the povider is null");
3779 }
3780 LauncherModel.deleteItemFromDatabase(this, item);
3781 return;
3782 }
Sunny Goyalff572272014-07-23 13:58:07 -07003783
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003784 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003785 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003786 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3787 // Id has not been allocated yet. Allocate a new id.
3788 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3789 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003790
Sunny Goyald478c832016-04-01 12:04:16 -07003791 // Also try to bind the widget. If the bind fails, the user will be shown
3792 // a click to setup UI, which will ask for the bind permission.
3793 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3794 pendingInfo.spanX = item.spanX;
3795 pendingInfo.spanY = item.spanY;
3796 pendingInfo.minSpanX = item.minSpanX;
3797 pendingInfo.minSpanY = item.minSpanY;
3798 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyal86df1382016-08-10 15:03:22 -07003799
3800 boolean isDirectConfig =
3801 item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
3802 if (isDirectConfig && item.bindOptions != null) {
3803 Bundle newOptions = item.bindOptions.getExtras();
3804 if (options != null) {
3805 newOptions.putAll(options);
3806 }
3807 options = newOptions;
3808 }
Sunny Goyald478c832016-04-01 12:04:16 -07003809 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3810 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003811
Sunny Goyal86df1382016-08-10 15:03:22 -07003812 // We tried to bind once. If we were not able to bind, we would need to
3813 // go through the permission dialog, which means we cannot skip the config
3814 // activity.
3815 item.bindOptions = null;
3816 item.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG;
3817
Sunny Goyald478c832016-04-01 12:04:16 -07003818 // Bind succeeded
3819 if (success) {
3820 // If the widget has a configure activity, it is still needs to set it up,
3821 // otherwise the widget is ready to go.
Sunny Goyal86df1382016-08-10 15:03:22 -07003822 item.restoreStatus = (appWidgetInfo.configure == null) || isDirectConfig
Sunny Goyald478c832016-04-01 12:04:16 -07003823 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3824 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003825 }
Sunny Goyald478c832016-04-01 12:04:16 -07003826
3827 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003828 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003829 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003830 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003831 // The widget was marked as UI not ready, but there is no configure activity to
3832 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003833 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3834 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003835 }
Sunny Goyalff572272014-07-23 13:58:07 -07003836 }
3837
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003838 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003839 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003840 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3841 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003842 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003843
Sunny Goyal56c73602015-09-25 12:55:01 -07003844 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003845 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003846 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003847 deleteWidgetInfo(item);
3848 return;
3849 }
3850
Sunny Goyal233ee962015-08-03 13:05:01 -07003851 item.minSpanX = appWidgetInfo.minSpanX;
3852 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003853 addAppWidgetToWorkspace(
3854 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3855 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003856 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003857 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003858 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003859 view.updateAppWidget(null);
3860 view.setOnClickListener(this);
3861 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003862 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003863 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003864
Daniel Sandler843e8602010-06-07 14:59:01 -04003865 if (DEBUG_WIDGETS) {
3866 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3867 + (SystemClock.uptimeMillis()-start) + "ms");
3868 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003869 }
3870
Sunny Goyalff572272014-07-23 13:58:07 -07003871 /**
3872 * Restores a pending widget.
3873 *
3874 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003875 */
Sunny Goyald478c832016-04-01 12:04:16 -07003876 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003877 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3878 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3879 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003880 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003881 }
3882
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003883 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003884 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003885
3886 mWorkspace.reinflateWidgetsIfNecessary();
3887 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003888 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003889 }
3890
Adam Cohen1462de32012-07-24 22:34:36 -07003891 public void onPageBoundSynchronously(int page) {
3892 mSynchronouslyBoundPages.add(page);
3893 }
3894
Sunny Goyal527c7d32015-08-28 15:19:36 -07003895 @Override
3896 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3897 if (mPendingExecutor != null) {
3898 mPendingExecutor.markCompleted();
3899 }
3900 mPendingExecutor = executor;
3901 executor.attachTo(this);
3902 }
3903
3904 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3905 if (mPendingExecutor == executor) {
3906 mPendingExecutor = null;
3907 }
3908 }
3909
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07003910 @Override
3911 public void finishFirstPageBind(final ViewOnDrawExecutor executor) {
3912 Runnable r = new Runnable() {
3913 public void run() {
3914 finishFirstPageBind(executor);
3915 }
3916 };
3917 if (waitUntilResume(r)) {
3918 return;
3919 }
3920
3921 Runnable onComplete = new Runnable() {
3922 @Override
3923 public void run() {
3924 if (executor != null) {
3925 executor.onLoadAnimationCompleted();
3926 }
3927 }
3928 };
3929 if (mDragLayer.getAlpha() < 1) {
3930 mDragLayer.animate().alpha(1).withEndAction(onComplete).start();
3931 } else {
3932 onComplete.run();
3933 }
3934 }
3935
Joe Onorato9c1289c2009-08-17 11:03:03 -04003936 /**
3937 * Callback saying that there aren't any more items to bind.
3938 *
3939 * Implementation of the method from LauncherModel.Callbacks.
3940 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003941 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003942 Runnable r = new Runnable() {
3943 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003944 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003945 }
3946 };
3947 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003948 return;
3949 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003950 if (LauncherAppState.PROFILE_STARTUP) {
3951 Trace.beginSection("Page bind completed");
3952 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003953 if (mSavedState != null) {
3954 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003955 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003956 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003957
Joe Onorato9c1289c2009-08-17 11:03:03 -04003958 mSavedState = null;
3959 }
3960
Adam Cohen1462de32012-07-24 22:34:36 -07003961 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003962
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003963 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003964
Sunny Goyal2100c782016-08-22 16:00:03 -07003965 if (mPendingActivityResult != null) {
3966 handleActivityResult(mPendingActivityResult.requestCode,
3967 mPendingActivityResult.resultCode, mPendingActivityResult.data);
3968 mPendingActivityResult = null;
Adam Cohendb364c32014-05-20 14:23:40 -07003969 }
3970
Sunny Goyal756adbc2015-04-16 15:20:51 -07003971 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003972
3973 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003974 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003975 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003976 if (LauncherAppState.PROFILE_STARTUP) {
3977 Trace.endSection();
3978 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003979 }
3980
Winson Chunga2413752012-04-03 14:22:34 -07003981 private boolean canRunNewAppsAnimation() {
3982 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07003983 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07003984 }
3985
Winson Chungc9168342013-06-26 14:54:55 -07003986 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003987 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07003988 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3989 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07003990 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07003991 return bounceAnim;
3992 }
3993
Adam Cohen27772732013-11-11 14:00:56 +00003994 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07003995 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00003996 }
3997
Tony Wickham55616cd2015-09-23 14:55:17 -07003998 public int getSearchBarHeight() {
3999 if (mLauncherCallbacks != null) {
4000 return mLauncherCallbacks.getSearchBarHeight();
4001 }
4002 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4003 }
4004
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004005 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004006 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4007 * multiple calls to bind the same list.)
4008 */
4009 @Thunk ArrayList<AppInfo> mTmpAppsList;
4010 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4011 public void run() {
4012 bindAllApplications(mTmpAppsList);
4013 mTmpAppsList = null;
4014 }
4015 };
4016
4017 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004018 * Add the icons for all apps.
4019 *
4020 * Implementation of the method from LauncherModel.Callbacks.
4021 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004022 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004023 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4024 mTmpAppsList = apps;
4025 return;
4026 }
4027
Winson Chungb745afb2015-03-02 11:51:23 -08004028 if (mAppsView != null) {
4029 mAppsView.setApps(apps);
4030 }
Adam Cohen9211d422014-10-07 18:14:53 -07004031 if (mLauncherCallbacks != null) {
4032 mLauncherCallbacks.bindAllApplications(apps);
4033 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004034 }
4035
4036 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004037 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
4038 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
4039 */
4040 @Override
4041 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
4042 mDeepShortcutMap = deepShortcutMapCopy;
4043 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
4044 }
4045
Tony Wickham1bce7fd2016-04-28 17:39:03 -07004046 public List<String> getShortcutIdsForItem(ItemInfo info) {
4047 if (!DeepShortcutManager.supportsShortcuts(info)) {
4048 return Collections.EMPTY_LIST;
4049 }
4050 ComponentName component = info.getTargetComponent();
Hyunyoung Songd3bf9802016-08-29 14:43:53 -07004051 if (component == null) {
4052 return Collections.EMPTY_LIST;
4053 }
4054
Tony Wickham1bce7fd2016-04-28 17:39:03 -07004055 List<String> ids = mDeepShortcutMap.get(new ComponentKey(component, info.user));
4056 return ids == null ? Collections.EMPTY_LIST : ids;
4057 }
4058
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004059 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004060 * A package was updated.
4061 *
4062 * Implementation of the method from LauncherModel.Callbacks.
4063 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004064 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004065 Runnable r = new Runnable() {
4066 public void run() {
4067 bindAppsUpdated(apps);
4068 }
4069 };
4070 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004071 return;
4072 }
4073
Winson Chungb745afb2015-03-02 11:51:23 -08004074 if (mAppsView != null) {
4075 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004076 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004077 }
4078
Sunny Goyal4390ace2014-10-13 11:33:11 -07004079 @Override
4080 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4081 Runnable r = new Runnable() {
4082 public void run() {
4083 bindWidgetsRestored(widgets);
4084 }
4085 };
4086 if (waitUntilResume(r)) {
4087 return;
4088 }
4089 mWorkspace.widgetsRestored(widgets);
4090 }
4091
Joe Onorato9c1289c2009-08-17 11:03:03 -04004092 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004093 * Some shortcuts were updated in the background.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004094 * Implementation of the method from LauncherModel.Callbacks.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004095 *
4096 * @param updated list of shortcuts which have changed.
4097 * @param removed list of shortcuts which were deleted in the background. This can happen when
4098 * an app gets removed from the system or some of its components are no longer
4099 * available.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004100 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004101 @Override
4102 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4103 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004104 Runnable r = new Runnable() {
4105 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004106 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004107 }
4108 };
4109 if (waitUntilResume(r)) {
4110 return;
4111 }
4112
Sunny Goyal4390ace2014-10-13 11:33:11 -07004113 if (!updated.isEmpty()) {
4114 mWorkspace.updateShortcuts(updated);
4115 }
4116
4117 if (!removed.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004118 HashSet<ComponentName> removedComponents = new HashSet<>();
4119 HashSet<ShortcutKey> removedDeepShortcuts = new HashSet<>();
4120
Sunny Goyal4390ace2014-10-13 11:33:11 -07004121 for (ShortcutInfo si : removed) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004122 if (si.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07004123 removedDeepShortcuts.add(ShortcutKey.fromShortcutInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004124 } else {
4125 removedComponents.add(si.getTargetComponent());
4126 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004127 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004128
4129 if (!removedComponents.isEmpty()) {
4130 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(removedComponents, user);
4131 mWorkspace.removeItemsByMatcher(matcher);
4132 mDragController.onAppsRemoved(matcher);
4133 }
4134
4135 if (!removedDeepShortcuts.isEmpty()) {
4136 ItemInfoMatcher matcher = ItemInfoMatcher.ofShortcutKeys(removedDeepShortcuts);
4137 mWorkspace.removeItemsByMatcher(matcher);
4138 mDragController.onAppsRemoved(matcher);
4139 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004140 }
4141 }
4142
4143 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004144 * Update the state of a package, typically related to install state.
4145 *
4146 * Implementation of the method from LauncherModel.Callbacks.
4147 */
Sunny Goyale755d462014-07-22 13:48:29 -07004148 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004149 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4150 Runnable r = new Runnable() {
4151 public void run() {
4152 bindRestoreItemsChange(updates);
4153 }
4154 };
4155 if (waitUntilResume(r)) {
4156 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004157 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004158
Sunny Goyal756adbc2015-04-16 15:20:51 -07004159 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004160 }
4161
4162 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004163 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004164 * in addition to specific applications to remove, the reason being that
4165 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004166 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004167 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004168 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004169 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004170 public void bindWorkspaceComponentsRemoved(
4171 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4172 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004173 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004174 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004175 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004176 }
Winson Chungd64d1762013-08-20 14:37:16 -07004177 };
4178 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004179 return;
4180 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004181 if (!packageNames.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004182 ItemInfoMatcher matcher = ItemInfoMatcher.ofPackages(packageNames, user);
4183 mWorkspace.removeItemsByMatcher(matcher);
4184 mDragController.onAppsRemoved(matcher);
4185
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004186 }
4187 if (!components.isEmpty()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07004188 ItemInfoMatcher matcher = ItemInfoMatcher.ofComponents(components, user);
4189 mWorkspace.removeItemsByMatcher(matcher);
4190 mDragController.onAppsRemoved(matcher);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004191 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004192 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004193
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004194 @Override
4195 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4196 Runnable r = new Runnable() {
4197 public void run() {
4198 bindAppInfosRemoved(appInfos);
4199 }
4200 };
4201 if (waitUntilResume(r)) {
4202 return;
Joe Onorato36115782010-06-17 13:28:48 -04004203 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004204
Winson Chungdf95eb12013-10-16 14:57:07 -07004205 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004206 if (mAppsView != null) {
4207 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004208 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004209 }
Joe Onoratobe386092009-11-17 17:32:16 -08004210
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004211 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004212 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004213 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004214 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004215 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004216
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004217 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004218 public void bindWidgetsModel(WidgetsModel model) {
4219 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004220 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004221 return;
4222 }
4223
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004224 if (mWidgetsView != null && model != null) {
4225 mWidgetsView.addWidgets(model);
4226 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004227 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004228 }
4229
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004230 @Override
4231 public void notifyWidgetProvidersChanged() {
4232 if (mWorkspace.getState().shouldUpdateWidget) {
4233 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4234 }
4235 }
4236
Winson Chung400438b2011-01-16 17:53:48 -08004237 private int mapConfigurationOriActivityInfoOri(int configOri) {
4238 final Display d = getWindowManager().getDefaultDisplay();
4239 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4240 switch (d.getRotation()) {
4241 case Surface.ROTATION_0:
4242 case Surface.ROTATION_180:
4243 // We are currently in the same basic orientation as the natural orientation
4244 naturalOri = configOri;
4245 break;
4246 case Surface.ROTATION_90:
4247 case Surface.ROTATION_270:
4248 // We are currently in the other basic orientation to the natural orientation
4249 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4250 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4251 break;
4252 }
4253
4254 int[] oriMap = {
4255 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4256 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4257 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4258 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4259 };
4260 // Since the map starts at portrait, we need to offset if this device's natural orientation
4261 // is landscape.
4262 int indexOffset = 0;
4263 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4264 indexOffset = 1;
4265 }
4266 return oriMap[(d.getRotation() + indexOffset) % 4];
4267 }
Adam Cohen446e9402011-09-15 18:21:21 -07004268
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004269 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004270 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004271 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004272 if (Utilities.ATLEAST_JB_MR2) {
4273 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4274 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004275 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4276 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004277 }
Winson Chung4b919f82012-05-01 10:44:08 -07004278 }
4279 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004280
Winson Chung4b919f82012-05-01 10:44:08 -07004281 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004282 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004283 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004284 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004285 } else {
4286 mHandler.postDelayed(new Runnable() {
4287 public void run() {
4288 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4289 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004290 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004291 }
Winson Chung4b919f82012-05-01 10:44:08 -07004292 }
Winson Chung400438b2011-01-16 17:53:48 -08004293 }
4294
Hyunyoung Songc001cf52016-07-21 17:32:43 -07004295 private void markAppsViewShown() {
4296 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4297 return;
4298 }
4299 mSharedPrefs.edit().putBoolean(APPS_VIEW_SHOWN, true).apply();
4300 }
4301
4302 private boolean shouldShowDiscoveryBounce() {
4303 if (mState != mState.WORKSPACE) {
4304 return false;
4305 }
4306 if (mLauncherCallbacks != null && mLauncherCallbacks.shouldShowDiscoveryBounce()) {
4307 return true;
4308 }
4309 if (!mIsResumeFromActionScreenOff) {
4310 return false;
4311 }
4312 if (mSharedPrefs.getBoolean(APPS_VIEW_SHOWN, false)) {
4313 return false;
4314 }
4315 return true;
4316 }
4317
Winson Chung5ffd51d2015-06-25 11:36:50 -07004318 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004319 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004320 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004321 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004322 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004323 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004324 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4325 if (userHandle != null) {
4326 user = UserHandleCompat.fromUser(userHandle);
4327 }
4328 }
4329 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004330 }
4331
4332 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004333 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004334 if (user == null) {
4335 user = UserHandleCompat.myUserHandle();
4336 }
4337
4338 // Called from search suggestion, add the profile extra to the intent to ensure that we
4339 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004340 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004341 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004342 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004343 return null;
4344 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004345 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004346 }
4347
Winson Chung5ffd51d2015-06-25 11:36:50 -07004348 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004349 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4350 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004351 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004352 UserHandleCompat.myUserHandle());
4353 }
4354
Winson Chung5ffd51d2015-06-25 11:36:50 -07004355 protected void moveWorkspaceToDefaultScreen() {
4356 mWorkspace.moveToDefaultScreen(false);
4357 }
4358
Winson Chung80baf5a2010-08-09 16:03:15 -07004359 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004360 * Returns a FastBitmapDrawable with the icon, accurately sized.
4361 */
4362 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4363 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4364 d.setFilterBitmap(true);
4365 resizeIconDrawable(d);
4366 return d;
4367 }
4368
4369 /**
4370 * Resizes an icon drawable to the correct icon size.
4371 */
Winson5fbe0742015-09-30 15:33:00 -07004372 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004373 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004374 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004375 }
4376
4377 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004378 * Prints out out state for debugging.
4379 */
4380 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004381 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004382 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004383 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
Sunny Goyal2100c782016-08-22 16:00:03 -07004384 Log.d(TAG, "mPendingRequestArgs=" + mPendingRequestArgs);
4385 Log.d(TAG, "mPendingActivityResult=" + mPendingActivityResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004386 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004387 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004388
Daniel Sandler325dc232013-06-05 22:57:57 -04004389 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004390 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004391
4392 @Override
4393 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4394 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004395 // Dump workspace
4396 writer.println(prefix + "Workspace Items");
4397 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4398 writer.println(prefix + " Homescreen " + i);
4399
4400 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4401 for (int j = 0; j < layout.getChildCount(); j++) {
4402 Object tag = layout.getChildAt(j).getTag();
4403 if (tag != null) {
4404 writer.println(prefix + " " + tag.toString());
4405 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004406 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004407 }
Sunny Goyala1365452015-10-01 15:46:24 -07004408
4409 writer.println(prefix + " Hotseat");
4410 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4411 for (int j = 0; j < layout.getChildCount(); j++) {
4412 Object tag = layout.getChildAt(j).getTag();
4413 if (tag != null) {
4414 writer.println(prefix + " " + tag.toString());
4415 }
4416 }
4417
Sunny Goyal713edfc2016-05-06 09:58:34 -07004418 try {
4419 FileLog.flushAll(writer);
4420 } catch (Exception e) {
4421 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004422 }
4423
Adam Cohen9211d422014-10-07 18:14:53 -07004424 if (mLauncherCallbacks != null) {
4425 mLauncherCallbacks.dump(prefix, fd, writer, args);
4426 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004427 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004428
Sunny Goyal66b24572016-09-21 15:57:55 -07004429 @Override
4430 @TargetApi(Build.VERSION_CODES.N)
4431 public void onProvideKeyboardShortcuts(
4432 List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
4433
4434 ArrayList<KeyboardShortcutInfo> shortcutInfos = new ArrayList<>();
4435 if (mState == State.WORKSPACE) {
4436 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.all_apps_button_label),
4437 KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON));
4438 }
4439 View currentFocus = getCurrentFocus();
4440 if (new CustomActionsPopup(this, currentFocus).canShow()) {
4441 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.custom_actions),
4442 KeyEvent.KEYCODE_O, KeyEvent.META_CTRL_ON));
4443 }
4444 if (currentFocus instanceof BubbleTextView &&
4445 ((BubbleTextView) currentFocus).hasDeepShortcuts()) {
4446 shortcutInfos.add(new KeyboardShortcutInfo(getString(R.string.action_deep_shortcut),
4447 KeyEvent.KEYCODE_S, KeyEvent.META_CTRL_ON));
4448 }
4449 if (!shortcutInfos.isEmpty()) {
4450 data.add(new KeyboardShortcutGroup(getString(R.string.home_screen), shortcutInfos));
4451 }
4452
4453 super.onProvideKeyboardShortcuts(data, menu, deviceId);
4454 }
4455
4456 @Override
4457 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
4458 if (event.hasModifiers(KeyEvent.META_CTRL_ON)) {
4459 switch (keyCode) {
4460 case KeyEvent.KEYCODE_A:
4461 if (mState == State.WORKSPACE) {
4462 showAppsView(true, true, false);
4463 return true;
4464 }
4465 break;
4466 case KeyEvent.KEYCODE_S: {
4467 View focusedView = getCurrentFocus();
4468 if (focusedView instanceof BubbleTextView
4469 && focusedView.getTag() instanceof ItemInfo
4470 && mAccessibilityDelegate.performAction(focusedView,
4471 (ItemInfo) focusedView.getTag(),
4472 LauncherAccessibilityDelegate.DEEP_SHORTCUTS)) {
4473 getOpenShortcutsContainer().requestFocus();
4474 return true;
4475 }
4476 break;
4477 }
4478 case KeyEvent.KEYCODE_O:
4479 if (new CustomActionsPopup(this, getCurrentFocus()).show()) {
4480 return true;
4481 }
4482 break;
4483 }
4484 }
4485 return super.onKeyShortcut(keyCode, event);
4486 }
4487
Adam Cohen59400422014-03-05 18:07:04 -08004488 public static CustomAppWidget getCustomAppWidget(String name) {
4489 return sCustomAppWidgets.get(name);
4490 }
4491
4492 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4493 return sCustomAppWidgets;
4494 }
4495
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004496 public static Launcher getLauncher(Context context) {
4497 if (context instanceof Launcher) {
4498 return (Launcher) context;
4499 }
4500 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4501 }
4502
Sunny Goyal745bad92016-05-02 10:54:12 -07004503 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4504
4505 @Override
4506 public void onSharedPreferenceChanged(
4507 SharedPreferences sharedPreferences, String key) {
4508 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4509 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4510 if (!waitUntilResume(this, true)) {
4511 run();
4512 }
4513 }
4514 }
4515
4516 @Override
4517 public void run() {
4518 setOrientation();
4519 }
4520 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004521}