blob: b7f033ef497399494fc503cc7e88e205f0e8d9a2 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyal28c6b962015-10-12 11:42:05 -070019import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070025import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000026import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080037import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Sunny Goyal756cd262015-08-20 12:33:21 -070039import android.content.ContentValues;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070041import android.content.ContextWrapper;
Chris Wren40c5ed32014-06-24 18:24:23 -040042import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070044import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070045import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070046import android.content.SharedPreferences;
Sunny Goyal745bad92016-05-02 10:54:12 -070047import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070048import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.pm.PackageManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070051import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070052import android.graphics.Bitmap;
53import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070054import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070055import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070057import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.drawable.Drawable;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020060import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080062import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070063import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070064import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070066import android.os.Trace;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070067import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.HapticFeedbackConstants;
75import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070077import android.view.MotionEvent;
78import android.view.Surface;
79import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070080import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080081import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.view.ViewGroup;
83import android.view.ViewTreeObserver;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080085import android.view.accessibility.AccessibilityEvent;
Tony Wickhame2217252016-03-22 16:34:23 -070086import android.view.accessibility.AccessibilityManager;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070087import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080091import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070093
Sunny Goyal651077b2014-06-30 14:15:31 -070094import com.android.launcher3.DropTarget.DragObject;
Sunny Goyala7ce1662016-05-31 15:01:35 -070095import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyalae502842016-06-17 08:43:56 -070096import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070097import com.android.launcher3.allapps.AllAppsContainerView;
Hyunyoung Song645764e2016-06-06 14:19:02 -070098import com.android.launcher3.allapps.AllAppsTransitionController;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080099import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700100import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100101import com.android.launcher3.compat.LauncherActivityInfoCompat;
102import com.android.launcher3.compat.LauncherAppsCompat;
103import com.android.launcher3.compat.UserHandleCompat;
104import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800105import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700106import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700107import com.android.launcher3.dragndrop.DragController;
108import com.android.launcher3.dragndrop.DragLayer;
109import com.android.launcher3.dragndrop.DragView;
Tony Wickham827cef22016-03-17 15:39:39 -0700110import com.android.launcher3.dynamicui.ExtractedColors;
Sunny Goyal26119432016-02-18 22:09:23 +0000111import com.android.launcher3.folder.Folder;
112import com.android.launcher3.folder.FolderIcon;
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700113import com.android.launcher3.keyboard.ViewGroupFocusHelper;
Sunny Goyala7ce1662016-05-31 15:01:35 -0700114import com.android.launcher3.logging.FileLog;
Hyunyoung Songaa953652016-04-19 18:30:24 -0700115import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700116import com.android.launcher3.model.WidgetsModel;
Hyunyoung Song7d2fc812016-06-15 12:51:30 -0700117import com.android.launcher3.pageindicators.PageIndicator;
Tony Wickham1bce7fd2016-04-28 17:39:03 -0700118import com.android.launcher3.shortcuts.DeepShortcutManager;
Tony Wickham49c8d292016-07-01 11:44:34 -0700119import com.android.launcher3.shortcuts.DeepShortcutsContainer;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700120import com.android.launcher3.util.ComponentKey;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700121import com.android.launcher3.util.MultiHashMap;
Sunny Goyal04cc3a72016-05-17 10:32:43 -0700122import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyal322d5562015-06-25 19:35:49 -0700123import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700124import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700125import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700126import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700127import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700128import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700129
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700130import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700131import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700132import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700133import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800134import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700135import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700136import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700137import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700138
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139/**
140 * Default launcher application.
141 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100142public class Launcher extends Activity
Adam Cohen79d90c52016-04-22 13:29:20 -0700143 implements LauncherExterns, View.OnClickListener, OnLongClickListener,
144 LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
Tony Wickhame2217252016-03-22 16:34:23 -0700145 AccessibilityManager.AccessibilityStateChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700146 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700147 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700149 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700150 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400151 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700154 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700155 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700156 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
Michael Jurka8b805b12012-04-18 14:23:14 -0700158 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyald478c832016-04-01 12:04:16 -0700159 private static final int REQUEST_BIND_PENDING_APPWIDGET = 14;
Sunny Goyalff572272014-07-23 13:58:07 -0700160 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700161
Sunny Goyal28c6b962015-10-12 11:42:05 -0700162 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
163
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700164 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
165 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
166 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
167
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700168 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
169
Mathew Inwood876a8462013-06-14 14:12:41 +0100170 /**
171 * IntentStarter uses request codes starting with this. This must be greater than all activity
172 * request codes used internally.
173 */
174 protected static final int REQUEST_LAST = 100;
175
Michael Jurka0a457bf2012-11-19 14:05:05 -0800176 // To turn on these properties, type
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700177 // adb shell setprop logTap.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800178 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
Winson Chung2672ff92012-05-04 16:22:30 -0700180 // The Intent extra that defines whether to ignore the launch animation
181 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400182 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700183
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700184 public static final String ACTION_APPWIDGET_HOST_RESET =
185 "com.android.launcher3.intent.ACTION_APPWIDGET_HOST_RESET";
186
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 // Type: int
188 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700189 // Type: int
190 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700191 // Type: Content Values / parcelable
192 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700193 // Type: parcelable
194 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000195 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800196 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197
Adam Cohen432609a2014-03-13 17:03:22 -0700198 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800199 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
200
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700201 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700202 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
203 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700204
Adam Cohen091440a2015-03-18 14:16:05 -0700205 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700206 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700207
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700208 private boolean mIsSafeModeEnabled;
209
Joe Onorato9c1289c2009-08-17 11:03:03 -0400210 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800211 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700212 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700213 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214
Winson Chunga2413752012-04-03 14:22:34 -0700215 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700216 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
217 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700218 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700219
Sunny Goyalda4fe1a2016-05-26 16:05:17 -0700220 private final BroadcastReceiver mUiBroadcastReceiver = new BroadcastReceiver() {
221
222 @Override
223 public void onReceive(Context context, Intent intent) {
224 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
225 closeSystemDialogs();
226 } else if (ACTION_APPWIDGET_HOST_RESET.equals(intent.getAction())) {
227 if (mAppWidgetHost != null) {
228 mAppWidgetHost.startListening();
229 }
230 }
231 }
232 };
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800233
Adam Cohen091440a2015-03-18 14:16:05 -0700234 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700235 private View mLauncherView;
Adam Cohen091440a2015-03-18 14:16:05 -0700236 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800237 private DragController mDragController;
Sunny Goyal6178f132016-07-11 17:30:03 -0700238 private View mQsbContainer;
Sunny Goyal322d5562015-06-25 19:35:49 -0700239
240 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700241
Sunny Goyalffe83f12014-08-14 17:39:34 -0700242 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700243 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700244
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700245 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800246 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800247 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700248
Michael Jurka0280c3b2010-09-17 15:00:07 -0700249 private int[] mTmpAddItemCellCoordinates = new int[2];
250
Sunny Goyal316490e2015-06-02 09:38:28 -0700251 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800252 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700253
Michael Jurka838a4ca2011-02-07 13:33:06 -0800254 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700255 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700256
Sunny Goyal47328fd2016-05-25 18:56:41 -0700257 private DropTargetBar mDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700258
259 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700260 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song645764e2016-06-06 14:19:02 -0700261 AllAppsTransitionController mAllAppsController;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700262
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700263 // Main container view and the model for the widget tray screen.
264 @Thunk WidgetsContainerView mWidgetsView;
265 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700266
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700268 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
269 // scroll issues (because the workspace may not have been measured yet) and extra work.
270 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
271 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272
273 private SpannableStringBuilder mDefaultKeySsb = null;
274
Adam Cohen091440a2015-03-18 14:16:05 -0700275 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400276
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800277 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278 private boolean mRestoring;
279 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700280 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281
Michael Jurka1e2f4652013-07-08 18:03:46 -0700282 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700283 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700284 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700285
Joe Onorato9c1289c2009-08-17 11:03:03 -0400286 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800287 private IconCache mIconCache;
Tony Wickham827cef22016-03-17 15:39:39 -0700288 private ExtractedColors mExtractedColors;
Sunny Goyalae502842016-06-17 08:43:56 -0700289 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Adam Cohen091440a2015-03-18 14:16:05 -0700290 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800291 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700292 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800293 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400294
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700295 /** Maps launcher activity components to their list of shortcut ids. */
296 private MultiHashMap<ComponentKey, String> mDeepShortcutMap = new MultiHashMap<>();
297
Adam Cohen61f560d2013-09-30 15:58:20 -0700298 private View.OnTouchListener mHapticFeedbackTouchListener;
299
Adam Cohended9f8d2010-11-03 13:25:16 -0700300 // Related to the auto-advancing of widgets
301 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700302 private static final int ADVANCE_INTERVAL = 20000;
303 private static final int ADVANCE_STAGGER = 250;
304
305 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700306 private long mAutoAdvanceSentTime;
307 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700308 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700309
Winson Chung400438b2011-01-16 17:53:48 -0800310 // Determines how long to wait after a rotation before restoring the screen orientation to
311 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700312 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800313
Adam Cohen091440a2015-03-18 14:16:05 -0700314 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700315
Adam Cohen1462de32012-07-24 22:34:36 -0700316 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700317 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700318
Winson Chung46353de2012-02-16 14:05:10 -0800319 // We only want to get the SharedPreferences once since it does an FS stat each time we get
320 // it from the context.
321 private SharedPreferences mSharedPrefs;
322
Winson Chungf0c6ae02012-03-21 16:10:31 -0700323 // Holds the page that we need to animate to, and the icon views that we need to animate up
324 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700325 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700326 private Bitmap mFolderIconBitmap;
327 private Canvas mFolderIconCanvas;
328 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700329
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700330 private DeviceProfile mDeviceProfile;
331
Adam Cohen4b66bf32015-09-18 12:15:19 -0700332 private boolean mMoveToDefaultScreenFromNewIntent;
333
Winson Chung13eb5272015-05-11 16:30:13 -0700334 // This is set to the view that launched the activity that navigated the user away from
335 // launcher. Since there is no callback for when the activity has finished launching, enable
336 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800337 private BubbleTextView mWaitingForResume;
338
Adam Cohen59400422014-03-05 18:07:04 -0800339 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
340 new HashMap<String, CustomAppWidget>();
341
Adam Cohen59400422014-03-05 18:07:04 -0800342 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700343 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
344 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800345 }
346 }
347
Adam Cohen091440a2015-03-18 14:16:05 -0700348 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700349 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700350 if (mWorkspace != null) {
351 mWorkspace.buildPageHardwareLayers();
352 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700353 }
354 };
355
Adam Cohendb364c32014-05-20 14:23:40 -0700356 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800357
Adam Cohen091440a2015-03-18 14:16:05 -0700358 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800359 int requestCode;
360 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700361 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700362 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800363 int cellX;
364 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700365 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800366 }
367
Hyunyoung Songaa953652016-04-19 18:30:24 -0700368 private UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800369
Sunny Goyal3333b0c2016-05-09 20:43:21 -0700370 public ViewGroupFocusHelper mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700371 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400372
373 @Thunk void setOrientation() {
374 if (mRotationEnabled) {
375 unlockScreenOrientation(true);
376 } else {
377 setRequestedOrientation(
378 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700379 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400380 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700381
Sunny Goyal745bad92016-05-02 10:54:12 -0700382 private RotationPrefChangeHandler mRotationPrefChangeHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700383
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 @Override
385 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700386 if (DEBUG_STRICT_MODE) {
387 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
388 .detectDiskReads()
389 .detectDiskWrites()
390 .detectNetwork() // or .detectAll() for all detectable problems
391 .penaltyLog()
392 .build());
393 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
394 .detectLeakedSqlLiteObjects()
395 .detectLeakedClosableObjects()
396 .penaltyLog()
397 .penaltyDeath()
398 .build());
399 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700400 if (LauncherAppState.PROFILE_STARTUP) {
401 Trace.beginSection("Launcher-onCreate");
402 }
Winson Chunga2413752012-04-03 14:22:34 -0700403
Adam Cohen9211d422014-10-07 18:14:53 -0700404 if (mLauncherCallbacks != null) {
405 mLauncherCallbacks.preOnCreate();
406 }
407
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400409
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400410 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700411
Adam Cohen2e6da152015-05-06 11:42:25 -0700412 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700413 mDeviceProfile = getResources().getConfiguration().orientation
414 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700415 app.getInvariantDeviceProfile().landscapeProfile
416 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700417
Sunny Goyalf7258242015-10-19 16:59:07 -0700418 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700419 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800420 mModel = app.setLauncher(this);
421 mIconCache = app.getIconCache();
Sunny Goyalae502842016-06-17 08:43:56 -0700422 mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
Adam Cohen2e6da152015-05-06 11:42:25 -0700423
Joe Onorato41a12d22009-10-31 18:30:00 -0400424 mDragController = new DragController(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -0700425 mAllAppsController = new AllAppsTransitionController(this);
426 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
Romain Guycbb89e42009-06-08 15:52:54 -0700427
Sunny Goyalffe83f12014-08-14 17:39:34 -0700428 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700429
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700430 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
431 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700432
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700433 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
434 // this also ensures that any synchronous binding below doesn't re-trigger another
435 // LauncherModel load.
436 mPaused = false;
437
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700439
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 setupViews();
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();
Adam Cohenc3e12c72014-10-31 16:15:36 -0700498 }
Adam Cohen9211d422014-10-07 18:14:53 -0700499 }
500
Sunny Goyal7779d622015-06-11 16:18:39 -0700501 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700502 public void onExtractedColorsChanged() {
503 loadExtractedColorsAndColorItems();
504 }
505
506 private void loadExtractedColorsAndColorItems() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700507 // TODO: do this in pre-N as well, once the extraction part is complete.
508 if (mExtractedColors != null && Utilities.isNycOrAbove()) {
Tony Wickham827cef22016-03-17 15:39:39 -0700509 mExtractedColors.load(this);
Tony Wickham462b5cc2016-04-01 16:00:49 -0700510 mHotseat.updateColor(mExtractedColors, !mPaused);
Sunny Goyald0a6ae72016-06-16 12:29:03 -0700511 mWorkspace.getPageIndicator().updateColor(mExtractedColors);
Tony Wickham827cef22016-03-17 15:39:39 -0700512 }
513 }
514
Adam Cohen9211d422014-10-07 18:14:53 -0700515 private LauncherCallbacks mLauncherCallbacks;
516
517 public void onPostCreate(Bundle savedInstanceState) {
518 super.onPostCreate(savedInstanceState);
519 if (mLauncherCallbacks != null) {
520 mLauncherCallbacks.onPostCreate(savedInstanceState);
521 }
522 }
523
Sunny Goyal32554d12015-12-03 15:31:25 -0800524 /**
525 * Call this after onCreate to set or clear overlay.
526 */
527 public void setLauncherOverlay(LauncherOverlay overlay) {
528 if (overlay != null) {
529 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
530 }
531 mWorkspace.setLauncherOverlay(overlay);
532 }
533
Adam Cohen9211d422014-10-07 18:14:53 -0700534 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
535 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700536 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700537 private boolean mWorkspaceImportanceStored = false;
538 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700539 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800540 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700541 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
542
543 @Override
544 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700545 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700546 return;
547 }
548 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700549 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700550 if (mWorkspace != null) {
551 mWorkspaceImportanceForAccessibility =
552 mWorkspace.getImportantForAccessibility();
553 mWorkspace.setImportantForAccessibility(
554 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
555 mWorkspaceImportanceStored = true;
556 }
557 if (mHotseat != null) {
558 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
559 mHotseat.setImportantForAccessibility(
560 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
561 mHotseatImportanceStored = true;
562 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700563 }
564
565 @Override
566 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700567 if (mWorkspaceImportanceStored && mWorkspace != null) {
568 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
569 }
570 if (mHotseatImportanceStored && mHotseat != null) {
571 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
572 }
573 mWorkspaceImportanceStored = false;
574 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700575 }
576 });
Adam Cohen9211d422014-10-07 18:14:53 -0700577 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700578 }
579
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700580 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700581 public void onLauncherProviderChange() {
582 if (mLauncherCallbacks != null) {
583 mLauncherCallbacks.onLauncherProviderChange();
584 }
585 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700586
Adam Cohen9211d422014-10-07 18:14:53 -0700587 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700588 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700589 if (mLauncherCallbacks != null) {
590 return mLauncherCallbacks.hasCustomContentToLeft();
591 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700592 return false;
593 }
594
Dave Hawkeya8881582013-09-17 15:55:33 -0600595 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500596 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600597 * {@link #addToCustomContentPage}. This will only be invoked if
598 * {@link #hasCustomContentToLeft()} is {@code true}.
599 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500600 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700601 if (mLauncherCallbacks != null) {
602 mLauncherCallbacks.populateCustomContentContainer();
603 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600604 }
605
606 /**
607 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
608 * ensure the custom content page is added or removed if necessary.
609 */
610 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600611 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600612 // Not bound yet, wait for bindScreens to be called.
613 return;
614 }
615
616 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
617 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500618 mWorkspace.createCustomContentContainer();
619 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600620 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
621 mWorkspace.removeCustomContentPage();
622 }
623 }
624
Hyunyoung Songaa953652016-04-19 18:30:24 -0700625 public UserEventDispatcher getUserEventDispatcher() {
626 if (mLauncherCallbacks != null) {
627 UserEventDispatcher dispatcher = mLauncherCallbacks.getUserEventDispatcher();
628 if (dispatcher != null) {
629 return dispatcher;
630 }
631 }
632
Sunny Goyal64976d52016-06-20 14:56:28 -0700633 // Logger object is a singleton and does not have to be coupled with the foreground
634 // activity. Since most user event logging is done on the UI, the object is retrieved
635 // from the callback for convenience.
Hyunyoung Songaa953652016-04-19 18:30:24 -0700636 if (mUserEventDispatcher == null) {
Sunny Goyal64976d52016-06-20 14:56:28 -0700637 mUserEventDispatcher = new UserEventDispatcher();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700638 }
639 return mUserEventDispatcher;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800640 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100641
Hyunyoung Song3f471442015-04-08 19:01:34 -0700642 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700643 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
644 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700645 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700646 }
647
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000648 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700649 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
650 // This cast is safe as long as the id < 0x00FFFFFF
651 // Since we jail all the dynamically generated views, there should be no clashes
652 // with any other views.
653 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000654 }
655
656 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700657 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
658 * a configuration step, this allows the proper animations to run after other transitions.
659 */
Adam Cohendb364c32014-05-20 14:23:40 -0700660 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700661 long screenId = args.screenId;
662 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
663 // When the screen id represents an actual screen (as opposed to a rank) we make sure
664 // that the drop page actually exists.
665 screenId = ensurePendingDropLayoutExists(args.screenId);
666 }
Adam Cohendb364c32014-05-20 14:23:40 -0700667
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800668 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800669 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700670 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700671 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700672 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800673 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700674 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700675 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700676 case REQUEST_RECONFIGURE_APPWIDGET:
Sunny Goyald478c832016-04-01 12:04:16 -0700677 completeRestoreAppWidget(args.appWidgetId, LauncherAppWidgetInfo.RESTORE_COMPLETED);
Sunny Goyalff572272014-07-23 13:58:07 -0700678 break;
Sunny Goyald478c832016-04-01 12:04:16 -0700679 case REQUEST_BIND_PENDING_APPWIDGET: {
680 int widgetId = args.appWidgetId;
681 LauncherAppWidgetInfo info =
682 completeRestoreAppWidget(widgetId, LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
683 if (info != null) {
684 // Since the view was just bound, also launch the configure activity if needed
685 LauncherAppWidgetProviderInfo provider = mAppWidgetManager
686 .getLauncherAppWidgetInfo(widgetId);
687 if (provider != null && provider.configure != null) {
688 startRestoredWidgetReconfigActivity(provider, info);
689 }
690 }
691 break;
692 }
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800693 }
Michael Jurka27614d22012-04-02 06:40:22 -0700694 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
695 // if you turned the screen off and then back while in All Apps, Launcher would not
696 // return to the workspace. Clearing mAddInfo.container here fixes this issue
697 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700698 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800699 }
700
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800701 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700702 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700703 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700704 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700705 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800706 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700707
Adam Cohenad4e15c2013-10-17 16:21:35 -0700708 Runnable exitSpringLoaded = new Runnable() {
709 @Override
710 public void run() {
711 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
712 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
713 }
714 };
715
Michael Jurka8b805b12012-04-18 14:23:14 -0700716 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700717 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700718 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700719 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
720 if (resultCode == RESULT_CANCELED) {
721 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700722 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700723 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700724 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800725 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700726 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700727 }
728 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200729 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
730 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700731 // User could have free-scrolled between pages before picking a wallpaper; make sure
732 // we move to the closest one now.
733 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700734 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200735 }
736 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700737 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200738
Adam Cohened66b2b2012-01-23 17:28:51 -0800739 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700740 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700741
Adam Cohendb364c32014-05-20 14:23:40 -0700742 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800743 // We have special handling for widgets
744 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800745 final int appWidgetId;
746 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
747 : -1;
748 if (widgetId < 0) {
749 appWidgetId = pendingAddWidgetId;
750 } else {
751 appWidgetId = widgetId;
752 }
753
Adam Cohenad4e15c2013-10-17 16:21:35 -0700754 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800755 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700756 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
757 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700758 result = RESULT_CANCELED;
759 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700760 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700761 @Override
762 public void run() {
763 exitSpringLoadedDragModeDelayed(false, 0, null);
764 }
765 };
Adam Cohendb364c32014-05-20 14:23:40 -0700766 if (workspaceLocked) {
767 // No need to remove the empty screen if we're mid-binding, as the
768 // the bind will not add the empty screen.
769 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
770 } else {
771 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
772 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
773 }
Winson Chung5aaab772012-04-27 15:24:02 -0700774 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700775 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700776 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
777 // When the screen id represents an actual screen (as opposed to a rank)
778 // we make sure that the drop page actually exists.
779 mPendingAddInfo.screenId =
780 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
781 }
Adam Cohendb364c32014-05-20 14:23:40 -0700782 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
783
784 dropLayout.setDropPending(true);
785 final Runnable onComplete = new Runnable() {
786 @Override
787 public void run() {
788 completeTwoStageWidgetDrop(resultCode, appWidgetId);
789 dropLayout.setDropPending(false);
790 }
791 };
792 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
793 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
794 } else {
795 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
796 mPendingAddInfo);
797 sPendingAddItem = args;
798 }
Winson Chung5aaab772012-04-27 15:24:02 -0700799 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800800 return;
801 }
802
Sunny Goyald478c832016-04-01 12:04:16 -0700803 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET
804 || requestCode == REQUEST_BIND_PENDING_APPWIDGET) {
Sunny Goyalff572272014-07-23 13:58:07 -0700805 if (resultCode == RESULT_OK) {
806 // Update the widget view.
807 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
808 pendingAddWidgetId, mPendingAddInfo);
809 if (workspaceLocked) {
810 sPendingAddItem = args;
811 } else {
812 completeAdd(args);
813 }
814 }
815 // Leave the widget in the pending state if the user canceled the configure.
816 return;
817 }
818
Sunny Goyalaad90582015-07-09 14:22:50 -0700819 if (requestCode == REQUEST_CREATE_SHORTCUT) {
820 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
821 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
822 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
823 mPendingAddInfo);
824 if (isWorkspaceLocked()) {
825 sPendingAddItem = args;
826 } else {
827 completeAdd(args);
828 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
829 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
830 }
831 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700832 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
833 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800836 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800837
838 }
839
840 @Override
841 protected void onActivityResult(
842 final int requestCode, final int resultCode, final Intent data) {
843 handleActivityResult(requestCode, resultCode, data);
844 if (mLauncherCallbacks != null) {
845 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
846 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800847 }
848
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600849 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700850 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600851 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700852 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
853 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
854 View v = null;
855 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
856 if (layout != null) {
857 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
858 }
859 Intent intent = sPendingAddItem.intent;
860 sPendingAddItem = null;
861 if (grantResults.length > 0
862 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Sunny Goyal85fc55a2016-05-31 10:44:03 -0700863 startActivitySafely(v, intent, null);
Sunny Goyal28c6b962015-10-12 11:42:05 -0700864 } else {
865 // TODO: Show a snack bar with link to settings
866 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
867 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
868 }
869 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600870 if (mLauncherCallbacks != null) {
871 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
872 grantResults);
873 }
874 }
875
Adam Cohendb364c32014-05-20 14:23:40 -0700876 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
877 appWidgetId, ItemInfo info) {
878 PendingAddArguments args = new PendingAddArguments();
879 args.requestCode = requestCode;
880 args.intent = data;
881 args.container = info.container;
882 args.screenId = info.screenId;
883 args.cellX = info.cellX;
884 args.cellY = info.cellY;
885 args.appWidgetId = appWidgetId;
886 return args;
887 }
888
889 /**
890 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
891 *
892 * @param screenId the screen id to check
893 * @return the new screen, or screenId if it exists
894 */
895 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800896 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700897 if (dropLayout == null) {
898 // it's possible that the add screen was removed because it was
899 // empty and a re-bind occurred
900 mWorkspace.addExtraEmptyScreen();
901 return mWorkspace.commitExtraEmptyScreen();
902 } else {
903 return screenId;
904 }
905 }
906
Adam Cohen091440a2015-03-18 14:16:05 -0700907 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800908 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800909 Runnable onCompleteRunnable = null;
910 int animationType = 0;
911
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700912 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800913 if (resultCode == RESULT_OK) {
914 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
915 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700916 mPendingAddWidgetInfo);
917 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800918 onCompleteRunnable = new Runnable() {
919 @Override
920 public void run() {
921 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700922 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700923 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
924 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800925 }
926 };
927 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800928 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800929 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800930 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700931 if (mDragLayer.getAnimatedView() != null) {
932 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
933 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
934 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700935 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700936 // The animated view may be null in the case of a rotation during widget configuration
937 onCompleteRunnable.run();
938 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939 }
940
941 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700942 protected void onStop() {
943 super.onStop();
944 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700945
946 if (mLauncherCallbacks != null) {
947 mLauncherCallbacks.onStop();
948 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700949
950 if (Utilities.isNycMR1OrAbove()) {
951 mAppWidgetHost.stopListening();
952 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700953 }
954
955 @Override
956 protected void onStart() {
957 super.onStart();
958 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700959
960 if (mLauncherCallbacks != null) {
961 mLauncherCallbacks.onStart();
962 }
Sunny Goyal5da78f42016-06-17 14:00:22 -0700963
964 if (Utilities.isNycMR1OrAbove()) {
965 mAppWidgetHost.startListening();
966 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700967 }
968
969 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200971 long startTime = 0;
972 if (DEBUG_RESUME_TIME) {
973 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400974 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200975 }
Adam Cohen9211d422014-10-07 18:14:53 -0700976
977 if (mLauncherCallbacks != null) {
978 mLauncherCallbacks.preOnResume();
979 }
980
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 super.onResume();
Hyunyoung Songaa953652016-04-19 18:30:24 -0700982 getUserEventDispatcher().resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700983
Winson Chung4a2afa32012-07-19 14:53:05 -0700984 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700985 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700986 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800987 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700988 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700989 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700990 // view after launching an app, as they may be depending on the UI to be static to
991 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700992 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700993 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800994 } else if (mOnResumeState == State.WIDGETS) {
995 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700996 }
997 mOnResumeState = State.NONE;
998
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700999 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001000 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1001 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001002
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001003 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001004 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001005 setWorkspaceLoading(true);
Sunny Goyal93f878c2016-03-30 17:31:24 -07001006 mModel.startLoader(getCurrentWorkspaceScreen());
Joe Onorato9c1289c2009-08-17 11:03:03 -04001007 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001008 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001010 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001011 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1012 // execute them here
1013 long startTimeCallbacks = 0;
1014 if (DEBUG_RESUME_TIME) {
1015 startTimeCallbacks = System.currentTimeMillis();
1016 }
1017
Michael Jurka1e2f4652013-07-08 18:03:46 -07001018 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1019 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001020 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001021 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001022 if (DEBUG_RESUME_TIME) {
1023 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1024 (System.currentTimeMillis() - startTimeCallbacks));
1025 }
Michael Jurka447bf842013-05-15 14:52:15 +02001026 }
Michael Jurka54554252013-08-01 12:52:23 +02001027 if (mOnResumeCallbacks.size() > 0) {
1028 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1029 mOnResumeCallbacks.get(i).run();
1030 }
1031 mOnResumeCallbacks.clear();
1032 }
Winson Chunge4e50662012-01-23 14:45:13 -08001033
1034 // Reset the pressed state of icons that were locked in the press state while activities
1035 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001036 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001037 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001038 mWaitingForResume.setStayPressed(false);
1039 }
Winson Chung82963d52013-10-09 11:20:57 -07001040
Adam Cohen06dff352012-06-01 17:17:08 -07001041 // It is possible that widgets can receive updates while launcher is not in the foreground.
1042 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1043 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1044 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001045 if (!isWorkspaceLoading()) {
1046 getWorkspace().reinflateWidgetsIfNecessary();
1047 }
Adam Cohenf9426d52012-06-04 17:26:21 -07001048
Michael Jurka447bf842013-05-15 14:52:15 +02001049 if (DEBUG_RESUME_TIME) {
1050 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1051 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001052
Adam Cohen4b66bf32015-09-18 12:15:19 -07001053 // We want to suppress callbacks about CustomContent being shown if we have just received
1054 // onNewIntent while the user was present within launcher. In that case, we post a call
1055 // to move the user to the main screen (which will occur after onResume). We don't want to
1056 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1057 // suppress here.
1058 if (mWorkspace.getCustomContentCallbacks() != null
1059 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001060 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001061 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001062 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1063 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001064 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001065 }
1066 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001067 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001068 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001069 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001070
Sunny Goyal756adbc2015-04-16 15:20:51 -07001071 if (!isWorkspaceLoading()) {
1072 // Process any items that were added while Launcher was away.
1073 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1074 }
Adam Cohen9211d422014-10-07 18:14:53 -07001075
1076 if (mLauncherCallbacks != null) {
1077 mLauncherCallbacks.onResume();
1078 }
Adam Cohen06dff352012-06-01 17:17:08 -07001079 }
1080
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001082 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001083 // Ensure that items added to Launcher are queued until Launcher returns
1084 InstallShortcutReceiver.enableInstallQueue();
1085
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001086 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001087 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001088 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001089 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001090
1091 // We call onHide() aggressively. The custom content callbacks should be able to
1092 // debounce excess onHide calls.
1093 if (mWorkspace.getCustomContentCallbacks() != null) {
1094 mWorkspace.getCustomContentCallbacks().onHide();
1095 }
Romain Guycbb89e42009-06-08 15:52:54 -07001096
Adam Cohen9211d422014-10-07 18:14:53 -07001097 if (mLauncherCallbacks != null) {
1098 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001099 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001100 }
1101
1102 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001103 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1104 // by a onResume or by scrolling otherwise.
1105 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001106
1107 // Custom content is completely hidden
1108 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001109
1110 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1111 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001112
1113 // Indicates whether the user is allowed to scroll away from the custom content.
1114 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001115 }
1116
Adam Cohenc2d6e892014-10-16 09:49:24 -07001117 public interface LauncherOverlay {
1118
1119 /**
1120 * Touch interaction leading to overscroll has begun
1121 */
1122 public void onScrollInteractionBegin();
1123
1124 /**
1125 * Touch interaction related to overscroll has ended
1126 */
1127 public void onScrollInteractionEnd();
1128
1129 /**
1130 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1131 * screen (or in the case of RTL, the rightmost screen).
1132 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001133 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001134
1135 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001136 * Called when the launcher is ready to use the overlay
1137 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001138 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001139 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001140 }
1141
Jun Mukai8af0cd82015-05-12 12:32:12 -07001142 public interface LauncherSearchCallbacks {
1143 /**
1144 * Called when the search overlay is shown.
1145 */
1146 public void onSearchOverlayOpened();
1147
1148 /**
1149 * Called when the search overlay is dismissed.
1150 */
1151 public void onSearchOverlayClosed();
1152 }
1153
Adam Cohenc2d6e892014-10-16 09:49:24 -07001154 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001155 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001156 }
1157
1158 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1159
Sunny Goyalc86df472016-02-25 09:19:38 -08001160 public void onScrollChanged(float progress) {
1161 if (mWorkspace != null) {
1162 mWorkspace.onOverlayScrollChanged(progress);
1163 }
1164 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001165 }
1166
Jorim Jaggid017f882014-01-14 17:08:48 -08001167 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001168 if (mLauncherCallbacks != null) {
1169 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001170 } else {
1171 // On devices with a locked orientation, we will at least have the allow rotation
1172 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001173 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001174 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001175 }
1176
Adam Cohen9211d422014-10-07 18:14:53 -07001177 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001178 CustomContentCallbacks callbacks, String description) {
1179 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001180 }
1181
Adam Cohenedb40762013-07-18 16:45:45 -07001182 // The custom content needs to offset its content to account for the QSB
1183 public int getTopOffsetForCustomContent() {
1184 return mWorkspace.getPaddingTop();
1185 }
1186
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001187 @Override
1188 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001189 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001190 if (mModel.isCurrentCallbacks(this)) {
1191 mModel.stopLoader();
1192 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001193 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1194
Romain Guy13c2e7b2010-03-10 19:45:00 -08001195 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001196 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001197
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001198 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001199 @Override
1200 public void onWindowFocusChanged(boolean hasFocus) {
1201 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001202 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001203
1204 if (mLauncherCallbacks != null) {
1205 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1206 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001207 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001208
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 private boolean acceptFilter() {
1210 final InputMethodManager inputManager = (InputMethodManager)
1211 getSystemService(Context.INPUT_METHOD_SERVICE);
1212 return !inputManager.isFullscreenMode();
1213 }
1214
1215 @Override
1216 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001217 final int uniChar = event.getUnicodeChar();
1218 final boolean handled = super.onKeyDown(keyCode, event);
1219 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1220 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1222 keyCode, event);
1223 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001224 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001225 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001226 // showSearchDialog()
1227 // If there are multiple keystrokes before the search dialog takes focus,
1228 // onSearchRequested() will be called for every keystroke,
1229 // but it is idempotent, so it's fine.
1230 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 }
1232 }
1233
Joe Onorato8a9625e2010-01-28 15:55:35 -08001234 // Eat the long press event so the keyboard doesn't come up.
1235 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1236 return true;
1237 }
1238
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001239 return handled;
1240 }
1241
Winson46163472015-09-22 17:31:56 -07001242 @Override
1243 public boolean onKeyUp(int keyCode, KeyEvent event) {
1244 if (keyCode == KeyEvent.KEYCODE_MENU) {
1245 // Ignore the menu key if we are currently dragging or are on the custom content screen
1246 if (!isOnCustomContent() && !mDragController.isDragging()) {
1247 // Close any open folders
1248 closeFolder();
1249
Tony Wickham49c8d292016-07-01 11:44:34 -07001250 // Close any shortcuts containers
1251 closeShortcutsContainer();
1252
Winson46163472015-09-22 17:31:56 -07001253 // Stop resizing any widgets
1254 mWorkspace.exitWidgetResizeMode();
1255
1256 // Show the overview mode if we are on the workspace
1257 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1258 !mWorkspace.isSwitchingState()) {
1259 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001260 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001261 }
1262 }
1263 return true;
1264 }
1265 return super.onKeyUp(keyCode, event);
1266 }
1267
Karl Rosaen138a0412009-04-23 19:00:21 -07001268 private String getTypedText() {
1269 return mDefaultKeySsb.toString();
1270 }
1271
1272 private void clearTypedText() {
1273 mDefaultKeySsb.clear();
1274 mDefaultKeySsb.clearSpans();
1275 Selection.setSelection(mDefaultKeySsb, 0);
1276 }
1277
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001279 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1280 * State
1281 */
1282 private static State intToState(int stateOrdinal) {
1283 State state = State.WORKSPACE;
1284 final State[] stateValues = State.values();
1285 for (int i = 0; i < stateValues.length; i++) {
1286 if (stateValues[i].ordinal() == stateOrdinal) {
1287 state = stateValues[i];
1288 break;
1289 }
1290 }
1291 return state;
1292 }
1293
1294 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 * Restores the previous state, if it exists.
1296 *
1297 * @param savedState The previous state.
1298 */
1299 private void restoreState(Bundle savedState) {
1300 if (savedState == null) {
1301 return;
1302 }
1303
Michael Jurka883f55b2010-10-21 15:47:14 -07001304 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001305 if (state == State.APPS || state == State.WIDGETS) {
1306 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001307 }
1308
Adam Cohen21cd0022013-10-09 18:57:02 -07001309 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1310 PagedView.INVALID_RESTORE_PAGE);
1311 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001312 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 }
1314
Sunny Goyal756cd262015-08-20 12:33:21 -07001315 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1316 if (itemValues != null) {
1317 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001318 AppWidgetProviderInfo info = savedState.getParcelable(
1319 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001320 mPendingAddWidgetInfo = info == null ?
1321 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1322
Adam Cohen4637b5a2013-11-04 18:21:24 -08001323 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001324 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 mRestoring = true;
1326 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 }
1328
1329 /**
1330 * Finds all the views we need and configure them properly.
1331 */
1332 private void setupViews() {
Craig Mautner360310b2012-10-26 15:13:08 -07001333 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001334 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
Sunny Goyal3333b0c2016-05-09 20:43:21 -07001335 mFocusHandler = mDragLayer.getFocusIndicatorHelper();
Winson Chung4c98d922011-05-31 16:50:48 -07001336 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Sunny Goyal6178f132016-07-11 17:30:03 -07001337 mQsbContainer = mDragLayer.findViewById(mDeviceProfile.isVerticalBarLayout()
1338 ? R.id.workspace_blocked_row : R.id.qsb_container);
Sunny Goyald0a6ae72016-06-16 12:29:03 -07001339 mWorkspace.initParentViews(mDragLayer);
Craig Mautner360310b2012-10-26 15:13:08 -07001340
Sunny Goyal784f9c32016-03-23 16:56:54 -07001341 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1342 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
1343 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Craig Mautner360310b2012-10-26 15:13:08 -07001344 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345
Winson Chung4c98d922011-05-31 16:50:48 -07001346 // Setup the drag layer
Hyunyoung Song645764e2016-06-06 14:19:02 -07001347
1348 mDragLayer.setup(this, mDragController, mAllAppsController);
Winson Chung4c98d922011-05-31 16:50:48 -07001349
Winson Chung3d503fb2011-07-13 17:25:49 -07001350 // Setup the hotseat
1351 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1352 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001353 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001354 }
1355
Winsone9f27272015-10-13 10:47:51 -07001356 // Setup the overview panel
1357 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001358
Winson Chung4c98d922011-05-31 16:50:48 -07001359 // Setup the workspace
1360 mWorkspace.setHapticFeedbackEnabled(false);
1361 mWorkspace.setOnLongClickListener(this);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001362 mWorkspace.setup(mDragController);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001363 mWorkspace.bindAndInitFirstWorkspaceScreen(null /* recycled qsb */);
Hyunyoung Song645764e2016-06-06 14:19:02 -07001364 mDragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001365
Tony Wickham34d2c912015-09-11 09:27:58 -07001366 // Get the search/delete/uninstall bar
Sunny Goyal47328fd2016-05-25 18:56:41 -07001367 mDropTargetBar = (DropTargetBar) mDragLayer.findViewById(R.id.drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001368
Winson Chungef7f8742015-06-04 17:18:17 -07001369 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001370 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001371 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001372 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1373 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1374 } else {
1375 mAppsView.setSearchBarController(new DefaultAppSearchController());
1376 }
Craig Mautner360310b2012-10-26 15:13:08 -07001377
Winson Chung3d503fb2011-07-13 17:25:49 -07001378 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Hyunyoung Song645764e2016-06-06 14:19:02 -07001379 mDragController.setDragScoller(mWorkspace);
1380 mDragController.setScrollView(mDragLayer);
1381 mDragController.setMoveTarget(mWorkspace);
1382 mDragController.addDropTarget(mWorkspace);
Sunny Goyal47328fd2016-05-25 18:56:41 -07001383 mDropTargetBar.setup(mDragController);
Daniel Sandler924b9932013-06-12 00:38:25 -04001384
Peter Schiller310a9882016-06-30 13:52:36 -07001385 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP) {
1386 mAllAppsController.setupViews(mAppsView, mHotseat, mWorkspace);
1387 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -07001388
Sunny Goyal322d5562015-06-25 19:35:49 -07001389 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1390 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001391 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 }
1393
Winsone9f27272015-10-13 10:47:51 -07001394 private void setupOverviewPanel() {
1395 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1396
1397 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1398 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1399 @Override
1400 public boolean onLongClick(View v) {
1401 return v.performClick();
1402 }
1403 };
1404
1405 // Bind wallpaper button actions
1406 View wallpaperButton = findViewById(R.id.wallpaper_button);
1407 wallpaperButton.setOnClickListener(new OnClickListener() {
1408 @Override
1409 public void onClick(View view) {
1410 if (!mWorkspace.isSwitchingState()) {
1411 onClickWallpaperPicker(view);
1412 }
1413 }
1414 });
1415 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1416 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1417
1418 // Bind widget button actions
1419 mWidgetsButton = findViewById(R.id.widget_button);
1420 mWidgetsButton.setOnClickListener(new OnClickListener() {
1421 @Override
1422 public void onClick(View view) {
1423 if (!mWorkspace.isSwitchingState()) {
1424 onClickAddWidgetButton(view);
1425 }
1426 }
1427 });
1428 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1429 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1430
1431 // Bind settings actions
1432 View settingsButton = findViewById(R.id.settings_button);
1433 boolean hasSettings = hasSettings();
1434 if (hasSettings) {
1435 settingsButton.setOnClickListener(new OnClickListener() {
1436 @Override
1437 public void onClick(View view) {
1438 if (!mWorkspace.isSwitchingState()) {
1439 onClickSettingsButton(view);
1440 }
1441 }
1442 });
1443 settingsButton.setOnLongClickListener(performClickOnLongClick);
1444 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1445 } else {
1446 settingsButton.setVisibility(View.GONE);
1447 }
1448
1449 mOverviewPanel.setAlpha(0f);
1450 }
1451
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001453 * Sets the all apps button. This method is called from {@link Hotseat}.
Sunny Goyalbb011da2016-06-15 15:42:29 -07001454 * TODO: Get rid of this.
Anjali Koppal5ad44842014-03-10 20:34:39 -07001455 */
1456 public void setAllAppsButton(View allAppsButton) {
1457 mAllAppsButton = allAppsButton;
1458 }
1459
Sunny Goyalbb011da2016-06-15 15:42:29 -07001460 public View getStartViewForAllAppsRevealAnimation() {
Sunny Goyal2e084092016-06-22 14:37:34 -07001461 return FeatureFlags.NO_ALL_APPS_ICON ? mWorkspace.getPageIndicator() : mAllAppsButton;
Anjali Koppal5ad44842014-03-10 20:34:39 -07001462 }
1463
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001464 public View getWidgetsButton() {
1465 return mWidgetsButton;
1466 }
1467
Anjali Koppal5ad44842014-03-10 20:34:39 -07001468 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 * Creates a view representing a shortcut.
1470 *
1471 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001473 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001474 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 }
1476
1477 /**
1478 * Creates a view representing a shortcut inflated from the specified resource.
1479 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 * @param parent The group the shortcut belongs to.
1481 * @param info The data structure describing the shortcut.
1482 *
1483 * @return A View inflated from layoutResId.
1484 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001485 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001486 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001487 parent, false);
1488 favorite.applyFromShortcutInfo(info, mIconCache);
1489 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001491 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 return favorite;
1493 }
1494
1495 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 * Add a shortcut to the workspace.
1497 *
1498 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001500 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001501 int cellY) {
1502 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001503 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001504
Sunny Goyal5c97f512015-05-19 16:03:28 -07001505 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001506 if (info == null) {
1507 return;
1508 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001509 final View view = createShortcut(info);
1510
Sunny Goyal5c97f512015-05-19 16:03:28 -07001511 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001512 // First we check if we already know the exact location where we want to add this item.
1513 if (cellX >= 0 && cellY >= 0) {
1514 cellXY[0] = cellX;
1515 cellXY[1] = cellY;
1516 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001517
1518 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001519 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001520 true, null,null)) {
1521 return;
1522 }
1523 DragObject dragObject = new DragObject();
1524 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001525 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1526 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001527 return;
1528 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001529 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001530 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001531 }
1532
1533 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001534 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001535 return;
1536 }
1537
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001538 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539
1540 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001541 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001542 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 }
1544 }
1545
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001547 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001549 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 */
Adam Cohen091440a2015-03-18 14:16:05 -07001551 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001552 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1553
1554 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001555 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001556 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001557 }
Romain Guycbb89e42009-06-08 15:52:54 -07001558
Adam Cohen59400422014-03-05 18:07:04 -08001559 if (appWidgetInfo.isCustomWidget) {
1560 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001561 }
1562
Adam Cohen59400422014-03-05 18:07:04 -08001563 LauncherAppWidgetInfo launcherInfo;
1564 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1565 launcherInfo.spanX = info.spanX;
1566 launcherInfo.spanY = info.spanY;
1567 launcherInfo.minSpanX = info.minSpanX;
1568 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001569 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001570
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001572 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573
1574 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001575 if (hostView == null) {
1576 // Perform actual inflation because we're live
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001577 hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001578 }
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001579 hostView.setVisibility(View.VISIBLE);
1580 addAppWidgetToWorkspace(hostView, launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001581 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001582 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 }
Romain Guycbb89e42009-06-08 15:52:54 -07001584
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001585 private void addAppWidgetToWorkspace(
1586 AppWidgetHostView hostView, LauncherAppWidgetInfo item,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001587 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001588 hostView.setTag(item);
1589 item.onBindAppWidget(this, hostView);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001590
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001591 hostView.setFocusable(true);
1592 hostView.setOnFocusChangeListener(mFocusHandler);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001593
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001594 mWorkspace.addInScreen(hostView, item.container, item.screenId,
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001595 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1596
1597 if (!item.isCustomWidget()) {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07001598 addWidgetToAutoAdvanceIfNeeded(hostView, appWidgetInfo);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001599 }
1600 }
1601
Adam Cohended9f8d2010-11-03 13:25:16 -07001602 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1603 @Override
1604 public void onReceive(Context context, Intent intent) {
1605 final String action = intent.getAction();
1606 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1607 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001608 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001609 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001610
Winson Chungc100e8e2011-08-09 16:02:43 -07001611 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001612 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001613 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001614 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001615 if (!showWorkspace(false)) {
1616 // If we are already on the workspace, then manually reset all apps
1617 mAppsView.reset();
1618 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001619 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001620 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1621 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001622 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001623 }
1624 }
1625 };
1626
1627 @Override
1628 public void onAttachedToWindow() {
1629 super.onAttachedToWindow();
1630
1631 // Listen for broadcasts related to user-presence
1632 final IntentFilter filter = new IntentFilter();
1633 filter.addAction(Intent.ACTION_SCREEN_OFF);
1634 filter.addAction(Intent.ACTION_USER_PRESENT);
1635 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001636 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001637 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001638 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001639
1640 if (mLauncherCallbacks != null) {
1641 mLauncherCallbacks.onAttachedToWindow();
1642 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001643 }
1644
1645 @Override
1646 public void onDetachedFromWindow() {
1647 super.onDetachedFromWindow();
1648 mVisible = false;
1649
Adam Cohend113e0c2010-11-11 10:48:05 -08001650 if (mAttached) {
1651 unregisterReceiver(mReceiver);
1652 mAttached = false;
1653 }
Winson Chungb745afb2015-03-02 11:51:23 -08001654 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001655
1656 if (mLauncherCallbacks != null) {
1657 mLauncherCallbacks.onDetachedFromWindow();
1658 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001659 }
1660
1661 public void onWindowVisibilityChanged(int visibility) {
1662 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001663 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001664 // The following code used to be in onResume, but it turns out onResume is called when
1665 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1666 // is a more appropriate event to handle
1667 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001668 if (!mWorkspaceLoading) {
1669 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001670 // We want to let Launcher draw itself at least once before we force it to build
1671 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001672 // apps is nice and speedy.
1673 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001674 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001675 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001676 if (mStarted) return;
1677 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001678 // We delay the layer building a bit in order to give
1679 // other message processing a time to run. In particular
1680 // this avoids a delay in hiding the IME if it was
1681 // currently shown, because doing that may involve
1682 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001683 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001684 final ViewTreeObserver.OnDrawListener listener = this;
1685 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001686 public void run() {
1687 if (mWorkspace != null &&
1688 mWorkspace.getViewTreeObserver() != null) {
1689 mWorkspace.getViewTreeObserver().
1690 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001691 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001692 }
1693 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001694 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001695 }
1696 });
1697 }
1698 clearTypedText();
1699 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001700 }
1701
Adam Cohen091440a2015-03-18 14:16:05 -07001702 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001703 mHandler.removeMessages(ADVANCE_MSG);
1704 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1705 mHandler.sendMessageDelayed(msg, delay);
1706 mAutoAdvanceSentTime = System.currentTimeMillis();
1707 }
1708
Adam Cohen091440a2015-03-18 14:16:05 -07001709 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001710 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1711 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1712 mAutoAdvanceRunning = autoAdvanceRunning;
1713 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001714 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001715 sendAdvanceMessage(delay);
1716 } else {
1717 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001718 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001719 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1720 }
1721 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001722 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001723 }
1724 }
1725 }
1726
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001727 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1728
Adam Cohended9f8d2010-11-03 13:25:16 -07001729 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001730 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001731 if (msg.what == ADVANCE_MSG) {
1732 int i = 0;
1733 for (View key: mWidgetsToAdvance.keySet()) {
1734 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001735 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001736 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001737 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001738 public void run() {
1739 ((Advanceable) v).advance();
1740 }
1741 }, delay);
1742 }
1743 i++;
1744 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001745 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001746 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001747 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001748 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001749 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001750
Winsonc0b52fe2015-09-09 16:38:15 -07001751 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001752 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001753 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1754 if (v instanceof Advanceable) {
1755 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001756 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001757 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001758 }
1759 }
1760
Winsonc0b52fe2015-09-09 16:38:15 -07001761 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001762 if (mWidgetsToAdvance.containsKey(hostView)) {
1763 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001764 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001765 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001766 }
1767
Hyunyoung Song3f471442015-04-08 19:01:34 -07001768 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001769 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1770 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001771 }
1772
Winson Chunga6945242014-01-08 14:04:34 -08001773 public DragLayer getDragLayer() {
1774 return mDragLayer;
1775 }
1776
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001777 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001778 return mAppsView;
1779 }
1780
Hyunyoung Song3f471442015-04-08 19:01:34 -07001781 public WidgetsContainerView getWidgetsView() {
1782 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001783 }
1784
Winson Chunga6945242014-01-08 14:04:34 -08001785 public Workspace getWorkspace() {
1786 return mWorkspace;
1787 }
1788
Sunny Goyal6178f132016-07-11 17:30:03 -07001789 public View getQsbContainer() {
1790 return mQsbContainer;
1791 }
1792
Winson Chunga6945242014-01-08 14:04:34 -08001793 public Hotseat getHotseat() {
1794 return mHotseat;
1795 }
1796
Jorim Jaggid017f882014-01-14 17:08:48 -08001797 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001798 return mOverviewPanel;
1799 }
1800
Sunny Goyal47328fd2016-05-25 18:56:41 -07001801 public DropTargetBar getDropTargetBar() {
1802 return mDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -07001803 }
1804
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001805 public LauncherAppWidgetHost getAppWidgetHost() {
1806 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807 }
Romain Guycbb89e42009-06-08 15:52:54 -07001808
Winson Chunga9abd0e2010-10-27 17:18:37 -07001809 public LauncherModel getModel() {
1810 return mModel;
1811 }
1812
Adam Cohen79d90c52016-04-22 13:29:20 -07001813 public SharedPreferences getSharedPrefs() {
Winson Chunga6945242014-01-08 14:04:34 -08001814 return mSharedPrefs;
1815 }
1816
Adam Cohen2e6da152015-05-06 11:42:25 -07001817 public DeviceProfile getDeviceProfile() {
1818 return mDeviceProfile;
1819 }
1820
Bjorn Bringertc459e522013-06-07 19:36:01 +01001821 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001822 getWindow().closeAllPanels();
1823
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001824 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001825 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001826 }
1827
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001828 @Override
1829 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001830 long startTime = 0;
1831 if (DEBUG_RESUME_TIME) {
1832 startTime = System.currentTimeMillis();
1833 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834 super.onNewIntent(intent);
1835
1836 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001837 Folder openFolder = mWorkspace.getOpenFolder();
1838 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1839 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1840 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1841 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1842 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001843 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001844 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001845
Adam Cohen6fecd412013-10-02 17:41:50 -07001846 if (mWorkspace == null) {
1847 // Can be cases where mWorkspace is null, this prevents a NPE
1848 return;
1849 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001850 // In all these cases, only animate if we're already on home
1851 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001852
Sunny Goyal935fca12015-10-13 10:19:01 -07001853 closeFolder(alreadyOnHome);
Tony Wickham49c8d292016-07-01 11:44:34 -07001854 closeShortcutsContainer();
Adam Cohen6fecd412013-10-02 17:41:50 -07001855 exitSpringLoadedDragMode();
1856
1857 // If we are already on home, then just animate back to the workspace,
1858 // otherwise, just wait until onResume to set the state back to Workspace
1859 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001860 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001861 } else {
1862 mOnResumeState = State.WORKSPACE;
1863 }
1864
1865 final View v = getWindow().peekDecorView();
1866 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001867 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001868 INPUT_METHOD_SERVICE);
1869 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1870 }
1871
Winson Chungb745afb2015-03-02 11:51:23 -08001872 // Reset the apps view
1873 if (!alreadyOnHome && mAppsView != null) {
1874 mAppsView.scrollToTop();
1875 }
1876
Hyunyoung Song3f471442015-04-08 19:01:34 -07001877 // Reset the widgets view
1878 if (!alreadyOnHome && mWidgetsView != null) {
1879 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001880 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001881
Adam Cohen9211d422014-10-07 18:14:53 -07001882 if (mLauncherCallbacks != null) {
1883 mLauncherCallbacks.onHomeIntent();
1884 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 }
Adam Cohened307df2013-10-02 09:37:31 -07001886
Adam Cohen9211d422014-10-07 18:14:53 -07001887 if (mLauncherCallbacks != null) {
1888 mLauncherCallbacks.onNewIntent(intent);
1889 }
Winson15f8b172015-08-19 11:02:39 -07001890
1891 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1892 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1893 // animation.
1894 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001895 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1896 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001897 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1898 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001899
1900 // We use this flag to suppress noisy callbacks above custom content state
1901 // from onResume.
1902 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001903 mWorkspace.post(new Runnable() {
1904 @Override
1905 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001906 if (mWorkspace != null) {
1907 mWorkspace.moveToDefaultScreen(true);
1908 }
Winson15f8b172015-08-19 11:02:39 -07001909 }
1910 });
1911 }
1912 }
1913
1914 if (DEBUG_RESUME_TIME) {
1915 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1916 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001917 }
1918
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001920 public void onRestoreInstanceState(Bundle state) {
1921 super.onRestoreInstanceState(state);
1922 for (int page: mSynchronouslyBoundPages) {
1923 mWorkspace.restoreInstanceStateForChild(page);
1924 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 }
1926
1927 @Override
1928 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001929 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001930 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1931 mWorkspace.getCurrentPageOffsetFromCustomContent());
Hyunyoung Song645764e2016-06-06 14:19:02 -07001932
Adam Cohen21cd0022013-10-09 18:57:02 -07001933 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001934 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935
Michael Jurka883f55b2010-10-21 15:47:14 -07001936 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001937 // We close any open folder since it will not be re-opened, and we need to make sure
1938 // this state is reflected.
1939 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1940 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941
Tony Wickham49c8d292016-07-01 11:44:34 -07001942 closeShortcutsContainer();
1943
Adam Cohendcd297f2013-06-18 13:13:40 -07001944 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001945 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001946 ContentValues itemValues = new ContentValues();
1947 mPendingAddInfo.writeToValues(itemValues);
1948 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001949 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001950 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951 }
1952
Adam Cohen9211d422014-10-07 18:14:53 -07001953 if (mLauncherCallbacks != null) {
1954 mLauncherCallbacks.onSaveInstanceState(outState);
1955 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956 }
1957
1958 @Override
1959 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001961
Winson Chunge7a03942011-08-05 15:05:12 -07001962 // Remove all pending runnables
1963 mHandler.removeMessages(ADVANCE_MSG);
1964 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001965 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001966 mWorkspace.removeFolderListeners();
Winson Chunge7a03942011-08-05 15:05:12 -07001967
Winson Chungcd2b0142011-06-08 16:02:26 -07001968 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001969 // It's possible to receive onDestroy after a new Launcher activity has
1970 // been created. In this case, don't interfere with the new Launcher.
1971 if (mModel.isCurrentCallbacks(this)) {
1972 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001973 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001974 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001975
Sunny Goyal745bad92016-05-02 10:54:12 -07001976 if (mRotationPrefChangeHandler != null) {
1977 mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
1978 }
1979
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001981 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001983 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001985 mAppWidgetHost = null;
1986
1987 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988
1989 TextKeyListener.getInstance().release();
1990
Tony Wickhame2217252016-03-22 16:34:23 -07001991 ((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
1992 .removeAccessibilityStateChangeListener(this);
1993
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001994 unregisterReceiver(mUiBroadcastReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001995
Michael Jurka2ecf9952012-06-18 12:52:28 -07001996 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001997
1998 if (mLauncherCallbacks != null) {
1999 mLauncherCallbacks.onDestroy();
2000 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 }
2002
Sunny Goyalae502842016-06-17 08:43:56 -07002003 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
2004 return mAccessibilityDelegate;
2005 }
2006
Adam Cohena9cf38f2011-05-02 15:36:58 -07002007 public DragController getDragController() {
2008 return mDragController;
2009 }
2010
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011 @Override
2012 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002013 onStartForResult(requestCode);
2014 super.startActivityForResult(intent, requestCode);
2015 }
2016
2017 @Override
2018 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2019 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2020 onStartForResult(requestCode);
2021 try {
2022 super.startIntentSenderForResult(intent, requestCode,
2023 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2024 } catch (IntentSender.SendIntentException e) {
2025 throw new ActivityNotFoundException();
2026 }
2027 }
2028
2029 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002030 if (requestCode >= 0) {
2031 setWaitingForResult(true);
2032 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002033 }
2034
Winson Chung70d72102011-08-12 11:24:35 -07002035 /**
2036 * Indicates that we want global search for this activity by setting the globalSearch
2037 * argument for {@link #startSearch} to true.
2038 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002040 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002042
Karl Rosaen138a0412009-04-23 19:00:21 -07002043 if (initialQuery == null) {
2044 // Use any text typed in the launcher as the initial query
2045 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002046 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 if (appSearchData == null) {
2048 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002049 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002050 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07002051
2052 // TODO send proper bounds.
2053 Rect sourceBounds = null;
Romain Guycbb89e42009-06-08 15:52:54 -07002054
Ian Parkinson047036e2014-09-01 15:40:53 +01002055 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002056 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002057 if (clearTextImmediately) {
2058 clearTypedText();
2059 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002060
2061 // We need to show the workspace after starting the search
2062 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002063 }
2064
Ian Parkinson047036e2014-09-01 15:40:53 +01002065 /**
2066 * Start a text search.
2067 *
2068 * @return {@code true} if the search will start immediately, so any further keypresses
2069 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2070 * to buffer keypresses.
2071 */
2072 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002073 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002074 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2075 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2076 sourceBounds);
2077 }
2078
Michael Jurkaa33411c2012-06-14 16:18:21 -07002079 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002080 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002081 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002082 }
2083
2084 /**
2085 * Starts the global search activity. This code is a copied from SearchManager
2086 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002087 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002088 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002089 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002090 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2091 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2092 if (globalSearchActivity == null) {
2093 Log.w(TAG, "No global search activity found.");
2094 return;
2095 }
2096 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2097 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2098 intent.setComponent(globalSearchActivity);
2099 // Make sure that we have a Bundle to put source in
2100 if (appSearchData == null) {
2101 appSearchData = new Bundle();
2102 } else {
2103 appSearchData = new Bundle(appSearchData);
2104 }
Adam Cohen9211d422014-10-07 18:14:53 -07002105 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002106 if (!appSearchData.containsKey("source")) {
2107 appSearchData.putString("source", getPackageName());
2108 }
2109 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2110 if (!TextUtils.isEmpty(initialQuery)) {
2111 intent.putExtra(SearchManager.QUERY, initialQuery);
2112 }
2113 if (selectInitialQuery) {
2114 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2115 }
2116 intent.setSourceBounds(sourceBounds);
2117 try {
2118 startActivity(intent);
2119 } catch (ActivityNotFoundException ex) {
2120 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2121 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002122 }
2123
Yura4f93ec62014-02-04 14:15:21 +00002124 public boolean isOnCustomContent() {
2125 return mWorkspace.isOnOrMovingToCustomContent();
2126 }
2127
Winson Chung70d72102011-08-12 11:24:35 -07002128 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002129 public boolean onPrepareOptionsMenu(Menu menu) {
2130 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002131 if (mLauncherCallbacks != null) {
2132 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2133 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002134 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002135 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002136
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002137 @Override
2138 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002139 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002140 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002141 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002142 }
2143
Joe Onorato9c1289c2009-08-17 11:03:03 -04002144 public boolean isWorkspaceLocked() {
2145 return mWorkspaceLoading || mWaitingForResult;
2146 }
2147
Adam Cohen517a7f52014-03-01 12:12:59 -08002148 public boolean isWorkspaceLoading() {
2149 return mWorkspaceLoading;
2150 }
2151
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002152 private void setWorkspaceLoading(boolean value) {
2153 boolean isLocked = isWorkspaceLocked();
2154 mWorkspaceLoading = value;
2155 if (isLocked != isWorkspaceLocked()) {
2156 onWorkspaceLockedChanged();
2157 }
2158 }
2159
2160 private void setWaitingForResult(boolean value) {
2161 boolean isLocked = isWorkspaceLocked();
2162 mWaitingForResult = value;
2163 if (isLocked != isWorkspaceLocked()) {
2164 onWorkspaceLockedChanged();
2165 }
2166 }
2167
Adam Cohen9211d422014-10-07 18:14:53 -07002168 protected void onWorkspaceLockedChanged() {
2169 if (mLauncherCallbacks != null) {
2170 mLauncherCallbacks.onWorkspaceLockedChanged();
2171 }
2172 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002173
Michael Jurka0280c3b2010-09-17 15:00:07 -07002174 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002175 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002176 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002177 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2178 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002179 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002180 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002181
Tony Wickhama0628cc2015-10-14 15:23:04 -07002182 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002183 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002184 if (LOGD) {
2185 Log.d(TAG, "Adding widget from drop");
2186 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002187 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2188 }
2189
Sunny Goyale6b63a32015-01-16 16:14:29 -08002190 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002191 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2192 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002193 if (appWidgetInfo.configure != null) {
2194 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002195 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002196
Bjorn Bringert7984c942009-12-09 15:38:25 +00002197 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002198 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2199 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2200
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002201 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002202 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002203 Runnable onComplete = new Runnable() {
2204 @Override
2205 public void run() {
2206 // Exit spring loaded mode if necessary after adding the widget
2207 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2208 null);
2209 }
2210 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002211 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002212 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002213 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214 }
2215 }
Romain Guycbb89e42009-06-08 15:52:54 -07002216
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002217 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002218 // Close any folders that may be open.
2219 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002220 mWorkspace.moveToCustomContentScreen(animate);
2221 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002222
2223 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2224 int[] cell, int spanX, int spanY) {
2225 switch (info.itemType) {
2226 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2227 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2228 int span[] = new int[2];
2229 span[0] = spanX;
2230 span[1] = spanY;
2231 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2232 container, screenId, cell, span);
2233 break;
2234 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2235 processShortcutFromDrop(info.componentName, container, screenId, cell);
2236 break;
2237 default:
2238 throw new IllegalStateException("Unknown item type: " + info.itemType);
2239 }
2240 }
2241
Adam Cohenfbba09b2011-07-18 21:43:05 -07002242 /**
2243 * Process a shortcut drop.
2244 *
2245 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002246 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002247 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002248 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002249 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2250 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002251 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002252 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002253 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002254
2255 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002256 mPendingAddInfo.cellX = cell[0];
2257 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002258 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002259
2260 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2261 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002262 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002263 }
2264
Adam Cohenfbba09b2011-07-18 21:43:05 -07002265 /**
2266 * Process a widget drop.
2267 *
2268 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002269 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002270 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002271 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002272 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2273 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002274 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002275 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002276 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002277 mPendingAddInfo.minSpanX = info.minSpanX;
2278 mPendingAddInfo.minSpanY = info.minSpanY;
2279
Adam Cohenfbba09b2011-07-18 21:43:05 -07002280 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002281 mPendingAddInfo.cellX = cell[0];
2282 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002283 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002284 if (span != null) {
2285 mPendingAddInfo.spanX = span[0];
2286 mPendingAddInfo.spanY = span[1];
2287 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002288
Adam Cohened66b2b2012-01-23 17:28:51 -08002289 AppWidgetHostView hostView = info.boundWidget;
2290 int appWidgetId;
2291 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002292 // In the case where we've prebound the widget, we remove it from the DragLayer
2293 if (LOGD) {
2294 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2295 }
2296 getDragLayer().removeView(hostView);
2297
Adam Cohened66b2b2012-01-23 17:28:51 -08002298 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002299 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002300
2301 // Clear the boundWidget so that it doesn't get destroyed.
2302 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002303 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002304 // In this case, we either need to start an activity to get permission to bind
2305 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002306 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002307 Bundle options = info.bindOptions;
2308
Sunny Goyalffe83f12014-08-14 17:39:34 -07002309 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2310 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002311 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002312 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002313 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002314 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002315 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2316 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2317 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002318 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2319 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002320 // TODO: we need to make sure that this accounts for the options bundle.
2321 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002322 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2323 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002324 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002325 }
2326
Adam Cohendcd297f2013-06-18 13:13:40 -07002327 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002328 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002329 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002330 folderInfo.title = getText(R.string.folder_name);
2331
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002332 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002333 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2334 cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002335
2336 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002337 FolderIcon newFolder =
2338 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002339 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002340 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002341 // Force measure the new folder icon
2342 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2343 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002344 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002345 }
Romain Guycbb89e42009-06-08 15:52:54 -07002346
Winsonc0b52fe2015-09-09 16:38:15 -07002347 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002348 * Unbinds the view for the specified item, and removes the item and all its children.
2349 *
2350 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002351 * @param itemInfo the {@link ItemInfo} for this view.
2352 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002353 */
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002354 public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002355 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002356 // Remove the shortcut from the folder before removing it from launcher
Sunny Goyal44c06432016-04-02 10:56:02 -07002357 View folderIcon = mWorkspace.getHomescreenIconByItemId(itemInfo.container);
2358 if (folderIcon instanceof FolderIcon) {
Sunny Goyalc52ba712016-04-05 15:59:05 -07002359 ((FolderInfo) folderIcon.getTag()).remove((ShortcutInfo) itemInfo, true);
Winsonfa56b3f2015-09-14 12:01:13 -07002360 } else {
2361 mWorkspace.removeWorkspaceItem(v);
2362 }
Winsonc0b52fe2015-09-09 16:38:15 -07002363 if (deleteFromDb) {
2364 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2365 }
2366 } else if (itemInfo instanceof FolderInfo) {
2367 final FolderInfo folderInfo = (FolderInfo) itemInfo;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07002368 if (v instanceof FolderIcon) {
2369 ((FolderIcon) v).removeListeners();
2370 }
Winsonc0b52fe2015-09-09 16:38:15 -07002371 mWorkspace.removeWorkspaceItem(v);
2372 if (deleteFromDb) {
2373 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2374 }
2375 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2376 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002377 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07002378 removeWidgetToAutoAdvance(v);
Winsonc0b52fe2015-09-09 16:38:15 -07002379 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002380 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002381 }
2382 } else {
2383 return false;
2384 }
2385 return true;
2386 }
2387
2388 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002389 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002390 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002391 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002392 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyald478c832016-04-01 12:04:16 -07002393 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdAllocated()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002394 // Deleting an app widget ID is a void call but writes to disk before returning
2395 // to the caller...
2396 new AsyncTask<Void, Void, Void>() {
2397 public Void doInBackground(Void ... args) {
2398 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2399 return null;
2400 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002401 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002402 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002403 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002404 }
2405
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002406 @Override
2407 public boolean dispatchKeyEvent(KeyEvent event) {
2408 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2409 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002410 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002411 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002412 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002413 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002414 dumpState();
2415 return true;
2416 }
2417 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002418 }
2419 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2420 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002421 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002422 return true;
2423 }
2424 }
2425
2426 return super.dispatchKeyEvent(event);
2427 }
2428
Joe Onorato88ec0992009-11-19 13:16:06 -08002429 @Override
2430 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002431 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2432 return;
2433 }
2434
Sunny Goyal45478022015-06-08 16:52:41 -07002435 if (mDragController.isDragging()) {
2436 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002437 return;
2438 }
2439
Tony Wickham49c8d292016-07-01 11:44:34 -07002440 if (getOpenShortcutsContainer() != null) {
2441 closeShortcutsContainer();
2442 } else if (isAppsViewVisible()) {
Winson Chungb745afb2015-03-02 11:51:23 -08002443 showWorkspace(true);
2444 } else if (isWidgetsViewVisible()) {
2445 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002446 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002447 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002448 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002449 Folder openFolder = mWorkspace.getOpenFolder();
2450 if (openFolder.isEditingName()) {
2451 openFolder.dismissEditingName();
2452 } else {
2453 closeFolder();
2454 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002455 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002456 mWorkspace.exitWidgetResizeMode();
2457
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002458 // Back button is a no-op here, but give at least some feedback for the button press
2459 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002460 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002461 }
2462
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002463 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002464 * Launches the intent referred by the clicked shortcut.
2465 *
2466 * @param v The view representing the clicked shortcut.
2467 */
2468 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002469 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2470 // view has detached (it's possible for this to happen if the view is removed mid touch).
2471 if (v.getWindowToken() == null) {
2472 return;
2473 }
2474
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002475 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002476 return;
2477 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002478
Adam Cohen1697b792013-09-17 19:08:21 -07002479 if (v instanceof Workspace) {
2480 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002481 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002482 }
2483 return;
2484 }
2485
2486 if (v instanceof CellLayout) {
2487 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002488 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2489 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002490 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002491 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002492 }
2493
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002494 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002495 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002496 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002497 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002498 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002499 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002500 }
Sunny Goyalbb011da2016-06-15 15:42:29 -07002501 } else if (v instanceof PageIndicator || (v == mAllAppsButton && mAllAppsButton != null)) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002502 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002503 } else if (tag instanceof AppInfo) {
2504 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002505 } else if (tag instanceof LauncherAppWidgetInfo) {
2506 if (v instanceof PendingAppWidgetHostView) {
2507 onClickPendingWidget((PendingAppWidgetHostView) v);
2508 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002509 }
2510 }
2511
Sunny Goyal70660032015-05-14 00:07:08 -07002512 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002513 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002514 return false;
2515 }
2516
Michael Jurkaaf442092010-06-10 17:01:57 -07002517 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002518 * Event handler for the app widget view which has not fully restored.
2519 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002520 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002521 if (mIsSafeModeEnabled) {
2522 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2523 return;
2524 }
2525
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002526 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002527 if (v.isReadyForClickSetup()) {
Sunny Goyald478c832016-04-01 12:04:16 -07002528 if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
2529 if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
2530 // This should not happen, as we make sure that an Id is allocated during bind.
2531 return;
2532 }
2533 LauncherAppWidgetProviderInfo appWidgetInfo =
2534 mAppWidgetManager.findProvider(info.providerName, info.user);
2535 if (appWidgetInfo != null) {
2536 mPendingAddWidgetId = info.appWidgetId;
2537 mPendingAddInfo.copyFrom(info);
2538 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002539
Sunny Goyald478c832016-04-01 12:04:16 -07002540 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2541 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingAddWidgetId);
2542 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider);
2543 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2544 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
2545 startActivityForResult(intent, REQUEST_BIND_PENDING_APPWIDGET);
2546 }
2547 } else {
2548 LauncherAppWidgetProviderInfo appWidgetInfo =
2549 mAppWidgetManager.getLauncherAppWidgetInfo(info.appWidgetId);
2550 if (appWidgetInfo != null) {
2551 startRestoredWidgetReconfigActivity(appWidgetInfo, info);
2552 }
Sunny Goyalff572272014-07-23 13:58:07 -07002553 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002554 } else if (info.installProgress < 0) {
2555 // The install has not been queued
2556 final String packageName = info.providerName.getPackageName();
2557 showBrokenAppInstallDialog(packageName,
2558 new DialogInterface.OnClickListener() {
2559 public void onClick(DialogInterface dialog, int id) {
2560 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2561 }
2562 });
2563 } else {
2564 // Download has started.
2565 final String packageName = info.providerName.getPackageName();
2566 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002567 }
2568 }
2569
Sunny Goyald478c832016-04-01 12:04:16 -07002570 private void startRestoredWidgetReconfigActivity(
2571 LauncherAppWidgetProviderInfo provider, LauncherAppWidgetInfo info) {
2572 mPendingAddWidgetInfo = provider;
2573 mPendingAddInfo.copyFrom(info);
2574 mPendingAddWidgetId = info.appWidgetId;
2575 mAppWidgetManager.startConfigActivity(provider,
2576 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
2577 }
2578
Sunny Goyalff572272014-07-23 13:58:07 -07002579 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002580 * Event handler for the "grid" button that appears on the home screen, which
2581 * enters all apps mode.
2582 *
2583 * @param v The view that was clicked.
2584 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002585 protected void onClickAllAppsButton(View v) {
2586 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002587 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002588 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002589 true /* updatePredictedApps */, false /* focusSearchBar */);
2590 }
2591 }
2592
2593 protected void onLongClickAllAppsButton(View v) {
2594 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2595 if (!isAppsViewVisible()) {
2596 showAppsView(true /* animated */, false /* resetListToTop */,
2597 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002598 }
2599 }
2600
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002601 private void showBrokenAppInstallDialog(final String packageName,
2602 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002603 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002604 .setTitle(R.string.abandoned_promises_title)
2605 .setMessage(R.string.abandoned_promise_explanation)
2606 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2607 .setNeutralButton(R.string.abandoned_clean_this,
2608 new DialogInterface.OnClickListener() {
2609 public void onClick(DialogInterface dialog, int id) {
2610 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2611 mWorkspace.removeAbandonedPromise(packageName, user);
2612 }
2613 })
2614 .create().show();
2615 return;
2616 }
2617
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002618 /**
2619 * Event handler for an app shortcut click.
2620 *
2621 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2622 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002623 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002624 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2625 Object tag = v.getTag();
2626 if (!(tag instanceof ShortcutInfo)) {
2627 throw new IllegalArgumentException("Input must be a Shortcut");
2628 }
2629
2630 // Open shortcut
2631 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002632
2633 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002634 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2635 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002636 // Launch activity anyway, framework will tell the user why the app is suspended.
2637 } else {
2638 int error = R.string.activity_not_available;
2639 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2640 error = R.string.safemode_shortcut_error;
2641 }
2642 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2643 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002644 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002645 }
2646
Chris Wren40c5ed32014-06-24 18:24:23 -04002647 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002648 if ((v instanceof BubbleTextView)
2649 && shortcut.isPromise()
2650 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002651 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002652 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002653 new DialogInterface.OnClickListener() {
2654 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002655 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002656 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002657 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002658 return;
2659 }
2660
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002661 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002662 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002663 }
2664
Sunny Goyala7ce1662016-05-31 15:01:35 -07002665 private void startAppShortcutOrInfoActivity(View v) {
2666 ItemInfo item = (ItemInfo) v.getTag();
2667 Intent intent = item.getIntent();
2668 if (intent == null) {
2669 throw new IllegalArgumentException("Input must have a valid intent");
Sunny Goyal508da152014-08-14 10:53:27 -07002670 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002671 boolean success = startActivitySafely(v, intent, item);
Hyunyoung Songaa953652016-04-19 18:30:24 -07002672 getUserEventDispatcher().logAppLaunch(v, intent);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002673
2674 if (success && v instanceof BubbleTextView) {
2675 mWaitingForResume = (BubbleTextView) v;
2676 mWaitingForResume.setStayPressed(true);
2677 }
2678 }
2679
2680 /**
2681 * Event handler for a folder icon click.
2682 *
2683 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2684 */
2685 protected void onClickFolderIcon(View v) {
2686 if (LOGD) Log.d(TAG, "onClickFolder");
2687 if (!(v instanceof FolderIcon)){
2688 throw new IllegalArgumentException("Input must be a FolderIcon");
2689 }
2690
2691 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002692 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002693 // Open the requested folder
Adam Cohen37b2a492016-04-06 18:36:06 -07002694 openFolder(folderIcon);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002695 }
Michael Jurka5130e402011-10-13 04:55:35 -07002696 }
2697
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002698 /**
2699 * Event handler for the (Add) Widgets button that appears after a long press
2700 * on the home screen.
2701 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002702 public void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002703 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002704 if (mIsSafeModeEnabled) {
2705 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2706 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002707 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Adam Cohen9211d422014-10-07 18:14:53 -07002708 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002709 }
2710
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002711 /**
2712 * Event handler for the wallpaper picker button that appears after a long press
2713 * on the home screen.
2714 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002715 public void onClickWallpaperPicker(View v) {
Sunny Goyal1ed6c4a2016-04-21 15:16:11 -07002716 if (!Utilities.isWallapaperAllowed(this)) {
2717 Toast.makeText(this, R.string.msg_disabled_by_admin, Toast.LENGTH_SHORT).show();
2718 return;
2719 }
2720
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002721 String pickerPackage = getString(R.string.wallpaper_picker_package);
2722 if (TextUtils.isEmpty(pickerPackage)) {
2723 pickerPackage = PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
2724 }
2725
Tony Wickham785f7a52015-08-31 17:28:32 -07002726 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2727 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
Sunny Goyal04cc3a72016-05-17 10:32:43 -07002728 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER)
2729 .setPackage(pickerPackage)
2730 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset),
2731 REQUEST_PICK_WALLPAPER);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002732 }
2733
2734 /**
2735 * Event handler for a click on the settings button that appears after a long press
2736 * on the home screen.
2737 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07002738 public void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002739 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Sunny Goyal745bad92016-05-02 10:54:12 -07002740 startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
2741 .setPackage(getPackageName()));
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002742 }
2743
Adam Cohen61f560d2013-09-30 15:58:20 -07002744 public View.OnTouchListener getHapticFeedbackTouchListener() {
2745 if (mHapticFeedbackTouchListener == null) {
2746 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002747 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002748 @Override
2749 public boolean onTouch(View v, MotionEvent event) {
2750 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2751 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2752 }
2753 return false;
2754 }
2755 };
2756 }
2757 return mHapticFeedbackTouchListener;
2758 }
2759
Tony Wickhame2217252016-03-22 16:34:23 -07002760 @Override
2761 public void onAccessibilityStateChanged(boolean enabled) {
2762 mDragLayer.onAccessibilityStateChanged(enabled);
2763 }
2764
Adam Cohen9211d422014-10-07 18:14:53 -07002765 public void onDragStarted(View view) {
2766 if (isOnCustomContent()) {
2767 // Custom content screen doesn't participate in drag and drop. If on custom
2768 // content screen, move to default.
2769 moveWorkspaceToDefaultScreen();
2770 }
Adam Cohen9211d422014-10-07 18:14:53 -07002771 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002772
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002773 /**
2774 * Called when the user stops interacting with the launcher.
2775 * This implies that the user is now on the homescreen and is not doing housekeeping.
2776 */
Adam Cohen9211d422014-10-07 18:14:53 -07002777 protected void onInteractionEnd() {
2778 if (mLauncherCallbacks != null) {
2779 mLauncherCallbacks.onInteractionEnd();
2780 }
2781 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002782
2783 /**
2784 * Called when the user starts interacting with the launcher.
2785 * The possible interactions are:
2786 * - open all apps
2787 * - reorder an app shortcut, or a widget
2788 * - open the overview mode.
2789 * This is a good time to stop doing things that only make sense
2790 * when the user is on the homescreen and not doing housekeeping.
2791 */
Adam Cohen9211d422014-10-07 18:14:53 -07002792 protected void onInteractionBegin() {
2793 if (mLauncherCallbacks != null) {
2794 mLauncherCallbacks.onInteractionBegin();
2795 }
2796 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002797
Winson Chungcd99cd32015-04-29 11:03:24 -07002798 /** Updates the interaction state. */
2799 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002800 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002801 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002802 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2803 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002804 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002805 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002806 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002807 onInteractionEnd();
2808 }
2809 }
2810
Sunny Goyala7ce1662016-05-31 15:01:35 -07002811 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002812 try {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002813 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
2814 try {
2815 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
2816 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
2817 // is enabled by default on NYC.
2818 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
2819 .penaltyLog().build());
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002820
2821 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
2822 String id = ((ShortcutInfo) info).getDeepShortcutId();
2823 String packageName = intent.getPackage();
Sunny Goyal9994b2b2016-06-23 14:17:24 -07002824 LauncherAppState.getInstance().getShortcutManager().startShortcut(
2825 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002826 } else {
2827 // Could be launching some bookkeeping activity
2828 startActivity(intent, optsBundle);
2829 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002830 } finally {
2831 StrictMode.setVmPolicy(oldPolicy);
Kenny Guy1317e2d2014-05-08 18:52:50 +01002832 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002833 } catch (SecurityException e) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002834 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2835 // corresponding permission. Show the appropriate permission prompt if that
2836 // is the case.
2837 if (intent.getComponent() == null
2838 && Intent.ACTION_CALL.equals(intent.getAction())
2839 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2840 PackageManager.PERMISSION_GRANTED) {
2841 // TODO: Rename sPendingAddItem to a generic name.
2842 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2843 0, info);
2844 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2845 REQUEST_PERMISSION_CALL_PHONE);
2846 } else {
2847 // No idea why this was thrown.
2848 throw e;
Sunny Goyal28c6b962015-10-12 11:42:05 -07002849 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002850 }
2851 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002852
Sunny Goyala7ce1662016-05-31 15:01:35 -07002853 private Bundle getActivityLaunchOptions(View v) {
2854 if (Utilities.ATLEAST_MARSHMALLOW) {
2855 int left = 0, top = 0;
2856 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2857 if (v instanceof TextView) {
2858 // Launch from center of icon, not entire view
2859 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2860 if (icon != null) {
2861 Rect bounds = icon.getBounds();
2862 left = (width - bounds.width()) / 2;
2863 top = v.getPaddingTop();
2864 width = bounds.width();
2865 height = bounds.height();
2866 }
2867 }
2868 return ActivityOptions.makeClipRevealAnimation(v, left, top, width, height).toBundle();
2869 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
2870 // On L devices, we use the device default slide-up transition.
2871 // On L MR1 devices, we use a custom version of the slide-up transition which
2872 // doesn't have the delay present in the device default.
2873 return ActivityOptions.makeCustomAnimation(
2874 this, R.anim.task_open_enter, R.anim.no_anim).toBundle();
2875 }
2876 return null;
2877 }
2878
2879 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002880 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2881 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2882 return false;
2883 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002884 // Only launch using the new animation if the shortcut has not opted out (this is a
2885 // private contract between launcher and may be ignored in the future).
2886 boolean useLaunchAnimation = (v != null) &&
2887 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2888 Bundle optsBundle = useLaunchAnimation ? getActivityLaunchOptions(v) : null;
2889
2890 UserHandleCompat user = null;
2891 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2892 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2893 user = UserManagerCompat.getInstance(this).getUserForSerialNumber(serialNumber);
Michael Jurka86a720e2012-05-09 11:23:23 -07002894 }
Sunny Goyala7ce1662016-05-31 15:01:35 -07002895
2896 // Prepare intent
2897 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2898 if (v != null) {
2899 int[] pos = new int[2];
2900 v.getLocationOnScreen(pos);
2901 intent.setSourceBounds(
2902 new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2903 }
2904 try {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002905 if (Utilities.ATLEAST_MARSHMALLOW && item != null
2906 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
2907 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)) {
Sunny Goyala7ce1662016-05-31 15:01:35 -07002908 // Shortcuts need some special checks due to legacy reasons.
2909 startShortcutIntentSafely(intent, optsBundle, item);
2910 } else if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2911 // Could be launching some bookkeeping activity
2912 startActivity(intent, optsBundle);
2913 } else {
2914 LauncherAppsCompat.getInstance(this).startActivityForProfile(
2915 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
2916 }
2917 return true;
2918 } catch (ActivityNotFoundException|SecurityException e) {
2919 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2920 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
2921 }
2922 return false;
Michael Jurka86a720e2012-05-09 11:23:23 -07002923 }
2924
Adam Cohen268c4752012-06-06 17:47:33 -07002925 /**
2926 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2927 * in the DragLayer in the exact absolute location of the original FolderIcon.
2928 */
2929 private void copyFolderIconToImage(FolderIcon fi) {
2930 final int width = fi.getMeasuredWidth();
2931 final int height = fi.getMeasuredHeight();
2932
2933 // Lazy load ImageView, Bitmap and Canvas
2934 if (mFolderIconImageView == null) {
2935 mFolderIconImageView = new ImageView(this);
2936 }
2937 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2938 mFolderIconBitmap.getHeight() != height) {
2939 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2940 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2941 }
2942
2943 DragLayer.LayoutParams lp;
2944 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2945 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2946 } else {
2947 lp = new DragLayer.LayoutParams(width, height);
2948 }
2949
Adam Cohen307fe232012-08-16 17:55:58 -07002950 // The layout from which the folder is being opened may be scaled, adjust the starting
2951 // view size by this scale factor.
2952 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002953 lp.customPosition = true;
2954 lp.x = mRectForFolderAnimation.left;
2955 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002956 lp.width = (int) (scale * width);
2957 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002958
2959 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2960 fi.draw(mFolderIconCanvas);
2961 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002962 if (fi.getFolder() != null) {
2963 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2964 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002965 }
Adam Cohen268c4752012-06-06 17:47:33 -07002966 // Just in case this image view is still in the drag layer from a previous animation,
2967 // we remove it and re-add it.
2968 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2969 mDragLayer.removeView(mFolderIconImageView);
2970 }
2971 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002972 if (fi.getFolder() != null) {
2973 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002974 }
Adam Cohen268c4752012-06-06 17:47:33 -07002975 }
2976
Adam Cohen37b2a492016-04-06 18:36:06 -07002977 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002978 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002979 FolderInfo info = (FolderInfo) fi.getTag();
2980 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2981 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002982 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2983 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002984 }
2985
Adam Cohen268c4752012-06-06 17:47:33 -07002986 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2987 copyFolderIconToImage(fi);
2988 fi.setVisibility(View.INVISIBLE);
2989
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002990 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2991 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002992 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002993 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2994 }
2995 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002996 oa.start();
2997 }
2998
Sunny Goyal935fca12015-10-13 10:19:01 -07002999 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003000 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003001 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003002
Adam Cohen268c4752012-06-06 17:47:33 -07003003 // We remove and re-draw the FolderIcon in-case it has changed
3004 mDragLayer.removeView(mFolderIconImageView);
3005 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08003006
3007 if (cl != null) {
3008 cl.clearFolderLeaveBehind();
3009 }
3010
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003011 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003012 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003013 oa.addListener(new AnimatorListenerAdapter() {
3014 @Override
3015 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003016 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003017 // Remove the ImageView copy of the FolderIcon and make the original visible.
3018 mDragLayer.removeView(mFolderIconImageView);
3019 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003020 }
3021 }
3022 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003023 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003024 if (!animate) {
3025 oa.end();
3026 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003027 }
3028
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003029 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003030 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003031 * is animated relative to the specified View. If the View is null, no animation
3032 * is played.
3033 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003034 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003035 */
Adam Cohen37b2a492016-04-06 18:36:06 -07003036 public void openFolder(FolderIcon folderIcon) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003037
Adam Cohenfb91f302012-06-11 15:45:18 -07003038 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003039 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3040 if (openFolder != null && openFolder != folder) {
3041 // Close any open folder before opening a folder.
3042 closeFolder();
3043 }
3044
Adam Cohena9cf38f2011-05-02 15:36:58 -07003045 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003046
Adam Cohena9cf38f2011-05-02 15:36:58 -07003047 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003048
Sunny Goyalf4066152015-04-15 09:42:19 -07003049 // While the folder is open, the position of the icon cannot change.
3050 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3051
Adam Cohen4554ee12011-08-03 16:13:21 -07003052 // Just verify that the folder hasn't already been added to the DragLayer.
3053 // There was a one-off crash where the folder had a parent already.
3054 if (folder.getParent() == null) {
3055 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003056 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003057 } else {
3058 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3059 folder.getParent() + ").");
3060 }
Adam Cohen37b2a492016-04-06 18:36:06 -07003061 folder.animateOpen();
3062
3063 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003064
3065 // Notify the accessibility manager that this folder "window" has appeared and occluded
3066 // the workspace items
3067 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3068 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003069 }
3070
3071 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003072 closeFolder(true);
3073 }
3074
3075 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003076 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003077 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003078 if (folder.isEditingName()) {
3079 folder.dismissEditingName();
3080 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003081 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003082 }
3083 }
3084
Sunny Goyal935fca12015-10-13 10:19:01 -07003085 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003086 animate &= !Utilities.isPowerSaverOn(this);
3087
Adam Cohen4554ee12011-08-03 16:13:21 -07003088 folder.getInfo().opened = false;
3089
3090 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3091 if (parent != null) {
3092 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003093 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003094 if (fi != null) {
3095 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3096 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003097 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003098 if (animate) {
3099 folder.animateClosed();
3100 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003101 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003102 }
Winson Chung83ca4802013-04-12 15:10:52 -07003103
Sunny Goyal935fca12015-10-13 10:19:01 -07003104 // Notify the accessibility manager that this folder "window" has disappeared and no
3105 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003106 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003107 }
3108
Tony Wickham49c8d292016-07-01 11:44:34 -07003109 public void closeShortcutsContainer() {
3110 DeepShortcutsContainer deepShortcutsContainer = getOpenShortcutsContainer();
3111 if (deepShortcutsContainer != null) {
3112 mDragController.removeDragListener(deepShortcutsContainer);
3113 mDragLayer.removeView(deepShortcutsContainer);
3114 }
3115 }
3116
3117 /**
3118 * @return The open shortcuts container, or null if there is none
3119 */
3120 public DeepShortcutsContainer getOpenShortcutsContainer() {
3121 return (DeepShortcutsContainer) mDragLayer.findViewById(R.id.deep_shortcuts_container);
3122 }
3123
Tony Wickhamdadb3042016-02-24 11:07:00 -08003124 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003125 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003126 if (!isDraggingEnabled()) return false;
3127 if (isWorkspaceLocked()) return false;
3128 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003129
Sunny Goyalbb011da2016-06-15 15:42:29 -07003130 if (v == mAllAppsButton && mAllAppsButton != null) {
Winson Chung76648c52015-07-10 14:33:23 -07003131 onLongClickAllAppsButton(v);
3132 return true;
3133 }
3134
Adam Cohen1697b792013-09-17 19:08:21 -07003135 if (v instanceof Workspace) {
3136 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003137 if (!mWorkspace.isTouchActive()) {
3138 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003139 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3140 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3141 return true;
3142 } else {
3143 return false;
3144 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003145 } else {
3146 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003147 }
Adam Cohen1697b792013-09-17 19:08:21 -07003148 }
3149
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003150 CellLayout.CellInfo longClickCellInfo = null;
3151 View itemUnderLongClick = null;
3152 if (v.getTag() instanceof ItemInfo) {
3153 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003154 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003155 itemUnderLongClick = longClickCellInfo.cell;
3156 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003157 }
3158
Winson Chung3d503fb2011-07-13 17:25:49 -07003159 // The hotseat touch handling does not go through Workspace, and we always allow long press
3160 // on hotseat items.
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003161 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003162 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003163 // User long pressed on empty space
3164 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3165 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003166 if (mWorkspace.isInOverviewMode()) {
3167 mWorkspace.startReordering(v);
3168 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003169 showOverviewMode(true);
Hyunyoung Song645764e2016-06-06 14:19:02 -07003170 mHotseat.requestDisallowInterceptTouchEvent(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003171 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003172 } else {
Sunny Goyalbb011da2016-06-15 15:42:29 -07003173 final boolean isAllAppsButton =
3174 !FeatureFlags.NO_ALL_APPS_ICON && isHotseatLayout(v) &&
3175 mDeviceProfile.inv.isAllAppsButtonRank(mHotseat.getOrderInHotseat(
3176 longClickCellInfo.cellX, longClickCellInfo.cellY));
Dan Sandlere26d0942014-01-13 14:30:14 -05003177 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003178 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003179 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003180 }
3181 }
3182 }
3183 return true;
3184 }
3185
Winson Chung3d503fb2011-07-13 17:25:49 -07003186 boolean isHotseatLayout(View layout) {
3187 return mHotseat != null && layout != null &&
3188 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3189 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003190
Winson Chung3d503fb2011-07-13 17:25:49 -07003191 /**
3192 * Returns the CellLayout of the specified container at the specified screen.
3193 */
Sunny Goyal92820592015-03-02 11:31:35 -08003194 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003195 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3196 if (mHotseat != null) {
3197 return mHotseat.getLayout();
3198 } else {
3199 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003200 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003201 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003202 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003203 }
3204 }
3205
Winson Chungb745afb2015-03-02 11:51:23 -08003206 /**
3207 * For overridden classes.
3208 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003209 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003210 return isAppsViewVisible();
3211 }
3212
3213 public boolean isAppsViewVisible() {
3214 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3215 }
3216
3217 public boolean isWidgetsViewVisible() {
3218 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003219 }
3220
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003221 private void setWorkspaceBackground(int background) {
3222 switch (background) {
3223 case WORKSPACE_BACKGROUND_TRANSPARENT:
3224 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3225 break;
3226 case WORKSPACE_BACKGROUND_BLACK:
3227 getWindow().setBackgroundDrawable(null);
3228 break;
3229 default:
3230 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3231 }
Craig Mautner360310b2012-10-26 15:13:08 -07003232 }
3233
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003234 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003235 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3236 int curflags = getWindow().getAttributes().flags
3237 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3238 if (wpflags != curflags) {
3239 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3240 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003241 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003242 }
3243
Michael Jurkae326f182011-11-21 14:05:46 -08003244 @Override
3245 public void onTrimMemory(int level) {
3246 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003247 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3248 // The widget preview db can result in holding onto over
3249 // 3MB of memory for caching which isn't necessary.
3250 SQLiteDatabase.releaseMemory();
3251
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003252 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003253 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003254 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003255 if (mLauncherCallbacks != null) {
3256 mLauncherCallbacks.onTrimMemory(level);
3257 }
Michael Jurkae326f182011-11-21 14:05:46 -08003258 }
3259
Winson5c6bdbb2015-09-03 11:36:19 -07003260 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003261 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003262 }
3263
Winson5c6bdbb2015-09-03 11:36:19 -07003264 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003265 boolean changed = mState != State.WORKSPACE ||
3266 mWorkspace.getState() != Workspace.State.NORMAL;
3267 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003268 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003269 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003270 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003271
Michael Jurkab3e22d92011-10-31 15:58:33 -07003272 // Set focus to the AppsCustomize button
3273 if (mAllAppsButton != null) {
3274 mAllAppsButton.requestFocus();
3275 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003276 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003277
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003278 // Change the state *after* we've called all the transition code
3279 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003280
Winson Chung5fb63472011-02-02 17:03:37 -08003281 // Resume the auto-advance of widgets
3282 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003283 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003284
Winson Chung0f785722015-04-08 10:27:49 -07003285 if (changed) {
3286 // Send an accessibility event to announce the context change
3287 getWindow().getDecorView()
3288 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003289 }
Winson5c6bdbb2015-09-03 11:36:19 -07003290 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003291 }
3292
Winsone9f27272015-10-13 10:47:51 -07003293 /**
3294 * Shows the overview button.
3295 */
Sunny Goyald0a6ae72016-06-16 12:29:03 -07003296 public void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003297 showOverviewMode(animated, false);
3298 }
3299
3300 /**
3301 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3302 * onto one of the overview panel buttons.
3303 */
3304 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3305 Runnable postAnimRunnable = null;
3306 if (requestButtonFocus) {
3307 postAnimRunnable = new Runnable() {
3308 @Override
3309 public void run() {
3310 // Hitting the menu button when in touch mode does not trigger touch mode to
3311 // be disabled, so if requested, force focus on one of the overview panel
3312 // buttons.
3313 mOverviewPanel.requestFocusFromTouch();
3314 }
3315 };
3316 }
Adam Cohened307df2013-10-02 09:37:31 -07003317 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003318 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003319 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003320 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003321 }
3322
Winson Chungb745afb2015-03-02 11:51:23 -08003323 /**
3324 * Shows the apps view.
3325 */
Hyunyoung Song645764e2016-06-06 14:19:02 -07003326 public void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
Winson Chung76648c52015-07-10 14:33:23 -07003327 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003328 if (resetListToTop) {
3329 mAppsView.scrollToTop();
3330 }
Winson Chung4ac30062015-05-08 17:34:17 -07003331 if (updatePredictedApps) {
3332 tryAndUpdatePredictedApps();
3333 }
Winson Chung76648c52015-07-10 14:33:23 -07003334 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003335 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003336
Winson Chungb745afb2015-03-02 11:51:23 -08003337 /**
3338 * Shows the widgets view.
3339 */
3340 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003341 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003342 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003343 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003344 }
Winson Chung76648c52015-07-10 14:33:23 -07003345 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003346
3347 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003348 @Override
3349 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003350 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003351 }
3352 });
Winson Chungb745afb2015-03-02 11:51:23 -08003353 }
3354
3355 /**
3356 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003357 *
3358 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003359 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003360 // TODO: calling method should use the return value so that when {@code false} is returned
3361 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003362 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003363 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3364 mState != State.WIDGETS_SPRING_LOADED) {
3365 return false;
3366 }
3367 if (toState != State.APPS && toState != State.WIDGETS) {
3368 return false;
3369 }
Winson Chungb745afb2015-03-02 11:51:23 -08003370
3371 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003372 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3373 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003374 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003375 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003376 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003377
Michael Jurkab3e22d92011-10-31 15:58:33 -07003378 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003379 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003380
3381 // Pause the auto-advance of widgets until we are out of AllApps
3382 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003383 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003384 closeFolder();
Tony Wickham49c8d292016-07-01 11:44:34 -07003385 closeShortcutsContainer();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003386
3387 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003388 getWindow().getDecorView()
3389 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003390 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003391 }
3392
Winson Chungcd99cd32015-04-29 11:03:24 -07003393 /**
3394 * Updates the workspace and interaction state on state change, and return the animation to this
3395 * new state.
3396 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003397 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003398 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003399 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003400 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003401 updateInteraction(fromState, toState);
3402 return anim;
3403 }
3404
Hyunyoung Song3f471442015-04-08 19:01:34 -07003405 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003406 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003407 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003408 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003409 }
Winson Chungb745afb2015-03-02 11:51:23 -08003410
Winson Chung006ee262015-08-03 14:40:11 -07003411 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003412 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003413 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003414
3415 if (isAppsViewVisible()) {
3416 mState = State.APPS_SPRING_LOADED;
3417 } else if (isWidgetsViewVisible()) {
3418 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003419 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003420 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003421 } else {
3422 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003423 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003424 }
Adam Cohen7777d962011-08-18 18:58:38 -07003425
Hyunyoung Song3f471442015-04-08 19:01:34 -07003426 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003427 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003428 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003429
Winson Chunge7a03942011-08-05 15:05:12 -07003430 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003431 @Override
3432 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003433 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003434 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3435 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003436 // Before we show workspace, hide all apps again because
3437 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3438 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003439 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003440 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003441 } else {
3442 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003443 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003444 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003445 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003446 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003447
Tony Wickhame0c33232016-02-08 11:37:04 -08003448 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003449 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3450 || mState == State.WIDGETS_SPRING_LOADED;
3451 }
3452
Michael Jurkad3ef3062010-11-23 16:23:58 -08003453 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003454 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003455 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003456 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003457 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003458 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003459 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3460 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003461 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003462 }
3463
Winson Chung4ac30062015-05-08 17:34:17 -07003464 /**
3465 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3466 * resumed.
3467 */
Hyunyoung Songa9a8a422016-06-15 16:45:48 -07003468 public void tryAndUpdatePredictedApps() {
Winson Chung4ac30062015-05-08 17:34:17 -07003469 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003470 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003471 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003472 mAppsView.setPredictedApps(apps);
Hyunyoung Songaa953652016-04-19 18:30:24 -07003473 getUserEventDispatcher().setPredictedApps(apps);
Winson Chung4ac30062015-05-08 17:34:17 -07003474 }
3475 }
3476 }
3477
Michael Jurkab3e22d92011-10-31 15:58:33 -07003478 void lockAllApps() {
3479 // TODO
3480 }
3481
3482 void unlockAllApps() {
3483 // TODO
3484 }
3485
Winsonf768d932015-09-25 16:12:35 -07003486 public boolean launcherCallbacksProvidesSearch() {
3487 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3488 }
3489
Michael Jurkad7c28052012-04-27 15:43:36 -07003490 @Override
3491 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003492 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003493 final List<CharSequence> text = event.getText();
3494 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003495 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003496 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003497 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003498 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003499 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003500 } else if (mWorkspace != null) {
3501 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003502 } else {
3503 text.add(getString(R.string.all_apps_home_button_label));
3504 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003505 return result;
3506 }
3507
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003508 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003509 * If the activity is currently paused, signal that we need to run the passed Runnable
3510 * in onResume.
3511 *
3512 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003513 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3514 * wrong when we're not running, and if the activity comes back to what the configuration was
3515 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003516 *
3517 * Implementation of the method from LauncherModel.Callbacks.
3518 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003519 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003520 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003521 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003522 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003523 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003524 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003525 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003526 }
3527 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003528 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003529 return true;
3530 } else {
3531 return false;
3532 }
3533 }
3534
Michael Jurkac402cd92013-05-20 15:49:32 +02003535 private boolean waitUntilResume(Runnable run) {
3536 return waitUntilResume(run, false);
3537 }
3538
Michael Jurka1e2f4652013-07-08 18:03:46 -07003539 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003540 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003541 }
3542
Michael Jurka7607c2f2013-04-03 14:33:19 -07003543 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003544 * If the activity is currently paused, signal that we need to re-run the loader
3545 * in onResume.
3546 *
3547 * This needs to be called from incoming places where resources might have been loaded
3548 * while we are paused. That is becaues the Configuration might be wrong
3549 * when we're not running, and if it comes back to what it was when we
3550 * were paused, we are not restarted.
3551 *
3552 * Implementation of the method from LauncherModel.Callbacks.
3553 *
3554 * @return true if we are currently paused. The caller might be able to
3555 * skip some work in that case since we will come back again.
3556 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003557 @Override
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003558 public boolean setLoadOnResume() {
3559 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003560 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003561 mOnResumeNeedsLoad = true;
3562 return true;
3563 } else {
3564 return false;
3565 }
3566 }
3567
3568 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003569 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003570 */
Sunny Goyal93f878c2016-03-30 17:31:24 -07003571 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04003572 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003573 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003574 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003575 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003576 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003577 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003578 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003579
Joe Onorato9c1289c2009-08-17 11:03:03 -04003580 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003581 * Clear any pending bind callbacks. This is called when is loader is planning to
3582 * perform a full rebind from scratch.
3583 */
3584 @Override
3585 public void clearPendingBinds() {
3586 mBindOnResumeCallbacks.clear();
3587 if (mPendingExecutor != null) {
3588 mPendingExecutor.markCompleted();
3589 mPendingExecutor = null;
3590 }
3591 }
3592
3593 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003594 * Refreshes the shortcuts shown on the workspace.
3595 *
3596 * Implementation of the method from LauncherModel.Callbacks.
3597 */
3598 public void startBinding() {
Sunny Goyale26d1002016-06-20 14:52:14 -07003599 if (LauncherAppState.PROFILE_STARTUP) {
3600 Trace.beginSection("Starting page bind");
3601 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003602 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003603
Winson Chungd64d1762013-08-20 14:37:16 -07003604 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003605 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003606 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003607
Michael Jurka05bf644e2011-11-30 20:28:53 -08003608 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003609 if (mHotseat != null) {
3610 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003611 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003612 if (LauncherAppState.PROFILE_STARTUP) {
3613 Trace.endSection();
3614 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003615 }
3616
Adam Cohendcd297f2013-06-18 13:13:40 -07003617 @Override
3618 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003619 // Make sure the first screen is always at the start.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003620 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
3621 orderedScreenIds.indexOf(Workspace.FIRST_SCREEN_ID) != 0) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003622 orderedScreenIds.remove(Workspace.FIRST_SCREEN_ID);
3623 orderedScreenIds.add(0, Workspace.FIRST_SCREEN_ID);
3624 mModel.updateWorkspaceScreenOrder(this, orderedScreenIds);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003625 } else if (!FeatureFlags.QSB_ON_FIRST_SCREEN && orderedScreenIds.isEmpty()) {
3626 // If there are no screens, we need to have an empty screen
3627 mWorkspace.addExtraEmptyScreen();
Adam Cohen5084cba2013-09-03 12:01:16 -07003628 }
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003629 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003630
3631 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003632 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003633 if (hasCustomContentToLeft()) {
3634 mWorkspace.createCustomContentContainer();
3635 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003636 }
Winson Chung64359a52013-07-08 17:17:08 -07003637 }
3638
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003639 private void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003640 int count = orderedScreenIds.size();
3641 for (int i = 0; i < count; i++) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003642 long screenId = orderedScreenIds.get(i);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07003643 if (!FeatureFlags.QSB_ON_FIRST_SCREEN || screenId != Workspace.FIRST_SCREEN_ID) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003644 // No need to bind the first screen, as its always bound.
3645 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
3646 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003647 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003648 }
3649
Winson Chungd64d1762013-08-20 14:37:16 -07003650 public void bindAppsAdded(final ArrayList<Long> newScreens,
3651 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003652 final ArrayList<ItemInfo> addAnimated,
3653 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003654 Runnable r = new Runnable() {
3655 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003656 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003657 }
3658 };
3659 if (waitUntilResume(r)) {
3660 return;
3661 }
3662
Winson Chungd64d1762013-08-20 14:37:16 -07003663 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003664 if (newScreens != null) {
3665 bindAddScreens(newScreens);
3666 }
Winson Chungd64d1762013-08-20 14:37:16 -07003667
3668 // We add the items without animation on non-visible pages, and with
3669 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003670 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003671 bindItems(addNotAnimated, 0,
3672 addNotAnimated.size(), false);
3673 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003674 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003675 bindItems(addAnimated, 0,
3676 addAnimated.size(), true);
3677 }
Winson Chungc58497e2013-09-03 17:48:37 -07003678
Winson Chung87412982013-10-03 18:34:14 -07003679 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003680 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003681
Winson Chungb745afb2015-03-02 11:51:23 -08003682 if (addedApps != null && mAppsView != null) {
3683 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003684 }
Winson Chungd64d1762013-08-20 14:37:16 -07003685 }
3686
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003687 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003688 * Bind the items start-end from the list.
3689 *
3690 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003691 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003692 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003693 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3694 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003695 Runnable r = new Runnable() {
3696 public void run() {
3697 bindItems(shortcuts, start, end, forceAnimateIcons);
3698 }
3699 };
3700 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003701 return;
3702 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003703
Winson Chungf0c6ae02012-03-21 16:10:31 -07003704 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003705 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3706 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003707 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003708 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003709 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003710 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003711 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003712
3713 // Short circuit if we are loading dock items for a configuration which has no dock
3714 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3715 mHotseat == null) {
3716 continue;
3717 }
3718
Sunny Goyal639e9062015-08-19 19:17:06 -07003719 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003720 switch (item.itemType) {
3721 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3722 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003723 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003724 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003725 view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003726 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003727 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003728 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003729 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003730 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003731 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003732 default:
3733 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003734 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003735
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07003736 /*
3737 * Remove colliding items.
3738 */
3739 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3740 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3741 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3742 View v = cl.getChildAt(item.cellX, item.cellY);
3743 Object tag = v.getTag();
3744 String desc = "Collision while binding workspace item: " + item
3745 + ". Collides with " + tag;
3746 if (ProviderConfig.IS_DOGFOOD_BUILD) {
3747 throw (new RuntimeException(desc));
3748 } else {
3749 Log.d(TAG, desc);
3750 LauncherModel.deleteItemFromDatabase(this, item);
3751 continue;
3752 }
3753 }
3754 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003755 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3756 item.cellY, 1, 1);
3757 if (animateIcons) {
3758 // Animate all the applications up now
3759 view.setAlpha(0f);
3760 view.setScaleX(0f);
3761 view.setScaleY(0f);
3762 bounceAnims.add(createNewAppBounceAnimation(view, i));
3763 newShortcutsScreenId = item.screenId;
3764 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003765 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003766
Winson Chung997a9232013-07-24 15:33:46 -07003767 if (animateIcons) {
3768 // Animate to the correct page
3769 if (newShortcutsScreenId > -1) {
3770 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003771 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003772 final Runnable startBounceAnimRunnable = new Runnable() {
3773 public void run() {
3774 anim.playTogether(bounceAnims);
3775 anim.start();
3776 }
3777 };
Winson Chung997a9232013-07-24 15:33:46 -07003778 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003779 // We post the animation slightly delayed to prevent slowdowns
3780 // when we are loading right after we return to launcher.
3781 mWorkspace.postDelayed(new Runnable() {
3782 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003783 if (mWorkspace != null) {
3784 mWorkspace.snapToPage(newScreenIndex);
3785 mWorkspace.postDelayed(startBounceAnimRunnable,
3786 NEW_APPS_ANIMATION_DELAY);
3787 }
Winson Chung94d67682013-09-25 16:29:40 -07003788 }
3789 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003790 } else {
3791 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003792 }
3793 }
Winson Chung64359a52013-07-08 17:17:08 -07003794 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003795 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003796 }
3797
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003798 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3799 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3800 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003801 view.updateAppWidget(null);
3802 view.setOnClickListener(this);
3803 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003804 mWorkspace.requestLayout();
3805 }
3806
Joe Onorato9c1289c2009-08-17 11:03:03 -04003807 /**
3808 * Add the views for a widget to the workspace.
3809 *
3810 * Implementation of the method from LauncherModel.Callbacks.
3811 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003812 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003813 Runnable r = new Runnable() {
3814 public void run() {
3815 bindAppWidget(item);
3816 }
3817 };
3818 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003819 return;
3820 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003821
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003822 if (mIsSafeModeEnabled) {
3823 bindSafeModeWidget(item);
3824 return;
3825 }
3826
Daniel Sandler843e8602010-06-07 14:59:01 -04003827 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3828 if (DEBUG_WIDGETS) {
3829 Log.d(TAG, "bindAppWidget: " + item);
3830 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003831
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003832 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003833
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003834 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3835 // If the provider is not ready, bind as a pending widget.
3836 appWidgetInfo = null;
3837 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3838 // The widget id is not valid. Try to find the widget based on the provider info.
3839 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3840 } else {
3841 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3842 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003843
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003844 // If the provider is ready, but the width is not yet restored, try to restore it.
3845 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3846 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003847 if (appWidgetInfo == null) {
3848 if (DEBUG_WIDGETS) {
3849 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3850 + " belongs to component " + item.providerName
3851 + ", as the povider is null");
3852 }
3853 LauncherModel.deleteItemFromDatabase(this, item);
3854 return;
3855 }
Sunny Goyalff572272014-07-23 13:58:07 -07003856
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003857 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003858 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyald478c832016-04-01 12:04:16 -07003859 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
3860 // Id has not been allocated yet. Allocate a new id.
3861 item.appWidgetId = mAppWidgetHost.allocateAppWidgetId();
3862 item.restoreStatus |= LauncherAppWidgetInfo.FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -07003863
Sunny Goyald478c832016-04-01 12:04:16 -07003864 // Also try to bind the widget. If the bind fails, the user will be shown
3865 // a click to setup UI, which will ask for the bind permission.
3866 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
3867 pendingInfo.spanX = item.spanX;
3868 pendingInfo.spanY = item.spanY;
3869 pendingInfo.minSpanX = item.minSpanX;
3870 pendingInfo.minSpanY = item.minSpanY;
3871 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
3872 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3873 item.appWidgetId, appWidgetInfo, options);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003874
Sunny Goyald478c832016-04-01 12:04:16 -07003875 // Bind succeeded
3876 if (success) {
3877 // If the widget has a configure activity, it is still needs to set it up,
3878 // otherwise the widget is ready to go.
3879 item.restoreStatus = (appWidgetInfo.configure == null)
3880 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3881 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003882 }
Sunny Goyald478c832016-04-01 12:04:16 -07003883
3884 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003885 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003886 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003887 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003888 // The widget was marked as UI not ready, but there is no configure activity to
3889 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003890 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3891 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003892 }
Sunny Goyalff572272014-07-23 13:58:07 -07003893 }
3894
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003895 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003896 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003897 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3898 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003899 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003900
Sunny Goyal56c73602015-09-25 12:55:01 -07003901 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003902 if (appWidgetInfo == null) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07003903 FileLog.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07003904 deleteWidgetInfo(item);
3905 return;
3906 }
3907
Sunny Goyal233ee962015-08-03 13:05:01 -07003908 item.minSpanX = appWidgetInfo.minSpanX;
3909 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003910 addAppWidgetToWorkspace(
3911 mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo),
3912 item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003913 } else {
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003914 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, false);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003915 view.updateIcon(mIconCache);
Sunny Goyal87af0fd2016-05-16 14:56:02 -07003916 view.updateAppWidget(null);
3917 view.setOnClickListener(this);
3918 addAppWidgetToWorkspace(view, item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07003919 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003920 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003921
Daniel Sandler843e8602010-06-07 14:59:01 -04003922 if (DEBUG_WIDGETS) {
3923 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3924 + (SystemClock.uptimeMillis()-start) + "ms");
3925 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003926 }
3927
Sunny Goyalff572272014-07-23 13:58:07 -07003928 /**
3929 * Restores a pending widget.
3930 *
3931 * @param appWidgetId The app widget id
Sunny Goyalff572272014-07-23 13:58:07 -07003932 */
Sunny Goyald478c832016-04-01 12:04:16 -07003933 private LauncherAppWidgetInfo completeRestoreAppWidget(int appWidgetId, int finalRestoreFlag) {
Sunny Goyalff572272014-07-23 13:58:07 -07003934 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3935 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3936 Log.e(TAG, "Widget update called, when the widget no longer exists.");
Sunny Goyald478c832016-04-01 12:04:16 -07003937 return null;
Sunny Goyalff572272014-07-23 13:58:07 -07003938 }
3939
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003940 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyald478c832016-04-01 12:04:16 -07003941 info.restoreStatus = finalRestoreFlag;
Sunny Goyalff572272014-07-23 13:58:07 -07003942
3943 mWorkspace.reinflateWidgetsIfNecessary();
3944 LauncherModel.updateItemInDatabase(this, info);
Sunny Goyald478c832016-04-01 12:04:16 -07003945 return info;
Sunny Goyalff572272014-07-23 13:58:07 -07003946 }
3947
Adam Cohen1462de32012-07-24 22:34:36 -07003948 public void onPageBoundSynchronously(int page) {
3949 mSynchronouslyBoundPages.add(page);
3950 }
3951
Sunny Goyal527c7d32015-08-28 15:19:36 -07003952 @Override
3953 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3954 if (mPendingExecutor != null) {
3955 mPendingExecutor.markCompleted();
3956 }
3957 mPendingExecutor = executor;
3958 executor.attachTo(this);
3959 }
3960
3961 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
3962 if (mPendingExecutor == executor) {
3963 mPendingExecutor = null;
3964 }
3965 }
3966
Joe Onorato9c1289c2009-08-17 11:03:03 -04003967 /**
3968 * Callback saying that there aren't any more items to bind.
3969 *
3970 * Implementation of the method from LauncherModel.Callbacks.
3971 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003972 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003973 Runnable r = new Runnable() {
3974 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003975 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003976 }
3977 };
3978 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003979 return;
3980 }
Sunny Goyale26d1002016-06-20 14:52:14 -07003981 if (LauncherAppState.PROFILE_STARTUP) {
3982 Trace.beginSection("Page bind completed");
3983 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003984 if (mSavedState != null) {
3985 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003986 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003987 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003988
Joe Onorato9c1289c2009-08-17 11:03:03 -04003989 mSavedState = null;
3990 }
3991
Adam Cohen1462de32012-07-24 22:34:36 -07003992 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003993
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003994 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07003995
3996 // If we received the result of any pending adds while the loader was running (e.g. the
3997 // widget configuration forced an orientation change), process them now.
3998 if (sPendingAddItem != null) {
3999 final long screenId = completeAdd(sPendingAddItem);
4000
4001 // TODO: this moves the user to the page where the pending item was added. Ideally,
4002 // the screen would be guaranteed to exist after bind, and the page would be set through
4003 // the workspace restore process.
4004 mWorkspace.post(new Runnable() {
4005 @Override
4006 public void run() {
4007 mWorkspace.snapToScreenId(screenId);
4008 }
4009 });
4010 sPendingAddItem = null;
4011 }
4012
Sunny Goyal756adbc2015-04-16 15:20:51 -07004013 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004014
4015 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004016 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004017 }
Sunny Goyale26d1002016-06-20 14:52:14 -07004018 if (LauncherAppState.PROFILE_STARTUP) {
4019 Trace.endSection();
4020 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004021 }
4022
Winson Chunga2413752012-04-03 14:22:34 -07004023 private boolean canRunNewAppsAnimation() {
4024 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Andrew Sapperstein932eb832016-06-30 18:10:09 -07004025 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
Winson Chunga2413752012-04-03 14:22:34 -07004026 }
4027
Winson Chungc9168342013-06-26 14:54:55 -07004028 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004029 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004030 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4031 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004032 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004033 return bounceAnim;
4034 }
4035
Adam Cohen27772732013-11-11 14:00:56 +00004036 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004037 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004038 }
4039
Tony Wickham55616cd2015-09-23 14:55:17 -07004040 public int getSearchBarHeight() {
4041 if (mLauncherCallbacks != null) {
4042 return mLauncherCallbacks.getSearchBarHeight();
4043 }
4044 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4045 }
4046
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004047 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004048 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4049 * multiple calls to bind the same list.)
4050 */
4051 @Thunk ArrayList<AppInfo> mTmpAppsList;
4052 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4053 public void run() {
4054 bindAllApplications(mTmpAppsList);
4055 mTmpAppsList = null;
4056 }
4057 };
4058
4059 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004060 * Add the icons for all apps.
4061 *
4062 * Implementation of the method from LauncherModel.Callbacks.
4063 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004064 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004065 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4066 mTmpAppsList = apps;
4067 return;
4068 }
4069
Winson Chungb745afb2015-03-02 11:51:23 -08004070 if (mAppsView != null) {
4071 mAppsView.setApps(apps);
4072 }
Adam Cohen9211d422014-10-07 18:14:53 -07004073 if (mLauncherCallbacks != null) {
4074 mLauncherCallbacks.bindAllApplications(apps);
4075 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004076 }
4077
4078 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004079 * Copies LauncherModel's map of activities to shortcut ids to Launcher's. This is necessary
4080 * because LauncherModel's map is updated in the background, while Launcher runs on the UI.
4081 */
4082 @Override
4083 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
4084 mDeepShortcutMap = deepShortcutMapCopy;
4085 if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
4086 }
4087
Tony Wickham1bce7fd2016-04-28 17:39:03 -07004088 public List<String> getShortcutIdsForItem(ItemInfo info) {
4089 if (!DeepShortcutManager.supportsShortcuts(info)) {
4090 return Collections.EMPTY_LIST;
4091 }
4092 ComponentName component = info.getTargetComponent();
4093 List<String> ids = mDeepShortcutMap.get(new ComponentKey(component, info.user));
4094 return ids == null ? Collections.EMPTY_LIST : ids;
4095 }
4096
Tony Wickhambfbf7f92016-05-19 11:19:39 -07004097 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004098 * A package was updated.
4099 *
4100 * Implementation of the method from LauncherModel.Callbacks.
4101 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004102 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004103 Runnable r = new Runnable() {
4104 public void run() {
4105 bindAppsUpdated(apps);
4106 }
4107 };
4108 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004109 return;
4110 }
4111
Winson Chungb745afb2015-03-02 11:51:23 -08004112 if (mAppsView != null) {
4113 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004114 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004115 }
4116
Sunny Goyal4390ace2014-10-13 11:33:11 -07004117 @Override
4118 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4119 Runnable r = new Runnable() {
4120 public void run() {
4121 bindWidgetsRestored(widgets);
4122 }
4123 };
4124 if (waitUntilResume(r)) {
4125 return;
4126 }
4127 mWorkspace.widgetsRestored(widgets);
4128 }
4129
Joe Onorato9c1289c2009-08-17 11:03:03 -04004130 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004131 * Some shortcuts were updated in the background.
4132 *
4133 * Implementation of the method from LauncherModel.Callbacks.
4134 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004135 @Override
4136 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4137 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004138 Runnable r = new Runnable() {
4139 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004140 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004141 }
4142 };
4143 if (waitUntilResume(r)) {
4144 return;
4145 }
4146
Sunny Goyal4390ace2014-10-13 11:33:11 -07004147 if (!updated.isEmpty()) {
4148 mWorkspace.updateShortcuts(updated);
4149 }
4150
4151 if (!removed.isEmpty()) {
4152 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4153 for (ShortcutInfo si : removed) {
4154 removedComponents.add(si.getTargetComponent());
4155 }
4156 mWorkspace.removeItemsByComponentName(removedComponents, user);
4157 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004158 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004159 }
4160 }
4161
4162 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004163 * Update the state of a package, typically related to install state.
4164 *
4165 * Implementation of the method from LauncherModel.Callbacks.
4166 */
Sunny Goyale755d462014-07-22 13:48:29 -07004167 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004168 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4169 Runnable r = new Runnable() {
4170 public void run() {
4171 bindRestoreItemsChange(updates);
4172 }
4173 };
4174 if (waitUntilResume(r)) {
4175 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004176 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004177
Sunny Goyal756adbc2015-04-16 15:20:51 -07004178 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004179 }
4180
4181 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004182 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004183 * in addition to specific applications to remove, the reason being that
4184 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004185 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004186 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004187 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004188 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004189 public void bindWorkspaceComponentsRemoved(
4190 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4191 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004192 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004193 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004194 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004195 }
Winson Chungd64d1762013-08-20 14:37:16 -07004196 };
4197 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004198 return;
4199 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004200 if (!packageNames.isEmpty()) {
4201 mWorkspace.removeItemsByPackageName(packageNames, user);
4202 }
4203 if (!components.isEmpty()) {
4204 mWorkspace.removeItemsByComponentName(components, user);
4205 }
4206 // Notify the drag controller
4207 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004208
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004209 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004210
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004211 @Override
4212 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4213 Runnable r = new Runnable() {
4214 public void run() {
4215 bindAppInfosRemoved(appInfos);
4216 }
4217 };
4218 if (waitUntilResume(r)) {
4219 return;
Joe Onorato36115782010-06-17 13:28:48 -04004220 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004221
Winson Chungdf95eb12013-10-16 14:57:07 -07004222 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004223 if (mAppsView != null) {
4224 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004225 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004226 }
Joe Onoratobe386092009-11-17 17:32:16 -08004227
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004228 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004229 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004230 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004231 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004232 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004233
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004234 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004235 public void bindWidgetsModel(WidgetsModel model) {
4236 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004237 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004238 return;
4239 }
4240
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004241 if (mWidgetsView != null && model != null) {
4242 mWidgetsView.addWidgets(model);
4243 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004244 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004245 }
4246
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004247 @Override
4248 public void notifyWidgetProvidersChanged() {
4249 if (mWorkspace.getState().shouldUpdateWidget) {
4250 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4251 }
4252 }
4253
Winson Chung400438b2011-01-16 17:53:48 -08004254 private int mapConfigurationOriActivityInfoOri(int configOri) {
4255 final Display d = getWindowManager().getDefaultDisplay();
4256 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4257 switch (d.getRotation()) {
4258 case Surface.ROTATION_0:
4259 case Surface.ROTATION_180:
4260 // We are currently in the same basic orientation as the natural orientation
4261 naturalOri = configOri;
4262 break;
4263 case Surface.ROTATION_90:
4264 case Surface.ROTATION_270:
4265 // We are currently in the other basic orientation to the natural orientation
4266 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4267 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4268 break;
4269 }
4270
4271 int[] oriMap = {
4272 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4273 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4274 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4275 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4276 };
4277 // Since the map starts at portrait, we need to offset if this device's natural orientation
4278 // is landscape.
4279 int indexOffset = 0;
4280 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4281 indexOffset = 1;
4282 }
4283 return oriMap[(d.getRotation() + indexOffset) % 4];
4284 }
Adam Cohen446e9402011-09-15 18:21:21 -07004285
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004286 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004287 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004288 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004289 if (Utilities.ATLEAST_JB_MR2) {
4290 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4291 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004292 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4293 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004294 }
Winson Chung4b919f82012-05-01 10:44:08 -07004295 }
4296 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004297
Winson Chung4b919f82012-05-01 10:44:08 -07004298 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004299 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004300 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004301 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004302 } else {
4303 mHandler.postDelayed(new Runnable() {
4304 public void run() {
4305 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4306 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004307 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004308 }
Winson Chung4b919f82012-05-01 10:44:08 -07004309 }
Winson Chung400438b2011-01-16 17:53:48 -08004310 }
4311
Adam Cohenea90f832014-05-21 15:03:34 -07004312 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004313 * To be overridden by subclasses to indicate that there is an activity to launch
4314 * before showing the standard launcher experience.
4315 */
4316 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004317 if (mLauncherCallbacks != null) {
4318 return mLauncherCallbacks.hasFirstRunActivity();
4319 }
Adam Cohen432609a2014-03-13 17:03:22 -07004320 return false;
4321 }
4322
4323 /**
4324 * To be overridden by subclasses to launch any first run activity
4325 */
4326 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004327 if (mLauncherCallbacks != null) {
4328 return mLauncherCallbacks.getFirstRunActivity();
4329 }
Adam Cohen432609a2014-03-13 17:03:22 -07004330 return null;
4331 }
4332
Winson Chunga6945242014-01-08 14:04:34 -08004333 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004334 return !ActivityManager.isRunningInTestHarness() &&
4335 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004336 }
4337
Adam Cohen4a9423d2014-03-28 14:22:31 -07004338 protected boolean hasRunFirstRunActivity() {
4339 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4340 }
4341
Adam Cohen432609a2014-03-13 17:03:22 -07004342 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004343 if (shouldRunFirstRunActivity() &&
4344 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004345 Intent firstRunIntent = getFirstRunActivity();
4346 if (firstRunIntent != null) {
4347 startActivity(firstRunIntent);
4348 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004349 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004350 }
4351 }
Adam Cohen432609a2014-03-13 17:03:22 -07004352 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004353 }
4354
4355 private void markFirstRunActivityShown() {
4356 SharedPreferences.Editor editor = mSharedPrefs.edit();
4357 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4358 editor.apply();
4359 }
4360
Adam Cohen432609a2014-03-13 17:03:22 -07004361 /**
4362 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4363 * screen that must be displayed and dismissed.
4364 */
4365 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004366 if (mLauncherCallbacks != null) {
4367 return mLauncherCallbacks.hasDismissableIntroScreen();
4368 }
Adam Cohen432609a2014-03-13 17:03:22 -07004369 return false;
4370 }
4371
4372 /**
4373 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4374 */
4375 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004376 if (mLauncherCallbacks != null) {
4377 return mLauncherCallbacks.getIntroScreen();
4378 }
Adam Cohen432609a2014-03-13 17:03:22 -07004379 return null;
4380 }
4381
4382 /**
4383 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4384 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4385 */
4386 private boolean shouldShowIntroScreen() {
4387 return hasDismissableIntroScreen() &&
4388 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4389 }
4390
4391 protected void showIntroScreen() {
4392 View introScreen = getIntroScreen();
4393 changeWallpaperVisiblity(false);
4394 if (introScreen != null) {
4395 mDragLayer.showOverlayView(introScreen);
4396 }
4397 }
4398
Winson Chung5ffd51d2015-06-25 11:36:50 -07004399 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004400 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004401 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004402 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004403 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004404 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004405 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4406 if (userHandle != null) {
4407 user = UserHandleCompat.fromUser(userHandle);
4408 }
4409 }
4410 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004411 }
4412
4413 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004414 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004415 if (user == null) {
4416 user = UserHandleCompat.myUserHandle();
4417 }
4418
4419 // Called from search suggestion, add the profile extra to the intent to ensure that we
4420 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004421 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004422 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004423 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004424 return null;
4425 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004426 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004427 }
4428
Winson Chung5ffd51d2015-06-25 11:36:50 -07004429 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004430 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4431 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004432 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004433 UserHandleCompat.myUserHandle());
4434 }
4435
Winson Chung5ffd51d2015-06-25 11:36:50 -07004436 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004437 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4438 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004439 mWorkspace.onExternalDragStartedWithItem(dragView);
4440 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004441 }
4442
Winson Chung5ffd51d2015-06-25 11:36:50 -07004443 protected void moveWorkspaceToDefaultScreen() {
4444 mWorkspace.moveToDefaultScreen(false);
4445 }
4446
Winson Chung80baf5a2010-08-09 16:03:15 -07004447 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004448 * Returns a FastBitmapDrawable with the icon, accurately sized.
4449 */
4450 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4451 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4452 d.setFilterBitmap(true);
4453 resizeIconDrawable(d);
4454 return d;
4455 }
4456
4457 /**
4458 * Resizes an icon drawable to the correct icon size.
4459 */
Winson5fbe0742015-09-30 15:33:00 -07004460 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004461 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004462 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004463 }
4464
4465 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004466 * Prints out out state for debugging.
4467 */
4468 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004469 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004470 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004471 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4472 Log.d(TAG, "mRestoring=" + mRestoring);
4473 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Joe Onoratobe386092009-11-17 17:32:16 -08004474 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004475 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004476
Daniel Sandler325dc232013-06-05 22:57:57 -04004477 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004478 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004479
4480 @Override
4481 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4482 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004483 // Dump workspace
4484 writer.println(prefix + "Workspace Items");
4485 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4486 writer.println(prefix + " Homescreen " + i);
4487
4488 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4489 for (int j = 0; j < layout.getChildCount(); j++) {
4490 Object tag = layout.getChildAt(j).getTag();
4491 if (tag != null) {
4492 writer.println(prefix + " " + tag.toString());
4493 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004494 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004495 }
Sunny Goyala1365452015-10-01 15:46:24 -07004496
4497 writer.println(prefix + " Hotseat");
4498 ViewGroup layout = mHotseat.getLayout().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 }
4504 }
4505
Sunny Goyal713edfc2016-05-06 09:58:34 -07004506 try {
4507 FileLog.flushAll(writer);
4508 } catch (Exception e) {
4509 // Ignore
Sunny Goyala1365452015-10-01 15:46:24 -07004510 }
4511
Adam Cohen9211d422014-10-07 18:14:53 -07004512 if (mLauncherCallbacks != null) {
4513 mLauncherCallbacks.dump(prefix, fd, writer, args);
4514 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004515 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004516
Adam Cohen59400422014-03-05 18:07:04 -08004517 public static CustomAppWidget getCustomAppWidget(String name) {
4518 return sCustomAppWidgets.get(name);
4519 }
4520
4521 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4522 return sCustomAppWidgets;
4523 }
4524
Sunny Goyal29538332016-02-18 09:10:19 -08004525 public static List<View> getFolderContents(View icon) {
4526 if (icon instanceof FolderIcon) {
4527 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4528 } else {
4529 return Collections.EMPTY_LIST;
4530 }
4531 }
Sunny Goyal745bad92016-05-02 10:54:12 -07004532
Andrew Sappersteinabef55a2016-06-19 12:49:00 -07004533 public static Launcher getLauncher(Context context) {
4534 if (context instanceof Launcher) {
4535 return (Launcher) context;
4536 }
4537 return ((Launcher) ((ContextWrapper) context).getBaseContext());
4538 }
4539
Sunny Goyal745bad92016-05-02 10:54:12 -07004540 private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
4541
4542 @Override
4543 public void onSharedPreferenceChanged(
4544 SharedPreferences sharedPreferences, String key) {
4545 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
4546 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
4547 if (!waitUntilResume(this, true)) {
4548 run();
4549 }
4550 }
4551 }
4552
4553 @Override
4554 public void run() {
4555 setOrientation();
4556 }
4557 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004558}