blob: 84c29dcfa4495297c294ff49d3b9c278beea4a4f [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;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700119import com.android.launcher3.util.ComponentKey;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700120import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700121import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal322d5562015-06-25 19:35:49 -0700122import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700123import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700124import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700125import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700126import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700127import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700128
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700129import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700130import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700131import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700132import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800133import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700134import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700135import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700136import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700137
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138/**
139 * Default launcher application.
140 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100141public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700142 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
143 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700144 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700145 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700146 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700148 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700149 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400150 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700151
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700153 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700154 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700155 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
Michael Jurka8b805b12012-04-18 14:23:14 -0700157 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700158 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700159 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700160
Sunny Goyal28c6b962015-10-12 11:42:05 -0700161 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
162
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700163 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
164 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
165 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
166
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700167 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
168
Mathew Inwood876a8462013-06-14 14:12:41 +0100169 /**
170 * IntentStarter uses request codes starting with this. This must be greater than all activity
171 * request codes used internally.
172 */
173 protected static final int REQUEST_LAST = 100;
174
Michael Jurka0a457bf2012-11-19 14:05:05 -0800175 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700176 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800177 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
Winson Chung2672ff92012-05-04 16:22:30 -0700179 // The Intent extra that defines whether to ignore the launch animation
180 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400181 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700182
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700183 public static final String ACTION_APPWIDGET_HOST_RESET =
184 "com.android.launcher3.intent.ACTION_APPWIDGET_HOST_RESET";
185
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 // Type: int
187 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700188 // Type: int
189 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700190 // Type: Content Values / parcelable
191 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700192 // Type: parcelable
193 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000194 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800195 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
Adam Cohen432609a2014-03-13 17:03:22 -0700197 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800198 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
199
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700200 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700201 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
202 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700203
Adam Cohen091440a2015-03-18 14:16:05 -0700204 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700205 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700206
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700207 private boolean mIsSafeModeEnabled;
208
Joe Onorato9c1289c2009-08-17 11:03:03 -0400209 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800210 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700211 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700212 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213
Winson Chunga2413752012-04-03 14:22:34 -0700214 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700215 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
216 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700217 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700218
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700219 private final BroadcastReceiver mUiBroadcastReceiver = new BroadcastReceiver() {
220
221 @Override
222 public void onReceive(Context context, Intent intent) {
223 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
224 closeSystemDialogs();
225 } else if (ACTION_APPWIDGET_HOST_RESET.equals(intent.getAction())) {
226 if (mAppWidgetHost != null) {
227 mAppWidgetHost.startListening();
228 }
229 }
230 }
231 };
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800232
Adam Cohen091440a2015-03-18 14:16:05 -0700233 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700234 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700235 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800236 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700237
238 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700239
Sunny Goyalffe83f12014-08-14 17:39:34 -0700240 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700241 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700242
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700243 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800244 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800245 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700246
Michael Jurka0280c3b2010-09-17 15:00:07 -0700247 private int[] mTmpAddItemCellCoordinates = new int[2];
248
Sunny Goyal316490e2015-06-02 09:38:28 -0700249 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800250 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700251
Michael Jurka838a4ca2011-02-07 13:33:06 -0800252 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700253 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700254
Sunny Goyal47328fd2016-05-25 18:56:41 -0700255 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700256
257 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700258 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700259 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700260
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700261 // Main container view and the model for the widget tray screen.
262 @Thunk WidgetsContainerView mWidgetsView;
263 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700264
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700266 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
267 // scroll issues (because the workspace may not have been measured yet) and extra work.
268 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
269 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270
271 private SpannableStringBuilder mDefaultKeySsb = null;
272
Adam Cohen091440a2015-03-18 14:16:05 -0700273 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400274
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800275 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 private boolean mRestoring;
277 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700278 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279
Michael Jurka1e2f4652013-07-08 18:03:46 -0700280 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700281 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700282 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700283
Joe Onorato9c1289c2009-08-17 11:03:03 -0400284 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800285 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700286 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700287 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Adam Cohen091440a2015-03-18 14:16:05 -0700288 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800289 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700290 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800291 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400292
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700293 /** Maps launcher activity components to their list of shortcut ids. */
294 private MultiHashMap<ComponentKey, String> mDeepShortcutMap = new MultiHashMap<>();
295
Sunny Goyal639e9062015-08-19 19:17:06 -0700296 private LauncherClings mClings;
297
Adam Cohen61f560d2013-09-30 15:58:20 -0700298 private View.OnTouchListener mHapticFeedbackTouchListener;
299
Adam Cohended9f8d2010-11-03 13:25:16 -0700300 // Related to the auto-advancing of widgets
301 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700302 private static final int ADVANCE_INTERVAL = 20000;
303 private static final int ADVANCE_STAGGER = 250;
304
305 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700306 private long mAutoAdvanceSentTime;
307 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700308 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700309
Winson Chung400438b2011-01-16 17:53:48 -0800310 // Determines how long to wait after a rotation before restoring the screen orientation to
311 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700312 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800313
Adam Cohen091440a2015-03-18 14:16:05 -0700314 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700315
Adam Cohen1462de32012-07-24 22:34:36 -0700316 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700317 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700318
Winson Chung46353de2012-02-16 14:05:10 -0800319 // We only want to get the SharedPreferences once since it does an FS stat each time we get
320 // it from the context.
321 private SharedPreferences mSharedPrefs;
322
Winson Chungf0c6ae02012-03-21 16:10:31 -0700323 // Holds the page that we need to animate to, and the icon views that we need to animate up
324 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700325 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700326 private Bitmap mFolderIconBitmap;
327 private Canvas mFolderIconCanvas;
328 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700329
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700330 private DeviceProfile mDeviceProfile;
331
Adam Cohen4b66bf32015-09-18 12:15:19 -0700332 private boolean mMoveToDefaultScreenFromNewIntent;
333
Winson Chung13eb5272015-05-11 16:30:13 -0700334 // This is set to the view that launched the activity that navigated the user away from
335 // launcher. Since there is no callback for when the activity has finished launching, enable
336 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800337 private BubbleTextView mWaitingForResume;
338
Adam Cohen59400422014-03-05 18:07:04 -0800339 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
340 new HashMap<String, CustomAppWidget>();
341
Adam Cohen59400422014-03-05 18:07:04 -0800342 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700343 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
344 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800345 }
346 }
347
Adam Cohen091440a2015-03-18 14:16:05 -0700348 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700349 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700350 if (mWorkspace != null) {
351 mWorkspace.buildPageHardwareLayers();
352 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700353 }
354 };
355
Adam Cohendb364c32014-05-20 14:23:40 -0700356 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800357
Adam Cohen091440a2015-03-18 14:16:05 -0700358 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800359 int requestCode;
360 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700361 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700362 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800363 int cellX;
364 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700365 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800366 }
367
Hyunyoung Songaa953652016-04-19 18:30:24 -0700368 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800369
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700370 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700371 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400372
373 @Thunk void setOrientation() {
374 if (mRotationEnabled) {
375 unlockScreenOrientation(true);
376 } else {
377 setRequestedOrientation(
378 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700379 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400380 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700381
Sunny Goyal745bad92016-05-02 10:54:12 -0700382 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700383
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 @Override
385 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700386 if (DEBUG_STRICT_MODE) {
387 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
388 .detectDiskReads()
389 .detectDiskWrites()
390 .detectNetwork() // or .detectAll() for all detectable problems
391 .penaltyLog()
392 .build());
393 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
394 .detectLeakedSqlLiteObjects()
395 .detectLeakedClosableObjects()
396 .penaltyLog()
397 .penaltyDeath()
398 .build());
399 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700400 if (LauncherAppState.PROFILE_STARTUP) {
401 Trace.beginSection("Launcher-onCreate");
402 }
Winson Chunga2413752012-04-03 14:22:34 -0700403
Adam Cohen9211d422014-10-07 18:14:53 -0700404 if (mLauncherCallbacks != null) {
405 mLauncherCallbacks.preOnCreate();
406 }
407
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400409
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400410 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700411
Adam Cohen2e6da152015-05-06 11:42:25 -0700412 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700413 mDeviceProfile = getResources().getConfiguration().orientation
414 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700415 app.getInvariantDeviceProfile().landscapeProfile
416 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700417
Sunny Goyalf7258242015-10-19 16:59:07 -0700418 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700419 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800420 mModel = app.setLauncher(this);
421 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700422 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700423
Joe Onorato41a12d22009-10-31 18:30:00 -0400424 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700425 mAllAppsController = new AllAppsTransitionController(this);
426 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700427
Sunny Goyalffe83f12014-08-14 17:39:34 -0700428 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700429
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700430 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
431 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700432
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700433 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
434 // this also ensures that any synchronous binding below doesn't re-trigger another
435 // LauncherModel load.
436 mPaused = false;
437
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700439
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700441 mDeviceProfile.layout(this);
Tony Wickham827cef22016-03-17 15:39:39 -0700442 mExtractedColors = new ExtractedColors();
443 loadExtractedColorsAndColorItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444
Tony Wickhame2217252016-03-22 16:34:23 -0700445 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
446 .addAccessibilityStateChangeListener(this);
447
Joe Onorato7c312c12009-08-13 21:36:53 -0700448 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700449
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 mSavedState = savedInstanceState;
451 restoreState(mSavedState);
452
Sunny Goyale26d1002016-06-20 14:52:14 -0700453 if (LauncherAppState.PROFILE_STARTUP) {
454 Trace.endSection();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 }
456
457 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700458 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700459 // If the user leaves launcher, then we should just load items asynchronously when
460 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700461 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700462 } else {
463 // We only load the page synchronously if the user rotates (or triggers a
464 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700465 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700466 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467 }
468
469 // For handling default keys
470 mDefaultKeySsb = new SpannableStringBuilder();
471 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800472
473 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700474 filter.addAction(ACTION_APPWIDGET_HOST_RESET);
475 registerReceiver(mUiBroadcastReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800476
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800477 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700478 // In case we are on a device with locked rotation, we should look at preferences to check
479 // if the user has specifically allowed rotation.
480 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700481 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Sunny Goyal745bad92016-05-02 10:54:12 -0700482 mRotationPrefChangeHandler = new RotationPrefChangeHandler();
483 mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700484 }
485
486 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
487 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400488 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700489
Adam Cohen9211d422014-10-07 18:14:53 -0700490 if (mLauncherCallbacks != null) {
491 mLauncherCallbacks.onCreate(savedInstanceState);
492 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700493
494 if (shouldShowIntroScreen()) {
495 showIntroScreen();
496 } else {
497 showFirstRunActivity();
498 showFirstRunClings();
499 }
Adam Cohen9211d422014-10-07 18:14:53 -0700500 }
501
Sunny Goyal7779d622015-06-11 16:18:39 -0700502 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700503 public void onExtractedColorsChanged() {
504 loadExtractedColorsAndColorItems();
505 }
506
507 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700508 // TODO: do this in pre-N as well, once the extraction part is complete.
509 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700510 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700511 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700512 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham827cef22016-03-17 15:39:39 -0700513 }
514 }
515
Adam Cohen9211d422014-10-07 18:14:53 -0700516 private LauncherCallbacks mLauncherCallbacks;
517
518 public void onPostCreate(Bundle savedInstanceState) {
519 super.onPostCreate(savedInstanceState);
520 if (mLauncherCallbacks != null) {
521 mLauncherCallbacks.onPostCreate(savedInstanceState);
522 }
523 }
524
Sunny Goyal32554d12015-12-03 15:31:25 -0800525 /**
526 * Call this after onCreate to set or clear overlay.
527 */
528 public void setLauncherOverlay(LauncherOverlay overlay) {
529 if (overlay != null) {
530 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
531 }
532 mWorkspace.setLauncherOverlay(overlay);
533 }
534
Adam Cohen9211d422014-10-07 18:14:53 -0700535 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
536 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700537 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700538 private boolean mWorkspaceImportanceStored = false;
539 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700540 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800541 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700542 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
543
544 @Override
545 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700546 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700547 return;
548 }
549 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700550 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700551 if (mWorkspace != null) {
552 mWorkspaceImportanceForAccessibility =
553 mWorkspace.getImportantForAccessibility();
554 mWorkspace.setImportantForAccessibility(
555 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
556 mWorkspaceImportanceStored = true;
557 }
558 if (mHotseat != null) {
559 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
560 mHotseat.setImportantForAccessibility(
561 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
562 mHotseatImportanceStored = true;
563 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700564 }
565
566 @Override
567 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700568 if (mWorkspaceImportanceStored && mWorkspace != null) {
569 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
570 }
571 if (mHotseatImportanceStored && mHotseat != null) {
572 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
573 }
574 mWorkspaceImportanceStored = false;
575 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700576 }
577 });
Adam Cohen9211d422014-10-07 18:14:53 -0700578 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700579 }
580
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700581 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700582 public void onLauncherProviderChange() {
583 if (mLauncherCallbacks != null) {
584 mLauncherCallbacks.onLauncherProviderChange();
585 }
586 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700587
Adam Cohen9211d422014-10-07 18:14:53 -0700588 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700589 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700590 if (mLauncherCallbacks != null) {
591 return mLauncherCallbacks.hasCustomContentToLeft();
592 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700593 return false;
594 }
595
Dave Hawkeya8881582013-09-17 15:55:33 -0600596 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500597 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600598 * {@link #addToCustomContentPage}. This will only be invoked if
599 * {@link #hasCustomContentToLeft()} is {@code true}.
600 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500601 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700602 if (mLauncherCallbacks != null) {
603 mLauncherCallbacks.populateCustomContentContainer();
604 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600605 }
606
607 /**
608 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
609 * ensure the custom content page is added or removed if necessary.
610 */
611 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600612 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600613 // Not bound yet, wait for bindScreens to be called.
614 return;
615 }
616
617 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
618 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500619 mWorkspace.createCustomContentContainer();
620 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600621 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
622 mWorkspace.removeCustomContentPage();
623 }
624 }
625
Hyunyoung Songaa953652016-04-19 18:30:24 -0700626 public UserEventDispatcher getUserEventDispatcher() {
627 if (mLauncherCallbacks != null) {
628 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
629 if (dispatcher != null) {
630 return dispatcher;
631 }
632 }
633
Sunny Goyal64976d52016-06-20 14:56:28 -0700634 // Logger object is a singleton and does not have to be coupled with the foreground
635 // activity. Since most user event logging is done on the UI, the object is retrieved
636 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700637 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700638 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700639 }
640 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800641 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100642
Hyunyoung Song3f471442015-04-08 19:01:34 -0700643 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700644 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
645 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700646 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700647 }
648
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000649 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700650 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
651 // This cast is safe as long as the id < 0x00FFFFFF
652 // Since we jail all the dynamically generated views, there should be no clashes
653 // with any other views.
654 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000655 }
656
657 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700658 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
659 * a configuration step, this allows the proper animations to run after other transitions.
660 */
Adam Cohendb364c32014-05-20 14:23:40 -0700661 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700662 long screenId = args.screenId;
663 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
664 // When the screen id represents an actual screen (as opposed to a rank) we make sure
665 // that the drop page actually exists.
666 screenId = ensurePendingDropLayoutExists(args.screenId);
667 }
Adam Cohendb364c32014-05-20 14:23:40 -0700668
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800669 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800670 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700671 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700672 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700673 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800674 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700675 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700676 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700677 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyald478c832016-04-01 12:04:16 -0700678 completeRestoreAppWidget(args.appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700679 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700680 case REQUEST_BIND_PENDING_APPWIDGET: {
681 int widgetId = args.appWidgetId;
682 LauncherAppWidgetInfo info =
683 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
684 if (info != null) {
685 // Since the view was just bound, also launch the configure activity if needed
686 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
687 .getLauncherAppWidgetInfo(widgetId);
688 if (provider != null && provider.configure != null) {
689 startRestoredWidgetReconfigActivity(provider, info);
690 }
691 }
692 break;
693 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800694 }
Michael Jurka27614d22012-04-02 06:40:22 -0700695 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
696 // if you turned the screen off and then back while in All Apps, Launcher would not
697 // return to the workspace. Clearing mAddInfo.container here fixes this issue
698 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700699 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800700 }
701
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800702 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700703 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700704 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700705 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700706 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800707 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700708
Adam Cohenad4e15c2013-10-17 16:21:35 -0700709 Runnable exitSpringLoaded = new Runnable() {
710 @Override
711 public void run() {
712 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
713 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
714 }
715 };
716
Michael Jurka8b805b12012-04-18 14:23:14 -0700717 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700718 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700719 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700720 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
721 if (resultCode == RESULT_CANCELED) {
722 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700723 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700724 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700725 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800726 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700727 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700728 }
729 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200730 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
731 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700732 // User could have free-scrolled between pages before picking a wallpaper; make sure
733 // we move to the closest one now.
734 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700735 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200736 }
737 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700738 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200739
Adam Cohened66b2b2012-01-23 17:28:51 -0800740 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700741 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700742
Adam Cohendb364c32014-05-20 14:23:40 -0700743 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800744 // We have special handling for widgets
745 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800746 final int appWidgetId;
747 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
748 : -1;
749 if (widgetId < 0) {
750 appWidgetId = pendingAddWidgetId;
751 } else {
752 appWidgetId = widgetId;
753 }
754
Adam Cohenad4e15c2013-10-17 16:21:35 -0700755 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800756 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700757 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
758 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700759 result = RESULT_CANCELED;
760 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700761 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700762 @Override
763 public void run() {
764 exitSpringLoadedDragModeDelayed(false, 0, null);
765 }
766 };
Adam Cohendb364c32014-05-20 14:23:40 -0700767 if (workspaceLocked) {
768 // No need to remove the empty screen if we're mid-binding, as the
769 // the bind will not add the empty screen.
770 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
771 } else {
772 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
773 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
774 }
Winson Chung5aaab772012-04-27 15:24:02 -0700775 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700776 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700777 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
778 // When the screen id represents an actual screen (as opposed to a rank)
779 // we make sure that the drop page actually exists.
780 mPendingAddInfo.screenId =
781 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
782 }
Adam Cohendb364c32014-05-20 14:23:40 -0700783 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
784
785 dropLayout.setDropPending(true);
786 final Runnable onComplete = new Runnable() {
787 @Override
788 public void run() {
789 completeTwoStageWidgetDrop(resultCode, appWidgetId);
790 dropLayout.setDropPending(false);
791 }
792 };
793 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
794 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
795 } else {
796 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
797 mPendingAddInfo);
798 sPendingAddItem = args;
799 }
Winson Chung5aaab772012-04-27 15:24:02 -0700800 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800801 return;
802 }
803
Sunny Goyald478c832016-04-01 12:04:16 -0700804 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
805 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700806 if (resultCode == RESULT_OK) {
807 // Update the widget view.
808 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
809 pendingAddWidgetId, mPendingAddInfo);
810 if (workspaceLocked) {
811 sPendingAddItem = args;
812 } else {
813 completeAdd(args);
814 }
815 }
816 // Leave the widget in the pending state if the user canceled the configure.
817 return;
818 }
819
Sunny Goyalaad90582015-07-09 14:22:50 -0700820 if (requestCode == REQUEST_CREATE_SHORTCUT) {
821 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
822 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
823 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
824 mPendingAddInfo);
825 if (isWorkspaceLocked()) {
826 sPendingAddItem = args;
827 } else {
828 completeAdd(args);
829 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
830 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
831 }
832 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700833 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
834 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800837 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800838
839 }
840
841 @Override
842 protected void onActivityResult(
843 final int requestCode, final int resultCode, final Intent data) {
844 handleActivityResult(requestCode, resultCode, data);
845 if (mLauncherCallbacks != null) {
846 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
847 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800848 }
849
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600850 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700851 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600852 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700853 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
854 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
855 View v = null;
856 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
857 if (layout != null) {
858 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
859 }
860 Intent intent = sPendingAddItem.intent;
861 sPendingAddItem = null;
862 if (grantResults.length > 0
863 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700864 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700865 } else {
866 // TODO: Show a snack bar with link to settings
867 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
868 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
869 }
870 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600871 if (mLauncherCallbacks != null) {
872 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
873 grantResults);
874 }
875 }
876
Adam Cohendb364c32014-05-20 14:23:40 -0700877 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
878 appWidgetId, ItemInfo info) {
879 PendingAddArguments args = new PendingAddArguments();
880 args.requestCode = requestCode;
881 args.intent = data;
882 args.container = info.container;
883 args.screenId = info.screenId;
884 args.cellX = info.cellX;
885 args.cellY = info.cellY;
886 args.appWidgetId = appWidgetId;
887 return args;
888 }
889
890 /**
891 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
892 *
893 * @param screenId the screen id to check
894 * @return the new screen, or screenId if it exists
895 */
896 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800897 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700898 if (dropLayout == null) {
899 // it's possible that the add screen was removed because it was
900 // empty and a re-bind occurred
901 mWorkspace.addExtraEmptyScreen();
902 return mWorkspace.commitExtraEmptyScreen();
903 } else {
904 return screenId;
905 }
906 }
907
Adam Cohen091440a2015-03-18 14:16:05 -0700908 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800909 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800910 Runnable onCompleteRunnable = null;
911 int animationType = 0;
912
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700913 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800914 if (resultCode == RESULT_OK) {
915 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
916 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700917 mPendingAddWidgetInfo);
918 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800919 onCompleteRunnable = new Runnable() {
920 @Override
921 public void run() {
922 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700923 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700924 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
925 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800926 }
927 };
928 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800929 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800930 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800931 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700932 if (mDragLayer.getAnimatedView() != null) {
933 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
934 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
935 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700936 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700937 // The animated view may be null in the case of a rotation during widget configuration
938 onCompleteRunnable.run();
939 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 }
941
942 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700943 protected void onStop() {
944 super.onStop();
945 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700946
947 if (mLauncherCallbacks != null) {
948 mLauncherCallbacks.onStop();
949 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700950 }
951
952 @Override
953 protected void onStart() {
954 super.onStart();
955 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700956
957 if (mLauncherCallbacks != null) {
958 mLauncherCallbacks.onStart();
959 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700960 }
961
962 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200964 long startTime = 0;
965 if (DEBUG_RESUME_TIME) {
966 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400967 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200968 }
Adam Cohen9211d422014-10-07 18:14:53 -0700969
970 if (mLauncherCallbacks != null) {
971 mLauncherCallbacks.preOnResume();
972 }
973
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700975 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700976
Winson Chung4a2afa32012-07-19 14:53:05 -0700977 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700978 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700979 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800980 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700981 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700982 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700983 // view after launching an app, as they may be depending on the UI to be static to
984 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700985 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700986 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800987 } else if (mOnResumeState == State.WIDGETS) {
988 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700989 }
990 mOnResumeState = State.NONE;
991
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700992 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700993 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
994 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700995
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800996 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700997 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700998 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -0700999 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001000 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001001 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001003 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001004 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1005 // execute them here
1006 long startTimeCallbacks = 0;
1007 if (DEBUG_RESUME_TIME) {
1008 startTimeCallbacks = System.currentTimeMillis();
1009 }
1010
Michael Jurka1e2f4652013-07-08 18:03:46 -07001011 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1012 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001013 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001014 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001015 if (DEBUG_RESUME_TIME) {
1016 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1017 (System.currentTimeMillis() - startTimeCallbacks));
1018 }
Michael Jurka447bf842013-05-15 14:52:15 +02001019 }
Michael Jurka54554252013-08-01 12:52:23 +02001020 if (mOnResumeCallbacks.size() > 0) {
1021 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1022 mOnResumeCallbacks.get(i).run();
1023 }
1024 mOnResumeCallbacks.clear();
1025 }
Winson Chunge4e50662012-01-23 14:45:13 -08001026
1027 // Reset the pressed state of icons that were locked in the press state while activities
1028 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001029 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001030 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001031 mWaitingForResume.setStayPressed(false);
1032 }
Winson Chung82963d52013-10-09 11:20:57 -07001033
Adam Cohen06dff352012-06-01 17:17:08 -07001034 // It is possible that widgets can receive updates while launcher is not in the foreground.
1035 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1036 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1037 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001038 if (!isWorkspaceLoading()) {
1039 getWorkspace().reinflateWidgetsIfNecessary();
1040 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001041
Michael Jurka447bf842013-05-15 14:52:15 +02001042 if (DEBUG_RESUME_TIME) {
1043 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1044 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001045
Adam Cohen4b66bf32015-09-18 12:15:19 -07001046 // We want to suppress callbacks about CustomContent being shown if we have just received
1047 // onNewIntent while the user was present within launcher. In that case, we post a call
1048 // to move the user to the main screen (which will occur after onResume). We don't want to
1049 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1050 // suppress here.
1051 if (mWorkspace.getCustomContentCallbacks() != null
1052 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001053 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001054 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001055 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1056 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001057 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001058 }
1059 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001060 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001061 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001062 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001063
Sunny Goyal756adbc2015-04-16 15:20:51 -07001064 if (!isWorkspaceLoading()) {
1065 // Process any items that were added while Launcher was away.
1066 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1067 }
Adam Cohen9211d422014-10-07 18:14:53 -07001068
1069 if (mLauncherCallbacks != null) {
1070 mLauncherCallbacks.onResume();
1071 }
Adam Cohen06dff352012-06-01 17:17:08 -07001072 }
1073
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001075 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001076 // Ensure that items added to Launcher are queued until Launcher returns
1077 InstallShortcutReceiver.enableInstallQueue();
1078
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001079 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001080 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001081 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001082 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001083
1084 // We call onHide() aggressively. The custom content callbacks should be able to
1085 // debounce excess onHide calls.
1086 if (mWorkspace.getCustomContentCallbacks() != null) {
1087 mWorkspace.getCustomContentCallbacks().onHide();
1088 }
Romain Guycbb89e42009-06-08 15:52:54 -07001089
Adam Cohen9211d422014-10-07 18:14:53 -07001090 if (mLauncherCallbacks != null) {
1091 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001092 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001093 }
1094
1095 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001096 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1097 // by a onResume or by scrolling otherwise.
1098 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001099
1100 // Custom content is completely hidden
1101 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001102
1103 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1104 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001105
1106 // Indicates whether the user is allowed to scroll away from the custom content.
1107 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001108 }
1109
Adam Cohenc2d6e892014-10-16 09:49:24 -07001110 public interface LauncherOverlay {
1111
1112 /**
1113 * Touch interaction leading to overscroll has begun
1114 */
1115 public void onScrollInteractionBegin();
1116
1117 /**
1118 * Touch interaction related to overscroll has ended
1119 */
1120 public void onScrollInteractionEnd();
1121
1122 /**
1123 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1124 * screen (or in the case of RTL, the rightmost screen).
1125 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001126 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001127
1128 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001129 * Called when the launcher is ready to use the overlay
1130 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001131 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001132 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001133 }
1134
Jun Mukai8af0cd82015-05-12 12:32:12 -07001135 public interface LauncherSearchCallbacks {
1136 /**
1137 * Called when the search overlay is shown.
1138 */
1139 public void onSearchOverlayOpened();
1140
1141 /**
1142 * Called when the search overlay is dismissed.
1143 */
1144 public void onSearchOverlayClosed();
1145 }
1146
Adam Cohenc2d6e892014-10-16 09:49:24 -07001147 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001148 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001149 }
1150
1151 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1152
Sunny Goyalc86df472016-02-25 09:19:38 -08001153 public void onScrollChanged(float progress) {
1154 if (mWorkspace != null) {
1155 mWorkspace.onOverlayScrollChanged(progress);
1156 }
1157 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001158 }
1159
Jorim Jaggid017f882014-01-14 17:08:48 -08001160 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001161 if (mLauncherCallbacks != null) {
1162 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001163 } else {
1164 // On devices with a locked orientation, we will at least have the allow rotation
1165 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001166 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001167 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001168 }
1169
Adam Cohen9211d422014-10-07 18:14:53 -07001170 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001171 CustomContentCallbacks callbacks, String description) {
1172 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001173 }
1174
Adam Cohenedb40762013-07-18 16:45:45 -07001175 // The custom content needs to offset its content to account for the QSB
1176 public int getTopOffsetForCustomContent() {
1177 return mWorkspace.getPaddingTop();
1178 }
1179
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001180 @Override
1181 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001182 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001183 if (mModel.isCurrentCallbacks(this)) {
1184 mModel.stopLoader();
1185 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001186 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1187
Romain Guy13c2e7b2010-03-10 19:45:00 -08001188 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001189 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001190
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001191 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001192 @Override
1193 public void onWindowFocusChanged(boolean hasFocus) {
1194 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001195 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001196
1197 if (mLauncherCallbacks != null) {
1198 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1199 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001200 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001201
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 private boolean acceptFilter() {
1203 final InputMethodManager inputManager = (InputMethodManager)
1204 getSystemService(Context.INPUT_METHOD_SERVICE);
1205 return !inputManager.isFullscreenMode();
1206 }
1207
1208 @Override
1209 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001210 final int uniChar = event.getUnicodeChar();
1211 final boolean handled = super.onKeyDown(keyCode, event);
1212 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1213 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001214 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1215 keyCode, event);
1216 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001217 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001218 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001219 // showSearchDialog()
1220 // If there are multiple keystrokes before the search dialog takes focus,
1221 // onSearchRequested() will be called for every keystroke,
1222 // but it is idempotent, so it's fine.
1223 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 }
1225 }
1226
Joe Onorato8a9625e2010-01-28 15:55:35 -08001227 // Eat the long press event so the keyboard doesn't come up.
1228 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1229 return true;
1230 }
1231
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 return handled;
1233 }
1234
Winson46163472015-09-22 17:31:56 -07001235 @Override
1236 public boolean onKeyUp(int keyCode, KeyEvent event) {
1237 if (keyCode == KeyEvent.KEYCODE_MENU) {
1238 // Ignore the menu key if we are currently dragging or are on the custom content screen
1239 if (!isOnCustomContent() && !mDragController.isDragging()) {
1240 // Close any open folders
1241 closeFolder();
1242
1243 // Stop resizing any widgets
1244 mWorkspace.exitWidgetResizeMode();
1245
1246 // Show the overview mode if we are on the workspace
1247 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1248 !mWorkspace.isSwitchingState()) {
1249 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001250 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001251 }
1252 }
1253 return true;
1254 }
1255 return super.onKeyUp(keyCode, event);
1256 }
1257
Karl Rosaen138a0412009-04-23 19:00:21 -07001258 private String getTypedText() {
1259 return mDefaultKeySsb.toString();
1260 }
1261
1262 private void clearTypedText() {
1263 mDefaultKeySsb.clear();
1264 mDefaultKeySsb.clearSpans();
1265 Selection.setSelection(mDefaultKeySsb, 0);
1266 }
1267
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001269 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1270 * State
1271 */
1272 private static State intToState(int stateOrdinal) {
1273 State state = State.WORKSPACE;
1274 final State[] stateValues = State.values();
1275 for (int i = 0; i < stateValues.length; i++) {
1276 if (stateValues[i].ordinal() == stateOrdinal) {
1277 state = stateValues[i];
1278 break;
1279 }
1280 }
1281 return state;
1282 }
1283
1284 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 * Restores the previous state, if it exists.
1286 *
1287 * @param savedState The previous state.
1288 */
1289 private void restoreState(Bundle savedState) {
1290 if (savedState == null) {
1291 return;
1292 }
1293
Michael Jurka883f55b2010-10-21 15:47:14 -07001294 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001295 if (state == State.APPS || state == State.WIDGETS) {
1296 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001297 }
1298
Adam Cohen21cd0022013-10-09 18:57:02 -07001299 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1300 PagedView.INVALID_RESTORE_PAGE);
1301 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001302 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 }
1304
Sunny Goyal756cd262015-08-20 12:33:21 -07001305 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1306 if (itemValues != null) {
1307 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001308 AppWidgetProviderInfo info = savedState.getParcelable(
1309 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001310 mPendingAddWidgetInfo = info == null ?
1311 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1312
Adam Cohen4637b5a2013-11-04 18:21:24 -08001313 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001314 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 mRestoring = true;
1316 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 }
1318
1319 /**
1320 * Finds all the views we need and configure them properly.
1321 */
1322 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001323 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001324 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001325 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001326 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001327 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001328
Sunny Goyal784f9c32016-03-23 16:56:54 -07001329 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1330 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1331 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001332 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333
Winson Chung4c98d922011-05-31 16:50:48 -07001334 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001335
1336 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001337
Winson Chung3d503fb2011-07-13 17:25:49 -07001338 // Setup the hotseat
1339 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1340 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001341 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001342 }
1343
Winsone9f27272015-10-13 10:47:51 -07001344 // Setup the overview panel
1345 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001346
Winson Chung4c98d922011-05-31 16:50:48 -07001347 // Setup the workspace
1348 mWorkspace.setHapticFeedbackEnabled(false);
1349 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001350 mWorkspace.setup(mDragController);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001351 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001352 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001353
Tony Wickham34d2c912015-09-11 09:27:58 -07001354 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001355 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001356
Winson Chungef7f8742015-06-04 17:18:17 -07001357 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001358 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001359 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001360 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1361 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1362 } else {
1363 mAppsView.setSearchBarController(new DefaultAppSearchController());
1364 }
Craig Mautner360310b2012-10-26 15:13:08 -07001365
Winson Chung3d503fb2011-07-13 17:25:49 -07001366 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001367 mDragController.setDragScoller(mWorkspace);
1368 mDragController.setScrollView(mDragLayer);
1369 mDragController.setMoveTarget(mWorkspace);
1370 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001371 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001372
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001373 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1374
Sunny Goyal322d5562015-06-25 19:35:49 -07001375 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1376 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001377 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 }
1379
Winsone9f27272015-10-13 10:47:51 -07001380 private void setupOverviewPanel() {
1381 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1382
1383 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1384 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1385 @Override
1386 public boolean onLongClick(View v) {
1387 return v.performClick();
1388 }
1389 };
1390
1391 // Bind wallpaper button actions
1392 View wallpaperButton = findViewById(R.id.wallpaper_button);
1393 wallpaperButton.setOnClickListener(new OnClickListener() {
1394 @Override
1395 public void onClick(View view) {
1396 if (!mWorkspace.isSwitchingState()) {
1397 onClickWallpaperPicker(view);
1398 }
1399 }
1400 });
1401 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1402 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1403
1404 // Bind widget button actions
1405 mWidgetsButton = findViewById(R.id.widget_button);
1406 mWidgetsButton.setOnClickListener(new OnClickListener() {
1407 @Override
1408 public void onClick(View view) {
1409 if (!mWorkspace.isSwitchingState()) {
1410 onClickAddWidgetButton(view);
1411 }
1412 }
1413 });
1414 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1415 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1416
1417 // Bind settings actions
1418 View settingsButton = findViewById(R.id.settings_button);
1419 boolean hasSettings = hasSettings();
1420 if (hasSettings) {
1421 settingsButton.setOnClickListener(new OnClickListener() {
1422 @Override
1423 public void onClick(View view) {
1424 if (!mWorkspace.isSwitchingState()) {
1425 onClickSettingsButton(view);
1426 }
1427 }
1428 });
1429 settingsButton.setOnLongClickListener(performClickOnLongClick);
1430 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1431 } else {
1432 settingsButton.setVisibility(View.GONE);
1433 }
1434
1435 mOverviewPanel.setAlpha(0f);
1436 }
1437
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001439 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001440 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001441 */
1442 public void setAllAppsButton(View allAppsButton) {
1443 mAllAppsButton = allAppsButton;
1444 }
1445
Sunny Goyalbb011da2016-06-15 15:42:29 -07001446 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001447 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001448 }
1449
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001450 public View getWidgetsButton() {
1451 return mWidgetsButton;
1452 }
1453
Anjali Koppal5ad44842014-03-10 20:34:39 -07001454 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 * Creates a view representing a shortcut.
1456 *
1457 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001459 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001460 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 }
1462
1463 /**
1464 * Creates a view representing a shortcut inflated from the specified resource.
1465 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 * @param parent The group the shortcut belongs to.
1467 * @param info The data structure describing the shortcut.
1468 *
1469 * @return A View inflated from layoutResId.
1470 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001471 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001472 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001473 parent, false);
1474 favorite.applyFromShortcutInfo(info, mIconCache);
1475 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001477 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 return favorite;
1479 }
1480
1481 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 * Add a shortcut to the workspace.
1483 *
1484 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001486 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001487 int cellY) {
1488 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001489 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001490
Sunny Goyal5c97f512015-05-19 16:03:28 -07001491 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001492 if (info == null) {
1493 return;
1494 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001495 final View view = createShortcut(info);
1496
Sunny Goyal5c97f512015-05-19 16:03:28 -07001497 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001498 // First we check if we already know the exact location where we want to add this item.
1499 if (cellX >= 0 && cellY >= 0) {
1500 cellXY[0] = cellX;
1501 cellXY[1] = cellY;
1502 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001503
1504 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001505 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001506 true, null,null)) {
1507 return;
1508 }
1509 DragObject dragObject = new DragObject();
1510 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001511 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1512 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001513 return;
1514 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001515 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001516 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001517 }
1518
1519 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001520 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001521 return;
1522 }
1523
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001524 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525
1526 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001527 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001528 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 }
1530 }
1531
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001533 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001535 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 */
Adam Cohen091440a2015-03-18 14:16:05 -07001537 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001538 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1539
1540 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001541 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001542 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001543 }
Romain Guycbb89e42009-06-08 15:52:54 -07001544
Adam Cohen59400422014-03-05 18:07:04 -08001545 if (appWidgetInfo.isCustomWidget) {
1546 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001547 }
1548
Adam Cohen59400422014-03-05 18:07:04 -08001549 LauncherAppWidgetInfo launcherInfo;
1550 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1551 launcherInfo.spanX = info.spanX;
1552 launcherInfo.spanY = info.spanY;
1553 launcherInfo.minSpanX = info.minSpanX;
1554 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001555 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001556
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001558 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559
1560 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001561 if (hostView == null) {
1562 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001563 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001564 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001565 hostView.setVisibility(View.VISIBLE);
1566 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001568 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 }
Romain Guycbb89e42009-06-08 15:52:54 -07001570
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001571 private void addAppWidgetToWorkspace(
1572 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001573 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001574 hostView.setTag(item);
1575 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001576
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001577 hostView.setFocusable(true);
1578 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001579
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001580 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001581 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1582
1583 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001584 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001585 }
1586 }
1587
Adam Cohended9f8d2010-11-03 13:25:16 -07001588 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1589 @Override
1590 public void onReceive(Context context, Intent intent) {
1591 final String action = intent.getAction();
1592 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1593 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001594 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001595 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001596
Winson Chungc100e8e2011-08-09 16:02:43 -07001597 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001598 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001599 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001600 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001601 if (!showWorkspace(false)) {
1602 // If we are already on the workspace, then manually reset all apps
1603 mAppsView.reset();
1604 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001605 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001606 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1607 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001608 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001609 }
1610 }
1611 };
1612
1613 @Override
1614 public void onAttachedToWindow() {
1615 super.onAttachedToWindow();
1616
1617 // Listen for broadcasts related to user-presence
1618 final IntentFilter filter = new IntentFilter();
1619 filter.addAction(Intent.ACTION_SCREEN_OFF);
1620 filter.addAction(Intent.ACTION_USER_PRESENT);
1621 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001622 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001623 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001624 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001625
1626 if (mLauncherCallbacks != null) {
1627 mLauncherCallbacks.onAttachedToWindow();
1628 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001629 }
1630
1631 @Override
1632 public void onDetachedFromWindow() {
1633 super.onDetachedFromWindow();
1634 mVisible = false;
1635
Adam Cohend113e0c2010-11-11 10:48:05 -08001636 if (mAttached) {
1637 unregisterReceiver(mReceiver);
1638 mAttached = false;
1639 }
Winson Chungb745afb2015-03-02 11:51:23 -08001640 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001641
1642 if (mLauncherCallbacks != null) {
1643 mLauncherCallbacks.onDetachedFromWindow();
1644 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001645 }
1646
1647 public void onWindowVisibilityChanged(int visibility) {
1648 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001649 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001650 // The following code used to be in onResume, but it turns out onResume is called when
1651 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1652 // is a more appropriate event to handle
1653 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001654 if (!mWorkspaceLoading) {
1655 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001656 // We want to let Launcher draw itself at least once before we force it to build
1657 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001658 // apps is nice and speedy.
1659 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001660 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001661 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001662 if (mStarted) return;
1663 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001664 // We delay the layer building a bit in order to give
1665 // other message processing a time to run. In particular
1666 // this avoids a delay in hiding the IME if it was
1667 // currently shown, because doing that may involve
1668 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001669 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001670 final ViewTreeObserver.OnDrawListener listener = this;
1671 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001672 public void run() {
1673 if (mWorkspace != null &&
1674 mWorkspace.getViewTreeObserver() != null) {
1675 mWorkspace.getViewTreeObserver().
1676 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001677 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001678 }
1679 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001680 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001681 }
1682 });
1683 }
1684 clearTypedText();
1685 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001686 }
1687
Adam Cohen091440a2015-03-18 14:16:05 -07001688 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001689 mHandler.removeMessages(ADVANCE_MSG);
1690 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1691 mHandler.sendMessageDelayed(msg, delay);
1692 mAutoAdvanceSentTime = System.currentTimeMillis();
1693 }
1694
Adam Cohen091440a2015-03-18 14:16:05 -07001695 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001696 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1697 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1698 mAutoAdvanceRunning = autoAdvanceRunning;
1699 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001700 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001701 sendAdvanceMessage(delay);
1702 } else {
1703 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001704 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001705 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1706 }
1707 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001708 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001709 }
1710 }
1711 }
1712
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001713 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1714
Adam Cohended9f8d2010-11-03 13:25:16 -07001715 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001716 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001717 if (msg.what == ADVANCE_MSG) {
1718 int i = 0;
1719 for (View key: mWidgetsToAdvance.keySet()) {
1720 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001721 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001722 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001723 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001724 public void run() {
1725 ((Advanceable) v).advance();
1726 }
1727 }, delay);
1728 }
1729 i++;
1730 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001731 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001732 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001733 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001734 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001735 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001736
Winsonc0b52fe2015-09-09 16:38:15 -07001737 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001738 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001739 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1740 if (v instanceof Advanceable) {
1741 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001742 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001743 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001744 }
1745 }
1746
Winsonc0b52fe2015-09-09 16:38:15 -07001747 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001748 if (mWidgetsToAdvance.containsKey(hostView)) {
1749 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001750 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001751 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001752 }
1753
Hyunyoung Song3f471442015-04-08 19:01:34 -07001754 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001755 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1756 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001757 }
1758
Winson Chunga6945242014-01-08 14:04:34 -08001759 public DragLayer getDragLayer() {
1760 return mDragLayer;
1761 }
1762
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001763 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001764 return mAppsView;
1765 }
1766
Hyunyoung Song3f471442015-04-08 19:01:34 -07001767 public WidgetsContainerView getWidgetsView() {
1768 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001769 }
1770
Winson Chunga6945242014-01-08 14:04:34 -08001771 public Workspace getWorkspace() {
1772 return mWorkspace;
1773 }
1774
1775 public Hotseat getHotseat() {
1776 return mHotseat;
1777 }
1778
Jorim Jaggid017f882014-01-14 17:08:48 -08001779 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001780 return mOverviewPanel;
1781 }
1782
Sunny Goyal47328fd2016-05-25 18:56:41 -07001783 public DropTargetBar getDropTargetBar() {
1784 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001785 }
1786
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001787 public LauncherAppWidgetHost getAppWidgetHost() {
1788 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789 }
Romain Guycbb89e42009-06-08 15:52:54 -07001790
Winson Chunga9abd0e2010-10-27 17:18:37 -07001791 public LauncherModel getModel() {
1792 return mModel;
1793 }
1794
Adam Cohen79d90c52016-04-22 13:29:20 -07001795 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001796 return mSharedPrefs;
1797 }
1798
Adam Cohen2e6da152015-05-06 11:42:25 -07001799 public DeviceProfile getDeviceProfile() {
1800 return mDeviceProfile;
1801 }
1802
Bjorn Bringertc459e522013-06-07 19:36:01 +01001803 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001804 getWindow().closeAllPanels();
1805
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001806 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001807 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001808 }
1809
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001810 @Override
1811 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001812 long startTime = 0;
1813 if (DEBUG_RESUME_TIME) {
1814 startTime = System.currentTimeMillis();
1815 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001816 super.onNewIntent(intent);
1817
1818 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001819 Folder openFolder = mWorkspace.getOpenFolder();
1820 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1821 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1822 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1823 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1824 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001825 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001826 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001827
Adam Cohen6fecd412013-10-02 17:41:50 -07001828 if (mWorkspace == null) {
1829 // Can be cases where mWorkspace is null, this prevents a NPE
1830 return;
1831 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001832 // In all these cases, only animate if we're already on home
1833 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001834
Sunny Goyal935fca12015-10-13 10:19:01 -07001835 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001836 exitSpringLoadedDragMode();
1837
1838 // If we are already on home, then just animate back to the workspace,
1839 // otherwise, just wait until onResume to set the state back to Workspace
1840 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001841 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001842 } else {
1843 mOnResumeState = State.WORKSPACE;
1844 }
1845
1846 final View v = getWindow().peekDecorView();
1847 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001848 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001849 INPUT_METHOD_SERVICE);
1850 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1851 }
1852
Winson Chungb745afb2015-03-02 11:51:23 -08001853 // Reset the apps view
1854 if (!alreadyOnHome && mAppsView != null) {
1855 mAppsView.scrollToTop();
1856 }
1857
Hyunyoung Song3f471442015-04-08 19:01:34 -07001858 // Reset the widgets view
1859 if (!alreadyOnHome && mWidgetsView != null) {
1860 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001861 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001862
Adam Cohen9211d422014-10-07 18:14:53 -07001863 if (mLauncherCallbacks != null) {
1864 mLauncherCallbacks.onHomeIntent();
1865 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 }
Adam Cohened307df2013-10-02 09:37:31 -07001867
Adam Cohen9211d422014-10-07 18:14:53 -07001868 if (mLauncherCallbacks != null) {
1869 mLauncherCallbacks.onNewIntent(intent);
1870 }
Winson15f8b172015-08-19 11:02:39 -07001871
1872 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1873 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1874 // animation.
1875 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001876 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1877 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001878 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1879 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001880
1881 // We use this flag to suppress noisy callbacks above custom content state
1882 // from onResume.
1883 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001884 mWorkspace.post(new Runnable() {
1885 @Override
1886 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001887 if (mWorkspace != null) {
1888 mWorkspace.moveToDefaultScreen(true);
1889 }
Winson15f8b172015-08-19 11:02:39 -07001890 }
1891 });
1892 }
1893 }
1894
1895 if (DEBUG_RESUME_TIME) {
1896 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1897 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001898 }
1899
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001901 public void onRestoreInstanceState(Bundle state) {
1902 super.onRestoreInstanceState(state);
1903 for (int page: mSynchronouslyBoundPages) {
1904 mWorkspace.restoreInstanceStateForChild(page);
1905 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 }
1907
1908 @Override
1909 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001910 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001911 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1912 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001913
Adam Cohen21cd0022013-10-09 18:57:02 -07001914 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001915 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916
Michael Jurka883f55b2010-10-21 15:47:14 -07001917 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001918 // We close any open folder since it will not be re-opened, and we need to make sure
1919 // this state is reflected.
1920 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1921 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001922
Adam Cohendcd297f2013-06-18 13:13:40 -07001923 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001924 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001925 ContentValues itemValues = new ContentValues();
1926 mPendingAddInfo.writeToValues(itemValues);
1927 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001928 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001929 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930 }
1931
Adam Cohen9211d422014-10-07 18:14:53 -07001932 if (mLauncherCallbacks != null) {
1933 mLauncherCallbacks.onSaveInstanceState(outState);
1934 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 }
1936
1937 @Override
1938 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001939 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001940
Winson Chunge7a03942011-08-05 15:05:12 -07001941 // Remove all pending runnables
1942 mHandler.removeMessages(ADVANCE_MSG);
1943 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001944 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001945 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001946
Winson Chungcd2b0142011-06-08 16:02:26 -07001947 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001948 // It's possible to receive onDestroy after a new Launcher activity has
1949 // been created. In this case, don't interfere with the new Launcher.
1950 if (mModel.isCurrentCallbacks(this)) {
1951 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001952 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001953 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001954
Sunny Goyal745bad92016-05-02 10:54:12 -07001955 if (mRotationPrefChangeHandler != null) {
1956 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1957 }
1958
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001960 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001961 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001962 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001964 mAppWidgetHost = null;
1965
1966 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967
1968 TextKeyListener.getInstance().release();
1969
Tony Wickhame2217252016-03-22 16:34:23 -07001970 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1971 .removeAccessibilityStateChangeListener(this);
1972
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001973 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001974
Michael Jurka2ecf9952012-06-18 12:52:28 -07001975 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001976
1977 if (mLauncherCallbacks != null) {
1978 mLauncherCallbacks.onDestroy();
1979 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 }
1981
Sunny Goyalae502842016-06-17 08:43:56 -07001982 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
1983 return mAccessibilityDelegate;
1984 }
1985
Adam Cohena9cf38f2011-05-02 15:36:58 -07001986 public DragController getDragController() {
1987 return mDragController;
1988 }
1989
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 @Override
1991 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07001992 onStartForResult(requestCode);
1993 super.startActivityForResult(intent, requestCode);
1994 }
1995
1996 @Override
1997 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1998 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
1999 onStartForResult(requestCode);
2000 try {
2001 super.startIntentSenderForResult(intent, requestCode,
2002 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2003 } catch (IntentSender.SendIntentException e) {
2004 throw new ActivityNotFoundException();
2005 }
2006 }
2007
2008 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002009 if (requestCode >= 0) {
2010 setWaitingForResult(true);
2011 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002012 }
2013
Winson Chung70d72102011-08-12 11:24:35 -07002014 /**
2015 * Indicates that we want global search for this activity by setting the globalSearch
2016 * argument for {@link #startSearch} to true.
2017 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002018 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002019 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002021
Karl Rosaen138a0412009-04-23 19:00:21 -07002022 if (initialQuery == null) {
2023 // Use any text typed in the launcher as the initial query
2024 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002025 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 if (appSearchData == null) {
2027 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002028 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002030
2031 // TODO send proper bounds.
2032 Rect sourceBounds = null;
Romain Guycbb89e42009-06-08 15:52:54 -07002033
Ian Parkinson047036e2014-09-01 15:40:53 +01002034 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002035 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002036 if (clearTextImmediately) {
2037 clearTypedText();
2038 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002039
2040 // We need to show the workspace after starting the search
2041 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002042 }
2043
Ian Parkinson047036e2014-09-01 15:40:53 +01002044 /**
2045 * Start a text search.
2046 *
2047 * @return {@code true} if the search will start immediately, so any further keypresses
2048 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2049 * to buffer keypresses.
2050 */
2051 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002052 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002053 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2054 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2055 sourceBounds);
2056 }
2057
Michael Jurkaa33411c2012-06-14 16:18:21 -07002058 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002059 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002060 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002061 }
2062
2063 /**
2064 * Starts the global search activity. This code is a copied from SearchManager
2065 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002066 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002067 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002068 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002069 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2070 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2071 if (globalSearchActivity == null) {
2072 Log.w(TAG, "No global search activity found.");
2073 return;
2074 }
2075 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2076 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2077 intent.setComponent(globalSearchActivity);
2078 // Make sure that we have a Bundle to put source in
2079 if (appSearchData == null) {
2080 appSearchData = new Bundle();
2081 } else {
2082 appSearchData = new Bundle(appSearchData);
2083 }
Adam Cohen9211d422014-10-07 18:14:53 -07002084 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002085 if (!appSearchData.containsKey("source")) {
2086 appSearchData.putString("source", getPackageName());
2087 }
2088 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2089 if (!TextUtils.isEmpty(initialQuery)) {
2090 intent.putExtra(SearchManager.QUERY, initialQuery);
2091 }
2092 if (selectInitialQuery) {
2093 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2094 }
2095 intent.setSourceBounds(sourceBounds);
2096 try {
2097 startActivity(intent);
2098 } catch (ActivityNotFoundException ex) {
2099 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2100 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101 }
2102
Yura4f93ec62014-02-04 14:15:21 +00002103 public boolean isOnCustomContent() {
2104 return mWorkspace.isOnOrMovingToCustomContent();
2105 }
2106
Winson Chung70d72102011-08-12 11:24:35 -07002107 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002108 public boolean onPrepareOptionsMenu(Menu menu) {
2109 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002110 if (mLauncherCallbacks != null) {
2111 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2112 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002113 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002114 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002116 @Override
2117 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002118 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002119 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002120 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002121 }
2122
Joe Onorato9c1289c2009-08-17 11:03:03 -04002123 public boolean isWorkspaceLocked() {
2124 return mWorkspaceLoading || mWaitingForResult;
2125 }
2126
Adam Cohen517a7f52014-03-01 12:12:59 -08002127 public boolean isWorkspaceLoading() {
2128 return mWorkspaceLoading;
2129 }
2130
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002131 private void setWorkspaceLoading(boolean value) {
2132 boolean isLocked = isWorkspaceLocked();
2133 mWorkspaceLoading = value;
2134 if (isLocked != isWorkspaceLocked()) {
2135 onWorkspaceLockedChanged();
2136 }
2137 }
2138
2139 private void setWaitingForResult(boolean value) {
2140 boolean isLocked = isWorkspaceLocked();
2141 mWaitingForResult = value;
2142 if (isLocked != isWorkspaceLocked()) {
2143 onWorkspaceLockedChanged();
2144 }
2145 }
2146
Adam Cohen9211d422014-10-07 18:14:53 -07002147 protected void onWorkspaceLockedChanged() {
2148 if (mLauncherCallbacks != null) {
2149 mLauncherCallbacks.onWorkspaceLockedChanged();
2150 }
2151 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002152
Michael Jurka0280c3b2010-09-17 15:00:07 -07002153 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002154 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002155 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002156 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2157 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002158 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002159 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002160
Tony Wickhama0628cc2015-10-14 15:23:04 -07002161 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002162 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002163 if (LOGD) {
2164 Log.d(TAG, "Adding widget from drop");
2165 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002166 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2167 }
2168
Sunny Goyale6b63a32015-01-16 16:14:29 -08002169 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002170 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2171 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002172 if (appWidgetInfo.configure != null) {
2173 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002174 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002175
Bjorn Bringert7984c942009-12-09 15:38:25 +00002176 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002177 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2178 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2179
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002180 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002181 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002182 Runnable onComplete = new Runnable() {
2183 @Override
2184 public void run() {
2185 // Exit spring loaded mode if necessary after adding the widget
2186 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2187 null);
2188 }
2189 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002190 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002191 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002192 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002193 }
2194 }
Romain Guycbb89e42009-06-08 15:52:54 -07002195
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002196 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002197 // Close any folders that may be open.
2198 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002199 mWorkspace.moveToCustomContentScreen(animate);
2200 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002201
2202 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2203 int[] cell, int spanX, int spanY) {
2204 switch (info.itemType) {
2205 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2206 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2207 int span[] = new int[2];
2208 span[0] = spanX;
2209 span[1] = spanY;
2210 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2211 container, screenId, cell, span);
2212 break;
2213 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2214 processShortcutFromDrop(info.componentName, container, screenId, cell);
2215 break;
2216 default:
2217 throw new IllegalStateException("Unknown item type: " + info.itemType);
2218 }
2219 }
2220
Adam Cohenfbba09b2011-07-18 21:43:05 -07002221 /**
2222 * Process a shortcut drop.
2223 *
2224 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002225 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002226 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002227 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002228 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2229 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002230 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002231 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002232 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002233
2234 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002235 mPendingAddInfo.cellX = cell[0];
2236 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002237 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002238
2239 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2240 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002241 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002242 }
2243
Adam Cohenfbba09b2011-07-18 21:43:05 -07002244 /**
2245 * Process a widget drop.
2246 *
2247 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002248 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002249 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002250 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002251 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2252 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002253 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002254 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002255 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002256 mPendingAddInfo.minSpanX = info.minSpanX;
2257 mPendingAddInfo.minSpanY = info.minSpanY;
2258
Adam Cohenfbba09b2011-07-18 21:43:05 -07002259 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002260 mPendingAddInfo.cellX = cell[0];
2261 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002262 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002263 if (span != null) {
2264 mPendingAddInfo.spanX = span[0];
2265 mPendingAddInfo.spanY = span[1];
2266 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002267
Adam Cohened66b2b2012-01-23 17:28:51 -08002268 AppWidgetHostView hostView = info.boundWidget;
2269 int appWidgetId;
2270 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002271 // In the case where we've prebound the widget, we remove it from the DragLayer
2272 if (LOGD) {
2273 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2274 }
2275 getDragLayer().removeView(hostView);
2276
Adam Cohened66b2b2012-01-23 17:28:51 -08002277 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002278 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002279
2280 // Clear the boundWidget so that it doesn't get destroyed.
2281 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002282 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002283 // In this case, we either need to start an activity to get permission to bind
2284 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002285 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002286 Bundle options = info.bindOptions;
2287
Sunny Goyalffe83f12014-08-14 17:39:34 -07002288 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2289 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002290 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002291 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002292 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002293 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002294 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2295 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2296 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002297 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2298 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002299 // TODO: we need to make sure that this accounts for the options bundle.
2300 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002301 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2302 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002303 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002304 }
2305
Adam Cohendcd297f2013-06-18 13:13:40 -07002306 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002307 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002308 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002309 folderInfo.title = getText(R.string.folder_name);
2310
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002311 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002312 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2313 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002314
2315 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002316 FolderIcon newFolder =
2317 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002318 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002319 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002320 // Force measure the new folder icon
2321 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2322 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002323 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002324 }
Romain Guycbb89e42009-06-08 15:52:54 -07002325
Winsonc0b52fe2015-09-09 16:38:15 -07002326 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002327 * Unbinds the view for the specified item, and removes the item and all its children.
2328 *
2329 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002330 * @param itemInfo the {@link ItemInfo} for this view.
2331 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002332 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002333 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002334 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002335 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002336 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2337 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002338 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002339 } else {
2340 mWorkspace.removeWorkspaceItem(v);
2341 }
Winsonc0b52fe2015-09-09 16:38:15 -07002342 if (deleteFromDb) {
2343 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2344 }
2345 } else if (itemInfo instanceof FolderInfo) {
2346 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002347 if (v instanceof FolderIcon) {
2348 ((FolderIcon) v).removeListeners();
2349 }
Winsonc0b52fe2015-09-09 16:38:15 -07002350 mWorkspace.removeWorkspaceItem(v);
2351 if (deleteFromDb) {
2352 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2353 }
2354 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2355 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002356 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002357 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002358 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002359 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002360 }
2361 } else {
2362 return false;
2363 }
2364 return true;
2365 }
2366
2367 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002368 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002369 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002370 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002371 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002372 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002373 // Deleting an app widget ID is a void call but writes to disk before returning
2374 // to the caller...
2375 new AsyncTask<Void, Void, Void>() {
2376 public Void doInBackground(Void ... args) {
2377 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2378 return null;
2379 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002380 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002381 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002382 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002383 }
2384
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002385 @Override
2386 public boolean dispatchKeyEvent(KeyEvent event) {
2387 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2388 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002389 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002390 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002391 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002392 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002393 dumpState();
2394 return true;
2395 }
2396 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002397 }
2398 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2399 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002400 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002401 return true;
2402 }
2403 }
2404
2405 return super.dispatchKeyEvent(event);
2406 }
2407
Joe Onorato88ec0992009-11-19 13:16:06 -08002408 @Override
2409 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002410 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2411 return;
2412 }
2413
Sunny Goyal45478022015-06-08 16:52:41 -07002414 if (mDragController.isDragging()) {
2415 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002416 return;
2417 }
2418
Winson Chungb745afb2015-03-02 11:51:23 -08002419 if (isAppsViewVisible()) {
2420 showWorkspace(true);
2421 } else if (isWidgetsViewVisible()) {
2422 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002423 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002424 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002425 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002426 Folder openFolder = mWorkspace.getOpenFolder();
2427 if (openFolder.isEditingName()) {
2428 openFolder.dismissEditingName();
2429 } else {
2430 closeFolder();
2431 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002432 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002433 mWorkspace.exitWidgetResizeMode();
2434
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002435 // Back button is a no-op here, but give at least some feedback for the button press
2436 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002437 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002438 }
2439
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002440 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002441 * Launches the intent referred by the clicked shortcut.
2442 *
2443 * @param v The view representing the clicked shortcut.
2444 */
2445 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002446 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2447 // view has detached (it's possible for this to happen if the view is removed mid touch).
2448 if (v.getWindowToken() == null) {
2449 return;
2450 }
2451
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002452 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002453 return;
2454 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002455
Adam Cohen1697b792013-09-17 19:08:21 -07002456 if (v instanceof Workspace) {
2457 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002458 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002459 }
2460 return;
2461 }
2462
2463 if (v instanceof CellLayout) {
2464 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002465 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2466 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002467 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002468 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002469 }
2470
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002471 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002472 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002473 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002474 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002475 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002476 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002477 }
Sunny Goyalbb011da2016-06-15 15:42:29 -07002478 } else if (v instanceof PageIndicator || (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002479 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002480 } else if (tag instanceof AppInfo) {
2481 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002482 } else if (tag instanceof LauncherAppWidgetInfo) {
2483 if (v instanceof PendingAppWidgetHostView) {
2484 onClickPendingWidget((PendingAppWidgetHostView) v);
2485 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002486 }
2487 }
2488
Sunny Goyal70660032015-05-14 00:07:08 -07002489 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002490 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002491 return false;
2492 }
2493
Michael Jurkaaf442092010-06-10 17:01:57 -07002494 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002495 * Event handler for the app widget view which has not fully restored.
2496 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002497 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002498 if (mIsSafeModeEnabled) {
2499 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2500 return;
2501 }
2502
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002503 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002504 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002505 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2506 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2507 // This should not happen, as we make sure that an Id is allocated during bind.
2508 return;
2509 }
2510 LauncherAppWidgetProviderInfo appWidgetInfo =
2511 mAppWidgetManager.findProvider(info.providerName, info.user);
2512 if (appWidgetInfo != null) {
2513 mPendingAddWidgetId = info.appWidgetId;
2514 mPendingAddInfo.copyFrom(info);
2515 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002516
Sunny Goyald478c832016-04-01 12:04:16 -07002517 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2518 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingAddWidgetId);
2519 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
2520 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2521 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2522 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2523 }
2524 } else {
2525 LauncherAppWidgetProviderInfo appWidgetInfo =
2526 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2527 if (appWidgetInfo != null) {
2528 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2529 }
Sunny Goyalff572272014-07-23 13:58:07 -07002530 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002531 } else if (info.installProgress < 0) {
2532 // The install has not been queued
2533 final String packageName = info.providerName.getPackageName();
2534 showBrokenAppInstallDialog(packageName,
2535 new DialogInterface.OnClickListener() {
2536 public void onClick(DialogInterface dialog, int id) {
2537 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2538 }
2539 });
2540 } else {
2541 // Download has started.
2542 final String packageName = info.providerName.getPackageName();
2543 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002544 }
2545 }
2546
Sunny Goyald478c832016-04-01 12:04:16 -07002547 private void startRestoredWidgetReconfigActivity(
2548 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
2549 mPendingAddWidgetInfo = provider;
2550 mPendingAddInfo.copyFrom(info);
2551 mPendingAddWidgetId = info.appWidgetId;
2552 mAppWidgetManager.startConfigActivity(provider,
2553 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2554 }
2555
Sunny Goyalff572272014-07-23 13:58:07 -07002556 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002557 * Event handler for the "grid" button that appears on the home screen, which
2558 * enters all apps mode.
2559 *
2560 * @param v The view that was clicked.
2561 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002562 protected void onClickAllAppsButton(View v) {
2563 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002564 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002565 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002566 true /* updatePredictedApps */, false /* focusSearchBar */);
2567 }
2568 }
2569
2570 protected void onLongClickAllAppsButton(View v) {
2571 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2572 if (!isAppsViewVisible()) {
2573 showAppsView(true /* animated */, false /* resetListToTop */,
2574 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002575 }
2576 }
2577
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002578 private void showBrokenAppInstallDialog(final String packageName,
2579 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002580 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002581 .setTitle(R.string.abandoned_promises_title)
2582 .setMessage(R.string.abandoned_promise_explanation)
2583 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2584 .setNeutralButton(R.string.abandoned_clean_this,
2585 new DialogInterface.OnClickListener() {
2586 public void onClick(DialogInterface dialog, int id) {
2587 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2588 mWorkspace.removeAbandonedPromise(packageName, user);
2589 }
2590 })
2591 .create().show();
2592 return;
2593 }
2594
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002595 /**
2596 * Event handler for an app shortcut click.
2597 *
2598 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2599 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002600 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002601 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2602 Object tag = v.getTag();
2603 if (!(tag instanceof ShortcutInfo)) {
2604 throw new IllegalArgumentException("Input must be a Shortcut");
2605 }
2606
2607 // Open shortcut
2608 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002609
2610 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002611 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2612 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002613 // Launch activity anyway, framework will tell the user why the app is suspended.
2614 } else {
2615 int error = R.string.activity_not_available;
2616 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2617 error = R.string.safemode_shortcut_error;
2618 }
2619 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2620 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002621 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002622 }
2623
Chris Wren40c5ed32014-06-24 18:24:23 -04002624 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002625 if ((v instanceof BubbleTextView)
2626 && shortcut.isPromise()
2627 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002628 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002629 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002630 new DialogInterface.OnClickListener() {
2631 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002632 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002633 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002634 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002635 return;
2636 }
2637
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002638 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002639 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002640 }
2641
Sunny Goyala7ce1662016-05-31 15:01:35 -07002642 private void startAppShortcutOrInfoActivity(View v) {
2643 ItemInfo item = (ItemInfo) v.getTag();
2644 Intent intent = item.getIntent();
2645 if (intent == null) {
2646 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002647 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002648 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002649 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002650
2651 if (success && v instanceof BubbleTextView) {
2652 mWaitingForResume = (BubbleTextView) v;
2653 mWaitingForResume.setStayPressed(true);
2654 }
2655 }
2656
2657 /**
2658 * Event handler for a folder icon click.
2659 *
2660 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2661 */
2662 protected void onClickFolderIcon(View v) {
2663 if (LOGD) Log.d(TAG, "onClickFolder");
2664 if (!(v instanceof FolderIcon)){
2665 throw new IllegalArgumentException("Input must be a FolderIcon");
2666 }
2667
2668 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002669 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002670 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002671 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002672 }
Michael Jurka5130e402011-10-13 04:55:35 -07002673 }
2674
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002675 /**
2676 * Event handler for the (Add) Widgets button that appears after a long press
2677 * on the home screen.
2678 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002679 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002680 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002681 if (mIsSafeModeEnabled) {
2682 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2683 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002684 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002685 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002686 }
2687
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002688 /**
2689 * Event handler for the wallpaper picker button that appears after a long press
2690 * on the home screen.
2691 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002692 public void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002693 if (!Utilities.isWallapaperAllowed(this)) {
2694 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2695 return;
2696 }
2697
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002698 String pickerPackage = getString(R.string.wallpaper_picker_package);
2699 if (TextUtils.isEmpty(pickerPackage)) {
2700 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2701 }
2702
Tony Wickham785f7a52015-08-31 17:28:32 -07002703 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2704 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002705 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER)
2706 .setPackage(pickerPackage)
2707 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset),
2708 REQUEST_PICK_WALLPAPER);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002709 }
2710
2711 /**
2712 * Event handler for a click on the settings button that appears after a long press
2713 * on the home screen.
2714 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002715 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002716 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002717 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2718 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002719 }
2720
Adam Cohen61f560d2013-09-30 15:58:20 -07002721 public View.OnTouchListener getHapticFeedbackTouchListener() {
2722 if (mHapticFeedbackTouchListener == null) {
2723 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002724 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002725 @Override
2726 public boolean onTouch(View v, MotionEvent event) {
2727 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2728 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2729 }
2730 return false;
2731 }
2732 };
2733 }
2734 return mHapticFeedbackTouchListener;
2735 }
2736
Tony Wickhame2217252016-03-22 16:34:23 -07002737 @Override
2738 public void onAccessibilityStateChanged(boolean enabled) {
2739 mDragLayer.onAccessibilityStateChanged(enabled);
2740 }
2741
Adam Cohen9211d422014-10-07 18:14:53 -07002742 public void onDragStarted(View view) {
2743 if (isOnCustomContent()) {
2744 // Custom content screen doesn't participate in drag and drop. If on custom
2745 // content screen, move to default.
2746 moveWorkspaceToDefaultScreen();
2747 }
Adam Cohen9211d422014-10-07 18:14:53 -07002748 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002749
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002750 /**
2751 * Called when the user stops interacting with the launcher.
2752 * This implies that the user is now on the homescreen and is not doing housekeeping.
2753 */
Adam Cohen9211d422014-10-07 18:14:53 -07002754 protected void onInteractionEnd() {
2755 if (mLauncherCallbacks != null) {
2756 mLauncherCallbacks.onInteractionEnd();
2757 }
2758 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002759
2760 /**
2761 * Called when the user starts interacting with the launcher.
2762 * The possible interactions are:
2763 * - open all apps
2764 * - reorder an app shortcut, or a widget
2765 * - open the overview mode.
2766 * This is a good time to stop doing things that only make sense
2767 * when the user is on the homescreen and not doing housekeeping.
2768 */
Adam Cohen9211d422014-10-07 18:14:53 -07002769 protected void onInteractionBegin() {
2770 if (mLauncherCallbacks != null) {
2771 mLauncherCallbacks.onInteractionBegin();
2772 }
2773 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002774
Winson Chungcd99cd32015-04-29 11:03:24 -07002775 /** Updates the interaction state. */
2776 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002777 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002778 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002779 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2780 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002781 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002782 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002783 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002784 onInteractionEnd();
2785 }
2786 }
2787
Sunny Goyala7ce1662016-05-31 15:01:35 -07002788 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002789 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002790 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2791 try {
2792 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2793 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2794 // is enabled by default on NYC.
2795 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2796 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002797
2798 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2799 String id = ((ShortcutInfo) info).getDeepShortcutId();
2800 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002801 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2802 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002803 } else {
2804 // Could be launching some bookkeeping activity
2805 startActivity(intent, optsBundle);
2806 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002807 } finally {
2808 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002809 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002810 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002811 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2812 // corresponding permission. Show the appropriate permission prompt if that
2813 // is the case.
2814 if (intent.getComponent() == null
2815 && Intent.ACTION_CALL.equals(intent.getAction())
2816 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2817 PackageManager.PERMISSION_GRANTED) {
2818 // TODO: Rename sPendingAddItem to a generic name.
2819 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2820 0, info);
2821 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2822 REQUEST_PERMISSION_CALL_PHONE);
2823 } else {
2824 // No idea why this was thrown.
2825 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002826 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002827 }
2828 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002829
Sunny Goyala7ce1662016-05-31 15:01:35 -07002830 private Bundle getActivityLaunchOptions(View v) {
2831 if (Utilities.ATLEAST_MARSHMALLOW) {
2832 int left = 0, top = 0;
2833 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2834 if (v instanceof TextView) {
2835 // Launch from center of icon, not entire view
2836 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2837 if (icon != null) {
2838 Rect bounds = icon.getBounds();
2839 left = (width - bounds.width()) / 2;
2840 top = v.getPaddingTop();
2841 width = bounds.width();
2842 height = bounds.height();
2843 }
2844 }
2845 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2846 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2847 // On L devices, we use the device default slide-up transition.
2848 // On L MR1 devices, we use a custom version of the slide-up transition which
2849 // doesn't have the delay present in the device default.
2850 return ActivityOptions.makeCustomAnimation(
2851 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2852 }
2853 return null;
2854 }
2855
2856 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002857 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2858 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2859 return false;
2860 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002861 // Only launch using the new animation if the shortcut has not opted out (this is a
2862 // private contract between launcher and may be ignored in the future).
2863 boolean useLaunchAnimation = (v != null) &&
2864 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2865 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2866
2867 UserHandleCompat user = null;
2868 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2869 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2870 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002871 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002872
2873 // Prepare intent
2874 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2875 if (v != null) {
2876 int[] pos = new int[2];
2877 v.getLocationOnScreen(pos);
2878 intent.setSourceBounds(
2879 new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2880 }
2881 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002882 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2883 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
2884 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002885 // Shortcuts need some special checks due to legacy reasons.
2886 startShortcutIntentSafely(intent, optsBundle, item);
2887 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2888 // Could be launching some bookkeeping activity
2889 startActivity(intent, optsBundle);
2890 } else {
2891 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2892 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2893 }
2894 return true;
2895 } catch (ActivityNotFoundException|SecurityException e) {
2896 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2897 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2898 }
2899 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002900 }
2901
Adam Cohen268c4752012-06-06 17:47:33 -07002902 /**
2903 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2904 * in the DragLayer in the exact absolute location of the original FolderIcon.
2905 */
2906 private void copyFolderIconToImage(FolderIcon fi) {
2907 final int width = fi.getMeasuredWidth();
2908 final int height = fi.getMeasuredHeight();
2909
2910 // Lazy load ImageView, Bitmap and Canvas
2911 if (mFolderIconImageView == null) {
2912 mFolderIconImageView = new ImageView(this);
2913 }
2914 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2915 mFolderIconBitmap.getHeight() != height) {
2916 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2917 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2918 }
2919
2920 DragLayer.LayoutParams lp;
2921 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2922 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2923 } else {
2924 lp = new DragLayer.LayoutParams(width, height);
2925 }
2926
Adam Cohen307fe232012-08-16 17:55:58 -07002927 // The layout from which the folder is being opened may be scaled, adjust the starting
2928 // view size by this scale factor.
2929 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002930 lp.customPosition = true;
2931 lp.x = mRectForFolderAnimation.left;
2932 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002933 lp.width = (int) (scale * width);
2934 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002935
2936 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2937 fi.draw(mFolderIconCanvas);
2938 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002939 if (fi.getFolder() != null) {
2940 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2941 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002942 }
Adam Cohen268c4752012-06-06 17:47:33 -07002943 // Just in case this image view is still in the drag layer from a previous animation,
2944 // we remove it and re-add it.
2945 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2946 mDragLayer.removeView(mFolderIconImageView);
2947 }
2948 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002949 if (fi.getFolder() != null) {
2950 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002951 }
Adam Cohen268c4752012-06-06 17:47:33 -07002952 }
2953
Adam Cohen37b2a492016-04-06 18:36:06 -07002954 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002955 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002956 FolderInfo info = (FolderInfo) fi.getTag();
2957 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2958 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002959 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2960 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002961 }
2962
Adam Cohen268c4752012-06-06 17:47:33 -07002963 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2964 copyFolderIconToImage(fi);
2965 fi.setVisibility(View.INVISIBLE);
2966
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002967 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2968 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002969 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002970 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2971 }
2972 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002973 oa.start();
2974 }
2975
Sunny Goyal935fca12015-10-13 10:19:01 -07002976 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002977 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002978 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002979
Adam Cohen268c4752012-06-06 17:47:33 -07002980 // We remove and re-draw the FolderIcon in-case it has changed
2981 mDragLayer.removeView(mFolderIconImageView);
2982 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08002983
2984 if (cl != null) {
2985 cl.clearFolderLeaveBehind();
2986 }
2987
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002988 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002989 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002990 oa.addListener(new AnimatorListenerAdapter() {
2991 @Override
2992 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002993 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07002994 // Remove the ImageView copy of the FolderIcon and make the original visible.
2995 mDragLayer.removeView(mFolderIconImageView);
2996 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002997 }
2998 }
2999 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003000 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003001 if (!animate) {
3002 oa.end();
3003 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003004 }
3005
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003006 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003007 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003008 * is animated relative to the specified View. If the View is null, no animation
3009 * is played.
3010 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003011 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003012 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003013 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003014
Adam Cohenfb91f302012-06-11 15:45:18 -07003015 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003016 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3017 if (openFolder != null && openFolder != folder) {
3018 // Close any open folder before opening a folder.
3019 closeFolder();
3020 }
3021
Adam Cohena9cf38f2011-05-02 15:36:58 -07003022 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003023
Adam Cohena9cf38f2011-05-02 15:36:58 -07003024 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003025
Sunny Goyalf4066152015-04-15 09:42:19 -07003026 // While the folder is open, the position of the icon cannot change.
3027 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3028
Adam Cohen4554ee12011-08-03 16:13:21 -07003029 // Just verify that the folder hasn't already been added to the DragLayer.
3030 // There was a one-off crash where the folder had a parent already.
3031 if (folder.getParent() == null) {
3032 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003033 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003034 } else {
3035 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3036 folder.getParent() + ").");
3037 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003038 folder.animateOpen();
3039
3040 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003041
3042 // Notify the accessibility manager that this folder "window" has appeared and occluded
3043 // the workspace items
3044 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3045 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003046 }
3047
3048 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003049 closeFolder(true);
3050 }
3051
3052 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003053 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003054 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003055 if (folder.isEditingName()) {
3056 folder.dismissEditingName();
3057 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003058 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003059 }
3060 }
3061
Sunny Goyal935fca12015-10-13 10:19:01 -07003062 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003063 animate &= !Utilities.isPowerSaverOn(this);
3064
Adam Cohen4554ee12011-08-03 16:13:21 -07003065 folder.getInfo().opened = false;
3066
3067 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3068 if (parent != null) {
3069 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003070 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003071 if (fi != null) {
3072 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3073 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003074 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003075 if (animate) {
3076 folder.animateClosed();
3077 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003078 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003079 }
Winson Chung83ca4802013-04-12 15:10:52 -07003080
Sunny Goyal935fca12015-10-13 10:19:01 -07003081 // Notify the accessibility manager that this folder "window" has disappeared and no
3082 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003083 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003084 }
3085
Tony Wickhamdadb3042016-02-24 11:07:00 -08003086 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003087 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003088 if (!isDraggingEnabled()) return false;
3089 if (isWorkspaceLocked()) return false;
3090 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003091
Sunny Goyalbb011da2016-06-15 15:42:29 -07003092 if (v == mAllAppsButton && mAllAppsButton != null) {
Winson Chung76648c52015-07-10 14:33:23 -07003093 onLongClickAllAppsButton(v);
3094 return true;
3095 }
3096
Adam Cohen1697b792013-09-17 19:08:21 -07003097 if (v instanceof Workspace) {
3098 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003099 if (!mWorkspace.isTouchActive()) {
3100 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003101 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3102 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3103 return true;
3104 } else {
3105 return false;
3106 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003107 } else {
3108 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003109 }
Adam Cohen1697b792013-09-17 19:08:21 -07003110 }
3111
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003112 CellLayout.CellInfo longClickCellInfo = null;
3113 View itemUnderLongClick = null;
3114 if (v.getTag() instanceof ItemInfo) {
3115 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003116 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003117 itemUnderLongClick = longClickCellInfo.cell;
3118 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003119 }
3120
Winson Chung3d503fb2011-07-13 17:25:49 -07003121 // The hotseat touch handling does not go through Workspace, and we always allow long press
3122 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003123 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003124 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003125 // User long pressed on empty space
3126 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3127 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003128 if (mWorkspace.isInOverviewMode()) {
3129 mWorkspace.startReordering(v);
3130 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003131 showOverviewMode(true);
Hyunyoung Song645764e2016-06-06 14:19:02 -07003132 mHotseat.requestDisallowInterceptTouchEvent(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003133 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003134 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003135 final boolean isAllAppsButton =
3136 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3137 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3138 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003139 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003140 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003141 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003142 }
3143 }
3144 }
3145 return true;
3146 }
3147
Winson Chung3d503fb2011-07-13 17:25:49 -07003148 boolean isHotseatLayout(View layout) {
3149 return mHotseat != null && layout != null &&
3150 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3151 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003152
Winson Chung3d503fb2011-07-13 17:25:49 -07003153 /**
3154 * Returns the CellLayout of the specified container at the specified screen.
3155 */
Sunny Goyal92820592015-03-02 11:31:35 -08003156 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003157 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3158 if (mHotseat != null) {
3159 return mHotseat.getLayout();
3160 } else {
3161 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003162 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003163 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003164 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003165 }
3166 }
3167
Winson Chungb745afb2015-03-02 11:51:23 -08003168 /**
3169 * For overridden classes.
3170 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003171 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003172 return isAppsViewVisible();
3173 }
3174
3175 public boolean isAppsViewVisible() {
3176 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3177 }
3178
3179 public boolean isWidgetsViewVisible() {
3180 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003181 }
3182
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003183 private void setWorkspaceBackground(int background) {
3184 switch (background) {
3185 case WORKSPACE_BACKGROUND_TRANSPARENT:
3186 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3187 break;
3188 case WORKSPACE_BACKGROUND_BLACK:
3189 getWindow().setBackgroundDrawable(null);
3190 break;
3191 default:
3192 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3193 }
Craig Mautner360310b2012-10-26 15:13:08 -07003194 }
3195
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003196 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003197 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3198 int curflags = getWindow().getAttributes().flags
3199 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3200 if (wpflags != curflags) {
3201 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3202 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003203 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003204 }
3205
Michael Jurkae326f182011-11-21 14:05:46 -08003206 @Override
3207 public void onTrimMemory(int level) {
3208 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003209 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3210 // The widget preview db can result in holding onto over
3211 // 3MB of memory for caching which isn't necessary.
3212 SQLiteDatabase.releaseMemory();
3213
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003214 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003215 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003216 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003217 if (mLauncherCallbacks != null) {
3218 mLauncherCallbacks.onTrimMemory(level);
3219 }
Michael Jurkae326f182011-11-21 14:05:46 -08003220 }
3221
Winson5c6bdbb2015-09-03 11:36:19 -07003222 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003223 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003224 }
3225
Winson5c6bdbb2015-09-03 11:36:19 -07003226 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003227 boolean changed = mState != State.WORKSPACE ||
3228 mWorkspace.getState() != Workspace.State.NORMAL;
3229 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003230 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003231 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003232 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003233
Michael Jurkab3e22d92011-10-31 15:58:33 -07003234 // Set focus to the AppsCustomize button
3235 if (mAllAppsButton != null) {
3236 mAllAppsButton.requestFocus();
3237 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003238 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003239
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003240 // Change the state *after* we've called all the transition code
3241 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003242
Winson Chung5fb63472011-02-02 17:03:37 -08003243 // Resume the auto-advance of widgets
3244 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003245 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003246
Winson Chung0f785722015-04-08 10:27:49 -07003247 if (changed) {
3248 // Send an accessibility event to announce the context change
3249 getWindow().getDecorView()
3250 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003251 }
Winson5c6bdbb2015-09-03 11:36:19 -07003252 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003253 }
3254
Winsone9f27272015-10-13 10:47:51 -07003255 /**
3256 * Shows the overview button.
3257 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07003258 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003259 showOverviewMode(animated, false);
3260 }
3261
3262 /**
3263 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3264 * onto one of the overview panel buttons.
3265 */
3266 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3267 Runnable postAnimRunnable = null;
3268 if (requestButtonFocus) {
3269 postAnimRunnable = new Runnable() {
3270 @Override
3271 public void run() {
3272 // Hitting the menu button when in touch mode does not trigger touch mode to
3273 // be disabled, so if requested, force focus on one of the overview panel
3274 // buttons.
3275 mOverviewPanel.requestFocusFromTouch();
3276 }
3277 };
3278 }
Adam Cohened307df2013-10-02 09:37:31 -07003279 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003280 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003281 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003282 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003283 }
3284
Winson Chungb745afb2015-03-02 11:51:23 -08003285 /**
3286 * Shows the apps view.
3287 */
Hyunyoung Song645764e2016-06-06 14:19:02 -07003288 public void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003289 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003290 if (resetListToTop) {
3291 mAppsView.scrollToTop();
3292 }
Winson Chung4ac30062015-05-08 17:34:17 -07003293 if (updatePredictedApps) {
3294 tryAndUpdatePredictedApps();
3295 }
Winson Chung76648c52015-07-10 14:33:23 -07003296 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003297 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003298
Winson Chungb745afb2015-03-02 11:51:23 -08003299 /**
3300 * Shows the widgets view.
3301 */
3302 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003303 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003304 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003305 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003306 }
Winson Chung76648c52015-07-10 14:33:23 -07003307 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003308
3309 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003310 @Override
3311 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003312 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003313 }
3314 });
Winson Chungb745afb2015-03-02 11:51:23 -08003315 }
3316
3317 /**
3318 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003319 *
3320 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003321 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003322 // TODO: calling method should use the return value so that when {@code false} is returned
3323 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003324 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003325 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3326 mState != State.WIDGETS_SPRING_LOADED) {
3327 return false;
3328 }
3329 if (toState != State.APPS && toState != State.WIDGETS) {
3330 return false;
3331 }
Winson Chungb745afb2015-03-02 11:51:23 -08003332
3333 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003334 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3335 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003336 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003337 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003338 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003339
Michael Jurkab3e22d92011-10-31 15:58:33 -07003340 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003341 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003342
3343 // Pause the auto-advance of widgets until we are out of AllApps
3344 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003345 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003346 closeFolder();
3347
3348 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003349 getWindow().getDecorView()
3350 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003351 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003352 }
3353
Winson Chungcd99cd32015-04-29 11:03:24 -07003354 /**
3355 * Updates the workspace and interaction state on state change, and return the animation to this
3356 * new state.
3357 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003358 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003359 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003360 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003361 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003362 updateInteraction(fromState, toState);
3363 return anim;
3364 }
3365
Jun Mukaif0033da2015-08-04 18:34:30 -07003366 public void onLauncherClingShown() {
3367 // When a launcher cling appears, it should cover the underlying layers, so their focus
3368 // should be blocked.
3369 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3370 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3371 }
3372 }
3373
3374 public void onLauncherClingDismissed() {
3375 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3376 }
3377
Hyunyoung Song3f471442015-04-08 19:01:34 -07003378 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003379 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003380 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003381 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003382 }
Winson Chungb745afb2015-03-02 11:51:23 -08003383
Winson Chung006ee262015-08-03 14:40:11 -07003384 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003385 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003386 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003387
3388 if (isAppsViewVisible()) {
3389 mState = State.APPS_SPRING_LOADED;
3390 } else if (isWidgetsViewVisible()) {
3391 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003392 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003393 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003394 } else {
3395 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003396 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003397 }
Adam Cohen7777d962011-08-18 18:58:38 -07003398
Hyunyoung Song3f471442015-04-08 19:01:34 -07003399 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003400 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003401 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003402
Winson Chunge7a03942011-08-05 15:05:12 -07003403 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003404 @Override
3405 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003406 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003407 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3408 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003409 // Before we show workspace, hide all apps again because
3410 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3411 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003412 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003413 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003414 } else {
3415 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003416 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003417 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003418 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003419 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003420
Tony Wickhame0c33232016-02-08 11:37:04 -08003421 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003422 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3423 || mState == State.WIDGETS_SPRING_LOADED;
3424 }
3425
Michael Jurkad3ef3062010-11-23 16:23:58 -08003426 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003427 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003428 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003429 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003430 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003431 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003432 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3433 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003434 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003435 }
3436
Winson Chung4ac30062015-05-08 17:34:17 -07003437 /**
3438 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3439 * resumed.
3440 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003441 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003442 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003443 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003444 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003445 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003446 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003447 }
3448 }
3449 }
3450
Michael Jurkab3e22d92011-10-31 15:58:33 -07003451 void lockAllApps() {
3452 // TODO
3453 }
3454
3455 void unlockAllApps() {
3456 // TODO
3457 }
3458
Winsonf768d932015-09-25 16:12:35 -07003459 public boolean launcherCallbacksProvidesSearch() {
3460 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3461 }
3462
Michael Jurkad7c28052012-04-27 15:43:36 -07003463 @Override
3464 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003465 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003466 final List<CharSequence> text = event.getText();
3467 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003468 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003469 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003470 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003471 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003472 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003473 } else if (mWorkspace != null) {
3474 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003475 } else {
3476 text.add(getString(R.string.all_apps_home_button_label));
3477 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003478 return result;
3479 }
3480
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003481 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003482 * If the activity is currently paused, signal that we need to run the passed Runnable
3483 * in onResume.
3484 *
3485 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003486 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3487 * wrong when we're not running, and if the activity comes back to what the configuration was
3488 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003489 *
3490 * Implementation of the method from LauncherModel.Callbacks.
3491 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003492 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003493 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003494 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003495 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003496 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003497 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003498 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003499 }
3500 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003501 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003502 return true;
3503 } else {
3504 return false;
3505 }
3506 }
3507
Michael Jurkac402cd92013-05-20 15:49:32 +02003508 private boolean waitUntilResume(Runnable run) {
3509 return waitUntilResume(run, false);
3510 }
3511
Michael Jurka1e2f4652013-07-08 18:03:46 -07003512 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003513 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003514 }
3515
Michael Jurka7607c2f2013-04-03 14:33:19 -07003516 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003517 * If the activity is currently paused, signal that we need to re-run the loader
3518 * in onResume.
3519 *
3520 * This needs to be called from incoming places where resources might have been loaded
3521 * while we are paused. That is becaues the Configuration might be wrong
3522 * when we're not running, and if it comes back to what it was when we
3523 * were paused, we are not restarted.
3524 *
3525 * Implementation of the method from LauncherModel.Callbacks.
3526 *
3527 * @return true if we are currently paused. The caller might be able to
3528 * skip some work in that case since we will come back again.
3529 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003530 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003531 public boolean setLoadOnResume() {
3532 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003533 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003534 mOnResumeNeedsLoad = true;
3535 return true;
3536 } else {
3537 return false;
3538 }
3539 }
3540
3541 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003542 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003543 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003544 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003545 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003546 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003547 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003548 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003549 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003550 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003551 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003552
Joe Onorato9c1289c2009-08-17 11:03:03 -04003553 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003554 * Clear any pending bind callbacks. This is called when is loader is planning to
3555 * perform a full rebind from scratch.
3556 */
3557 @Override
3558 public void clearPendingBinds() {
3559 mBindOnResumeCallbacks.clear();
3560 if (mPendingExecutor != null) {
3561 mPendingExecutor.markCompleted();
3562 mPendingExecutor = null;
3563 }
3564 }
3565
3566 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003567 * Refreshes the shortcuts shown on the workspace.
3568 *
3569 * Implementation of the method from LauncherModel.Callbacks.
3570 */
3571 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003572 if (LauncherAppState.PROFILE_STARTUP) {
3573 Trace.beginSection("Starting page bind");
3574 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003575 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003576
Winson Chungd64d1762013-08-20 14:37:16 -07003577 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003578 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003579 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003580
Michael Jurka05bf644e2011-11-30 20:28:53 -08003581 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003582 if (mHotseat != null) {
3583 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003584 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003585 if (LauncherAppState.PROFILE_STARTUP) {
3586 Trace.endSection();
3587 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003588 }
3589
Adam Cohendcd297f2013-06-18 13:13:40 -07003590 @Override
3591 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003592 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003593 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3594 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003595 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3596 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3597 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003598 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3599 // If there are no screens, we need to have an empty screen
3600 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003601 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003602 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003603
3604 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003605 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003606 if (hasCustomContentToLeft()) {
3607 mWorkspace.createCustomContentContainer();
3608 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003609 }
Winson Chung64359a52013-07-08 17:17:08 -07003610 }
3611
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003612 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003613 int count = orderedScreenIds.size();
3614 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003615 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003616 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003617 // No need to bind the first screen, as its always bound.
3618 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3619 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003620 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003621 }
3622
Winson Chungd64d1762013-08-20 14:37:16 -07003623 public void bindAppsAdded(final ArrayList<Long> newScreens,
3624 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003625 final ArrayList<ItemInfo> addAnimated,
3626 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003627 Runnable r = new Runnable() {
3628 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003629 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003630 }
3631 };
3632 if (waitUntilResume(r)) {
3633 return;
3634 }
3635
Winson Chungd64d1762013-08-20 14:37:16 -07003636 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003637 if (newScreens != null) {
3638 bindAddScreens(newScreens);
3639 }
Winson Chungd64d1762013-08-20 14:37:16 -07003640
3641 // We add the items without animation on non-visible pages, and with
3642 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003643 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003644 bindItems(addNotAnimated, 0,
3645 addNotAnimated.size(), false);
3646 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003647 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003648 bindItems(addAnimated, 0,
3649 addAnimated.size(), true);
3650 }
Winson Chungc58497e2013-09-03 17:48:37 -07003651
Winson Chung87412982013-10-03 18:34:14 -07003652 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003653 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003654
Winson Chungb745afb2015-03-02 11:51:23 -08003655 if (addedApps != null && mAppsView != null) {
3656 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003657 }
Winson Chungd64d1762013-08-20 14:37:16 -07003658 }
3659
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003660 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003661 * Bind the items start-end from the list.
3662 *
3663 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003664 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003665 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003666 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3667 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003668 Runnable r = new Runnable() {
3669 public void run() {
3670 bindItems(shortcuts, start, end, forceAnimateIcons);
3671 }
3672 };
3673 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003674 return;
3675 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003676
Winson Chungf0c6ae02012-03-21 16:10:31 -07003677 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003678 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3679 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003680 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003681 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003682 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003683 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003684 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003685
3686 // Short circuit if we are loading dock items for a configuration which has no dock
3687 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3688 mHotseat == null) {
3689 continue;
3690 }
3691
Sunny Goyal639e9062015-08-19 19:17:06 -07003692 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003693 switch (item.itemType) {
3694 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3695 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003696 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003697 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003698 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003699 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003700 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003701 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003702 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003703 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003704 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003705 default:
3706 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003707 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003708
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003709 /*
3710 * Remove colliding items.
3711 */
3712 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3713 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3714 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3715 View v = cl.getChildAt(item.cellX, item.cellY);
3716 Object tag = v.getTag();
3717 String desc = "Collision while binding workspace item: " + item
3718 + ". Collides with " + tag;
3719 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3720 throw (new RuntimeException(desc));
3721 } else {
3722 Log.d(TAG, desc);
3723 LauncherModel.deleteItemFromDatabase(this, item);
3724 continue;
3725 }
3726 }
3727 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003728 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3729 item.cellY, 1, 1);
3730 if (animateIcons) {
3731 // Animate all the applications up now
3732 view.setAlpha(0f);
3733 view.setScaleX(0f);
3734 view.setScaleY(0f);
3735 bounceAnims.add(createNewAppBounceAnimation(view, i));
3736 newShortcutsScreenId = item.screenId;
3737 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003738 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003739
Winson Chung997a9232013-07-24 15:33:46 -07003740 if (animateIcons) {
3741 // Animate to the correct page
3742 if (newShortcutsScreenId > -1) {
3743 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003744 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003745 final Runnable startBounceAnimRunnable = new Runnable() {
3746 public void run() {
3747 anim.playTogether(bounceAnims);
3748 anim.start();
3749 }
3750 };
Winson Chung997a9232013-07-24 15:33:46 -07003751 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003752 // We post the animation slightly delayed to prevent slowdowns
3753 // when we are loading right after we return to launcher.
3754 mWorkspace.postDelayed(new Runnable() {
3755 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003756 if (mWorkspace != null) {
3757 mWorkspace.snapToPage(newScreenIndex);
3758 mWorkspace.postDelayed(startBounceAnimRunnable,
3759 NEW_APPS_ANIMATION_DELAY);
3760 }
Winson Chung94d67682013-09-25 16:29:40 -07003761 }
3762 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003763 } else {
3764 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003765 }
3766 }
Winson Chung64359a52013-07-08 17:17:08 -07003767 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003768 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003769 }
3770
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003771 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3772 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3773 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003774 view.updateAppWidget(null);
3775 view.setOnClickListener(this);
3776 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003777 mWorkspace.requestLayout();
3778 }
3779
Joe Onorato9c1289c2009-08-17 11:03:03 -04003780 /**
3781 * Add the views for a widget to the workspace.
3782 *
3783 * Implementation of the method from LauncherModel.Callbacks.
3784 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003785 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003786 Runnable r = new Runnable() {
3787 public void run() {
3788 bindAppWidget(item);
3789 }
3790 };
3791 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003792 return;
3793 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003794
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003795 if (mIsSafeModeEnabled) {
3796 bindSafeModeWidget(item);
3797 return;
3798 }
3799
Daniel Sandler843e8602010-06-07 14:59:01 -04003800 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3801 if (DEBUG_WIDGETS) {
3802 Log.d(TAG, "bindAppWidget: " + item);
3803 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003804
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003805 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003806
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003807 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3808 // If the provider is not ready, bind as a pending widget.
3809 appWidgetInfo = null;
3810 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3811 // The widget id is not valid. Try to find the widget based on the provider info.
3812 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3813 } else {
3814 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3815 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003816
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003817 // If the provider is ready, but the width is not yet restored, try to restore it.
3818 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3819 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003820 if (appWidgetInfo == null) {
3821 if (DEBUG_WIDGETS) {
3822 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3823 + " belongs to component " + item.providerName
3824 + ", as the povider is null");
3825 }
3826 LauncherModel.deleteItemFromDatabase(this, item);
3827 return;
3828 }
Sunny Goyalff572272014-07-23 13:58:07 -07003829
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003830 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003831 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003832 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3833 // Id has not been allocated yet. Allocate a new id.
3834 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3835 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003836
Sunny Goyald478c832016-04-01 12:04:16 -07003837 // Also try to bind the widget. If the bind fails, the user will be shown
3838 // a click to setup UI, which will ask for the bind permission.
3839 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3840 pendingInfo.spanX = item.spanX;
3841 pendingInfo.spanY = item.spanY;
3842 pendingInfo.minSpanX = item.minSpanX;
3843 pendingInfo.minSpanY = item.minSpanY;
3844 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
3845 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3846 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003847
Sunny Goyald478c832016-04-01 12:04:16 -07003848 // Bind succeeded
3849 if (success) {
3850 // If the widget has a configure activity, it is still needs to set it up,
3851 // otherwise the widget is ready to go.
3852 item.restoreStatus = (appWidgetInfo.configure == null)
3853 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3854 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003855 }
Sunny Goyald478c832016-04-01 12:04:16 -07003856
3857 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003858 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003859 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003860 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003861 // The widget was marked as UI not ready, but there is no configure activity to
3862 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003863 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3864 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003865 }
Sunny Goyalff572272014-07-23 13:58:07 -07003866 }
3867
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003868 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003869 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003870 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3871 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003872 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003873
Sunny Goyal56c73602015-09-25 12:55:01 -07003874 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003875 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003876 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003877 deleteWidgetInfo(item);
3878 return;
3879 }
3880
Sunny Goyal233ee962015-08-03 13:05:01 -07003881 item.minSpanX = appWidgetInfo.minSpanX;
3882 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003883 addAppWidgetToWorkspace(
3884 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3885 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003886 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003887 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003888 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003889 view.updateAppWidget(null);
3890 view.setOnClickListener(this);
3891 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003892 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003893 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003894
Daniel Sandler843e8602010-06-07 14:59:01 -04003895 if (DEBUG_WIDGETS) {
3896 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3897 + (SystemClock.uptimeMillis()-start) + "ms");
3898 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003899 }
3900
Sunny Goyalff572272014-07-23 13:58:07 -07003901 /**
3902 * Restores a pending widget.
3903 *
3904 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003905 */
Sunny Goyald478c832016-04-01 12:04:16 -07003906 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003907 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3908 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3909 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003910 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003911 }
3912
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003913 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003914 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003915
3916 mWorkspace.reinflateWidgetsIfNecessary();
3917 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003918 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003919 }
3920
Adam Cohen1462de32012-07-24 22:34:36 -07003921 public void onPageBoundSynchronously(int page) {
3922 mSynchronouslyBoundPages.add(page);
3923 }
3924
Sunny Goyal527c7d32015-08-28 15:19:36 -07003925 @Override
3926 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3927 if (mPendingExecutor != null) {
3928 mPendingExecutor.markCompleted();
3929 }
3930 mPendingExecutor = executor;
3931 executor.attachTo(this);
3932 }
3933
3934 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3935 if (mPendingExecutor == executor) {
3936 mPendingExecutor = null;
3937 }
3938 }
3939
Joe Onorato9c1289c2009-08-17 11:03:03 -04003940 /**
3941 * Callback saying that there aren't any more items to bind.
3942 *
3943 * Implementation of the method from LauncherModel.Callbacks.
3944 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003945 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003946 Runnable r = new Runnable() {
3947 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003948 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003949 }
3950 };
3951 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003952 return;
3953 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003954 if (LauncherAppState.PROFILE_STARTUP) {
3955 Trace.beginSection("Page bind completed");
3956 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003957 if (mSavedState != null) {
3958 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003959 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003960 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003961
Joe Onorato9c1289c2009-08-17 11:03:03 -04003962 mSavedState = null;
3963 }
3964
Adam Cohen1462de32012-07-24 22:34:36 -07003965 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003966
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003967 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003968
3969 // If we received the result of any pending adds while the loader was running (e.g. the
3970 // widget configuration forced an orientation change), process them now.
3971 if (sPendingAddItem != null) {
3972 final long screenId = completeAdd(sPendingAddItem);
3973
3974 // TODO: this moves the user to the page where the pending item was added. Ideally,
3975 // the screen would be guaranteed to exist after bind, and the page would be set through
3976 // the workspace restore process.
3977 mWorkspace.post(new Runnable() {
3978 @Override
3979 public void run() {
3980 mWorkspace.snapToScreenId(screenId);
3981 }
3982 });
3983 sPendingAddItem = null;
3984 }
3985
Sunny Goyal756adbc2015-04-16 15:20:51 -07003986 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003987
3988 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003989 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003990 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003991 if (LauncherAppState.PROFILE_STARTUP) {
3992 Trace.endSection();
3993 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003994 }
3995
Winson Chunga2413752012-04-03 14:22:34 -07003996 private boolean canRunNewAppsAnimation() {
3997 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07003998 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
3999 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004000 }
4001
Winson Chungc9168342013-06-26 14:54:55 -07004002 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004003 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004004 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4005 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004006 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004007 return bounceAnim;
4008 }
4009
Adam Cohen27772732013-11-11 14:00:56 +00004010 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004011 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004012 }
4013
Tony Wickham55616cd2015-09-23 14:55:17 -07004014 public int getSearchBarHeight() {
4015 if (mLauncherCallbacks != null) {
4016 return mLauncherCallbacks.getSearchBarHeight();
4017 }
4018 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4019 }
4020
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004021 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004022 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4023 * multiple calls to bind the same list.)
4024 */
4025 @Thunk ArrayList<AppInfo> mTmpAppsList;
4026 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4027 public void run() {
4028 bindAllApplications(mTmpAppsList);
4029 mTmpAppsList = null;
4030 }
4031 };
4032
4033 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004034 * Add the icons for all apps.
4035 *
4036 * Implementation of the method from LauncherModel.Callbacks.
4037 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004038 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004039 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4040 mTmpAppsList = apps;
4041 return;
4042 }
4043
Winson Chungb745afb2015-03-02 11:51:23 -08004044 if (mAppsView != null) {
4045 mAppsView.setApps(apps);
4046 }
Adam Cohen9211d422014-10-07 18:14:53 -07004047 if (mLauncherCallbacks != null) {
4048 mLauncherCallbacks.bindAllApplications(apps);
4049 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004050 }
4051
4052 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004053 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
4054 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
4055 */
4056 @Override
4057 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
4058 mDeepShortcutMap = deepShortcutMapCopy;
4059 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
4060 }
4061
Tony Wickham1bce7fd2016-04-28 17:39:03 -07004062 public List<String> getShortcutIdsForItem(ItemInfo info) {
4063 if (!DeepShortcutManager.supportsShortcuts(info)) {
4064 return Collections.EMPTY_LIST;
4065 }
4066 ComponentName component = info.getTargetComponent();
4067 List<String> ids = mDeepShortcutMap.get(new ComponentKey(component, info.user));
4068 return ids == null ? Collections.EMPTY_LIST : ids;
4069 }
4070
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004071 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004072 * A package was updated.
4073 *
4074 * Implementation of the method from LauncherModel.Callbacks.
4075 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004076 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004077 Runnable r = new Runnable() {
4078 public void run() {
4079 bindAppsUpdated(apps);
4080 }
4081 };
4082 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004083 return;
4084 }
4085
Winson Chungb745afb2015-03-02 11:51:23 -08004086 if (mAppsView != null) {
4087 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004088 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004089 }
4090
Sunny Goyal4390ace2014-10-13 11:33:11 -07004091 @Override
4092 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4093 Runnable r = new Runnable() {
4094 public void run() {
4095 bindWidgetsRestored(widgets);
4096 }
4097 };
4098 if (waitUntilResume(r)) {
4099 return;
4100 }
4101 mWorkspace.widgetsRestored(widgets);
4102 }
4103
Joe Onorato9c1289c2009-08-17 11:03:03 -04004104 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004105 * Some shortcuts were updated in the background.
4106 *
4107 * Implementation of the method from LauncherModel.Callbacks.
4108 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004109 @Override
4110 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4111 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004112 Runnable r = new Runnable() {
4113 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004114 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004115 }
4116 };
4117 if (waitUntilResume(r)) {
4118 return;
4119 }
4120
Sunny Goyal4390ace2014-10-13 11:33:11 -07004121 if (!updated.isEmpty()) {
4122 mWorkspace.updateShortcuts(updated);
4123 }
4124
4125 if (!removed.isEmpty()) {
4126 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4127 for (ShortcutInfo si : removed) {
4128 removedComponents.add(si.getTargetComponent());
4129 }
4130 mWorkspace.removeItemsByComponentName(removedComponents, user);
4131 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004132 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004133 }
4134 }
4135
4136 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004137 * Update the state of a package, typically related to install state.
4138 *
4139 * Implementation of the method from LauncherModel.Callbacks.
4140 */
Sunny Goyale755d462014-07-22 13:48:29 -07004141 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004142 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4143 Runnable r = new Runnable() {
4144 public void run() {
4145 bindRestoreItemsChange(updates);
4146 }
4147 };
4148 if (waitUntilResume(r)) {
4149 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004150 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004151
Sunny Goyal756adbc2015-04-16 15:20:51 -07004152 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004153 }
4154
4155 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004156 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004157 * in addition to specific applications to remove, the reason being that
4158 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004159 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004160 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004161 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004162 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004163 public void bindWorkspaceComponentsRemoved(
4164 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4165 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004166 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004167 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004168 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004169 }
Winson Chungd64d1762013-08-20 14:37:16 -07004170 };
4171 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004172 return;
4173 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004174 if (!packageNames.isEmpty()) {
4175 mWorkspace.removeItemsByPackageName(packageNames, user);
4176 }
4177 if (!components.isEmpty()) {
4178 mWorkspace.removeItemsByComponentName(components, user);
4179 }
4180 // Notify the drag controller
4181 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004182
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004183 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004184
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004185 @Override
4186 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4187 Runnable r = new Runnable() {
4188 public void run() {
4189 bindAppInfosRemoved(appInfos);
4190 }
4191 };
4192 if (waitUntilResume(r)) {
4193 return;
Joe Onorato36115782010-06-17 13:28:48 -04004194 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004195
Winson Chungdf95eb12013-10-16 14:57:07 -07004196 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004197 if (mAppsView != null) {
4198 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004199 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004200 }
Joe Onoratobe386092009-11-17 17:32:16 -08004201
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004202 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004203 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004204 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004205 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004206 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004207
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004208 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004209 public void bindWidgetsModel(WidgetsModel model) {
4210 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004211 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004212 return;
4213 }
4214
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004215 if (mWidgetsView != null && model != null) {
4216 mWidgetsView.addWidgets(model);
4217 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004218 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004219 }
4220
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004221 @Override
4222 public void notifyWidgetProvidersChanged() {
4223 if (mWorkspace.getState().shouldUpdateWidget) {
4224 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4225 }
4226 }
4227
Winson Chung400438b2011-01-16 17:53:48 -08004228 private int mapConfigurationOriActivityInfoOri(int configOri) {
4229 final Display d = getWindowManager().getDefaultDisplay();
4230 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4231 switch (d.getRotation()) {
4232 case Surface.ROTATION_0:
4233 case Surface.ROTATION_180:
4234 // We are currently in the same basic orientation as the natural orientation
4235 naturalOri = configOri;
4236 break;
4237 case Surface.ROTATION_90:
4238 case Surface.ROTATION_270:
4239 // We are currently in the other basic orientation to the natural orientation
4240 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4241 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4242 break;
4243 }
4244
4245 int[] oriMap = {
4246 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4247 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4248 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4249 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4250 };
4251 // Since the map starts at portrait, we need to offset if this device's natural orientation
4252 // is landscape.
4253 int indexOffset = 0;
4254 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4255 indexOffset = 1;
4256 }
4257 return oriMap[(d.getRotation() + indexOffset) % 4];
4258 }
Adam Cohen446e9402011-09-15 18:21:21 -07004259
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004260 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004261 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004262 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004263 if (Utilities.ATLEAST_JB_MR2) {
4264 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4265 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004266 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4267 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004268 }
Winson Chung4b919f82012-05-01 10:44:08 -07004269 }
4270 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004271
Winson Chung4b919f82012-05-01 10:44:08 -07004272 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004273 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004274 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004275 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004276 } else {
4277 mHandler.postDelayed(new Runnable() {
4278 public void run() {
4279 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4280 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004281 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004282 }
Winson Chung4b919f82012-05-01 10:44:08 -07004283 }
Winson Chung400438b2011-01-16 17:53:48 -08004284 }
4285
Adam Cohenea90f832014-05-21 15:03:34 -07004286 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004287 * To be overridden by subclasses to indicate that there is an activity to launch
4288 * before showing the standard launcher experience.
4289 */
4290 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004291 if (mLauncherCallbacks != null) {
4292 return mLauncherCallbacks.hasFirstRunActivity();
4293 }
Adam Cohen432609a2014-03-13 17:03:22 -07004294 return false;
4295 }
4296
4297 /**
4298 * To be overridden by subclasses to launch any first run activity
4299 */
4300 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004301 if (mLauncherCallbacks != null) {
4302 return mLauncherCallbacks.getFirstRunActivity();
4303 }
Adam Cohen432609a2014-03-13 17:03:22 -07004304 return null;
4305 }
4306
Winson Chunga6945242014-01-08 14:04:34 -08004307 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004308 return !ActivityManager.isRunningInTestHarness() &&
4309 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004310 }
4311
Adam Cohen4a9423d2014-03-28 14:22:31 -07004312 protected boolean hasRunFirstRunActivity() {
4313 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4314 }
4315
Adam Cohen432609a2014-03-13 17:03:22 -07004316 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004317 if (shouldRunFirstRunActivity() &&
4318 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004319 Intent firstRunIntent = getFirstRunActivity();
4320 if (firstRunIntent != null) {
4321 startActivity(firstRunIntent);
4322 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004323 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004324 }
4325 }
Adam Cohen432609a2014-03-13 17:03:22 -07004326 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004327 }
4328
4329 private void markFirstRunActivityShown() {
4330 SharedPreferences.Editor editor = mSharedPrefs.edit();
4331 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4332 editor.apply();
4333 }
4334
Adam Cohen432609a2014-03-13 17:03:22 -07004335 /**
4336 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4337 * screen that must be displayed and dismissed.
4338 */
4339 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004340 if (mLauncherCallbacks != null) {
4341 return mLauncherCallbacks.hasDismissableIntroScreen();
4342 }
Adam Cohen432609a2014-03-13 17:03:22 -07004343 return false;
4344 }
4345
4346 /**
4347 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4348 */
4349 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004350 if (mLauncherCallbacks != null) {
4351 return mLauncherCallbacks.getIntroScreen();
4352 }
Adam Cohen432609a2014-03-13 17:03:22 -07004353 return null;
4354 }
4355
4356 /**
4357 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4358 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4359 */
4360 private boolean shouldShowIntroScreen() {
4361 return hasDismissableIntroScreen() &&
4362 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4363 }
4364
4365 protected void showIntroScreen() {
4366 View introScreen = getIntroScreen();
4367 changeWallpaperVisiblity(false);
4368 if (introScreen != null) {
4369 mDragLayer.showOverlayView(introScreen);
4370 }
4371 }
4372
4373 public void dismissIntroScreen() {
4374 markIntroScreenDismissed();
4375 if (showFirstRunActivity()) {
4376 // We delay hiding the intro view until the first run activity is showing. This
4377 // avoids a blip.
4378 mWorkspace.postDelayed(new Runnable() {
4379 @Override
4380 public void run() {
4381 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004382 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004383 }
4384 }, ACTIVITY_START_DELAY);
4385 } else {
4386 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004387 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004388 }
4389 changeWallpaperVisiblity(true);
4390 }
4391
4392 private void markIntroScreenDismissed() {
4393 SharedPreferences.Editor editor = mSharedPrefs.edit();
4394 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4395 editor.apply();
4396 }
4397
Adam Cohen091440a2015-03-18 14:16:05 -07004398 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004399 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4400 // on the device, then we always show the first run cling experience (or if there is no
4401 // launcher2). Otherwise, we prompt the user upon started for migration
4402 LauncherClings launcherClings = new LauncherClings(this);
4403 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004404 mClings = launcherClings;
Sunny Goyalded0fdb2016-05-23 15:55:41 -07004405 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004406 }
4407 }
4408
Winson Chung5ffd51d2015-06-25 11:36:50 -07004409 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004410 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004411 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004412 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004413 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004414 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004415 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4416 if (userHandle != null) {
4417 user = UserHandleCompat.fromUser(userHandle);
4418 }
4419 }
4420 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004421 }
4422
4423 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004424 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004425 if (user == null) {
4426 user = UserHandleCompat.myUserHandle();
4427 }
4428
4429 // Called from search suggestion, add the profile extra to the intent to ensure that we
4430 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004431 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004432 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004433 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004434 return null;
4435 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004436 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004437 }
4438
Winson Chung5ffd51d2015-06-25 11:36:50 -07004439 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004440 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4441 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004442 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004443 UserHandleCompat.myUserHandle());
4444 }
4445
Winson Chung5ffd51d2015-06-25 11:36:50 -07004446 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004447 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4448 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004449 mWorkspace.onExternalDragStartedWithItem(dragView);
4450 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004451 }
4452
Winson Chung5ffd51d2015-06-25 11:36:50 -07004453 protected void moveWorkspaceToDefaultScreen() {
4454 mWorkspace.moveToDefaultScreen(false);
4455 }
4456
Winson Chung80baf5a2010-08-09 16:03:15 -07004457 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004458 * Returns a FastBitmapDrawable with the icon, accurately sized.
4459 */
4460 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4461 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4462 d.setFilterBitmap(true);
4463 resizeIconDrawable(d);
4464 return d;
4465 }
4466
4467 /**
4468 * Resizes an icon drawable to the correct icon size.
4469 */
Winson5fbe0742015-09-30 15:33:00 -07004470 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004471 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004472 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004473 }
4474
4475 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004476 * Prints out out state for debugging.
4477 */
4478 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004479 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004480 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004481 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4482 Log.d(TAG, "mRestoring=" + mRestoring);
4483 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004484 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004485 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004486
Daniel Sandler325dc232013-06-05 22:57:57 -04004487 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004488 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004489
4490 @Override
4491 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4492 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004493 // Dump workspace
4494 writer.println(prefix + "Workspace Items");
4495 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4496 writer.println(prefix + " Homescreen " + i);
4497
4498 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4499 for (int j = 0; j < layout.getChildCount(); j++) {
4500 Object tag = layout.getChildAt(j).getTag();
4501 if (tag != null) {
4502 writer.println(prefix + " " + tag.toString());
4503 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004504 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004505 }
Sunny Goyala1365452015-10-01 15:46:24 -07004506
4507 writer.println(prefix + " Hotseat");
4508 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4509 for (int j = 0; j < layout.getChildCount(); j++) {
4510 Object tag = layout.getChildAt(j).getTag();
4511 if (tag != null) {
4512 writer.println(prefix + " " + tag.toString());
4513 }
4514 }
4515
Sunny Goyal713edfc2016-05-06 09:58:34 -07004516 try {
4517 FileLog.flushAll(writer);
4518 } catch (Exception e) {
4519 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004520 }
4521
Adam Cohen9211d422014-10-07 18:14:53 -07004522 if (mLauncherCallbacks != null) {
4523 mLauncherCallbacks.dump(prefix, fd, writer, args);
4524 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004525 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004526
Adam Cohen59400422014-03-05 18:07:04 -08004527 public static CustomAppWidget getCustomAppWidget(String name) {
4528 return sCustomAppWidgets.get(name);
4529 }
4530
4531 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4532 return sCustomAppWidgets;
4533 }
4534
Sunny Goyal29538332016-02-18 09:10:19 -08004535 public static List<View> getFolderContents(View icon) {
4536 if (icon instanceof FolderIcon) {
4537 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4538 } else {
4539 return Collections.EMPTY_LIST;
4540 }
4541 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004542
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004543 public static Launcher getLauncher(Context context) {
4544 if (context instanceof Launcher) {
4545 return (Launcher) context;
4546 }
4547 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4548 }
4549
Sunny Goyal745bad92016-05-02 10:54:12 -07004550 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4551
4552 @Override
4553 public void onSharedPreferenceChanged(
4554 SharedPreferences sharedPreferences, String key) {
4555 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4556 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4557 if (!waitUntilResume(this, true)) {
4558 run();
4559 }
4560 }
4561 }
4562
4563 @Override
4564 public void run() {
4565 setOrientation();
4566 }
4567 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004568}