blob: 717b05998cf18bf4efd9f5091ea830331ff841ec [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyal28c6b962015-10-12 11:42:05 -070019import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070025import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000026import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080037import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Sunny Goyal756cd262015-08-20 12:33:21 -070039import android.content.ContentValues;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070041import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040042import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070044import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070045import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070046import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070047import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070048import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.pm.PackageManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070051import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070052import android.graphics.Bitmap;
53import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070054import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070055import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070057import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020060import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080062import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070063import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070064import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070066import android.os.Trace;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070067import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.HapticFeedbackConstants;
75import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070077import android.view.MotionEvent;
78import android.view.Surface;
79import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070080import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080081import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.view.ViewGroup;
83import android.view.ViewTreeObserver;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080085import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070086import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070087import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080091import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070093
Sunny Goyal651077b2014-06-30 14:15:31 -070094import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070095import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyalae502842016-06-17 08:43:56 -070096import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070097import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070098import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080099import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700100import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100101import com.android.launcher3.compat.LauncherActivityInfoCompat;
102import com.android.launcher3.compat.LauncherAppsCompat;
103import com.android.launcher3.compat.UserHandleCompat;
104import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800105import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700106import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700107import com.android.launcher3.dragndrop.DragController;
108import com.android.launcher3.dragndrop.DragLayer;
109import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -0700110import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000111import com.android.launcher3.folder.Folder;
112import com.android.launcher3.folder.FolderIcon;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700113import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700114import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700115import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700116import com.android.launcher3.model.WidgetsModel;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700117import com.android.launcher3.pageindicators.PageIndicator;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700118import com.android.launcher3.shortcuts.DeepShortcutManager;
Tony Wickham49c8d292016-07-01 11:44:34 -0700119import com.android.launcher3.shortcuts.DeepShortcutsContainer;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700120import com.android.launcher3.util.ComponentKey;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700121import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700122import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal322d5562015-06-25 19:35:49 -0700123import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700124import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700125import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700126import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700127import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700128import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700129
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700130import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700131import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700132import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700133import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800134import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700135import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700136import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700137import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700138
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139/**
140 * Default launcher application.
141 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100142public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700143 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
144 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700145 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700146 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700147 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700149 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700150 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400151 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700154 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700155 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700156 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
Michael Jurka8b805b12012-04-18 14:23:14 -0700158 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700159 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700160 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700161
Sunny Goyal28c6b962015-10-12 11:42:05 -0700162 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
163
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700164 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
165 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
166 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
167
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700168 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
169
Mathew Inwood876a8462013-06-14 14:12:41 +0100170 /**
171 * IntentStarter uses request codes starting with this. This must be greater than all activity
172 * request codes used internally.
173 */
174 protected static final int REQUEST_LAST = 100;
175
Michael Jurka0a457bf2012-11-19 14:05:05 -0800176 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700177 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800178 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
Winson Chung2672ff92012-05-04 16:22:30 -0700180 // The Intent extra that defines whether to ignore the launch animation
181 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400182 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700183
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700184 public static final String ACTION_APPWIDGET_HOST_RESET =
185 "com.android.launcher3.intent.ACTION_APPWIDGET_HOST_RESET";
186
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 // Type: int
188 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700189 // Type: int
190 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700191 // Type: Content Values / parcelable
192 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700193 // Type: parcelable
194 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000195 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800196 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197
Adam Cohen432609a2014-03-13 17:03:22 -0700198 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800199 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
200
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700201 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700202 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
203 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700204
Adam Cohen091440a2015-03-18 14:16:05 -0700205 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700206 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700207
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700208 private boolean mIsSafeModeEnabled;
209
Joe Onorato9c1289c2009-08-17 11:03:03 -0400210 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800211 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700212 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700213 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214
Winson Chunga2413752012-04-03 14:22:34 -0700215 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700216 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
217 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700218 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700219
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700220 private final BroadcastReceiver mUiBroadcastReceiver = new BroadcastReceiver() {
221
222 @Override
223 public void onReceive(Context context, Intent intent) {
224 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
225 closeSystemDialogs();
226 } else if (ACTION_APPWIDGET_HOST_RESET.equals(intent.getAction())) {
227 if (mAppWidgetHost != null) {
228 mAppWidgetHost.startListening();
229 }
230 }
231 }
232 };
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800233
Adam Cohen091440a2015-03-18 14:16:05 -0700234 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700235 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700236 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800237 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700238
239 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700240
Sunny Goyalffe83f12014-08-14 17:39:34 -0700241 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700242 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700243
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700244 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800245 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800246 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700247
Michael Jurka0280c3b2010-09-17 15:00:07 -0700248 private int[] mTmpAddItemCellCoordinates = new int[2];
249
Sunny Goyal316490e2015-06-02 09:38:28 -0700250 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800251 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700252
Michael Jurka838a4ca2011-02-07 13:33:06 -0800253 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700254 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700255
Sunny Goyal47328fd2016-05-25 18:56:41 -0700256 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700257
258 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700259 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700260 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700261
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700262 // Main container view and the model for the widget tray screen.
263 @Thunk WidgetsContainerView mWidgetsView;
264 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700265
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700267 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
268 // scroll issues (because the workspace may not have been measured yet) and extra work.
269 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
270 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271
272 private SpannableStringBuilder mDefaultKeySsb = null;
273
Adam Cohen091440a2015-03-18 14:16:05 -0700274 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400275
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800276 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277 private boolean mRestoring;
278 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700279 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280
Michael Jurka1e2f4652013-07-08 18:03:46 -0700281 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700282 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700283 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700284
Joe Onorato9c1289c2009-08-17 11:03:03 -0400285 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800286 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700287 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700288 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Adam Cohen091440a2015-03-18 14:16:05 -0700289 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800290 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700291 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800292 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400293
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700294 /** Maps launcher activity components to their list of shortcut ids. */
295 private MultiHashMap<ComponentKey, String> mDeepShortcutMap = new MultiHashMap<>();
296
Adam Cohen61f560d2013-09-30 15:58:20 -0700297 private View.OnTouchListener mHapticFeedbackTouchListener;
298
Adam Cohended9f8d2010-11-03 13:25:16 -0700299 // Related to the auto-advancing of widgets
300 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700301 private static final int ADVANCE_INTERVAL = 20000;
302 private static final int ADVANCE_STAGGER = 250;
303
304 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700305 private long mAutoAdvanceSentTime;
306 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700307 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700308
Winson Chung400438b2011-01-16 17:53:48 -0800309 // Determines how long to wait after a rotation before restoring the screen orientation to
310 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700311 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800312
Adam Cohen091440a2015-03-18 14:16:05 -0700313 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700314
Adam Cohen1462de32012-07-24 22:34:36 -0700315 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700316 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700317
Winson Chung46353de2012-02-16 14:05:10 -0800318 // We only want to get the SharedPreferences once since it does an FS stat each time we get
319 // it from the context.
320 private SharedPreferences mSharedPrefs;
321
Winson Chungf0c6ae02012-03-21 16:10:31 -0700322 // Holds the page that we need to animate to, and the icon views that we need to animate up
323 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700324 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700325 private Bitmap mFolderIconBitmap;
326 private Canvas mFolderIconCanvas;
327 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700328
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700329 private DeviceProfile mDeviceProfile;
330
Adam Cohen4b66bf32015-09-18 12:15:19 -0700331 private boolean mMoveToDefaultScreenFromNewIntent;
332
Winson Chung13eb5272015-05-11 16:30:13 -0700333 // This is set to the view that launched the activity that navigated the user away from
334 // launcher. Since there is no callback for when the activity has finished launching, enable
335 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800336 private BubbleTextView mWaitingForResume;
337
Adam Cohen59400422014-03-05 18:07:04 -0800338 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
339 new HashMap<String, CustomAppWidget>();
340
Adam Cohen59400422014-03-05 18:07:04 -0800341 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700342 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
343 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800344 }
345 }
346
Adam Cohen091440a2015-03-18 14:16:05 -0700347 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700348 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700349 if (mWorkspace != null) {
350 mWorkspace.buildPageHardwareLayers();
351 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700352 }
353 };
354
Adam Cohendb364c32014-05-20 14:23:40 -0700355 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800356
Adam Cohen091440a2015-03-18 14:16:05 -0700357 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800358 int requestCode;
359 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700360 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700361 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800362 int cellX;
363 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700364 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800365 }
366
Hyunyoung Songaa953652016-04-19 18:30:24 -0700367 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800368
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700369 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700370 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400371
372 @Thunk void setOrientation() {
373 if (mRotationEnabled) {
374 unlockScreenOrientation(true);
375 } else {
376 setRequestedOrientation(
377 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700378 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400379 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700380
Sunny Goyal745bad92016-05-02 10:54:12 -0700381 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700382
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 @Override
384 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700385 if (DEBUG_STRICT_MODE) {
386 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
387 .detectDiskReads()
388 .detectDiskWrites()
389 .detectNetwork() // or .detectAll() for all detectable problems
390 .penaltyLog()
391 .build());
392 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
393 .detectLeakedSqlLiteObjects()
394 .detectLeakedClosableObjects()
395 .penaltyLog()
396 .penaltyDeath()
397 .build());
398 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700399 if (LauncherAppState.PROFILE_STARTUP) {
400 Trace.beginSection("Launcher-onCreate");
401 }
Winson Chunga2413752012-04-03 14:22:34 -0700402
Adam Cohen9211d422014-10-07 18:14:53 -0700403 if (mLauncherCallbacks != null) {
404 mLauncherCallbacks.preOnCreate();
405 }
406
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400408
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400409 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700410
Adam Cohen2e6da152015-05-06 11:42:25 -0700411 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700412 mDeviceProfile = getResources().getConfiguration().orientation
413 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700414 app.getInvariantDeviceProfile().landscapeProfile
415 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700416
Sunny Goyalf7258242015-10-19 16:59:07 -0700417 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700418 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800419 mModel = app.setLauncher(this);
420 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700421 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700422
Joe Onorato41a12d22009-10-31 18:30:00 -0400423 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700424 mAllAppsController = new AllAppsTransitionController(this);
425 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700426
Sunny Goyalffe83f12014-08-14 17:39:34 -0700427 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700428
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700429 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
430 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700431
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700432 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
433 // this also ensures that any synchronous binding below doesn't re-trigger another
434 // LauncherModel load.
435 mPaused = false;
436
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700438
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700440 mDeviceProfile.layout(this);
Tony Wickham827cef22016-03-17 15:39:39 -0700441 mExtractedColors = new ExtractedColors();
442 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443
Tony Wickhame2217252016-03-22 16:34:23 -0700444 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
445 .addAccessibilityStateChangeListener(this);
446
Joe Onorato7c312c12009-08-13 21:36:53 -0700447 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700448
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 mSavedState = savedInstanceState;
450 restoreState(mSavedState);
451
Sunny Goyale26d1002016-06-20 14:52:14 -0700452 if (LauncherAppState.PROFILE_STARTUP) {
453 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 }
455
456 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700457 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700458 // If the user leaves launcher, then we should just load items asynchronously when
459 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700460 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700461 } else {
462 // We only load the page synchronously if the user rotates (or triggers a
463 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700464 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700465 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 }
467
468 // For handling default keys
469 mDefaultKeySsb = new SpannableStringBuilder();
470 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800471
472 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700473 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
474 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800475
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800476 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700477 // In case we are on a device with locked rotation, we should look at preferences to check
478 // if the user has specifically allowed rotation.
479 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700480 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700481 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
482 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700483 }
484
485 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
486 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400487 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700488
Adam Cohen9211d422014-10-07 18:14:53 -0700489 if (mLauncherCallbacks != null) {
490 mLauncherCallbacks.onCreate(savedInstanceState);
491 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700492
493 if (shouldShowIntroScreen()) {
494 showIntroScreen();
495 } else {
496 showFirstRunActivity();
Adam Cohenc3e12c72014-10-31 16:15:36 -0700497 }
Adam Cohen9211d422014-10-07 18:14:53 -0700498 }
499
Sunny Goyal7779d622015-06-11 16:18:39 -0700500 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700501 public void onExtractedColorsChanged() {
502 loadExtractedColorsAndColorItems();
503 }
504
505 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700506 // TODO: do this in pre-N as well, once the extraction part is complete.
507 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700508 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700509 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700510 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham827cef22016-03-17 15:39:39 -0700511 }
512 }
513
Adam Cohen9211d422014-10-07 18:14:53 -0700514 private LauncherCallbacks mLauncherCallbacks;
515
516 public void onPostCreate(Bundle savedInstanceState) {
517 super.onPostCreate(savedInstanceState);
518 if (mLauncherCallbacks != null) {
519 mLauncherCallbacks.onPostCreate(savedInstanceState);
520 }
521 }
522
Sunny Goyal32554d12015-12-03 15:31:25 -0800523 /**
524 * Call this after onCreate to set or clear overlay.
525 */
526 public void setLauncherOverlay(LauncherOverlay overlay) {
527 if (overlay != null) {
528 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
529 }
530 mWorkspace.setLauncherOverlay(overlay);
531 }
532
Adam Cohen9211d422014-10-07 18:14:53 -0700533 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
534 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700535 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700536 private boolean mWorkspaceImportanceStored = false;
537 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700538 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800539 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700540 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
541
542 @Override
543 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700544 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700545 return;
546 }
547 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700548 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700549 if (mWorkspace != null) {
550 mWorkspaceImportanceForAccessibility =
551 mWorkspace.getImportantForAccessibility();
552 mWorkspace.setImportantForAccessibility(
553 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
554 mWorkspaceImportanceStored = true;
555 }
556 if (mHotseat != null) {
557 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
558 mHotseat.setImportantForAccessibility(
559 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
560 mHotseatImportanceStored = true;
561 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700562 }
563
564 @Override
565 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700566 if (mWorkspaceImportanceStored && mWorkspace != null) {
567 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
568 }
569 if (mHotseatImportanceStored && mHotseat != null) {
570 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
571 }
572 mWorkspaceImportanceStored = false;
573 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700574 }
575 });
Adam Cohen9211d422014-10-07 18:14:53 -0700576 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700577 }
578
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700579 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700580 public void onLauncherProviderChange() {
581 if (mLauncherCallbacks != null) {
582 mLauncherCallbacks.onLauncherProviderChange();
583 }
584 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700585
Adam Cohen9211d422014-10-07 18:14:53 -0700586 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700587 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700588 if (mLauncherCallbacks != null) {
589 return mLauncherCallbacks.hasCustomContentToLeft();
590 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700591 return false;
592 }
593
Dave Hawkeya8881582013-09-17 15:55:33 -0600594 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500595 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600596 * {@link #addToCustomContentPage}. This will only be invoked if
597 * {@link #hasCustomContentToLeft()} is {@code true}.
598 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500599 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700600 if (mLauncherCallbacks != null) {
601 mLauncherCallbacks.populateCustomContentContainer();
602 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600603 }
604
605 /**
606 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
607 * ensure the custom content page is added or removed if necessary.
608 */
609 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600610 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600611 // Not bound yet, wait for bindScreens to be called.
612 return;
613 }
614
615 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
616 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500617 mWorkspace.createCustomContentContainer();
618 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600619 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
620 mWorkspace.removeCustomContentPage();
621 }
622 }
623
Hyunyoung Songaa953652016-04-19 18:30:24 -0700624 public UserEventDispatcher getUserEventDispatcher() {
625 if (mLauncherCallbacks != null) {
626 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
627 if (dispatcher != null) {
628 return dispatcher;
629 }
630 }
631
Sunny Goyal64976d52016-06-20 14:56:28 -0700632 // Logger object is a singleton and does not have to be coupled with the foreground
633 // activity. Since most user event logging is done on the UI, the object is retrieved
634 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700635 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700636 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700637 }
638 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800639 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100640
Hyunyoung Song3f471442015-04-08 19:01:34 -0700641 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700642 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
643 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700644 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700645 }
646
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000647 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700648 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
649 // This cast is safe as long as the id < 0x00FFFFFF
650 // Since we jail all the dynamically generated views, there should be no clashes
651 // with any other views.
652 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000653 }
654
655 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700656 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
657 * a configuration step, this allows the proper animations to run after other transitions.
658 */
Adam Cohendb364c32014-05-20 14:23:40 -0700659 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700660 long screenId = args.screenId;
661 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
662 // When the screen id represents an actual screen (as opposed to a rank) we make sure
663 // that the drop page actually exists.
664 screenId = ensurePendingDropLayoutExists(args.screenId);
665 }
Adam Cohendb364c32014-05-20 14:23:40 -0700666
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800667 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800668 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700669 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700670 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700671 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800672 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700673 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700674 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700675 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyald478c832016-04-01 12:04:16 -0700676 completeRestoreAppWidget(args.appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700677 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700678 case REQUEST_BIND_PENDING_APPWIDGET: {
679 int widgetId = args.appWidgetId;
680 LauncherAppWidgetInfo info =
681 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
682 if (info != null) {
683 // Since the view was just bound, also launch the configure activity if needed
684 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
685 .getLauncherAppWidgetInfo(widgetId);
686 if (provider != null && provider.configure != null) {
687 startRestoredWidgetReconfigActivity(provider, info);
688 }
689 }
690 break;
691 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800692 }
Michael Jurka27614d22012-04-02 06:40:22 -0700693 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
694 // if you turned the screen off and then back while in All Apps, Launcher would not
695 // return to the workspace. Clearing mAddInfo.container here fixes this issue
696 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700697 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800698 }
699
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800700 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700701 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700702 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700703 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700704 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800705 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700706
Adam Cohenad4e15c2013-10-17 16:21:35 -0700707 Runnable exitSpringLoaded = new Runnable() {
708 @Override
709 public void run() {
710 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
711 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
712 }
713 };
714
Michael Jurka8b805b12012-04-18 14:23:14 -0700715 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700716 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700717 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700718 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
719 if (resultCode == RESULT_CANCELED) {
720 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700721 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700722 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700723 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800724 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700725 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700726 }
727 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200728 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
729 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700730 // User could have free-scrolled between pages before picking a wallpaper; make sure
731 // we move to the closest one now.
732 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700733 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200734 }
735 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700736 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200737
Adam Cohened66b2b2012-01-23 17:28:51 -0800738 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700739 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700740
Adam Cohendb364c32014-05-20 14:23:40 -0700741 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800742 // We have special handling for widgets
743 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800744 final int appWidgetId;
745 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
746 : -1;
747 if (widgetId < 0) {
748 appWidgetId = pendingAddWidgetId;
749 } else {
750 appWidgetId = widgetId;
751 }
752
Adam Cohenad4e15c2013-10-17 16:21:35 -0700753 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800754 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700755 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
756 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700757 result = RESULT_CANCELED;
758 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700759 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700760 @Override
761 public void run() {
762 exitSpringLoadedDragModeDelayed(false, 0, null);
763 }
764 };
Adam Cohendb364c32014-05-20 14:23:40 -0700765 if (workspaceLocked) {
766 // No need to remove the empty screen if we're mid-binding, as the
767 // the bind will not add the empty screen.
768 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
769 } else {
770 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
771 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
772 }
Winson Chung5aaab772012-04-27 15:24:02 -0700773 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700774 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700775 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
776 // When the screen id represents an actual screen (as opposed to a rank)
777 // we make sure that the drop page actually exists.
778 mPendingAddInfo.screenId =
779 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
780 }
Adam Cohendb364c32014-05-20 14:23:40 -0700781 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
782
783 dropLayout.setDropPending(true);
784 final Runnable onComplete = new Runnable() {
785 @Override
786 public void run() {
787 completeTwoStageWidgetDrop(resultCode, appWidgetId);
788 dropLayout.setDropPending(false);
789 }
790 };
791 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
792 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
793 } else {
794 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
795 mPendingAddInfo);
796 sPendingAddItem = args;
797 }
Winson Chung5aaab772012-04-27 15:24:02 -0700798 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800799 return;
800 }
801
Sunny Goyald478c832016-04-01 12:04:16 -0700802 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
803 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700804 if (resultCode == RESULT_OK) {
805 // Update the widget view.
806 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
807 pendingAddWidgetId, mPendingAddInfo);
808 if (workspaceLocked) {
809 sPendingAddItem = args;
810 } else {
811 completeAdd(args);
812 }
813 }
814 // Leave the widget in the pending state if the user canceled the configure.
815 return;
816 }
817
Sunny Goyalaad90582015-07-09 14:22:50 -0700818 if (requestCode == REQUEST_CREATE_SHORTCUT) {
819 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
820 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
821 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
822 mPendingAddInfo);
823 if (isWorkspaceLocked()) {
824 sPendingAddItem = args;
825 } else {
826 completeAdd(args);
827 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
828 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
829 }
830 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700831 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
832 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800835 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800836
837 }
838
839 @Override
840 protected void onActivityResult(
841 final int requestCode, final int resultCode, final Intent data) {
842 handleActivityResult(requestCode, resultCode, data);
843 if (mLauncherCallbacks != null) {
844 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
845 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800846 }
847
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600848 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700849 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600850 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700851 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
852 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
853 View v = null;
854 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
855 if (layout != null) {
856 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
857 }
858 Intent intent = sPendingAddItem.intent;
859 sPendingAddItem = null;
860 if (grantResults.length > 0
861 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700862 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700863 } else {
864 // TODO: Show a snack bar with link to settings
865 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
866 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
867 }
868 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600869 if (mLauncherCallbacks != null) {
870 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
871 grantResults);
872 }
873 }
874
Adam Cohendb364c32014-05-20 14:23:40 -0700875 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
876 appWidgetId, ItemInfo info) {
877 PendingAddArguments args = new PendingAddArguments();
878 args.requestCode = requestCode;
879 args.intent = data;
880 args.container = info.container;
881 args.screenId = info.screenId;
882 args.cellX = info.cellX;
883 args.cellY = info.cellY;
884 args.appWidgetId = appWidgetId;
885 return args;
886 }
887
888 /**
889 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
890 *
891 * @param screenId the screen id to check
892 * @return the new screen, or screenId if it exists
893 */
894 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800895 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700896 if (dropLayout == null) {
897 // it's possible that the add screen was removed because it was
898 // empty and a re-bind occurred
899 mWorkspace.addExtraEmptyScreen();
900 return mWorkspace.commitExtraEmptyScreen();
901 } else {
902 return screenId;
903 }
904 }
905
Adam Cohen091440a2015-03-18 14:16:05 -0700906 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800907 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800908 Runnable onCompleteRunnable = null;
909 int animationType = 0;
910
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700911 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800912 if (resultCode == RESULT_OK) {
913 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
914 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700915 mPendingAddWidgetInfo);
916 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800917 onCompleteRunnable = new Runnable() {
918 @Override
919 public void run() {
920 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700921 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700922 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
923 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800924 }
925 };
926 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800927 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800928 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800929 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700930 if (mDragLayer.getAnimatedView() != null) {
931 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
932 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
933 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700934 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700935 // The animated view may be null in the case of a rotation during widget configuration
936 onCompleteRunnable.run();
937 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 }
939
940 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700941 protected void onStop() {
942 super.onStop();
943 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700944
945 if (mLauncherCallbacks != null) {
946 mLauncherCallbacks.onStop();
947 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700948
949 if (Utilities.isNycMR1OrAbove()) {
950 mAppWidgetHost.stopListening();
951 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700952 }
953
954 @Override
955 protected void onStart() {
956 super.onStart();
957 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700958
959 if (mLauncherCallbacks != null) {
960 mLauncherCallbacks.onStart();
961 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700962
963 if (Utilities.isNycMR1OrAbove()) {
964 mAppWidgetHost.startListening();
965 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700966 }
967
968 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200970 long startTime = 0;
971 if (DEBUG_RESUME_TIME) {
972 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400973 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200974 }
Adam Cohen9211d422014-10-07 18:14:53 -0700975
976 if (mLauncherCallbacks != null) {
977 mLauncherCallbacks.preOnResume();
978 }
979
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700981 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700982
Winson Chung4a2afa32012-07-19 14:53:05 -0700983 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700984 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700985 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800986 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700987 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700988 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700989 // view after launching an app, as they may be depending on the UI to be static to
990 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700991 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700992 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800993 } else if (mOnResumeState == State.WIDGETS) {
994 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700995 }
996 mOnResumeState = State.NONE;
997
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700998 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700999 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1000 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001001
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001002 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001003 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001004 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001005 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001006 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001007 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001009 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001010 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1011 // execute them here
1012 long startTimeCallbacks = 0;
1013 if (DEBUG_RESUME_TIME) {
1014 startTimeCallbacks = System.currentTimeMillis();
1015 }
1016
Michael Jurka1e2f4652013-07-08 18:03:46 -07001017 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1018 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001019 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001020 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001021 if (DEBUG_RESUME_TIME) {
1022 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1023 (System.currentTimeMillis() - startTimeCallbacks));
1024 }
Michael Jurka447bf842013-05-15 14:52:15 +02001025 }
Michael Jurka54554252013-08-01 12:52:23 +02001026 if (mOnResumeCallbacks.size() > 0) {
1027 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1028 mOnResumeCallbacks.get(i).run();
1029 }
1030 mOnResumeCallbacks.clear();
1031 }
Winson Chunge4e50662012-01-23 14:45:13 -08001032
1033 // Reset the pressed state of icons that were locked in the press state while activities
1034 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001035 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001036 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001037 mWaitingForResume.setStayPressed(false);
1038 }
Winson Chung82963d52013-10-09 11:20:57 -07001039
Adam Cohen06dff352012-06-01 17:17:08 -07001040 // It is possible that widgets can receive updates while launcher is not in the foreground.
1041 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1042 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1043 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001044 if (!isWorkspaceLoading()) {
1045 getWorkspace().reinflateWidgetsIfNecessary();
1046 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001047
Michael Jurka447bf842013-05-15 14:52:15 +02001048 if (DEBUG_RESUME_TIME) {
1049 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1050 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001051
Adam Cohen4b66bf32015-09-18 12:15:19 -07001052 // We want to suppress callbacks about CustomContent being shown if we have just received
1053 // onNewIntent while the user was present within launcher. In that case, we post a call
1054 // to move the user to the main screen (which will occur after onResume). We don't want to
1055 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1056 // suppress here.
1057 if (mWorkspace.getCustomContentCallbacks() != null
1058 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001059 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001060 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001061 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1062 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001063 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001064 }
1065 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001066 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001067 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001068 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001069
Sunny Goyal756adbc2015-04-16 15:20:51 -07001070 if (!isWorkspaceLoading()) {
1071 // Process any items that were added while Launcher was away.
1072 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1073 }
Adam Cohen9211d422014-10-07 18:14:53 -07001074
1075 if (mLauncherCallbacks != null) {
1076 mLauncherCallbacks.onResume();
1077 }
Adam Cohen06dff352012-06-01 17:17:08 -07001078 }
1079
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001081 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001082 // Ensure that items added to Launcher are queued until Launcher returns
1083 InstallShortcutReceiver.enableInstallQueue();
1084
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001085 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001086 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001087 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001088 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001089
1090 // We call onHide() aggressively. The custom content callbacks should be able to
1091 // debounce excess onHide calls.
1092 if (mWorkspace.getCustomContentCallbacks() != null) {
1093 mWorkspace.getCustomContentCallbacks().onHide();
1094 }
Romain Guycbb89e42009-06-08 15:52:54 -07001095
Adam Cohen9211d422014-10-07 18:14:53 -07001096 if (mLauncherCallbacks != null) {
1097 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001098 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001099 }
1100
1101 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001102 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1103 // by a onResume or by scrolling otherwise.
1104 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001105
1106 // Custom content is completely hidden
1107 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001108
1109 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1110 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001111
1112 // Indicates whether the user is allowed to scroll away from the custom content.
1113 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001114 }
1115
Adam Cohenc2d6e892014-10-16 09:49:24 -07001116 public interface LauncherOverlay {
1117
1118 /**
1119 * Touch interaction leading to overscroll has begun
1120 */
1121 public void onScrollInteractionBegin();
1122
1123 /**
1124 * Touch interaction related to overscroll has ended
1125 */
1126 public void onScrollInteractionEnd();
1127
1128 /**
1129 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1130 * screen (or in the case of RTL, the rightmost screen).
1131 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001132 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001133
1134 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001135 * Called when the launcher is ready to use the overlay
1136 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001137 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001138 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001139 }
1140
Jun Mukai8af0cd82015-05-12 12:32:12 -07001141 public interface LauncherSearchCallbacks {
1142 /**
1143 * Called when the search overlay is shown.
1144 */
1145 public void onSearchOverlayOpened();
1146
1147 /**
1148 * Called when the search overlay is dismissed.
1149 */
1150 public void onSearchOverlayClosed();
1151 }
1152
Adam Cohenc2d6e892014-10-16 09:49:24 -07001153 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001154 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001155 }
1156
1157 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1158
Sunny Goyalc86df472016-02-25 09:19:38 -08001159 public void onScrollChanged(float progress) {
1160 if (mWorkspace != null) {
1161 mWorkspace.onOverlayScrollChanged(progress);
1162 }
1163 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001164 }
1165
Jorim Jaggid017f882014-01-14 17:08:48 -08001166 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001167 if (mLauncherCallbacks != null) {
1168 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001169 } else {
1170 // On devices with a locked orientation, we will at least have the allow rotation
1171 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001172 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001173 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001174 }
1175
Adam Cohen9211d422014-10-07 18:14:53 -07001176 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001177 CustomContentCallbacks callbacks, String description) {
1178 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001179 }
1180
Adam Cohenedb40762013-07-18 16:45:45 -07001181 // The custom content needs to offset its content to account for the QSB
1182 public int getTopOffsetForCustomContent() {
1183 return mWorkspace.getPaddingTop();
1184 }
1185
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001186 @Override
1187 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001188 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001189 if (mModel.isCurrentCallbacks(this)) {
1190 mModel.stopLoader();
1191 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001192 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1193
Romain Guy13c2e7b2010-03-10 19:45:00 -08001194 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001195 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001196
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001197 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001198 @Override
1199 public void onWindowFocusChanged(boolean hasFocus) {
1200 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001201 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001202
1203 if (mLauncherCallbacks != null) {
1204 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1205 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001206 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001207
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001208 private boolean acceptFilter() {
1209 final InputMethodManager inputManager = (InputMethodManager)
1210 getSystemService(Context.INPUT_METHOD_SERVICE);
1211 return !inputManager.isFullscreenMode();
1212 }
1213
1214 @Override
1215 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001216 final int uniChar = event.getUnicodeChar();
1217 final boolean handled = super.onKeyDown(keyCode, event);
1218 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1219 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1221 keyCode, event);
1222 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001223 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001224 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001225 // showSearchDialog()
1226 // If there are multiple keystrokes before the search dialog takes focus,
1227 // onSearchRequested() will be called for every keystroke,
1228 // but it is idempotent, so it's fine.
1229 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 }
1231 }
1232
Joe Onorato8a9625e2010-01-28 15:55:35 -08001233 // Eat the long press event so the keyboard doesn't come up.
1234 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1235 return true;
1236 }
1237
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 return handled;
1239 }
1240
Winson46163472015-09-22 17:31:56 -07001241 @Override
1242 public boolean onKeyUp(int keyCode, KeyEvent event) {
1243 if (keyCode == KeyEvent.KEYCODE_MENU) {
1244 // Ignore the menu key if we are currently dragging or are on the custom content screen
1245 if (!isOnCustomContent() && !mDragController.isDragging()) {
1246 // Close any open folders
1247 closeFolder();
1248
Tony Wickham49c8d292016-07-01 11:44:34 -07001249 // Close any shortcuts containers
1250 closeShortcutsContainer();
1251
Winson46163472015-09-22 17:31:56 -07001252 // Stop resizing any widgets
1253 mWorkspace.exitWidgetResizeMode();
1254
1255 // Show the overview mode if we are on the workspace
1256 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1257 !mWorkspace.isSwitchingState()) {
1258 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001259 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001260 }
1261 }
1262 return true;
1263 }
1264 return super.onKeyUp(keyCode, event);
1265 }
1266
Karl Rosaen138a0412009-04-23 19:00:21 -07001267 private String getTypedText() {
1268 return mDefaultKeySsb.toString();
1269 }
1270
1271 private void clearTypedText() {
1272 mDefaultKeySsb.clear();
1273 mDefaultKeySsb.clearSpans();
1274 Selection.setSelection(mDefaultKeySsb, 0);
1275 }
1276
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001278 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1279 * State
1280 */
1281 private static State intToState(int stateOrdinal) {
1282 State state = State.WORKSPACE;
1283 final State[] stateValues = State.values();
1284 for (int i = 0; i < stateValues.length; i++) {
1285 if (stateValues[i].ordinal() == stateOrdinal) {
1286 state = stateValues[i];
1287 break;
1288 }
1289 }
1290 return state;
1291 }
1292
1293 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294 * Restores the previous state, if it exists.
1295 *
1296 * @param savedState The previous state.
1297 */
1298 private void restoreState(Bundle savedState) {
1299 if (savedState == null) {
1300 return;
1301 }
1302
Michael Jurka883f55b2010-10-21 15:47:14 -07001303 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001304 if (state == State.APPS || state == State.WIDGETS) {
1305 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001306 }
1307
Adam Cohen21cd0022013-10-09 18:57:02 -07001308 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1309 PagedView.INVALID_RESTORE_PAGE);
1310 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001311 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 }
1313
Sunny Goyal756cd262015-08-20 12:33:21 -07001314 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1315 if (itemValues != null) {
1316 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001317 AppWidgetProviderInfo info = savedState.getParcelable(
1318 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001319 mPendingAddWidgetInfo = info == null ?
1320 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1321
Adam Cohen4637b5a2013-11-04 18:21:24 -08001322 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001323 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 mRestoring = true;
1325 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326 }
1327
1328 /**
1329 * Finds all the views we need and configure them properly.
1330 */
1331 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001332 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001333 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001334 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001335 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001336 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001337
Sunny Goyal784f9c32016-03-23 16:56:54 -07001338 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1339 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1340 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001341 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342
Winson Chung4c98d922011-05-31 16:50:48 -07001343 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001344
1345 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001346
Winson Chung3d503fb2011-07-13 17:25:49 -07001347 // Setup the hotseat
1348 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1349 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001350 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001351 }
1352
Winsone9f27272015-10-13 10:47:51 -07001353 // Setup the overview panel
1354 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001355
Winson Chung4c98d922011-05-31 16:50:48 -07001356 // Setup the workspace
1357 mWorkspace.setHapticFeedbackEnabled(false);
1358 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001359 mWorkspace.setup(mDragController);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001360 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001361 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001362
Tony Wickham34d2c912015-09-11 09:27:58 -07001363 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001364 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001365
Winson Chungef7f8742015-06-04 17:18:17 -07001366 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001367 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001368 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001369 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1370 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1371 } else {
1372 mAppsView.setSearchBarController(new DefaultAppSearchController());
1373 }
Craig Mautner360310b2012-10-26 15:13:08 -07001374
Winson Chung3d503fb2011-07-13 17:25:49 -07001375 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001376 mDragController.setDragScoller(mWorkspace);
1377 mDragController.setScrollView(mDragLayer);
1378 mDragController.setMoveTarget(mWorkspace);
1379 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001380 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001381
Peter Schiller310a9882016-06-30 13:52:36 -07001382 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1383 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1384 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001385
Sunny Goyal322d5562015-06-25 19:35:49 -07001386 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1387 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001388 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 }
1390
Winsone9f27272015-10-13 10:47:51 -07001391 private void setupOverviewPanel() {
1392 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1393
1394 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1395 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1396 @Override
1397 public boolean onLongClick(View v) {
1398 return v.performClick();
1399 }
1400 };
1401
1402 // Bind wallpaper button actions
1403 View wallpaperButton = findViewById(R.id.wallpaper_button);
1404 wallpaperButton.setOnClickListener(new OnClickListener() {
1405 @Override
1406 public void onClick(View view) {
1407 if (!mWorkspace.isSwitchingState()) {
1408 onClickWallpaperPicker(view);
1409 }
1410 }
1411 });
1412 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1413 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1414
1415 // Bind widget button actions
1416 mWidgetsButton = findViewById(R.id.widget_button);
1417 mWidgetsButton.setOnClickListener(new OnClickListener() {
1418 @Override
1419 public void onClick(View view) {
1420 if (!mWorkspace.isSwitchingState()) {
1421 onClickAddWidgetButton(view);
1422 }
1423 }
1424 });
1425 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1426 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1427
1428 // Bind settings actions
1429 View settingsButton = findViewById(R.id.settings_button);
1430 boolean hasSettings = hasSettings();
1431 if (hasSettings) {
1432 settingsButton.setOnClickListener(new OnClickListener() {
1433 @Override
1434 public void onClick(View view) {
1435 if (!mWorkspace.isSwitchingState()) {
1436 onClickSettingsButton(view);
1437 }
1438 }
1439 });
1440 settingsButton.setOnLongClickListener(performClickOnLongClick);
1441 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1442 } else {
1443 settingsButton.setVisibility(View.GONE);
1444 }
1445
1446 mOverviewPanel.setAlpha(0f);
1447 }
1448
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001450 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001451 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001452 */
1453 public void setAllAppsButton(View allAppsButton) {
1454 mAllAppsButton = allAppsButton;
1455 }
1456
Sunny Goyalbb011da2016-06-15 15:42:29 -07001457 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001458 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001459 }
1460
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001461 public View getWidgetsButton() {
1462 return mWidgetsButton;
1463 }
1464
Anjali Koppal5ad44842014-03-10 20:34:39 -07001465 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 * Creates a view representing a shortcut.
1467 *
1468 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001470 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001471 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 }
1473
1474 /**
1475 * Creates a view representing a shortcut inflated from the specified resource.
1476 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 * @param parent The group the shortcut belongs to.
1478 * @param info The data structure describing the shortcut.
1479 *
1480 * @return A View inflated from layoutResId.
1481 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001482 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001483 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001484 parent, false);
1485 favorite.applyFromShortcutInfo(info, mIconCache);
1486 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001488 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 return favorite;
1490 }
1491
1492 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 * Add a shortcut to the workspace.
1494 *
1495 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001497 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001498 int cellY) {
1499 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001500 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001501
Sunny Goyal5c97f512015-05-19 16:03:28 -07001502 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001503 if (info == null) {
1504 return;
1505 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001506 final View view = createShortcut(info);
1507
Sunny Goyal5c97f512015-05-19 16:03:28 -07001508 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001509 // First we check if we already know the exact location where we want to add this item.
1510 if (cellX >= 0 && cellY >= 0) {
1511 cellXY[0] = cellX;
1512 cellXY[1] = cellY;
1513 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001514
1515 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001516 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001517 true, null,null)) {
1518 return;
1519 }
1520 DragObject dragObject = new DragObject();
1521 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001522 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1523 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001524 return;
1525 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001526 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001527 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001528 }
1529
1530 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001531 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001532 return;
1533 }
1534
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001535 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536
1537 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001538 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001539 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 }
1541 }
1542
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001544 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001546 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 */
Adam Cohen091440a2015-03-18 14:16:05 -07001548 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001549 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1550
1551 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001552 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001553 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001554 }
Romain Guycbb89e42009-06-08 15:52:54 -07001555
Adam Cohen59400422014-03-05 18:07:04 -08001556 if (appWidgetInfo.isCustomWidget) {
1557 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001558 }
1559
Adam Cohen59400422014-03-05 18:07:04 -08001560 LauncherAppWidgetInfo launcherInfo;
1561 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1562 launcherInfo.spanX = info.spanX;
1563 launcherInfo.spanY = info.spanY;
1564 launcherInfo.minSpanX = info.minSpanX;
1565 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001566 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001567
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001569 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570
1571 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001572 if (hostView == null) {
1573 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001574 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001575 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001576 hostView.setVisibility(View.VISIBLE);
1577 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001579 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 }
Romain Guycbb89e42009-06-08 15:52:54 -07001581
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001582 private void addAppWidgetToWorkspace(
1583 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001584 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001585 hostView.setTag(item);
1586 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001587
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001588 hostView.setFocusable(true);
1589 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001590
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001591 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001592 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1593
1594 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001595 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001596 }
1597 }
1598
Adam Cohended9f8d2010-11-03 13:25:16 -07001599 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1600 @Override
1601 public void onReceive(Context context, Intent intent) {
1602 final String action = intent.getAction();
1603 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1604 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001605 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001606 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001607
Winson Chungc100e8e2011-08-09 16:02:43 -07001608 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001609 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001610 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001611 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001612 if (!showWorkspace(false)) {
1613 // If we are already on the workspace, then manually reset all apps
1614 mAppsView.reset();
1615 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001616 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001617 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1618 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001619 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001620 }
1621 }
1622 };
1623
1624 @Override
1625 public void onAttachedToWindow() {
1626 super.onAttachedToWindow();
1627
1628 // Listen for broadcasts related to user-presence
1629 final IntentFilter filter = new IntentFilter();
1630 filter.addAction(Intent.ACTION_SCREEN_OFF);
1631 filter.addAction(Intent.ACTION_USER_PRESENT);
1632 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001633 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001634 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001635 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001636
1637 if (mLauncherCallbacks != null) {
1638 mLauncherCallbacks.onAttachedToWindow();
1639 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001640 }
1641
1642 @Override
1643 public void onDetachedFromWindow() {
1644 super.onDetachedFromWindow();
1645 mVisible = false;
1646
Adam Cohend113e0c2010-11-11 10:48:05 -08001647 if (mAttached) {
1648 unregisterReceiver(mReceiver);
1649 mAttached = false;
1650 }
Winson Chungb745afb2015-03-02 11:51:23 -08001651 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001652
1653 if (mLauncherCallbacks != null) {
1654 mLauncherCallbacks.onDetachedFromWindow();
1655 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001656 }
1657
1658 public void onWindowVisibilityChanged(int visibility) {
1659 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001660 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001661 // The following code used to be in onResume, but it turns out onResume is called when
1662 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1663 // is a more appropriate event to handle
1664 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001665 if (!mWorkspaceLoading) {
1666 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001667 // We want to let Launcher draw itself at least once before we force it to build
1668 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001669 // apps is nice and speedy.
1670 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001671 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001672 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001673 if (mStarted) return;
1674 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001675 // We delay the layer building a bit in order to give
1676 // other message processing a time to run. In particular
1677 // this avoids a delay in hiding the IME if it was
1678 // currently shown, because doing that may involve
1679 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001680 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001681 final ViewTreeObserver.OnDrawListener listener = this;
1682 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001683 public void run() {
1684 if (mWorkspace != null &&
1685 mWorkspace.getViewTreeObserver() != null) {
1686 mWorkspace.getViewTreeObserver().
1687 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001688 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001689 }
1690 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001691 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001692 }
1693 });
1694 }
1695 clearTypedText();
1696 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001697 }
1698
Adam Cohen091440a2015-03-18 14:16:05 -07001699 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001700 mHandler.removeMessages(ADVANCE_MSG);
1701 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1702 mHandler.sendMessageDelayed(msg, delay);
1703 mAutoAdvanceSentTime = System.currentTimeMillis();
1704 }
1705
Adam Cohen091440a2015-03-18 14:16:05 -07001706 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001707 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1708 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1709 mAutoAdvanceRunning = autoAdvanceRunning;
1710 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001711 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001712 sendAdvanceMessage(delay);
1713 } else {
1714 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001715 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001716 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1717 }
1718 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001719 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001720 }
1721 }
1722 }
1723
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001724 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1725
Adam Cohended9f8d2010-11-03 13:25:16 -07001726 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001727 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001728 if (msg.what == ADVANCE_MSG) {
1729 int i = 0;
1730 for (View key: mWidgetsToAdvance.keySet()) {
1731 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001732 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001733 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001734 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001735 public void run() {
1736 ((Advanceable) v).advance();
1737 }
1738 }, delay);
1739 }
1740 i++;
1741 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001742 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001743 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001744 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001745 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001746 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001747
Winsonc0b52fe2015-09-09 16:38:15 -07001748 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001749 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001750 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1751 if (v instanceof Advanceable) {
1752 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001753 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001754 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001755 }
1756 }
1757
Winsonc0b52fe2015-09-09 16:38:15 -07001758 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001759 if (mWidgetsToAdvance.containsKey(hostView)) {
1760 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001761 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001762 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001763 }
1764
Hyunyoung Song3f471442015-04-08 19:01:34 -07001765 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001766 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1767 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001768 }
1769
Winson Chunga6945242014-01-08 14:04:34 -08001770 public DragLayer getDragLayer() {
1771 return mDragLayer;
1772 }
1773
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001774 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001775 return mAppsView;
1776 }
1777
Hyunyoung Song3f471442015-04-08 19:01:34 -07001778 public WidgetsContainerView getWidgetsView() {
1779 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001780 }
1781
Winson Chunga6945242014-01-08 14:04:34 -08001782 public Workspace getWorkspace() {
1783 return mWorkspace;
1784 }
1785
1786 public Hotseat getHotseat() {
1787 return mHotseat;
1788 }
1789
Jorim Jaggid017f882014-01-14 17:08:48 -08001790 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001791 return mOverviewPanel;
1792 }
1793
Sunny Goyal47328fd2016-05-25 18:56:41 -07001794 public DropTargetBar getDropTargetBar() {
1795 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001796 }
1797
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001798 public LauncherAppWidgetHost getAppWidgetHost() {
1799 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001800 }
Romain Guycbb89e42009-06-08 15:52:54 -07001801
Winson Chunga9abd0e2010-10-27 17:18:37 -07001802 public LauncherModel getModel() {
1803 return mModel;
1804 }
1805
Adam Cohen79d90c52016-04-22 13:29:20 -07001806 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001807 return mSharedPrefs;
1808 }
1809
Adam Cohen2e6da152015-05-06 11:42:25 -07001810 public DeviceProfile getDeviceProfile() {
1811 return mDeviceProfile;
1812 }
1813
Bjorn Bringertc459e522013-06-07 19:36:01 +01001814 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001815 getWindow().closeAllPanels();
1816
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001817 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001818 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001819 }
1820
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 @Override
1822 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001823 long startTime = 0;
1824 if (DEBUG_RESUME_TIME) {
1825 startTime = System.currentTimeMillis();
1826 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 super.onNewIntent(intent);
1828
1829 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001830 Folder openFolder = mWorkspace.getOpenFolder();
1831 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1832 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1833 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1834 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1835 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001836 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001837 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001838
Adam Cohen6fecd412013-10-02 17:41:50 -07001839 if (mWorkspace == null) {
1840 // Can be cases where mWorkspace is null, this prevents a NPE
1841 return;
1842 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001843 // In all these cases, only animate if we're already on home
1844 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001845
Sunny Goyal935fca12015-10-13 10:19:01 -07001846 closeFolder(alreadyOnHome);
Tony Wickham49c8d292016-07-01 11:44:34 -07001847 closeShortcutsContainer();
Adam Cohen6fecd412013-10-02 17:41:50 -07001848 exitSpringLoadedDragMode();
1849
1850 // If we are already on home, then just animate back to the workspace,
1851 // otherwise, just wait until onResume to set the state back to Workspace
1852 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001853 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001854 } else {
1855 mOnResumeState = State.WORKSPACE;
1856 }
1857
1858 final View v = getWindow().peekDecorView();
1859 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001860 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001861 INPUT_METHOD_SERVICE);
1862 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1863 }
1864
Winson Chungb745afb2015-03-02 11:51:23 -08001865 // Reset the apps view
1866 if (!alreadyOnHome && mAppsView != null) {
1867 mAppsView.scrollToTop();
1868 }
1869
Hyunyoung Song3f471442015-04-08 19:01:34 -07001870 // Reset the widgets view
1871 if (!alreadyOnHome && mWidgetsView != null) {
1872 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001873 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001874
Adam Cohen9211d422014-10-07 18:14:53 -07001875 if (mLauncherCallbacks != null) {
1876 mLauncherCallbacks.onHomeIntent();
1877 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878 }
Adam Cohened307df2013-10-02 09:37:31 -07001879
Adam Cohen9211d422014-10-07 18:14:53 -07001880 if (mLauncherCallbacks != null) {
1881 mLauncherCallbacks.onNewIntent(intent);
1882 }
Winson15f8b172015-08-19 11:02:39 -07001883
1884 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1885 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1886 // animation.
1887 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001888 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1889 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001890 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1891 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001892
1893 // We use this flag to suppress noisy callbacks above custom content state
1894 // from onResume.
1895 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001896 mWorkspace.post(new Runnable() {
1897 @Override
1898 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001899 if (mWorkspace != null) {
1900 mWorkspace.moveToDefaultScreen(true);
1901 }
Winson15f8b172015-08-19 11:02:39 -07001902 }
1903 });
1904 }
1905 }
1906
1907 if (DEBUG_RESUME_TIME) {
1908 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1909 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001910 }
1911
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001913 public void onRestoreInstanceState(Bundle state) {
1914 super.onRestoreInstanceState(state);
1915 for (int page: mSynchronouslyBoundPages) {
1916 mWorkspace.restoreInstanceStateForChild(page);
1917 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918 }
1919
1920 @Override
1921 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001922 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001923 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1924 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001925
Adam Cohen21cd0022013-10-09 18:57:02 -07001926 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001927 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928
Michael Jurka883f55b2010-10-21 15:47:14 -07001929 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001930 // We close any open folder since it will not be re-opened, and we need to make sure
1931 // this state is reflected.
1932 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1933 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934
Tony Wickham49c8d292016-07-01 11:44:34 -07001935 closeShortcutsContainer();
1936
Adam Cohendcd297f2013-06-18 13:13:40 -07001937 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001938 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001939 ContentValues itemValues = new ContentValues();
1940 mPendingAddInfo.writeToValues(itemValues);
1941 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001942 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001943 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944 }
1945
Adam Cohen9211d422014-10-07 18:14:53 -07001946 if (mLauncherCallbacks != null) {
1947 mLauncherCallbacks.onSaveInstanceState(outState);
1948 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949 }
1950
1951 @Override
1952 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001954
Winson Chunge7a03942011-08-05 15:05:12 -07001955 // Remove all pending runnables
1956 mHandler.removeMessages(ADVANCE_MSG);
1957 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001958 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001959 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001960
Winson Chungcd2b0142011-06-08 16:02:26 -07001961 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001962 // It's possible to receive onDestroy after a new Launcher activity has
1963 // been created. In this case, don't interfere with the new Launcher.
1964 if (mModel.isCurrentCallbacks(this)) {
1965 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001966 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001967 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001968
Sunny Goyal745bad92016-05-02 10:54:12 -07001969 if (mRotationPrefChangeHandler != null) {
1970 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1971 }
1972
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001974 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001976 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001978 mAppWidgetHost = null;
1979
1980 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981
1982 TextKeyListener.getInstance().release();
1983
Tony Wickhame2217252016-03-22 16:34:23 -07001984 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1985 .removeAccessibilityStateChangeListener(this);
1986
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001987 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001988
Michael Jurka2ecf9952012-06-18 12:52:28 -07001989 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001990
1991 if (mLauncherCallbacks != null) {
1992 mLauncherCallbacks.onDestroy();
1993 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 }
1995
Sunny Goyalae502842016-06-17 08:43:56 -07001996 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1997 return mAccessibilityDelegate;
1998 }
1999
Adam Cohena9cf38f2011-05-02 15:36:58 -07002000 public DragController getDragController() {
2001 return mDragController;
2002 }
2003
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002004 @Override
2005 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002006 onStartForResult(requestCode);
2007 super.startActivityForResult(intent, requestCode);
2008 }
2009
2010 @Override
2011 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2012 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2013 onStartForResult(requestCode);
2014 try {
2015 super.startIntentSenderForResult(intent, requestCode,
2016 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2017 } catch (IntentSender.SendIntentException e) {
2018 throw new ActivityNotFoundException();
2019 }
2020 }
2021
2022 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002023 if (requestCode >= 0) {
2024 setWaitingForResult(true);
2025 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 }
2027
Winson Chung70d72102011-08-12 11:24:35 -07002028 /**
2029 * Indicates that we want global search for this activity by setting the globalSearch
2030 * argument for {@link #startSearch} to true.
2031 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002033 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002035
Karl Rosaen138a0412009-04-23 19:00:21 -07002036 if (initialQuery == null) {
2037 // Use any text typed in the launcher as the initial query
2038 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002039 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 if (appSearchData == null) {
2041 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002042 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002044
2045 // TODO send proper bounds.
2046 Rect sourceBounds = null;
Romain Guycbb89e42009-06-08 15:52:54 -07002047
Ian Parkinson047036e2014-09-01 15:40:53 +01002048 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002049 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002050 if (clearTextImmediately) {
2051 clearTypedText();
2052 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002053
2054 // We need to show the workspace after starting the search
2055 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002056 }
2057
Ian Parkinson047036e2014-09-01 15:40:53 +01002058 /**
2059 * Start a text search.
2060 *
2061 * @return {@code true} if the search will start immediately, so any further keypresses
2062 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2063 * to buffer keypresses.
2064 */
2065 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002066 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002067 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2068 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2069 sourceBounds);
2070 }
2071
Michael Jurkaa33411c2012-06-14 16:18:21 -07002072 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002073 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002074 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002075 }
2076
2077 /**
2078 * Starts the global search activity. This code is a copied from SearchManager
2079 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002080 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002081 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002082 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002083 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2084 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2085 if (globalSearchActivity == null) {
2086 Log.w(TAG, "No global search activity found.");
2087 return;
2088 }
2089 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2090 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2091 intent.setComponent(globalSearchActivity);
2092 // Make sure that we have a Bundle to put source in
2093 if (appSearchData == null) {
2094 appSearchData = new Bundle();
2095 } else {
2096 appSearchData = new Bundle(appSearchData);
2097 }
Adam Cohen9211d422014-10-07 18:14:53 -07002098 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002099 if (!appSearchData.containsKey("source")) {
2100 appSearchData.putString("source", getPackageName());
2101 }
2102 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2103 if (!TextUtils.isEmpty(initialQuery)) {
2104 intent.putExtra(SearchManager.QUERY, initialQuery);
2105 }
2106 if (selectInitialQuery) {
2107 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2108 }
2109 intent.setSourceBounds(sourceBounds);
2110 try {
2111 startActivity(intent);
2112 } catch (ActivityNotFoundException ex) {
2113 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2114 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115 }
2116
Yura4f93ec62014-02-04 14:15:21 +00002117 public boolean isOnCustomContent() {
2118 return mWorkspace.isOnOrMovingToCustomContent();
2119 }
2120
Winson Chung70d72102011-08-12 11:24:35 -07002121 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002122 public boolean onPrepareOptionsMenu(Menu menu) {
2123 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002124 if (mLauncherCallbacks != null) {
2125 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2126 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002127 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002128 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002129
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002130 @Override
2131 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002132 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002133 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002134 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002135 }
2136
Joe Onorato9c1289c2009-08-17 11:03:03 -04002137 public boolean isWorkspaceLocked() {
2138 return mWorkspaceLoading || mWaitingForResult;
2139 }
2140
Adam Cohen517a7f52014-03-01 12:12:59 -08002141 public boolean isWorkspaceLoading() {
2142 return mWorkspaceLoading;
2143 }
2144
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002145 private void setWorkspaceLoading(boolean value) {
2146 boolean isLocked = isWorkspaceLocked();
2147 mWorkspaceLoading = value;
2148 if (isLocked != isWorkspaceLocked()) {
2149 onWorkspaceLockedChanged();
2150 }
2151 }
2152
2153 private void setWaitingForResult(boolean value) {
2154 boolean isLocked = isWorkspaceLocked();
2155 mWaitingForResult = value;
2156 if (isLocked != isWorkspaceLocked()) {
2157 onWorkspaceLockedChanged();
2158 }
2159 }
2160
Adam Cohen9211d422014-10-07 18:14:53 -07002161 protected void onWorkspaceLockedChanged() {
2162 if (mLauncherCallbacks != null) {
2163 mLauncherCallbacks.onWorkspaceLockedChanged();
2164 }
2165 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002166
Michael Jurka0280c3b2010-09-17 15:00:07 -07002167 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002168 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002169 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002170 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2171 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002172 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002173 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002174
Tony Wickhama0628cc2015-10-14 15:23:04 -07002175 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002176 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002177 if (LOGD) {
2178 Log.d(TAG, "Adding widget from drop");
2179 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002180 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2181 }
2182
Sunny Goyale6b63a32015-01-16 16:14:29 -08002183 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002184 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2185 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002186 if (appWidgetInfo.configure != null) {
2187 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002188 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002189
Bjorn Bringert7984c942009-12-09 15:38:25 +00002190 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002191 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2192 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2193
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002194 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002195 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002196 Runnable onComplete = new Runnable() {
2197 @Override
2198 public void run() {
2199 // Exit spring loaded mode if necessary after adding the widget
2200 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2201 null);
2202 }
2203 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002204 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002205 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002206 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002207 }
2208 }
Romain Guycbb89e42009-06-08 15:52:54 -07002209
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002210 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002211 // Close any folders that may be open.
2212 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002213 mWorkspace.moveToCustomContentScreen(animate);
2214 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002215
2216 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2217 int[] cell, int spanX, int spanY) {
2218 switch (info.itemType) {
2219 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2220 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2221 int span[] = new int[2];
2222 span[0] = spanX;
2223 span[1] = spanY;
2224 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2225 container, screenId, cell, span);
2226 break;
2227 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2228 processShortcutFromDrop(info.componentName, container, screenId, cell);
2229 break;
2230 default:
2231 throw new IllegalStateException("Unknown item type: " + info.itemType);
2232 }
2233 }
2234
Adam Cohenfbba09b2011-07-18 21:43:05 -07002235 /**
2236 * Process a shortcut drop.
2237 *
2238 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002239 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002240 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002241 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002242 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2243 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002244 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002245 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002246 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002247
2248 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002249 mPendingAddInfo.cellX = cell[0];
2250 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002251 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002252
2253 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2254 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002255 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002256 }
2257
Adam Cohenfbba09b2011-07-18 21:43:05 -07002258 /**
2259 * Process a widget drop.
2260 *
2261 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002262 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002263 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002264 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002265 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2266 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002267 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002268 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002269 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002270 mPendingAddInfo.minSpanX = info.minSpanX;
2271 mPendingAddInfo.minSpanY = info.minSpanY;
2272
Adam Cohenfbba09b2011-07-18 21:43:05 -07002273 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002274 mPendingAddInfo.cellX = cell[0];
2275 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002276 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002277 if (span != null) {
2278 mPendingAddInfo.spanX = span[0];
2279 mPendingAddInfo.spanY = span[1];
2280 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002281
Adam Cohened66b2b2012-01-23 17:28:51 -08002282 AppWidgetHostView hostView = info.boundWidget;
2283 int appWidgetId;
2284 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002285 // In the case where we've prebound the widget, we remove it from the DragLayer
2286 if (LOGD) {
2287 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2288 }
2289 getDragLayer().removeView(hostView);
2290
Adam Cohened66b2b2012-01-23 17:28:51 -08002291 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002292 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002293
2294 // Clear the boundWidget so that it doesn't get destroyed.
2295 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002296 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002297 // In this case, we either need to start an activity to get permission to bind
2298 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002299 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002300 Bundle options = info.bindOptions;
2301
Sunny Goyalffe83f12014-08-14 17:39:34 -07002302 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2303 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002304 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002305 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002306 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002307 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002308 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2309 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2310 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002311 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2312 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002313 // TODO: we need to make sure that this accounts for the options bundle.
2314 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002315 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2316 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002317 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002318 }
2319
Adam Cohendcd297f2013-06-18 13:13:40 -07002320 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002321 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002322 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002323 folderInfo.title = getText(R.string.folder_name);
2324
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002325 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002326 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2327 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002328
2329 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002330 FolderIcon newFolder =
2331 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002332 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002333 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002334 // Force measure the new folder icon
2335 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2336 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002337 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002338 }
Romain Guycbb89e42009-06-08 15:52:54 -07002339
Winsonc0b52fe2015-09-09 16:38:15 -07002340 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002341 * Unbinds the view for the specified item, and removes the item and all its children.
2342 *
2343 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002344 * @param itemInfo the {@link ItemInfo} for this view.
2345 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002346 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002347 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002348 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002349 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002350 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2351 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002352 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002353 } else {
2354 mWorkspace.removeWorkspaceItem(v);
2355 }
Winsonc0b52fe2015-09-09 16:38:15 -07002356 if (deleteFromDb) {
2357 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2358 }
2359 } else if (itemInfo instanceof FolderInfo) {
2360 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002361 if (v instanceof FolderIcon) {
2362 ((FolderIcon) v).removeListeners();
2363 }
Winsonc0b52fe2015-09-09 16:38:15 -07002364 mWorkspace.removeWorkspaceItem(v);
2365 if (deleteFromDb) {
2366 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2367 }
2368 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2369 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002370 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002371 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002372 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002373 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002374 }
2375 } else {
2376 return false;
2377 }
2378 return true;
2379 }
2380
2381 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002382 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002383 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002384 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002385 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002386 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002387 // Deleting an app widget ID is a void call but writes to disk before returning
2388 // to the caller...
2389 new AsyncTask<Void, Void, Void>() {
2390 public Void doInBackground(Void ... args) {
2391 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2392 return null;
2393 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002394 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002395 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002396 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002397 }
2398
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002399 @Override
2400 public boolean dispatchKeyEvent(KeyEvent event) {
2401 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2402 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002403 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002404 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002405 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002406 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002407 dumpState();
2408 return true;
2409 }
2410 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002411 }
2412 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2413 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002414 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002415 return true;
2416 }
2417 }
2418
2419 return super.dispatchKeyEvent(event);
2420 }
2421
Joe Onorato88ec0992009-11-19 13:16:06 -08002422 @Override
2423 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002424 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2425 return;
2426 }
2427
Sunny Goyal45478022015-06-08 16:52:41 -07002428 if (mDragController.isDragging()) {
2429 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002430 return;
2431 }
2432
Tony Wickham49c8d292016-07-01 11:44:34 -07002433 if (getOpenShortcutsContainer() != null) {
2434 closeShortcutsContainer();
2435 } else if (isAppsViewVisible()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002436 showWorkspace(true);
2437 } else if (isWidgetsViewVisible()) {
2438 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002439 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002440 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002441 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002442 Folder openFolder = mWorkspace.getOpenFolder();
2443 if (openFolder.isEditingName()) {
2444 openFolder.dismissEditingName();
2445 } else {
2446 closeFolder();
2447 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002448 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002449 mWorkspace.exitWidgetResizeMode();
2450
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002451 // Back button is a no-op here, but give at least some feedback for the button press
2452 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002453 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002454 }
2455
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002456 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002457 * Launches the intent referred by the clicked shortcut.
2458 *
2459 * @param v The view representing the clicked shortcut.
2460 */
2461 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002462 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2463 // view has detached (it's possible for this to happen if the view is removed mid touch).
2464 if (v.getWindowToken() == null) {
2465 return;
2466 }
2467
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002468 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002469 return;
2470 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002471
Adam Cohen1697b792013-09-17 19:08:21 -07002472 if (v instanceof Workspace) {
2473 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002474 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002475 }
2476 return;
2477 }
2478
2479 if (v instanceof CellLayout) {
2480 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002481 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2482 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002483 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002484 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002485 }
2486
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002487 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002488 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002489 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002490 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002491 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002492 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002493 }
Sunny Goyalbb011da2016-06-15 15:42:29 -07002494 } else if (v instanceof PageIndicator || (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002495 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002496 } else if (tag instanceof AppInfo) {
2497 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002498 } else if (tag instanceof LauncherAppWidgetInfo) {
2499 if (v instanceof PendingAppWidgetHostView) {
2500 onClickPendingWidget((PendingAppWidgetHostView) v);
2501 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002502 }
2503 }
2504
Sunny Goyal70660032015-05-14 00:07:08 -07002505 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002506 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002507 return false;
2508 }
2509
Michael Jurkaaf442092010-06-10 17:01:57 -07002510 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002511 * Event handler for the app widget view which has not fully restored.
2512 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002513 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002514 if (mIsSafeModeEnabled) {
2515 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2516 return;
2517 }
2518
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002519 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002520 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002521 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2522 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2523 // This should not happen, as we make sure that an Id is allocated during bind.
2524 return;
2525 }
2526 LauncherAppWidgetProviderInfo appWidgetInfo =
2527 mAppWidgetManager.findProvider(info.providerName, info.user);
2528 if (appWidgetInfo != null) {
2529 mPendingAddWidgetId = info.appWidgetId;
2530 mPendingAddInfo.copyFrom(info);
2531 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002532
Sunny Goyald478c832016-04-01 12:04:16 -07002533 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2534 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingAddWidgetId);
2535 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
2536 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2537 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2538 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2539 }
2540 } else {
2541 LauncherAppWidgetProviderInfo appWidgetInfo =
2542 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2543 if (appWidgetInfo != null) {
2544 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2545 }
Sunny Goyalff572272014-07-23 13:58:07 -07002546 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002547 } else if (info.installProgress < 0) {
2548 // The install has not been queued
2549 final String packageName = info.providerName.getPackageName();
2550 showBrokenAppInstallDialog(packageName,
2551 new DialogInterface.OnClickListener() {
2552 public void onClick(DialogInterface dialog, int id) {
2553 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2554 }
2555 });
2556 } else {
2557 // Download has started.
2558 final String packageName = info.providerName.getPackageName();
2559 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002560 }
2561 }
2562
Sunny Goyald478c832016-04-01 12:04:16 -07002563 private void startRestoredWidgetReconfigActivity(
2564 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
2565 mPendingAddWidgetInfo = provider;
2566 mPendingAddInfo.copyFrom(info);
2567 mPendingAddWidgetId = info.appWidgetId;
2568 mAppWidgetManager.startConfigActivity(provider,
2569 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2570 }
2571
Sunny Goyalff572272014-07-23 13:58:07 -07002572 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002573 * Event handler for the "grid" button that appears on the home screen, which
2574 * enters all apps mode.
2575 *
2576 * @param v The view that was clicked.
2577 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002578 protected void onClickAllAppsButton(View v) {
2579 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002580 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002581 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002582 true /* updatePredictedApps */, false /* focusSearchBar */);
2583 }
2584 }
2585
2586 protected void onLongClickAllAppsButton(View v) {
2587 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2588 if (!isAppsViewVisible()) {
2589 showAppsView(true /* animated */, false /* resetListToTop */,
2590 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002591 }
2592 }
2593
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002594 private void showBrokenAppInstallDialog(final String packageName,
2595 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002596 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002597 .setTitle(R.string.abandoned_promises_title)
2598 .setMessage(R.string.abandoned_promise_explanation)
2599 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2600 .setNeutralButton(R.string.abandoned_clean_this,
2601 new DialogInterface.OnClickListener() {
2602 public void onClick(DialogInterface dialog, int id) {
2603 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2604 mWorkspace.removeAbandonedPromise(packageName, user);
2605 }
2606 })
2607 .create().show();
2608 return;
2609 }
2610
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002611 /**
2612 * Event handler for an app shortcut click.
2613 *
2614 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2615 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002616 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002617 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2618 Object tag = v.getTag();
2619 if (!(tag instanceof ShortcutInfo)) {
2620 throw new IllegalArgumentException("Input must be a Shortcut");
2621 }
2622
2623 // Open shortcut
2624 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002625
2626 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002627 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2628 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002629 // Launch activity anyway, framework will tell the user why the app is suspended.
2630 } else {
2631 int error = R.string.activity_not_available;
2632 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2633 error = R.string.safemode_shortcut_error;
2634 }
2635 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2636 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002637 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002638 }
2639
Chris Wren40c5ed32014-06-24 18:24:23 -04002640 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002641 if ((v instanceof BubbleTextView)
2642 && shortcut.isPromise()
2643 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002644 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002645 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002646 new DialogInterface.OnClickListener() {
2647 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002648 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002649 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002650 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002651 return;
2652 }
2653
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002654 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002655 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002656 }
2657
Sunny Goyala7ce1662016-05-31 15:01:35 -07002658 private void startAppShortcutOrInfoActivity(View v) {
2659 ItemInfo item = (ItemInfo) v.getTag();
2660 Intent intent = item.getIntent();
2661 if (intent == null) {
2662 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002663 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002664 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002665 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002666
2667 if (success && v instanceof BubbleTextView) {
2668 mWaitingForResume = (BubbleTextView) v;
2669 mWaitingForResume.setStayPressed(true);
2670 }
2671 }
2672
2673 /**
2674 * Event handler for a folder icon click.
2675 *
2676 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2677 */
2678 protected void onClickFolderIcon(View v) {
2679 if (LOGD) Log.d(TAG, "onClickFolder");
2680 if (!(v instanceof FolderIcon)){
2681 throw new IllegalArgumentException("Input must be a FolderIcon");
2682 }
2683
2684 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002685 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002686 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002687 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002688 }
Michael Jurka5130e402011-10-13 04:55:35 -07002689 }
2690
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002691 /**
2692 * Event handler for the (Add) Widgets button that appears after a long press
2693 * on the home screen.
2694 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002695 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002696 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002697 if (mIsSafeModeEnabled) {
2698 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2699 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002700 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002701 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002702 }
2703
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002704 /**
2705 * Event handler for the wallpaper picker button that appears after a long press
2706 * on the home screen.
2707 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002708 public void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002709 if (!Utilities.isWallapaperAllowed(this)) {
2710 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2711 return;
2712 }
2713
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002714 String pickerPackage = getString(R.string.wallpaper_picker_package);
2715 if (TextUtils.isEmpty(pickerPackage)) {
2716 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2717 }
2718
Tony Wickham785f7a52015-08-31 17:28:32 -07002719 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2720 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002721 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER)
2722 .setPackage(pickerPackage)
2723 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset),
2724 REQUEST_PICK_WALLPAPER);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002725 }
2726
2727 /**
2728 * Event handler for a click on the settings button that appears after a long press
2729 * on the home screen.
2730 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002731 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002732 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002733 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2734 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002735 }
2736
Adam Cohen61f560d2013-09-30 15:58:20 -07002737 public View.OnTouchListener getHapticFeedbackTouchListener() {
2738 if (mHapticFeedbackTouchListener == null) {
2739 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002740 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002741 @Override
2742 public boolean onTouch(View v, MotionEvent event) {
2743 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2744 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2745 }
2746 return false;
2747 }
2748 };
2749 }
2750 return mHapticFeedbackTouchListener;
2751 }
2752
Tony Wickhame2217252016-03-22 16:34:23 -07002753 @Override
2754 public void onAccessibilityStateChanged(boolean enabled) {
2755 mDragLayer.onAccessibilityStateChanged(enabled);
2756 }
2757
Adam Cohen9211d422014-10-07 18:14:53 -07002758 public void onDragStarted(View view) {
2759 if (isOnCustomContent()) {
2760 // Custom content screen doesn't participate in drag and drop. If on custom
2761 // content screen, move to default.
2762 moveWorkspaceToDefaultScreen();
2763 }
Adam Cohen9211d422014-10-07 18:14:53 -07002764 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002765
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002766 /**
2767 * Called when the user stops interacting with the launcher.
2768 * This implies that the user is now on the homescreen and is not doing housekeeping.
2769 */
Adam Cohen9211d422014-10-07 18:14:53 -07002770 protected void onInteractionEnd() {
2771 if (mLauncherCallbacks != null) {
2772 mLauncherCallbacks.onInteractionEnd();
2773 }
2774 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002775
2776 /**
2777 * Called when the user starts interacting with the launcher.
2778 * The possible interactions are:
2779 * - open all apps
2780 * - reorder an app shortcut, or a widget
2781 * - open the overview mode.
2782 * This is a good time to stop doing things that only make sense
2783 * when the user is on the homescreen and not doing housekeeping.
2784 */
Adam Cohen9211d422014-10-07 18:14:53 -07002785 protected void onInteractionBegin() {
2786 if (mLauncherCallbacks != null) {
2787 mLauncherCallbacks.onInteractionBegin();
2788 }
2789 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002790
Winson Chungcd99cd32015-04-29 11:03:24 -07002791 /** Updates the interaction state. */
2792 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002793 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002794 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002795 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2796 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002797 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002798 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002799 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002800 onInteractionEnd();
2801 }
2802 }
2803
Sunny Goyala7ce1662016-05-31 15:01:35 -07002804 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002805 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002806 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2807 try {
2808 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2809 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2810 // is enabled by default on NYC.
2811 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2812 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002813
2814 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2815 String id = ((ShortcutInfo) info).getDeepShortcutId();
2816 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002817 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2818 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002819 } else {
2820 // Could be launching some bookkeeping activity
2821 startActivity(intent, optsBundle);
2822 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002823 } finally {
2824 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002825 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002826 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002827 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2828 // corresponding permission. Show the appropriate permission prompt if that
2829 // is the case.
2830 if (intent.getComponent() == null
2831 && Intent.ACTION_CALL.equals(intent.getAction())
2832 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2833 PackageManager.PERMISSION_GRANTED) {
2834 // TODO: Rename sPendingAddItem to a generic name.
2835 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2836 0, info);
2837 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2838 REQUEST_PERMISSION_CALL_PHONE);
2839 } else {
2840 // No idea why this was thrown.
2841 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002842 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002843 }
2844 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002845
Sunny Goyala7ce1662016-05-31 15:01:35 -07002846 private Bundle getActivityLaunchOptions(View v) {
2847 if (Utilities.ATLEAST_MARSHMALLOW) {
2848 int left = 0, top = 0;
2849 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2850 if (v instanceof TextView) {
2851 // Launch from center of icon, not entire view
2852 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2853 if (icon != null) {
2854 Rect bounds = icon.getBounds();
2855 left = (width - bounds.width()) / 2;
2856 top = v.getPaddingTop();
2857 width = bounds.width();
2858 height = bounds.height();
2859 }
2860 }
2861 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2862 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2863 // On L devices, we use the device default slide-up transition.
2864 // On L MR1 devices, we use a custom version of the slide-up transition which
2865 // doesn't have the delay present in the device default.
2866 return ActivityOptions.makeCustomAnimation(
2867 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2868 }
2869 return null;
2870 }
2871
2872 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002873 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2874 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2875 return false;
2876 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002877 // Only launch using the new animation if the shortcut has not opted out (this is a
2878 // private contract between launcher and may be ignored in the future).
2879 boolean useLaunchAnimation = (v != null) &&
2880 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2881 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2882
2883 UserHandleCompat user = null;
2884 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2885 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2886 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002887 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002888
2889 // Prepare intent
2890 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2891 if (v != null) {
2892 int[] pos = new int[2];
2893 v.getLocationOnScreen(pos);
2894 intent.setSourceBounds(
2895 new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2896 }
2897 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002898 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2899 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
2900 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002901 // Shortcuts need some special checks due to legacy reasons.
2902 startShortcutIntentSafely(intent, optsBundle, item);
2903 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2904 // Could be launching some bookkeeping activity
2905 startActivity(intent, optsBundle);
2906 } else {
2907 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2908 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2909 }
2910 return true;
2911 } catch (ActivityNotFoundException|SecurityException e) {
2912 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2913 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2914 }
2915 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002916 }
2917
Adam Cohen268c4752012-06-06 17:47:33 -07002918 /**
2919 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2920 * in the DragLayer in the exact absolute location of the original FolderIcon.
2921 */
2922 private void copyFolderIconToImage(FolderIcon fi) {
2923 final int width = fi.getMeasuredWidth();
2924 final int height = fi.getMeasuredHeight();
2925
2926 // Lazy load ImageView, Bitmap and Canvas
2927 if (mFolderIconImageView == null) {
2928 mFolderIconImageView = new ImageView(this);
2929 }
2930 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2931 mFolderIconBitmap.getHeight() != height) {
2932 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2933 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2934 }
2935
2936 DragLayer.LayoutParams lp;
2937 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2938 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2939 } else {
2940 lp = new DragLayer.LayoutParams(width, height);
2941 }
2942
Adam Cohen307fe232012-08-16 17:55:58 -07002943 // The layout from which the folder is being opened may be scaled, adjust the starting
2944 // view size by this scale factor.
2945 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002946 lp.customPosition = true;
2947 lp.x = mRectForFolderAnimation.left;
2948 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002949 lp.width = (int) (scale * width);
2950 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002951
2952 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2953 fi.draw(mFolderIconCanvas);
2954 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002955 if (fi.getFolder() != null) {
2956 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2957 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002958 }
Adam Cohen268c4752012-06-06 17:47:33 -07002959 // Just in case this image view is still in the drag layer from a previous animation,
2960 // we remove it and re-add it.
2961 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2962 mDragLayer.removeView(mFolderIconImageView);
2963 }
2964 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002965 if (fi.getFolder() != null) {
2966 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002967 }
Adam Cohen268c4752012-06-06 17:47:33 -07002968 }
2969
Adam Cohen37b2a492016-04-06 18:36:06 -07002970 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002971 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002972 FolderInfo info = (FolderInfo) fi.getTag();
2973 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2974 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002975 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2976 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002977 }
2978
Adam Cohen268c4752012-06-06 17:47:33 -07002979 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2980 copyFolderIconToImage(fi);
2981 fi.setVisibility(View.INVISIBLE);
2982
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002983 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2984 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002985 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002986 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2987 }
2988 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002989 oa.start();
2990 }
2991
Sunny Goyal935fca12015-10-13 10:19:01 -07002992 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002993 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002994 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002995
Adam Cohen268c4752012-06-06 17:47:33 -07002996 // We remove and re-draw the FolderIcon in-case it has changed
2997 mDragLayer.removeView(mFolderIconImageView);
2998 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08002999
3000 if (cl != null) {
3001 cl.clearFolderLeaveBehind();
3002 }
3003
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003004 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003005 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003006 oa.addListener(new AnimatorListenerAdapter() {
3007 @Override
3008 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003009 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003010 // Remove the ImageView copy of the FolderIcon and make the original visible.
3011 mDragLayer.removeView(mFolderIconImageView);
3012 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003013 }
3014 }
3015 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003016 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003017 if (!animate) {
3018 oa.end();
3019 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003020 }
3021
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003022 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003023 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003024 * is animated relative to the specified View. If the View is null, no animation
3025 * is played.
3026 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003027 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003028 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003029 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003030
Adam Cohenfb91f302012-06-11 15:45:18 -07003031 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003032 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3033 if (openFolder != null && openFolder != folder) {
3034 // Close any open folder before opening a folder.
3035 closeFolder();
3036 }
3037
Adam Cohena9cf38f2011-05-02 15:36:58 -07003038 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003039
Adam Cohena9cf38f2011-05-02 15:36:58 -07003040 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003041
Sunny Goyalf4066152015-04-15 09:42:19 -07003042 // While the folder is open, the position of the icon cannot change.
3043 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3044
Adam Cohen4554ee12011-08-03 16:13:21 -07003045 // Just verify that the folder hasn't already been added to the DragLayer.
3046 // There was a one-off crash where the folder had a parent already.
3047 if (folder.getParent() == null) {
3048 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003049 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003050 } else {
3051 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3052 folder.getParent() + ").");
3053 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003054 folder.animateOpen();
3055
3056 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003057
3058 // Notify the accessibility manager that this folder "window" has appeared and occluded
3059 // the workspace items
3060 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3061 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003062 }
3063
3064 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003065 closeFolder(true);
3066 }
3067
3068 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003069 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003070 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003071 if (folder.isEditingName()) {
3072 folder.dismissEditingName();
3073 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003074 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003075 }
3076 }
3077
Sunny Goyal935fca12015-10-13 10:19:01 -07003078 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003079 animate &= !Utilities.isPowerSaverOn(this);
3080
Adam Cohen4554ee12011-08-03 16:13:21 -07003081 folder.getInfo().opened = false;
3082
3083 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3084 if (parent != null) {
3085 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003086 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003087 if (fi != null) {
3088 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3089 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003090 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003091 if (animate) {
3092 folder.animateClosed();
3093 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003094 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003095 }
Winson Chung83ca4802013-04-12 15:10:52 -07003096
Sunny Goyal935fca12015-10-13 10:19:01 -07003097 // Notify the accessibility manager that this folder "window" has disappeared and no
3098 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003099 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003100 }
3101
Tony Wickham49c8d292016-07-01 11:44:34 -07003102 public void closeShortcutsContainer() {
3103 DeepShortcutsContainer deepShortcutsContainer = getOpenShortcutsContainer();
3104 if (deepShortcutsContainer != null) {
3105 mDragController.removeDragListener(deepShortcutsContainer);
3106 mDragLayer.removeView(deepShortcutsContainer);
3107 }
3108 }
3109
3110 /**
3111 * @return The open shortcuts container, or null if there is none
3112 */
3113 public DeepShortcutsContainer getOpenShortcutsContainer() {
3114 return (DeepShortcutsContainer) mDragLayer.findViewById(R.id.deep_shortcuts_container);
3115 }
3116
Tony Wickhamdadb3042016-02-24 11:07:00 -08003117 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003118 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003119 if (!isDraggingEnabled()) return false;
3120 if (isWorkspaceLocked()) return false;
3121 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003122
Sunny Goyalbb011da2016-06-15 15:42:29 -07003123 if (v == mAllAppsButton && mAllAppsButton != null) {
Winson Chung76648c52015-07-10 14:33:23 -07003124 onLongClickAllAppsButton(v);
3125 return true;
3126 }
3127
Adam Cohen1697b792013-09-17 19:08:21 -07003128 if (v instanceof Workspace) {
3129 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003130 if (!mWorkspace.isTouchActive()) {
3131 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003132 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3133 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3134 return true;
3135 } else {
3136 return false;
3137 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003138 } else {
3139 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003140 }
Adam Cohen1697b792013-09-17 19:08:21 -07003141 }
3142
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003143 CellLayout.CellInfo longClickCellInfo = null;
3144 View itemUnderLongClick = null;
3145 if (v.getTag() instanceof ItemInfo) {
3146 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003147 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003148 itemUnderLongClick = longClickCellInfo.cell;
3149 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003150 }
3151
Winson Chung3d503fb2011-07-13 17:25:49 -07003152 // The hotseat touch handling does not go through Workspace, and we always allow long press
3153 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003154 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003155 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003156 // User long pressed on empty space
3157 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3158 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003159 if (mWorkspace.isInOverviewMode()) {
3160 mWorkspace.startReordering(v);
3161 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003162 showOverviewMode(true);
Hyunyoung Song645764e2016-06-06 14:19:02 -07003163 mHotseat.requestDisallowInterceptTouchEvent(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003164 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003165 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003166 final boolean isAllAppsButton =
3167 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3168 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3169 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003170 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003171 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003172 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003173 }
3174 }
3175 }
3176 return true;
3177 }
3178
Winson Chung3d503fb2011-07-13 17:25:49 -07003179 boolean isHotseatLayout(View layout) {
3180 return mHotseat != null && layout != null &&
3181 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3182 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003183
Winson Chung3d503fb2011-07-13 17:25:49 -07003184 /**
3185 * Returns the CellLayout of the specified container at the specified screen.
3186 */
Sunny Goyal92820592015-03-02 11:31:35 -08003187 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003188 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3189 if (mHotseat != null) {
3190 return mHotseat.getLayout();
3191 } else {
3192 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003193 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003194 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003195 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003196 }
3197 }
3198
Winson Chungb745afb2015-03-02 11:51:23 -08003199 /**
3200 * For overridden classes.
3201 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003202 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003203 return isAppsViewVisible();
3204 }
3205
3206 public boolean isAppsViewVisible() {
3207 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3208 }
3209
3210 public boolean isWidgetsViewVisible() {
3211 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003212 }
3213
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003214 private void setWorkspaceBackground(int background) {
3215 switch (background) {
3216 case WORKSPACE_BACKGROUND_TRANSPARENT:
3217 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3218 break;
3219 case WORKSPACE_BACKGROUND_BLACK:
3220 getWindow().setBackgroundDrawable(null);
3221 break;
3222 default:
3223 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3224 }
Craig Mautner360310b2012-10-26 15:13:08 -07003225 }
3226
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003227 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003228 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3229 int curflags = getWindow().getAttributes().flags
3230 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3231 if (wpflags != curflags) {
3232 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3233 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003234 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003235 }
3236
Michael Jurkae326f182011-11-21 14:05:46 -08003237 @Override
3238 public void onTrimMemory(int level) {
3239 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003240 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3241 // The widget preview db can result in holding onto over
3242 // 3MB of memory for caching which isn't necessary.
3243 SQLiteDatabase.releaseMemory();
3244
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003245 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003246 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003247 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003248 if (mLauncherCallbacks != null) {
3249 mLauncherCallbacks.onTrimMemory(level);
3250 }
Michael Jurkae326f182011-11-21 14:05:46 -08003251 }
3252
Winson5c6bdbb2015-09-03 11:36:19 -07003253 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003254 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003255 }
3256
Winson5c6bdbb2015-09-03 11:36:19 -07003257 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003258 boolean changed = mState != State.WORKSPACE ||
3259 mWorkspace.getState() != Workspace.State.NORMAL;
3260 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003261 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003262 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003263 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003264
Michael Jurkab3e22d92011-10-31 15:58:33 -07003265 // Set focus to the AppsCustomize button
3266 if (mAllAppsButton != null) {
3267 mAllAppsButton.requestFocus();
3268 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003269 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003270
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003271 // Change the state *after* we've called all the transition code
3272 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003273
Winson Chung5fb63472011-02-02 17:03:37 -08003274 // Resume the auto-advance of widgets
3275 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003276 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003277
Winson Chung0f785722015-04-08 10:27:49 -07003278 if (changed) {
3279 // Send an accessibility event to announce the context change
3280 getWindow().getDecorView()
3281 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003282 }
Winson5c6bdbb2015-09-03 11:36:19 -07003283 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003284 }
3285
Winsone9f27272015-10-13 10:47:51 -07003286 /**
3287 * Shows the overview button.
3288 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07003289 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003290 showOverviewMode(animated, false);
3291 }
3292
3293 /**
3294 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3295 * onto one of the overview panel buttons.
3296 */
3297 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3298 Runnable postAnimRunnable = null;
3299 if (requestButtonFocus) {
3300 postAnimRunnable = new Runnable() {
3301 @Override
3302 public void run() {
3303 // Hitting the menu button when in touch mode does not trigger touch mode to
3304 // be disabled, so if requested, force focus on one of the overview panel
3305 // buttons.
3306 mOverviewPanel.requestFocusFromTouch();
3307 }
3308 };
3309 }
Adam Cohened307df2013-10-02 09:37:31 -07003310 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003311 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003312 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003313 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003314 }
3315
Winson Chungb745afb2015-03-02 11:51:23 -08003316 /**
3317 * Shows the apps view.
3318 */
Hyunyoung Song645764e2016-06-06 14:19:02 -07003319 public void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003320 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003321 if (resetListToTop) {
3322 mAppsView.scrollToTop();
3323 }
Winson Chung4ac30062015-05-08 17:34:17 -07003324 if (updatePredictedApps) {
3325 tryAndUpdatePredictedApps();
3326 }
Winson Chung76648c52015-07-10 14:33:23 -07003327 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003328 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003329
Winson Chungb745afb2015-03-02 11:51:23 -08003330 /**
3331 * Shows the widgets view.
3332 */
3333 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003334 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003335 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003336 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003337 }
Winson Chung76648c52015-07-10 14:33:23 -07003338 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003339
3340 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003341 @Override
3342 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003343 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003344 }
3345 });
Winson Chungb745afb2015-03-02 11:51:23 -08003346 }
3347
3348 /**
3349 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003350 *
3351 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003352 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003353 // TODO: calling method should use the return value so that when {@code false} is returned
3354 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003355 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003356 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3357 mState != State.WIDGETS_SPRING_LOADED) {
3358 return false;
3359 }
3360 if (toState != State.APPS && toState != State.WIDGETS) {
3361 return false;
3362 }
Winson Chungb745afb2015-03-02 11:51:23 -08003363
3364 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003365 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3366 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003367 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003368 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003369 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003370
Michael Jurkab3e22d92011-10-31 15:58:33 -07003371 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003372 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003373
3374 // Pause the auto-advance of widgets until we are out of AllApps
3375 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003376 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003377 closeFolder();
Tony Wickham49c8d292016-07-01 11:44:34 -07003378 closeShortcutsContainer();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003379
3380 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003381 getWindow().getDecorView()
3382 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003383 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003384 }
3385
Winson Chungcd99cd32015-04-29 11:03:24 -07003386 /**
3387 * Updates the workspace and interaction state on state change, and return the animation to this
3388 * new state.
3389 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003390 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003391 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003392 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003393 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003394 updateInteraction(fromState, toState);
3395 return anim;
3396 }
3397
Hyunyoung Song3f471442015-04-08 19:01:34 -07003398 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003399 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003400 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003401 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003402 }
Winson Chungb745afb2015-03-02 11:51:23 -08003403
Winson Chung006ee262015-08-03 14:40:11 -07003404 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003405 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003406 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003407
3408 if (isAppsViewVisible()) {
3409 mState = State.APPS_SPRING_LOADED;
3410 } else if (isWidgetsViewVisible()) {
3411 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003412 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003413 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003414 } else {
3415 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003416 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003417 }
Adam Cohen7777d962011-08-18 18:58:38 -07003418
Hyunyoung Song3f471442015-04-08 19:01:34 -07003419 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003420 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003421 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003422
Winson Chunge7a03942011-08-05 15:05:12 -07003423 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003424 @Override
3425 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003426 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003427 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3428 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003429 // Before we show workspace, hide all apps again because
3430 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3431 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003432 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003433 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003434 } else {
3435 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003436 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003437 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003438 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003439 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003440
Tony Wickhame0c33232016-02-08 11:37:04 -08003441 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003442 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3443 || mState == State.WIDGETS_SPRING_LOADED;
3444 }
3445
Michael Jurkad3ef3062010-11-23 16:23:58 -08003446 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003447 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003448 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003449 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003450 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003451 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003452 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3453 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003454 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003455 }
3456
Winson Chung4ac30062015-05-08 17:34:17 -07003457 /**
3458 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3459 * resumed.
3460 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003461 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003462 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003463 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003464 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003465 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003466 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003467 }
3468 }
3469 }
3470
Michael Jurkab3e22d92011-10-31 15:58:33 -07003471 void lockAllApps() {
3472 // TODO
3473 }
3474
3475 void unlockAllApps() {
3476 // TODO
3477 }
3478
Winsonf768d932015-09-25 16:12:35 -07003479 public boolean launcherCallbacksProvidesSearch() {
3480 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3481 }
3482
Michael Jurkad7c28052012-04-27 15:43:36 -07003483 @Override
3484 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003485 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003486 final List<CharSequence> text = event.getText();
3487 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003488 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003489 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003490 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003491 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003492 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003493 } else if (mWorkspace != null) {
3494 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003495 } else {
3496 text.add(getString(R.string.all_apps_home_button_label));
3497 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003498 return result;
3499 }
3500
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003501 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003502 * If the activity is currently paused, signal that we need to run the passed Runnable
3503 * in onResume.
3504 *
3505 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003506 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3507 * wrong when we're not running, and if the activity comes back to what the configuration was
3508 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003509 *
3510 * Implementation of the method from LauncherModel.Callbacks.
3511 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003512 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003513 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003514 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003515 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003516 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003517 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003518 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003519 }
3520 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003521 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003522 return true;
3523 } else {
3524 return false;
3525 }
3526 }
3527
Michael Jurkac402cd92013-05-20 15:49:32 +02003528 private boolean waitUntilResume(Runnable run) {
3529 return waitUntilResume(run, false);
3530 }
3531
Michael Jurka1e2f4652013-07-08 18:03:46 -07003532 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003533 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003534 }
3535
Michael Jurka7607c2f2013-04-03 14:33:19 -07003536 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003537 * If the activity is currently paused, signal that we need to re-run the loader
3538 * in onResume.
3539 *
3540 * This needs to be called from incoming places where resources might have been loaded
3541 * while we are paused. That is becaues the Configuration might be wrong
3542 * when we're not running, and if it comes back to what it was when we
3543 * were paused, we are not restarted.
3544 *
3545 * Implementation of the method from LauncherModel.Callbacks.
3546 *
3547 * @return true if we are currently paused. The caller might be able to
3548 * skip some work in that case since we will come back again.
3549 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003550 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003551 public boolean setLoadOnResume() {
3552 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003553 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003554 mOnResumeNeedsLoad = true;
3555 return true;
3556 } else {
3557 return false;
3558 }
3559 }
3560
3561 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003562 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003563 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003564 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003565 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003566 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003567 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003568 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003569 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003570 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003571 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003572
Joe Onorato9c1289c2009-08-17 11:03:03 -04003573 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003574 * Clear any pending bind callbacks. This is called when is loader is planning to
3575 * perform a full rebind from scratch.
3576 */
3577 @Override
3578 public void clearPendingBinds() {
3579 mBindOnResumeCallbacks.clear();
3580 if (mPendingExecutor != null) {
3581 mPendingExecutor.markCompleted();
3582 mPendingExecutor = null;
3583 }
3584 }
3585
3586 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003587 * Refreshes the shortcuts shown on the workspace.
3588 *
3589 * Implementation of the method from LauncherModel.Callbacks.
3590 */
3591 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003592 if (LauncherAppState.PROFILE_STARTUP) {
3593 Trace.beginSection("Starting page bind");
3594 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003595 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003596
Winson Chungd64d1762013-08-20 14:37:16 -07003597 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003598 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003599 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003600
Michael Jurka05bf644e2011-11-30 20:28:53 -08003601 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003602 if (mHotseat != null) {
3603 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003604 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003605 if (LauncherAppState.PROFILE_STARTUP) {
3606 Trace.endSection();
3607 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003608 }
3609
Adam Cohendcd297f2013-06-18 13:13:40 -07003610 @Override
3611 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003612 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003613 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3614 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003615 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3616 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3617 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003618 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3619 // If there are no screens, we need to have an empty screen
3620 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003621 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003622 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003623
3624 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003625 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003626 if (hasCustomContentToLeft()) {
3627 mWorkspace.createCustomContentContainer();
3628 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003629 }
Winson Chung64359a52013-07-08 17:17:08 -07003630 }
3631
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003632 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003633 int count = orderedScreenIds.size();
3634 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003635 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003636 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003637 // No need to bind the first screen, as its always bound.
3638 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3639 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003640 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003641 }
3642
Winson Chungd64d1762013-08-20 14:37:16 -07003643 public void bindAppsAdded(final ArrayList<Long> newScreens,
3644 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003645 final ArrayList<ItemInfo> addAnimated,
3646 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003647 Runnable r = new Runnable() {
3648 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003649 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003650 }
3651 };
3652 if (waitUntilResume(r)) {
3653 return;
3654 }
3655
Winson Chungd64d1762013-08-20 14:37:16 -07003656 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003657 if (newScreens != null) {
3658 bindAddScreens(newScreens);
3659 }
Winson Chungd64d1762013-08-20 14:37:16 -07003660
3661 // We add the items without animation on non-visible pages, and with
3662 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003663 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003664 bindItems(addNotAnimated, 0,
3665 addNotAnimated.size(), false);
3666 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003667 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003668 bindItems(addAnimated, 0,
3669 addAnimated.size(), true);
3670 }
Winson Chungc58497e2013-09-03 17:48:37 -07003671
Winson Chung87412982013-10-03 18:34:14 -07003672 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003673 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003674
Winson Chungb745afb2015-03-02 11:51:23 -08003675 if (addedApps != null && mAppsView != null) {
3676 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003677 }
Winson Chungd64d1762013-08-20 14:37:16 -07003678 }
3679
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003680 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003681 * Bind the items start-end from the list.
3682 *
3683 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003684 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003685 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003686 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3687 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003688 Runnable r = new Runnable() {
3689 public void run() {
3690 bindItems(shortcuts, start, end, forceAnimateIcons);
3691 }
3692 };
3693 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003694 return;
3695 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003696
Winson Chungf0c6ae02012-03-21 16:10:31 -07003697 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003698 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3699 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003700 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003701 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003702 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003703 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003704 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003705
3706 // Short circuit if we are loading dock items for a configuration which has no dock
3707 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3708 mHotseat == null) {
3709 continue;
3710 }
3711
Sunny Goyal639e9062015-08-19 19:17:06 -07003712 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003713 switch (item.itemType) {
3714 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3715 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003716 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003717 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003718 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003719 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003720 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003721 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003722 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003723 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003724 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003725 default:
3726 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003727 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003728
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003729 /*
3730 * Remove colliding items.
3731 */
3732 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3733 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3734 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3735 View v = cl.getChildAt(item.cellX, item.cellY);
3736 Object tag = v.getTag();
3737 String desc = "Collision while binding workspace item: " + item
3738 + ". Collides with " + tag;
3739 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3740 throw (new RuntimeException(desc));
3741 } else {
3742 Log.d(TAG, desc);
3743 LauncherModel.deleteItemFromDatabase(this, item);
3744 continue;
3745 }
3746 }
3747 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003748 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3749 item.cellY, 1, 1);
3750 if (animateIcons) {
3751 // Animate all the applications up now
3752 view.setAlpha(0f);
3753 view.setScaleX(0f);
3754 view.setScaleY(0f);
3755 bounceAnims.add(createNewAppBounceAnimation(view, i));
3756 newShortcutsScreenId = item.screenId;
3757 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003758 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003759
Winson Chung997a9232013-07-24 15:33:46 -07003760 if (animateIcons) {
3761 // Animate to the correct page
3762 if (newShortcutsScreenId > -1) {
3763 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003764 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003765 final Runnable startBounceAnimRunnable = new Runnable() {
3766 public void run() {
3767 anim.playTogether(bounceAnims);
3768 anim.start();
3769 }
3770 };
Winson Chung997a9232013-07-24 15:33:46 -07003771 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003772 // We post the animation slightly delayed to prevent slowdowns
3773 // when we are loading right after we return to launcher.
3774 mWorkspace.postDelayed(new Runnable() {
3775 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003776 if (mWorkspace != null) {
3777 mWorkspace.snapToPage(newScreenIndex);
3778 mWorkspace.postDelayed(startBounceAnimRunnable,
3779 NEW_APPS_ANIMATION_DELAY);
3780 }
Winson Chung94d67682013-09-25 16:29:40 -07003781 }
3782 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003783 } else {
3784 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003785 }
3786 }
Winson Chung64359a52013-07-08 17:17:08 -07003787 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003788 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003789 }
3790
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003791 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3792 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3793 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003794 view.updateAppWidget(null);
3795 view.setOnClickListener(this);
3796 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003797 mWorkspace.requestLayout();
3798 }
3799
Joe Onorato9c1289c2009-08-17 11:03:03 -04003800 /**
3801 * Add the views for a widget to the workspace.
3802 *
3803 * Implementation of the method from LauncherModel.Callbacks.
3804 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003805 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003806 Runnable r = new Runnable() {
3807 public void run() {
3808 bindAppWidget(item);
3809 }
3810 };
3811 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003812 return;
3813 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003814
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003815 if (mIsSafeModeEnabled) {
3816 bindSafeModeWidget(item);
3817 return;
3818 }
3819
Daniel Sandler843e8602010-06-07 14:59:01 -04003820 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3821 if (DEBUG_WIDGETS) {
3822 Log.d(TAG, "bindAppWidget: " + item);
3823 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003824
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003825 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003826
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003827 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3828 // If the provider is not ready, bind as a pending widget.
3829 appWidgetInfo = null;
3830 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3831 // The widget id is not valid. Try to find the widget based on the provider info.
3832 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3833 } else {
3834 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3835 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003836
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003837 // If the provider is ready, but the width is not yet restored, try to restore it.
3838 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3839 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003840 if (appWidgetInfo == null) {
3841 if (DEBUG_WIDGETS) {
3842 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3843 + " belongs to component " + item.providerName
3844 + ", as the povider is null");
3845 }
3846 LauncherModel.deleteItemFromDatabase(this, item);
3847 return;
3848 }
Sunny Goyalff572272014-07-23 13:58:07 -07003849
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003850 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003851 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003852 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3853 // Id has not been allocated yet. Allocate a new id.
3854 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3855 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003856
Sunny Goyald478c832016-04-01 12:04:16 -07003857 // Also try to bind the widget. If the bind fails, the user will be shown
3858 // a click to setup UI, which will ask for the bind permission.
3859 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3860 pendingInfo.spanX = item.spanX;
3861 pendingInfo.spanY = item.spanY;
3862 pendingInfo.minSpanX = item.minSpanX;
3863 pendingInfo.minSpanY = item.minSpanY;
3864 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
3865 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3866 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003867
Sunny Goyald478c832016-04-01 12:04:16 -07003868 // Bind succeeded
3869 if (success) {
3870 // If the widget has a configure activity, it is still needs to set it up,
3871 // otherwise the widget is ready to go.
3872 item.restoreStatus = (appWidgetInfo.configure == null)
3873 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3874 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003875 }
Sunny Goyald478c832016-04-01 12:04:16 -07003876
3877 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003878 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003879 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003880 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003881 // The widget was marked as UI not ready, but there is no configure activity to
3882 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003883 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3884 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003885 }
Sunny Goyalff572272014-07-23 13:58:07 -07003886 }
3887
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003888 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003889 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003890 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3891 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003892 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003893
Sunny Goyal56c73602015-09-25 12:55:01 -07003894 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003895 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003896 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003897 deleteWidgetInfo(item);
3898 return;
3899 }
3900
Sunny Goyal233ee962015-08-03 13:05:01 -07003901 item.minSpanX = appWidgetInfo.minSpanX;
3902 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003903 addAppWidgetToWorkspace(
3904 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3905 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003906 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003907 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003908 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003909 view.updateAppWidget(null);
3910 view.setOnClickListener(this);
3911 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003912 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003913 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003914
Daniel Sandler843e8602010-06-07 14:59:01 -04003915 if (DEBUG_WIDGETS) {
3916 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3917 + (SystemClock.uptimeMillis()-start) + "ms");
3918 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003919 }
3920
Sunny Goyalff572272014-07-23 13:58:07 -07003921 /**
3922 * Restores a pending widget.
3923 *
3924 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003925 */
Sunny Goyald478c832016-04-01 12:04:16 -07003926 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003927 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3928 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3929 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003930 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003931 }
3932
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003933 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003934 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003935
3936 mWorkspace.reinflateWidgetsIfNecessary();
3937 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003938 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003939 }
3940
Adam Cohen1462de32012-07-24 22:34:36 -07003941 public void onPageBoundSynchronously(int page) {
3942 mSynchronouslyBoundPages.add(page);
3943 }
3944
Sunny Goyal527c7d32015-08-28 15:19:36 -07003945 @Override
3946 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3947 if (mPendingExecutor != null) {
3948 mPendingExecutor.markCompleted();
3949 }
3950 mPendingExecutor = executor;
3951 executor.attachTo(this);
3952 }
3953
3954 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3955 if (mPendingExecutor == executor) {
3956 mPendingExecutor = null;
3957 }
3958 }
3959
Joe Onorato9c1289c2009-08-17 11:03:03 -04003960 /**
3961 * Callback saying that there aren't any more items to bind.
3962 *
3963 * Implementation of the method from LauncherModel.Callbacks.
3964 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003965 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003966 Runnable r = new Runnable() {
3967 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003968 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003969 }
3970 };
3971 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003972 return;
3973 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003974 if (LauncherAppState.PROFILE_STARTUP) {
3975 Trace.beginSection("Page bind completed");
3976 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003977 if (mSavedState != null) {
3978 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003979 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003980 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003981
Joe Onorato9c1289c2009-08-17 11:03:03 -04003982 mSavedState = null;
3983 }
3984
Adam Cohen1462de32012-07-24 22:34:36 -07003985 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003986
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003987 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003988
3989 // If we received the result of any pending adds while the loader was running (e.g. the
3990 // widget configuration forced an orientation change), process them now.
3991 if (sPendingAddItem != null) {
3992 final long screenId = completeAdd(sPendingAddItem);
3993
3994 // TODO: this moves the user to the page where the pending item was added. Ideally,
3995 // the screen would be guaranteed to exist after bind, and the page would be set through
3996 // the workspace restore process.
3997 mWorkspace.post(new Runnable() {
3998 @Override
3999 public void run() {
4000 mWorkspace.snapToScreenId(screenId);
4001 }
4002 });
4003 sPendingAddItem = null;
4004 }
4005
Sunny Goyal756adbc2015-04-16 15:20:51 -07004006 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004007
4008 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004009 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004010 }
Sunny Goyale26d1002016-06-20 14:52:14 -07004011 if (LauncherAppState.PROFILE_STARTUP) {
4012 Trace.endSection();
4013 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004014 }
4015
Winson Chunga2413752012-04-03 14:22:34 -07004016 private boolean canRunNewAppsAnimation() {
4017 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07004018 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07004019 }
4020
Winson Chungc9168342013-06-26 14:54:55 -07004021 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004022 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004023 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4024 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004025 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004026 return bounceAnim;
4027 }
4028
Adam Cohen27772732013-11-11 14:00:56 +00004029 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004030 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004031 }
4032
Tony Wickham55616cd2015-09-23 14:55:17 -07004033 public int getSearchBarHeight() {
4034 if (mLauncherCallbacks != null) {
4035 return mLauncherCallbacks.getSearchBarHeight();
4036 }
4037 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4038 }
4039
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004040 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004041 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4042 * multiple calls to bind the same list.)
4043 */
4044 @Thunk ArrayList<AppInfo> mTmpAppsList;
4045 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4046 public void run() {
4047 bindAllApplications(mTmpAppsList);
4048 mTmpAppsList = null;
4049 }
4050 };
4051
4052 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004053 * Add the icons for all apps.
4054 *
4055 * Implementation of the method from LauncherModel.Callbacks.
4056 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004057 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004058 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4059 mTmpAppsList = apps;
4060 return;
4061 }
4062
Winson Chungb745afb2015-03-02 11:51:23 -08004063 if (mAppsView != null) {
4064 mAppsView.setApps(apps);
4065 }
Adam Cohen9211d422014-10-07 18:14:53 -07004066 if (mLauncherCallbacks != null) {
4067 mLauncherCallbacks.bindAllApplications(apps);
4068 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004069 }
4070
4071 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004072 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
4073 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
4074 */
4075 @Override
4076 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
4077 mDeepShortcutMap = deepShortcutMapCopy;
4078 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
4079 }
4080
Tony Wickham1bce7fd2016-04-28 17:39:03 -07004081 public List<String> getShortcutIdsForItem(ItemInfo info) {
4082 if (!DeepShortcutManager.supportsShortcuts(info)) {
4083 return Collections.EMPTY_LIST;
4084 }
4085 ComponentName component = info.getTargetComponent();
4086 List<String> ids = mDeepShortcutMap.get(new ComponentKey(component, info.user));
4087 return ids == null ? Collections.EMPTY_LIST : ids;
4088 }
4089
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004090 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004091 * A package was updated.
4092 *
4093 * Implementation of the method from LauncherModel.Callbacks.
4094 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004095 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004096 Runnable r = new Runnable() {
4097 public void run() {
4098 bindAppsUpdated(apps);
4099 }
4100 };
4101 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004102 return;
4103 }
4104
Winson Chungb745afb2015-03-02 11:51:23 -08004105 if (mAppsView != null) {
4106 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004107 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004108 }
4109
Sunny Goyal4390ace2014-10-13 11:33:11 -07004110 @Override
4111 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4112 Runnable r = new Runnable() {
4113 public void run() {
4114 bindWidgetsRestored(widgets);
4115 }
4116 };
4117 if (waitUntilResume(r)) {
4118 return;
4119 }
4120 mWorkspace.widgetsRestored(widgets);
4121 }
4122
Joe Onorato9c1289c2009-08-17 11:03:03 -04004123 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004124 * Some shortcuts were updated in the background.
4125 *
4126 * Implementation of the method from LauncherModel.Callbacks.
4127 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004128 @Override
4129 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4130 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004131 Runnable r = new Runnable() {
4132 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004133 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004134 }
4135 };
4136 if (waitUntilResume(r)) {
4137 return;
4138 }
4139
Sunny Goyal4390ace2014-10-13 11:33:11 -07004140 if (!updated.isEmpty()) {
4141 mWorkspace.updateShortcuts(updated);
4142 }
4143
4144 if (!removed.isEmpty()) {
4145 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4146 for (ShortcutInfo si : removed) {
4147 removedComponents.add(si.getTargetComponent());
4148 }
4149 mWorkspace.removeItemsByComponentName(removedComponents, user);
4150 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004151 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004152 }
4153 }
4154
4155 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004156 * Update the state of a package, typically related to install state.
4157 *
4158 * Implementation of the method from LauncherModel.Callbacks.
4159 */
Sunny Goyale755d462014-07-22 13:48:29 -07004160 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004161 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4162 Runnable r = new Runnable() {
4163 public void run() {
4164 bindRestoreItemsChange(updates);
4165 }
4166 };
4167 if (waitUntilResume(r)) {
4168 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004169 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004170
Sunny Goyal756adbc2015-04-16 15:20:51 -07004171 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004172 }
4173
4174 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004175 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004176 * in addition to specific applications to remove, the reason being that
4177 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004178 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004179 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004180 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004181 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004182 public void bindWorkspaceComponentsRemoved(
4183 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4184 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004185 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004186 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004187 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004188 }
Winson Chungd64d1762013-08-20 14:37:16 -07004189 };
4190 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004191 return;
4192 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004193 if (!packageNames.isEmpty()) {
4194 mWorkspace.removeItemsByPackageName(packageNames, user);
4195 }
4196 if (!components.isEmpty()) {
4197 mWorkspace.removeItemsByComponentName(components, user);
4198 }
4199 // Notify the drag controller
4200 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004201
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004202 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004203
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004204 @Override
4205 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4206 Runnable r = new Runnable() {
4207 public void run() {
4208 bindAppInfosRemoved(appInfos);
4209 }
4210 };
4211 if (waitUntilResume(r)) {
4212 return;
Joe Onorato36115782010-06-17 13:28:48 -04004213 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004214
Winson Chungdf95eb12013-10-16 14:57:07 -07004215 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004216 if (mAppsView != null) {
4217 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004218 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004219 }
Joe Onoratobe386092009-11-17 17:32:16 -08004220
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004221 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004222 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004223 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004224 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004225 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004226
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004227 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004228 public void bindWidgetsModel(WidgetsModel model) {
4229 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004230 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004231 return;
4232 }
4233
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004234 if (mWidgetsView != null && model != null) {
4235 mWidgetsView.addWidgets(model);
4236 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004237 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004238 }
4239
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004240 @Override
4241 public void notifyWidgetProvidersChanged() {
4242 if (mWorkspace.getState().shouldUpdateWidget) {
4243 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4244 }
4245 }
4246
Winson Chung400438b2011-01-16 17:53:48 -08004247 private int mapConfigurationOriActivityInfoOri(int configOri) {
4248 final Display d = getWindowManager().getDefaultDisplay();
4249 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4250 switch (d.getRotation()) {
4251 case Surface.ROTATION_0:
4252 case Surface.ROTATION_180:
4253 // We are currently in the same basic orientation as the natural orientation
4254 naturalOri = configOri;
4255 break;
4256 case Surface.ROTATION_90:
4257 case Surface.ROTATION_270:
4258 // We are currently in the other basic orientation to the natural orientation
4259 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4260 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4261 break;
4262 }
4263
4264 int[] oriMap = {
4265 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4266 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4267 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4268 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4269 };
4270 // Since the map starts at portrait, we need to offset if this device's natural orientation
4271 // is landscape.
4272 int indexOffset = 0;
4273 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4274 indexOffset = 1;
4275 }
4276 return oriMap[(d.getRotation() + indexOffset) % 4];
4277 }
Adam Cohen446e9402011-09-15 18:21:21 -07004278
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004279 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004280 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004281 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004282 if (Utilities.ATLEAST_JB_MR2) {
4283 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4284 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004285 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4286 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004287 }
Winson Chung4b919f82012-05-01 10:44:08 -07004288 }
4289 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004290
Winson Chung4b919f82012-05-01 10:44:08 -07004291 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004292 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004293 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004294 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004295 } else {
4296 mHandler.postDelayed(new Runnable() {
4297 public void run() {
4298 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4299 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004300 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004301 }
Winson Chung4b919f82012-05-01 10:44:08 -07004302 }
Winson Chung400438b2011-01-16 17:53:48 -08004303 }
4304
Adam Cohenea90f832014-05-21 15:03:34 -07004305 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004306 * To be overridden by subclasses to indicate that there is an activity to launch
4307 * before showing the standard launcher experience.
4308 */
4309 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004310 if (mLauncherCallbacks != null) {
4311 return mLauncherCallbacks.hasFirstRunActivity();
4312 }
Adam Cohen432609a2014-03-13 17:03:22 -07004313 return false;
4314 }
4315
4316 /**
4317 * To be overridden by subclasses to launch any first run activity
4318 */
4319 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004320 if (mLauncherCallbacks != null) {
4321 return mLauncherCallbacks.getFirstRunActivity();
4322 }
Adam Cohen432609a2014-03-13 17:03:22 -07004323 return null;
4324 }
4325
Winson Chunga6945242014-01-08 14:04:34 -08004326 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004327 return !ActivityManager.isRunningInTestHarness() &&
4328 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004329 }
4330
Adam Cohen4a9423d2014-03-28 14:22:31 -07004331 protected boolean hasRunFirstRunActivity() {
4332 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4333 }
4334
Adam Cohen432609a2014-03-13 17:03:22 -07004335 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004336 if (shouldRunFirstRunActivity() &&
4337 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004338 Intent firstRunIntent = getFirstRunActivity();
4339 if (firstRunIntent != null) {
4340 startActivity(firstRunIntent);
4341 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004342 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004343 }
4344 }
Adam Cohen432609a2014-03-13 17:03:22 -07004345 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004346 }
4347
4348 private void markFirstRunActivityShown() {
4349 SharedPreferences.Editor editor = mSharedPrefs.edit();
4350 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4351 editor.apply();
4352 }
4353
Adam Cohen432609a2014-03-13 17:03:22 -07004354 /**
4355 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4356 * screen that must be displayed and dismissed.
4357 */
4358 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004359 if (mLauncherCallbacks != null) {
4360 return mLauncherCallbacks.hasDismissableIntroScreen();
4361 }
Adam Cohen432609a2014-03-13 17:03:22 -07004362 return false;
4363 }
4364
4365 /**
4366 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4367 */
4368 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004369 if (mLauncherCallbacks != null) {
4370 return mLauncherCallbacks.getIntroScreen();
4371 }
Adam Cohen432609a2014-03-13 17:03:22 -07004372 return null;
4373 }
4374
4375 /**
4376 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4377 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4378 */
4379 private boolean shouldShowIntroScreen() {
4380 return hasDismissableIntroScreen() &&
4381 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4382 }
4383
4384 protected void showIntroScreen() {
4385 View introScreen = getIntroScreen();
4386 changeWallpaperVisiblity(false);
4387 if (introScreen != null) {
4388 mDragLayer.showOverlayView(introScreen);
4389 }
4390 }
4391
Winson Chung5ffd51d2015-06-25 11:36:50 -07004392 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004393 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004394 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004395 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004396 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004397 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004398 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4399 if (userHandle != null) {
4400 user = UserHandleCompat.fromUser(userHandle);
4401 }
4402 }
4403 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004404 }
4405
4406 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004407 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004408 if (user == null) {
4409 user = UserHandleCompat.myUserHandle();
4410 }
4411
4412 // Called from search suggestion, add the profile extra to the intent to ensure that we
4413 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004414 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004415 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004416 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004417 return null;
4418 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004419 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004420 }
4421
Winson Chung5ffd51d2015-06-25 11:36:50 -07004422 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004423 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4424 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004425 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004426 UserHandleCompat.myUserHandle());
4427 }
4428
Winson Chung5ffd51d2015-06-25 11:36:50 -07004429 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004430 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4431 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004432 mWorkspace.onExternalDragStartedWithItem(dragView);
4433 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004434 }
4435
Winson Chung5ffd51d2015-06-25 11:36:50 -07004436 protected void moveWorkspaceToDefaultScreen() {
4437 mWorkspace.moveToDefaultScreen(false);
4438 }
4439
Winson Chung80baf5a2010-08-09 16:03:15 -07004440 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004441 * Returns a FastBitmapDrawable with the icon, accurately sized.
4442 */
4443 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4444 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4445 d.setFilterBitmap(true);
4446 resizeIconDrawable(d);
4447 return d;
4448 }
4449
4450 /**
4451 * Resizes an icon drawable to the correct icon size.
4452 */
Winson5fbe0742015-09-30 15:33:00 -07004453 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004454 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004455 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004456 }
4457
4458 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004459 * Prints out out state for debugging.
4460 */
4461 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004462 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004463 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004464 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4465 Log.d(TAG, "mRestoring=" + mRestoring);
4466 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004467 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004468 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004469
Daniel Sandler325dc232013-06-05 22:57:57 -04004470 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004471 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004472
4473 @Override
4474 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4475 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004476 // Dump workspace
4477 writer.println(prefix + "Workspace Items");
4478 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4479 writer.println(prefix + " Homescreen " + i);
4480
4481 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4482 for (int j = 0; j < layout.getChildCount(); j++) {
4483 Object tag = layout.getChildAt(j).getTag();
4484 if (tag != null) {
4485 writer.println(prefix + " " + tag.toString());
4486 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004487 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004488 }
Sunny Goyala1365452015-10-01 15:46:24 -07004489
4490 writer.println(prefix + " Hotseat");
4491 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4492 for (int j = 0; j < layout.getChildCount(); j++) {
4493 Object tag = layout.getChildAt(j).getTag();
4494 if (tag != null) {
4495 writer.println(prefix + " " + tag.toString());
4496 }
4497 }
4498
Sunny Goyal713edfc2016-05-06 09:58:34 -07004499 try {
4500 FileLog.flushAll(writer);
4501 } catch (Exception e) {
4502 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004503 }
4504
Adam Cohen9211d422014-10-07 18:14:53 -07004505 if (mLauncherCallbacks != null) {
4506 mLauncherCallbacks.dump(prefix, fd, writer, args);
4507 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004508 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004509
Adam Cohen59400422014-03-05 18:07:04 -08004510 public static CustomAppWidget getCustomAppWidget(String name) {
4511 return sCustomAppWidgets.get(name);
4512 }
4513
4514 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4515 return sCustomAppWidgets;
4516 }
4517
Sunny Goyal29538332016-02-18 09:10:19 -08004518 public static List<View> getFolderContents(View icon) {
4519 if (icon instanceof FolderIcon) {
4520 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4521 } else {
4522 return Collections.EMPTY_LIST;
4523 }
4524 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004525
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004526 public static Launcher getLauncher(Context context) {
4527 if (context instanceof Launcher) {
4528 return (Launcher) context;
4529 }
4530 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4531 }
4532
Sunny Goyal745bad92016-05-02 10:54:12 -07004533 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4534
4535 @Override
4536 public void onSharedPreferenceChanged(
4537 SharedPreferences sharedPreferences, String key) {
4538 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4539 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4540 if (!waitUntilResume(this, true)) {
4541 run();
4542 }
4543 }
4544 }
4545
4546 @Override
4547 public void run() {
4548 setOrientation();
4549 }
4550 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004551}